Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions drive/quickstart/quickstart.gs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
/**
* 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){
Logger.log(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not critical, but here and elsewhere where catching exceptions for illustrative purposes to include a comment indicating two things:

  • The developer should likely modify the code
  • What is potentially failing

This is particularly important if the try block covers a large section of code as it's not always obvious where the failure might come from. Doesn't have to be a lengthy explanation, can be as short as "// TODO(developer) - Handle files.list() exceptions here"

}
}
// [END drive_quickstart]
23 changes: 14 additions & 9 deletions gmail/markup/Code.gs
Original file line number Diff line number Diff line change
@@ -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]
10 changes: 5 additions & 5 deletions gmail/markup/mail_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<head>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"description": "Check this out",
"@context": "https://schema.org",
"@type": "EmailMessage",
"description": "Check this out",
"potentialAction": {
"@type": "ViewAction",
"target": "https://www.youtube.com/watch?v=eH8KwfdkSqU"
"target": "https://www.youtube.com/watch?v=eH8KwfdkSqU"
}
}
</script>
Expand All @@ -17,4 +17,4 @@
This a test for a Go-To action in Gmail.
</p>
</body>
</html>
</html>
21 changes: 13 additions & 8 deletions gmail/quickstart/quickstart.gs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,18 +15,23 @@
*/
// [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){
Logger.log(err)
}
}
// [END gmail_quickstart]
87 changes: 50 additions & 37 deletions gmail/sendingEmails/sendingEmails.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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]