diff --git a/advanced/gmail.gs b/advanced/gmail.gs index d866e05f6..ba1b1f98e 100644 --- a/advanced/gmail.gs +++ b/advanced/gmail.gs @@ -13,101 +13,123 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// [START apps_script_gmail_label] +// [START gmail_label] /** * Lists the user's labels, including name, type, * ID and visibility information. */ -function listLabelInfo() { - var response = - Gmail.Users.Labels.list('me'); - for (var i = 0; i < response.labels.length; i++) { - var label = response.labels[i]; - Logger.log(JSON.stringify(label)); + +function listLabelInfo(){ + try{ + const response = + Gmail.Users.Labels.list('me'); + for (let i = 0; i < response.labels.length; i++) { + const label = response.labels[i]; + Logger.log(JSON.stringify(label)); + } + } + catch(err){ + Logger.log(err) } } -// [END apps_script_gmail_label] +// [END gmail_label] -// [START apps_script_gmail_inbox_snippets] +// [START gmail_inbox_snippets] /** * Lists, for each thread in the user's Inbox, a * snippet associated with that thread. */ function listInboxSnippets() { - var pageToken; - do { - var threadList = Gmail.Users.Threads.list('me', { - q: 'label:inbox', - pageToken: pageToken - }); - if (threadList.threads && threadList.threads.length > 0) { - threadList.threads.forEach(function(thread) { - Logger.log('Snippet: %s', thread.snippet); + + try{ + let pageToken; + do { + const threadList = Gmail.Users.Threads.list('me', { + q: 'label:inbox', + pageToken: pageToken }); - } - pageToken = threadList.nextPageToken; - } while (pageToken); + if (threadList.threads && threadList.threads.length > 0) { + threadList.threads.forEach(function(thread) { + Logger.log('Snippet: %s', thread.snippet); + }); + } + pageToken = threadList.nextPageToken; + } while (pageToken); + } + catch(err){ + Logger.log(err) + } } -// [END apps_script_gmail_inbox_snippets] +// [END gmail_inbox_snippets] -// [START apps_script_gmail_history] +// [START gmail_history] /** * Gets a history record ID associated with the most * recently sent message, then logs all the message IDs * that have changed since that message was sent. */ -function logRecentHistory() { - // Get the history ID associated with the most recent - // sent message. - var sent = Gmail.Users.Threads.list('me', { - q: 'label:sent', - maxResults: 1 - }); - if (!sent.threads || !sent.threads[0]) { - Logger.log('No sent threads found.'); - return; - } - var historyId = sent.threads[0].historyId; - - // Log the ID of each message changed since the most - // recent message was sent. - var pageToken; - var changed = []; - do { - var recordList = Gmail.Users.History.list('me', { - startHistoryId: historyId, - pageToken: pageToken +function logRecentHistory () { + try{ + // Get the history ID associated with the most recent + // sent message. + const sent = Gmail.Users.Threads.list('me', { + q: 'label:sent', + maxResults: 1 }); - var history = recordList.history; - if (history && history.length > 0) { - history.forEach(function(record) { - record.messages.forEach(function(message) { - if (changed.indexOf(message.id) === -1) { - changed.push(message.id); - } - }); - }); + if (!sent.threads || !sent.threads[0]) { + Logger.log('No sent threads found.'); + return; } - pageToken = recordList.nextPageToken; - } while (pageToken); + const historyId = sent.threads[0].historyId; - changed.forEach(function(id) { - Logger.log('Message Changed: %s', id); - }); + // Log the ID of each message changed since the most + // recent message was sent. + let pageToken; + let changed = []; + do { + const recordList = Gmail.Users.History.list('me', { + startHistoryId: historyId, + pageToken: pageToken + }); + const history = recordList.history; + if (history && history.length > 0) { + history.forEach(function(record) { + record.messages.forEach(function(message) { + if (changed.indexOf(message.id) === -1) { + changed.push(message.id); + } + }); + }); + } + pageToken = recordList.nextPageToken; + } while (pageToken); + + changed.forEach(function(id) { + Logger.log('Message Changed: %s', id); + }); + } + catch(err){ + Logger.log(err) + } } -// [END apps_script_gmail_history] +// [END gmail_history] -// [START apps_script_gmail_raw] +// [START gmail_raw] function getRawMessage() { - var messageId = Gmail.Users.Messages.list('me').messages[0].id; - console.log(messageId); - var message = Gmail.Users.Messages.get('me', messageId, { - 'format': 'raw' - }); + try{ + const messageId = Gmail.Users.Messages.list('me').messages[0].id; + console.log(messageId); + const message = Gmail.Users.Messages.get('me', messageId, { + 'format': 'raw' + }); - // Get raw content as base64url encoded string. - var encodedMessage = Utilities.base64Encode(message.raw); - console.log(encodedMessage); + // Get raw content as base64url encoded string. + const encodedMessage = Utilities.base64Encode(message.raw); + console.log(encodedMessage); + } + catch(err){ + Logger.log(err) + } } -// [END apps_script_gmail_raw] +// [END gmail_raw] diff --git a/drive/quickstart/quickstart.gs b/drive/quickstart/quickstart.gs index 182ef16c8..7f6680848 100644 --- a/drive/quickstart/quickstart.gs +++ b/drive/quickstart/quickstart.gs @@ -17,14 +17,20 @@ /** * Lists the names and IDs of up to 10 files. */ -function listFiles() { - var files = Drive.Files.list({ - fields: 'nextPageToken, items(id, title)', - maxResults: 10 - }).items; - for (var i = 0; i < files.length; i++) { - var file = files[i]; - Logger.log('%s (%s)', file.title, file.id); +function listFiles () { + try{ + const files = Drive.Files.list({ + fields: 'nextPageToken, items(id, title)', + maxResults: 10 + }).items; + for (let i = 0; i < files.length; i++) { + const file = files[i]; + Logger.log('%s (%s)', file.title, file.id); + } + } + catch(err){ + //TODO(developer)-Handle Files.list() exception + Logger.log('failed with error %s',err.toString()); } } // [END drive_quickstart] diff --git a/gmail/markup/Code.gs b/gmail/markup/Code.gs index 61e2048d0..94b390fa5 100644 --- a/gmail/markup/Code.gs +++ b/gmail/markup/Code.gs @@ -1,14 +1,19 @@ -// [START apps_script_gmail_markup] +// [START gmail_send_email_with_markup] /** - * Tests the schema. + * Send an email with schemas in order to test email markup. */ function testSchemas() { - var htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); + try{ + const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); - MailApp.sendEmail({ - to: Session.getActiveUser().getEmail(), - subject: 'Test Email markup - ' + new Date(), - htmlBody: htmlBody, - }); + MailApp.sendEmail({ + to: Session.getActiveUser().getEmail(), + subject: 'Test Email markup - ' + new Date(), + htmlBody: htmlBody, + }); + } + catch(err){ + Logger.log(err) + } } -// [END apps_script_gmail_markup] +// [END gmail_send_email_with_markup] diff --git a/gmail/markup/mail_template.html b/gmail/markup/mail_template.html index a0f9782f7..68f4e9f3b 100644 --- a/gmail/markup/mail_template.html +++ b/gmail/markup/mail_template.html @@ -2,12 +2,12 @@ @@ -17,4 +17,4 @@ This a test for a Go-To action in Gmail.

