diff --git a/gmail/add-ons/quickstart.gs b/gmail/add-ons/quickstart.gs index 4afbf310b..448611d15 100644 --- a/gmail/add-ons/quickstart.gs +++ b/gmail/add-ons/quickstart.gs @@ -15,14 +15,27 @@ */ // [START apps_script_gmail_quick_start] +/** + * This is a partial definition of the a Gmail add-on event object. + * For the full list of properties, see: + * https://developers.google.com/workspace/add-ons/concepts/event-objects + * + * @typedef {Object} gmailEvent + * @property {Object} messageMetadata + * @property {string} messageMetadata.accessToken + * @property {string} messageMetadata.messageId + * @property {Object} formInputs + * @property {string[]} formInputs.labels + */ + /** * Returns the array of cards that should be rendered for the current * e-mail thread. The name of this function is specified in the * manifest 'onTriggerFunction' field, indicating that this function * runs every time the add-on is started. * - * @param {Object} e The data provided by the Gmail UI. - * @return {Card[]} + * @param {gmailEvent} e The data provided by the Gmail UI. + * @return {CardService.Card[]} */ function buildAddOn(e) { // Activate temporary Gmail add-on scopes. @@ -71,9 +84,9 @@ function buildAddOn(e) { * user selections. Runs via the OnChangeAction for * each CHECK_BOX created. * - * @param {Object} e The data provided by the Gmail UI. + * @param {gmailEvent} e The data provided by the Gmail UI. */ -function toggleLabel(e){ +function toggleLabel(e) { var selected = e.formInputs.labels; // Activate temporary Gmail add-on scopes. @@ -85,30 +98,29 @@ function toggleLabel(e){ var thread = message.getThread(); if (selected != null){ - for each (var label in GmailApp.getUserLabels()) { - if(selected.indexOf(label.getName()) != -1){ - thread.addLabel(label); - } - else { - thread.removeLabel(label); - } - } - } - else { - for each (var label in GmailApp.getUserLabels()) { + for (const label of GmailApp.getUserLabels()) { + if (selected.indexOf(label.getName()) != -1) { + thread.addLabel(label); + } else { + thread.removeLabel(label); + } + } + } else { + for (const label of GmailApp.getUserLabels()) { thread.removeLabel(label); } } } + /** - * Converts an GmailLabel object to a array of strings. + * Converts a GmailLabel object to a array of strings. * Used for easy sorting and to determine if a value exists. * - * @param {labelsObjects} A GmailLabel object array. - * @return {lables[]} An array of labels names as strings. + * @param {GoogleAppsScript.Gmail.GmailLabel[]} labelsObjects + * @return {string[]} An array of labels names as strings. */ -function getLabelArray(labelsObjects){ +function getLabelArray(labelsObjects) { var labels = []; for(var i = 0; i < labelsObjects.length; i++) { labels[i] = labelsObjects[i].getName(); diff --git a/gmail/inlineimage/inlineimage.gs b/gmail/inlineimage/inlineimage.gs index 0ca113c6f..a0050a65e 100644 --- a/gmail/inlineimage/inlineimage.gs +++ b/gmail/inlineimage/inlineimage.gs @@ -20,6 +20,9 @@ function sendEmailToMyself() { sendEmailWithInlineImage(Session.getActiveUser().getEmail()); } +/** + * @param {string} toAddress + */ function sendEmailWithInlineImage(toAddress) { const options = {}; const imageName = 'cat_emoji'; diff --git a/gmail/markup/Code.gs b/gmail/markup/Code.gs index d4f65f6ed..4f5587d82 100644 --- a/gmail/markup/Code.gs +++ b/gmail/markup/Code.gs @@ -3,17 +3,14 @@ * Send an email with schemas in order to test email markup. */ function testSchemas() { - try { - const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); + const htmlBody = + HtmlService.createHtmlOutputFromFile('mail_template').getContent(); - MailApp.sendEmail({ - to: Session.getActiveUser().getEmail(), - subject: 'Test Email markup - ' + new Date(), - htmlBody: htmlBody - }); - } catch (err) { - console.log(err.message); - } + MailApp.sendEmail({ + to: Session.getActiveUser().getEmail(), + subject: 'Test Email markup - ' + new Date(), + htmlBody: htmlBody, + }); } // [END gmail_send_email_with_markup] diff --git a/gmail/quickstart/quickstart.gs b/gmail/quickstart/quickstart.gs index 51ca6a28c..613bda88b 100644 --- a/gmail/quickstart/quickstart.gs +++ b/gmail/quickstart/quickstart.gs @@ -20,22 +20,20 @@ * @see https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list */ function listLabels() { - try { - // Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox - const response = Gmail.Users.Labels.list('me'); - if (!response || response.labels.length === 0) { - // TODO (developer) - No labels are returned from the response - console.log('No labels found.'); - return; - } - // Print the Labels that are available. - console.log('Labels:'); - for (const label of response.labels ) { - console.log('- %s', label.name); - } - } catch (err) { - // TODO (developer) - Handle exception on Labels.list() API - console.log('Labels.list() API failed with error %s', err.toString()); + // Add this check to use the Gmail advanced service. + if (!Gmail || !Gmail.Users || !Gmail.Users.Labels) { + throw new Error('Enable the Gmail Advanced Service.'); + } + // Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox + const response = Gmail.Users.Labels.list('me'); + if (!response || !response.labels || response.labels.length === 0) { + console.log('No labels found.'); + return; + } + // Print the Labels that are available. + console.log('Labels:'); + for (const label of response.labels) { + console.log('- %s', label.name); } } // [END gmail_quickstart] diff --git a/gmail/sendingEmails/sendingEmails.gs b/gmail/sendingEmails/sendingEmails.gs index f8e8e0067..aa5ca3d82 100644 --- a/gmail/sendingEmails/sendingEmails.gs +++ b/gmail/sendingEmails/sendingEmails.gs @@ -19,20 +19,16 @@ * Sends emails with data from the current spreadsheet. */ function sendEmails() { - try { - const sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet in spreadsheet - const startRow = 2; // First row of data to process - const numRows = 2; // Number of rows to process - const dataRange = sheet.getRange(startRow, 1, numRows, 2); // Fetch the range of cells A2:B3 - const data = dataRange.getValues(); // Fetch values for each row in the Range. - for (const row of data) { - const emailAddress = row[0]; // First column - const message = row[1]; // Second column - const subject = 'Sending emails from a Spreadsheet'; - MailApp.sendEmail(emailAddress, subject, message); // Send emails to emailAddresses which are presents in First column - } - } catch (err) { - console.log(err); + const sheet = SpreadsheetApp.getActiveSheet(); + const startRow = 2; + const numRows = 2; + const dataRange = sheet.getRange(startRow, 1, numRows, 2); + const data = dataRange.getValues(); + for (const row of data) { + const emailAddress = row[0]; + const message = row[1]; + const subject = 'Sending emails from a Spreadsheet'; + MailApp.sendEmail(emailAddress, subject, message); } } // [END gmail_send_emails] @@ -42,29 +38,25 @@ function sendEmails() { * Sends non-duplicate emails with data from the current spreadsheet. */ function sendNonDuplicateEmails() { - const EMAIL_SENT = 'email sent'; //This constant is used to write the message in Column C of Sheet - try { - const sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet in spreadsheet - const startRow = 2; // First row of data to process - const numRows = 2; // Number of rows to process - const dataRange = sheet.getRange(startRow, 1, numRows, 3); // Fetch the range of cells A2:B3 - const data = dataRange.getValues(); // Fetch values for each row in the Range. - for (let i = 0; i < data.length; ++i) { - const row = data[i]; - const emailAddress = row[0]; // First column - const message = row[1]; // Second column - const emailSent = row[2]; // Third column - if (emailSent === EMAIL_SENT) { - console.log('Email already sent'); - return; - } - const subject = 'Sending emails from a Spreadsheet'; - MailApp.sendEmail(emailAddress, subject, message);// Send emails to emailAddresses which are presents in First column - sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT); - SpreadsheetApp.flush(); // Make sure the cell is updated right away in case the script is interrupted + const EMAIL_SENT = 'email sent'; + const sheet = SpreadsheetApp.getActiveSheet(); + const startRow = 2; + const numRows = 2; + const dataRange = sheet.getRange(startRow, 1, numRows, 3); + const data = dataRange.getValues(); + for (let i = 0; i < data.length; ++i) { + const row = data[i]; + const emailAddress = row[0]; + const message = row[1]; + const emailSent = row[2]; + if (emailSent === EMAIL_SENT) { + console.log('Email already sent'); + return; } - } catch (err) { - console.log(err); + const subject = 'Sending emails from a Spreadsheet'; + MailApp.sendEmail(emailAddress, subject, message); + sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT); + SpreadsheetApp.flush(); } } // [END gmail_send_non_duplicate_emails]