Skip to content

Commit 159fcc4

Browse files
fix(gmail): resolve type errors
Adds JSDoc annotations and makes minor code adjustments to resolve TypeScript check errors in the 'gmail' directory. - Replaces non-standard `for each...in` loops with `for...of`. - Removes unhelpful `try...catch` blocks. - Adds checks for advanced services and undefined properties. - Adds `@typedef` for the add-on event object. - Adds a link to the reference documentation for the `gmailEvent` `@typedef`. - Fixes a typo in a comment. - Addresses a PR comment by adding a local `@typedef` for `Card`.
1 parent 275a497 commit 159fcc4

5 files changed

Lines changed: 86 additions & 80 deletions

File tree

gmail/add-ons/quickstart.gs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,30 @@
1515
*/
1616

1717
// [START apps_script_gmail_quick_start]
18+
/**
19+
* @typedef {Object} Card
20+
*/
21+
22+
/**
23+
* This is a partial definition of the a Gmail add-on event object.
24+
* For the full list of properties, see:
25+
* https://developers.google.com/workspace/add-ons/concepts/event-objects
26+
*
27+
* @typedef {Object} gmailEvent
28+
* @property {Object} messageMetadata
29+
* @property {string} messageMetadata.accessToken
30+
* @property {string} messageMetadata.messageId
31+
* @property {Object} formInputs
32+
* @property {string[]} formInputs.labels
33+
*/
34+
1835
/**
1936
* Returns the array of cards that should be rendered for the current
2037
* e-mail thread. The name of this function is specified in the
2138
* manifest 'onTriggerFunction' field, indicating that this function
2239
* runs every time the add-on is started.
2340
*
24-
* @param {Object} e The data provided by the Gmail UI.
41+
* @param {gmailEvent} e The data provided by the Gmail UI.
2542
* @return {Card[]}
2643
*/
2744
function buildAddOn(e) {
@@ -71,9 +88,9 @@ function buildAddOn(e) {
7188
* user selections. Runs via the OnChangeAction for
7289
* each CHECK_BOX created.
7390
*
74-
* @param {Object} e The data provided by the Gmail UI.
91+
* @param {gmailEvent} e The data provided by the Gmail UI.
7592
*/
76-
function toggleLabel(e){
93+
function toggleLabel(e) {
7794
var selected = e.formInputs.labels;
7895

7996
// Activate temporary Gmail add-on scopes.
@@ -85,30 +102,29 @@ function toggleLabel(e){
85102
var thread = message.getThread();
86103

87104
if (selected != null){
88-
for each (var label in GmailApp.getUserLabels()) {
89-
if(selected.indexOf(label.getName()) != -1){
90-
thread.addLabel(label);
91-
}
92-
else {
93-
thread.removeLabel(label);
94-
}
95-
}
96-
}
97-
else {
98-
for each (var label in GmailApp.getUserLabels()) {
105+
for (const label of GmailApp.getUserLabels()) {
106+
if (selected.indexOf(label.getName()) != -1) {
107+
thread.addLabel(label);
108+
} else {
109+
thread.removeLabel(label);
110+
}
111+
}
112+
} else {
113+
for (const label of GmailApp.getUserLabels()) {
99114
thread.removeLabel(label);
100115
}
101116
}
102117
}
103118

119+
104120
/**
105-
* Converts an GmailLabel object to a array of strings.
121+
* Converts a GmailLabel object to a array of strings.
106122
* Used for easy sorting and to determine if a value exists.
107123
*
108-
* @param {labelsObjects} A GmailLabel object array.
109-
* @return {lables[]} An array of labels names as strings.
124+
* @param {GoogleAppsScript.Gmail.GmailLabel[]} labelsObjects
125+
* @return {string[]} An array of labels names as strings.
110126
*/
111-
function getLabelArray(labelsObjects){
127+
function getLabelArray(labelsObjects) {
112128
var labels = [];
113129
for(var i = 0; i < labelsObjects.length; i++) {
114130
labels[i] = labelsObjects[i].getName();

gmail/inlineimage/inlineimage.gs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function sendEmailToMyself() {
2020
sendEmailWithInlineImage(Session.getActiveUser().getEmail());
2121
}
2222

23+
/**
24+
* @param {string} toAddress
25+
*/
2326
function sendEmailWithInlineImage(toAddress) {
2427
const options = {};
2528
const imageName = 'cat_emoji';

gmail/markup/Code.gs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
* Send an email with schemas in order to test email markup.
44
*/
55
function testSchemas() {
6-
try {
7-
const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent();
6+
const htmlBody =
7+
HtmlService.createHtmlOutputFromFile('mail_template').getContent();
88

9-
MailApp.sendEmail({
10-
to: Session.getActiveUser().getEmail(),
11-
subject: 'Test Email markup - ' + new Date(),
12-
htmlBody: htmlBody
13-
});
14-
} catch (err) {
15-
console.log(err.message);
16-
}
9+
MailApp.sendEmail({
10+
to: Session.getActiveUser().getEmail(),
11+
subject: 'Test Email markup - ' + new Date(),
12+
htmlBody: htmlBody,
13+
});
1714
}
1815
// [END gmail_send_email_with_markup]
1916

gmail/quickstart/quickstart.gs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@
2020
* @see https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list
2121
*/
2222
function listLabels() {
23-
try {
24-
// Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox
25-
const response = Gmail.Users.Labels.list('me');
26-
if (!response || response.labels.length === 0) {
27-
// TODO (developer) - No labels are returned from the response
28-
console.log('No labels found.');
29-
return;
30-
}
31-
// Print the Labels that are available.
32-
console.log('Labels:');
33-
for (const label of response.labels ) {
34-
console.log('- %s', label.name);
35-
}
36-
} catch (err) {
37-
// TODO (developer) - Handle exception on Labels.list() API
38-
console.log('Labels.list() API failed with error %s', err.toString());
23+
// Add this check to use the Gmail advanced service.
24+
if (!Gmail || !Gmail.Users || !Gmail.Users.Labels) {
25+
throw new Error('Enable the Gmail Advanced Service.');
26+
}
27+
// Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox
28+
const response = Gmail.Users.Labels.list('me');
29+
if (!response || !response.labels || response.labels.length === 0) {
30+
console.log('No labels found.');
31+
return;
32+
}
33+
// Print the Labels that are available.
34+
console.log('Labels:');
35+
for (const label of response.labels) {
36+
console.log('- %s', label.name);
3937
}
4038
}
4139
// [END gmail_quickstart]

gmail/sendingEmails/sendingEmails.gs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,16 @@
1919
* Sends emails with data from the current spreadsheet.
2020
*/
2121
function sendEmails() {
22-
try {
23-
const sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet in spreadsheet
24-
const startRow = 2; // First row of data to process
25-
const numRows = 2; // Number of rows to process
26-
const dataRange = sheet.getRange(startRow, 1, numRows, 2); // Fetch the range of cells A2:B3
27-
const data = dataRange.getValues(); // Fetch values for each row in the Range.
28-
for (const row of data) {
29-
const emailAddress = row[0]; // First column
30-
const message = row[1]; // Second column
31-
const subject = 'Sending emails from a Spreadsheet';
32-
MailApp.sendEmail(emailAddress, subject, message); // Send emails to emailAddresses which are presents in First column
33-
}
34-
} catch (err) {
35-
console.log(err);
22+
const sheet = SpreadsheetApp.getActiveSheet();
23+
const startRow = 2;
24+
const numRows = 2;
25+
const dataRange = sheet.getRange(startRow, 1, numRows, 2);
26+
const data = dataRange.getValues();
27+
for (const row of data) {
28+
const emailAddress = row[0];
29+
const message = row[1];
30+
const subject = 'Sending emails from a Spreadsheet';
31+
MailApp.sendEmail(emailAddress, subject, message);
3632
}
3733
}
3834
// [END gmail_send_emails]
@@ -42,29 +38,25 @@ function sendEmails() {
4238
* Sends non-duplicate emails with data from the current spreadsheet.
4339
*/
4440
function sendNonDuplicateEmails() {
45-
const EMAIL_SENT = 'email sent'; //This constant is used to write the message in Column C of Sheet
46-
try {
47-
const sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet in spreadsheet
48-
const startRow = 2; // First row of data to process
49-
const numRows = 2; // Number of rows to process
50-
const dataRange = sheet.getRange(startRow, 1, numRows, 3); // Fetch the range of cells A2:B3
51-
const data = dataRange.getValues(); // Fetch values for each row in the Range.
52-
for (let i = 0; i < data.length; ++i) {
53-
const row = data[i];
54-
const emailAddress = row[0]; // First column
55-
const message = row[1]; // Second column
56-
const emailSent = row[2]; // Third column
57-
if (emailSent === EMAIL_SENT) {
58-
console.log('Email already sent');
59-
return;
60-
}
61-
const subject = 'Sending emails from a Spreadsheet';
62-
MailApp.sendEmail(emailAddress, subject, message);// Send emails to emailAddresses which are presents in First column
63-
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
64-
SpreadsheetApp.flush(); // Make sure the cell is updated right away in case the script is interrupted
41+
const EMAIL_SENT = 'email sent';
42+
const sheet = SpreadsheetApp.getActiveSheet();
43+
const startRow = 2;
44+
const numRows = 2;
45+
const dataRange = sheet.getRange(startRow, 1, numRows, 3);
46+
const data = dataRange.getValues();
47+
for (let i = 0; i < data.length; ++i) {
48+
const row = data[i];
49+
const emailAddress = row[0];
50+
const message = row[1];
51+
const emailSent = row[2];
52+
if (emailSent === EMAIL_SENT) {
53+
console.log('Email already sent');
54+
return;
6555
}
66-
} catch (err) {
67-
console.log(err);
56+
const subject = 'Sending emails from a Spreadsheet';
57+
MailApp.sendEmail(emailAddress, subject, message);
58+
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
59+
SpreadsheetApp.flush();
6860
}
6961
}
7062
// [END gmail_send_non_duplicate_emails]

0 commit comments

Comments
 (0)