- \ No newline at end of file + diff --git a/gmail/quickstart/quickstart.gs b/gmail/quickstart/quickstart.gs index 97f28a1de..14a67e7ce 100644 --- a/gmail/quickstart/quickstart.gs +++ b/gmail/quickstart/quickstart.gs @@ -1,5 +1,5 @@ /** - * Copyright Google LLC + * Copyright Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,18 +15,24 @@ */ // [START gmail_quickstart] /** - * Lists the labels in the user's account. + * Lists all labels in the user's mailbox */ function listLabels() { - var response = Gmail.Users.Labels.list('me'); - if (response.labels.length == 0) { - Logger.log('No labels found.'); - } else { + try{ + const response = Gmail.Users.Labels.list('me'); + if (response.labels.length == 0) { + Logger.log('No labels found.'); + return; + } Logger.log('Labels:'); - for (var i = 0; i < response.labels.length; i++) { - var label = response.labels[i]; + for (let i = 0; i < response.labels.length; i++) { + const label = response.labels[i]; Logger.log('- %s', label.name); } } + catch(err){ + //TODO(developer)-Handle Labels.list() exceptions + Logger.log('failed with error %s',err.toString()); + } } // [END gmail_quickstart] diff --git a/gmail/sendingEmails/sendingEmails.gs b/gmail/sendingEmails/sendingEmails.gs index 219e16ba8..92fdb1088 100644 --- a/gmail/sendingEmails/sendingEmails.gs +++ b/gmail/sendingEmails/sendingEmails.gs @@ -14,56 +14,69 @@ * limitations under the License. */ -// [START apps_script_gmail_send_emails] +// [START gmail_send_emails] /** * Sends emails with data from the current spreadsheet. */ function sendEmails() { - var sheet = SpreadsheetApp.getActiveSheet(); - var startRow = 2; // First row of data to process - var numRows = 2; // Number of rows to process - // Fetch the range of cells A2:B3 - var dataRange = sheet.getRange(startRow, 1, numRows, 2); - // Fetch values for each row in the Range. - var data = dataRange.getValues(); - for (var i in data) { - var row = data[i]; - var emailAddress = row[0]; // First column - var message = row[1]; // Second column - var subject = 'Sending emails from a Spreadsheet'; - MailApp.sendEmail(emailAddress, subject, message); + try{ + // Get the active sheet in spreadsheet + const sheet = SpreadsheetApp.getActiveSheet(); + let startRow = 2; // First row of data to process + let numRows = 2; // Number of rows to process + // Fetch the range of cells A2:B3 + const dataRange = sheet.getRange(startRow, 1, numRows, 2); + // Fetch values for each row in the Range. + const data = dataRange.getValues(); + for (let row of data) { + const emailAddress = row[0]; // First column + const message = row[1]; // Second column + let subject = 'Sending emails from a Spreadsheet'; + //Send emails to emailAddresses which are presents in First column + MailApp.sendEmail(emailAddress, subject, message); + } + } + catch(err){ + Logger.log(err) } } -// [END apps_script_gmail_send_emails] +// [END gmail_send_emails] -// [START apps_script_gmail_send_emails_2] +// [START gmail_send_non_duplicate_emails] // This constant is written in column C for rows for which an email // has been sent successfully. -var EMAIL_SENT = 'EMAIL_SENT'; +let EMAIL_SENT = 'EMAIL_SENT'; /** * Sends non-duplicate emails with data from the current spreadsheet. */ -function sendEmails2() { - var sheet = SpreadsheetApp.getActiveSheet(); - var startRow = 2; // First row of data to process - var numRows = 2; // Number of rows to process - // Fetch the range of cells A2:B3 - var dataRange = sheet.getRange(startRow, 1, numRows, 3); - // Fetch values for each row in the Range. - var data = dataRange.getValues(); - for (var i = 0; i < data.length; ++i) { - var row = data[i]; - var emailAddress = row[0]; // First column - var message = row[1]; // Second column - var emailSent = row[2]; // Third column - if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates - var subject = 'Sending emails from a Spreadsheet'; - MailApp.sendEmail(emailAddress, subject, message); - sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT); - // Make sure the cell is updated right away in case the script is interrupted - SpreadsheetApp.flush(); +function sendNonDuplicateEmails() { + try{ + // Get the active sheet in spreadsheet + const sheet = SpreadsheetApp.getActiveSheet(); + let startRow = 2; // First row of data to process + let numRows = 2; // Number of rows to process + // Fetch the range of cells A2:B3 + const dataRange = sheet.getRange(startRow, 1, numRows, 3); + // Fetch values for each row in the Range. + const data = dataRange.getValues(); + 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) { // Prevents sending duplicates + let subject = 'Sending emails from a Spreadsheet'; + // Send emails to emailAddresses which are presents in First column + MailApp.sendEmail(emailAddress, subject, message); + sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT); + // Make sure the cell is updated right away in case the script is interrupted + SpreadsheetApp.flush(); + } } } + catch(err){ + Logger.log(err) + } } -// [END apps_script_gmail_send_emails_2] +// [END gmail_send_non_duplicate_emails]