From 160d5dad4f43bcb97fde85fe0cdedb3f33673506 Mon Sep 17 00:00:00 2001 From: priyankarp12 <95030860+priyankarp12@users.noreply.github.com> Date: Fri, 26 Nov 2021 11:39:40 +0000 Subject: [PATCH 1/7] Update quickstart.gs --- gmail/quickstart/quickstart.gs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gmail/quickstart/quickstart.gs b/gmail/quickstart/quickstart.gs index 97f28a1de..d0ce76668 100644 --- a/gmail/quickstart/quickstart.gs +++ b/gmail/quickstart/quickstart.gs @@ -18,13 +18,13 @@ * Lists the labels in the user's account. */ function listLabels() { - var response = Gmail.Users.Labels.list('me'); + const response = Gmail.Users.Labels.list('me'); if (response.labels.length == 0) { Logger.log('No labels found.'); } else { 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); } } From 9ee82bb927b88e51c9db22827a47e682c8475679 Mon Sep 17 00:00:00 2001 From: priyankarp12 <95030860+priyankarp12@users.noreply.github.com> Date: Mon, 29 Nov 2021 17:19:21 +0530 Subject: [PATCH 2/7] Update sendEmail.gs --- gmail/sendingEmails/sendingEmails.gs | 52 +++++++++++++++------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/gmail/sendingEmails/sendingEmails.gs b/gmail/sendingEmails/sendingEmails.gs index 219e16ba8..341a03240 100644 --- a/gmail/sendingEmails/sendingEmails.gs +++ b/gmail/sendingEmails/sendingEmails.gs @@ -19,46 +19,50 @@ * 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 + // 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 - var dataRange = sheet.getRange(startRow, 1, numRows, 2); + const 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'; + const data = dataRange.getValues(); + for (let i in data) { + const row = data[i]; + 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); } } // [END apps_script_gmail_send_emails] -// [START apps_script_gmail_send_emails_2] +// [START apps_script_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 + // 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 - var dataRange = sheet.getRange(startRow, 1, numRows, 3); + const 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 + 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 - var subject = 'Sending emails from a Spreadsheet'; + 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 @@ -66,4 +70,4 @@ function sendEmails2() { } } } -// [END apps_script_gmail_send_emails_2] +// [END apps_script_gmail_send_non_duplicate_emails] From 7db4f6722b3914184f4584f4ffcb81abeca2a4b2 Mon Sep 17 00:00:00 2001 From: priyankarp12 <95030860+priyankarp12@users.noreply.github.com> Date: Mon, 29 Nov 2021 14:59:13 +0000 Subject: [PATCH 3/7] Update Code.gs Update region tag Update comment --- gmail/markup/Code.gs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gmail/markup/Code.gs b/gmail/markup/Code.gs index 61e2048d0..8fa0308b3 100644 --- a/gmail/markup/Code.gs +++ b/gmail/markup/Code.gs @@ -1,9 +1,9 @@ -// [START apps_script_gmail_markup] +// [START gmail_markup] /** - * Tests the schema. + * Send an email with schemas in order to test email markup. */ function testSchemas() { - var htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); + const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); MailApp.sendEmail({ to: Session.getActiveUser().getEmail(), @@ -11,4 +11,4 @@ function testSchemas() { htmlBody: htmlBody, }); } -// [END apps_script_gmail_markup] +// [END gmail_markup] From 92deecd33e5a381cf21419e4775f0a63367583bf Mon Sep 17 00:00:00 2001 From: priyankarp12 <95030860+priyankarp12@users.noreply.github.com> Date: Tue, 30 Nov 2021 18:33:25 +0530 Subject: [PATCH 4/7] Add try-catch block --- gmail/markup/Code.gs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gmail/markup/Code.gs b/gmail/markup/Code.gs index 8fa0308b3..af6e45ab6 100644 --- a/gmail/markup/Code.gs +++ b/gmail/markup/Code.gs @@ -3,6 +3,7 @@ * Send an email with schemas in order to test email markup. */ function testSchemas() { + try{ const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); MailApp.sendEmail({ @@ -10,5 +11,9 @@ function testSchemas() { subject: 'Test Email markup - ' + new Date(), htmlBody: htmlBody, }); + } + catch(err){ + Logger.log(err) + } } // [END gmail_markup] From 08efd5c9944c704fe24f5b99dc31d44c3ab73be1 Mon Sep 17 00:00:00 2001 From: priyankarp12 <95030860+priyankarp12@users.noreply.github.com> Date: Tue, 30 Nov 2021 18:36:26 +0530 Subject: [PATCH 5/7] Added try-catch --- gmail/quickstart/quickstart.gs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gmail/quickstart/quickstart.gs b/gmail/quickstart/quickstart.gs index d0ce76668..eb39cb01e 100644 --- a/gmail/quickstart/quickstart.gs +++ b/gmail/quickstart/quickstart.gs @@ -1,5 +1,5 @@ /** - * Copyright Google LLC + * Copyright 2021 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ * Lists the labels in the user's account. */ function listLabels() { + try{ const response = Gmail.Users.Labels.list('me'); if (response.labels.length == 0) { Logger.log('No labels found.'); @@ -28,5 +29,9 @@ function listLabels() { Logger.log('- %s', label.name); } } + } + catch(err){ + Logger.log(err) + } } // [END gmail_quickstart] From e656dc2fb037b57ade9e72e7da5b9104ae9cf4b7 Mon Sep 17 00:00:00 2001 From: priyankarp12 <95030860+priyankarp12@users.noreply.github.com> Date: Tue, 30 Nov 2021 18:38:54 +0530 Subject: [PATCH 6/7] Added try-catch --- gmail/sendingEmails/sendingEmails.gs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/gmail/sendingEmails/sendingEmails.gs b/gmail/sendingEmails/sendingEmails.gs index 341a03240..1817ac7b3 100644 --- a/gmail/sendingEmails/sendingEmails.gs +++ b/gmail/sendingEmails/sendingEmails.gs @@ -1,5 +1,5 @@ /** - * Copyright Google LLC + * Copyright 2021 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,11 +14,12 @@ * limitations under the License. */ -// [START apps_script_gmail_send_emails] +// [START gmail_send_emails] /** * Sends emails with data from the current spreadsheet. */ function sendEmails() { + try{ // Get the active sheet in spreadsheet const sheet = SpreadsheetApp.getActiveSheet(); let startRow = 2; // First row of data to process @@ -32,13 +33,17 @@ function sendEmails() { 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 + //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_non_duplicate_emails] +// [START gmail_send_non_duplicate_emails] // This constant is written in column C for rows for which an email // has been sent successfully. let EMAIL_SENT = 'EMAIL_SENT'; @@ -46,8 +51,9 @@ let EMAIL_SENT = 'EMAIL_SENT'; /** * Sends non-duplicate emails with data from the current spreadsheet. */ -function sendEmails2() { - // Get the active sheet in spreadsheet +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 @@ -69,5 +75,9 @@ function sendEmails2() { SpreadsheetApp.flush(); } } + } + catch(err){ + Logger.log(err) + } } -// [END apps_script_gmail_send_non_duplicate_emails] +// [END gmail_send_non_duplicate_emails] From f28c6989b96260b5cc7a2c4bc1dd9ea97721ac76 Mon Sep 17 00:00:00 2001 From: priyankarp12 <95030860+priyankarp12@users.noreply.github.com> Date: Tue, 30 Nov 2021 21:42:27 +0530 Subject: [PATCH 7/7] Added try-catch and update region comments 1.Updated code using let and const 2.Added try-catch 3.Updated region tag comments --- advanced/gmail.gs | 73 ++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/advanced/gmail.gs b/advanced/gmail.gs index d866e05f6..b8a55dd5a 100644 --- a/advanced/gmail.gs +++ b/advanced/gmail.gs @@ -1,5 +1,5 @@ /** - * Copyright Google LLC + * Copyright 2021 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,30 +13,37 @@ * 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 = + +function listLabelInfo(){ + try{ + const response = Gmail.Users.Labels.list('me'); - 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(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; + try{ + let pageToken; do { - var threadList = Gmail.Users.Threads.list('me', { + const threadList = Gmail.Users.Threads.list('me', { q: 'label:inbox', pageToken: pageToken }); @@ -47,20 +54,24 @@ function listInboxSnippets() { } 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() { +function logRecentHistory () { // Get the history ID associated with the most recent // sent message. - var sent = Gmail.Users.Threads.list('me', { + const sent = Gmail.Users.Threads.list('me', { q: 'label:sent', maxResults: 1 }); @@ -68,18 +79,19 @@ function logRecentHistory() { Logger.log('No sent threads found.'); return; } - var historyId = sent.threads[0].historyId; + const historyId = sent.threads[0].historyId; // Log the ID of each message changed since the most // recent message was sent. - var pageToken; - var changed = []; + try{ + let pageToken; + let changed = []; do { - var recordList = Gmail.Users.History.list('me', { + const recordList = Gmail.Users.History.list('me', { startHistoryId: historyId, pageToken: pageToken }); - var history = recordList.history; + const history = recordList.history; if (history && history.length > 0) { history.forEach(function(record) { record.messages.forEach(function(message) { @@ -91,23 +103,32 @@ function logRecentHistory() { } 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] - -// [START apps_script_gmail_raw] +// [END gmail_history] +// [START gmail_raw] function getRawMessage() { - var messageId = Gmail.Users.Messages.list('me').messages[0].id; + try{ + const messageId = Gmail.Users.Messages.list('me').messages[0].id; console.log(messageId); - var message = Gmail.Users.Messages.get('me', messageId, { + const message = Gmail.Users.Messages.get('me', messageId, { 'format': 'raw' }); // Get raw content as base64url encoded string. - var encodedMessage = Utilities.base64Encode(message.raw); + const encodedMessage = Utilities.base64Encode(message.raw); console.log(encodedMessage); + } + catch(err){ + Logger.log(err) + } } -// [END apps_script_gmail_raw] +// [END gmail_raw]