Skip to content

Commit 610b7e6

Browse files
Updated to ES6 (#240)
Co-authored-by: Priyankarp24 <[email protected]>
1 parent 79d05e1 commit 610b7e6

File tree

6 files changed

+188
-136
lines changed

6 files changed

+188
-136
lines changed

advanced/gmail.gs

Lines changed: 91 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,101 +13,123 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
// [START apps_script_gmail_label]
16+
// [START gmail_label]
1717
/**
1818
* Lists the user's labels, including name, type,
1919
* ID and visibility information.
2020
*/
21-
function listLabelInfo() {
22-
var response =
23-
Gmail.Users.Labels.list('me');
24-
for (var i = 0; i < response.labels.length; i++) {
25-
var label = response.labels[i];
26-
Logger.log(JSON.stringify(label));
21+
22+
function listLabelInfo(){
23+
try{
24+
const response =
25+
Gmail.Users.Labels.list('me');
26+
for (let i = 0; i < response.labels.length; i++) {
27+
const label = response.labels[i];
28+
Logger.log(JSON.stringify(label));
29+
}
30+
}
31+
catch(err){
32+
Logger.log(err)
2733
}
2834
}
29-
// [END apps_script_gmail_label]
35+
// [END gmail_label]
3036

31-
// [START apps_script_gmail_inbox_snippets]
37+
// [START gmail_inbox_snippets]
3238
/**
3339
* Lists, for each thread in the user's Inbox, a
3440
* snippet associated with that thread.
3541
*/
3642
function listInboxSnippets() {
37-
var pageToken;
38-
do {
39-
var threadList = Gmail.Users.Threads.list('me', {
40-
q: 'label:inbox',
41-
pageToken: pageToken
42-
});
43-
if (threadList.threads && threadList.threads.length > 0) {
44-
threadList.threads.forEach(function(thread) {
45-
Logger.log('Snippet: %s', thread.snippet);
43+
44+
try{
45+
let pageToken;
46+
do {
47+
const threadList = Gmail.Users.Threads.list('me', {
48+
q: 'label:inbox',
49+
pageToken: pageToken
4650
});
47-
}
48-
pageToken = threadList.nextPageToken;
49-
} while (pageToken);
51+
if (threadList.threads && threadList.threads.length > 0) {
52+
threadList.threads.forEach(function(thread) {
53+
Logger.log('Snippet: %s', thread.snippet);
54+
});
55+
}
56+
pageToken = threadList.nextPageToken;
57+
} while (pageToken);
58+
}
59+
catch(err){
60+
Logger.log(err)
61+
}
5062
}
51-
// [END apps_script_gmail_inbox_snippets]
63+
// [END gmail_inbox_snippets]
5264

5365

54-
// [START apps_script_gmail_history]
66+
// [START gmail_history]
5567
/**
5668
* Gets a history record ID associated with the most
5769
* recently sent message, then logs all the message IDs
5870
* that have changed since that message was sent.
5971
*/
60-
function logRecentHistory() {
61-
// Get the history ID associated with the most recent
62-
// sent message.
63-
var sent = Gmail.Users.Threads.list('me', {
64-
q: 'label:sent',
65-
maxResults: 1
66-
});
67-
if (!sent.threads || !sent.threads[0]) {
68-
Logger.log('No sent threads found.');
69-
return;
70-
}
71-
var historyId = sent.threads[0].historyId;
72-
73-
// Log the ID of each message changed since the most
74-
// recent message was sent.
75-
var pageToken;
76-
var changed = [];
77-
do {
78-
var recordList = Gmail.Users.History.list('me', {
79-
startHistoryId: historyId,
80-
pageToken: pageToken
72+
function logRecentHistory () {
73+
try{
74+
// Get the history ID associated with the most recent
75+
// sent message.
76+
const sent = Gmail.Users.Threads.list('me', {
77+
q: 'label:sent',
78+
maxResults: 1
8179
});
82-
var history = recordList.history;
83-
if (history && history.length > 0) {
84-
history.forEach(function(record) {
85-
record.messages.forEach(function(message) {
86-
if (changed.indexOf(message.id) === -1) {
87-
changed.push(message.id);
88-
}
89-
});
90-
});
80+
if (!sent.threads || !sent.threads[0]) {
81+
Logger.log('No sent threads found.');
82+
return;
9183
}
92-
pageToken = recordList.nextPageToken;
93-
} while (pageToken);
84+
const historyId = sent.threads[0].historyId;
9485

95-
changed.forEach(function(id) {
96-
Logger.log('Message Changed: %s', id);
97-
});
86+
// Log the ID of each message changed since the most
87+
// recent message was sent.
88+
let pageToken;
89+
let changed = [];
90+
do {
91+
const recordList = Gmail.Users.History.list('me', {
92+
startHistoryId: historyId,
93+
pageToken: pageToken
94+
});
95+
const history = recordList.history;
96+
if (history && history.length > 0) {
97+
history.forEach(function(record) {
98+
record.messages.forEach(function(message) {
99+
if (changed.indexOf(message.id) === -1) {
100+
changed.push(message.id);
101+
}
102+
});
103+
});
104+
}
105+
pageToken = recordList.nextPageToken;
106+
} while (pageToken);
107+
108+
changed.forEach(function(id) {
109+
Logger.log('Message Changed: %s', id);
110+
});
111+
}
112+
catch(err){
113+
Logger.log(err)
114+
}
98115
}
99-
// [END apps_script_gmail_history]
116+
// [END gmail_history]
100117

101-
// [START apps_script_gmail_raw]
118+
// [START gmail_raw]
102119
function getRawMessage() {
103-
var messageId = Gmail.Users.Messages.list('me').messages[0].id;
104-
console.log(messageId);
105-
var message = Gmail.Users.Messages.get('me', messageId, {
106-
'format': 'raw'
107-
});
120+
try{
121+
const messageId = Gmail.Users.Messages.list('me').messages[0].id;
122+
console.log(messageId);
123+
const message = Gmail.Users.Messages.get('me', messageId, {
124+
'format': 'raw'
125+
});
108126

109-
// Get raw content as base64url encoded string.
110-
var encodedMessage = Utilities.base64Encode(message.raw);
111-
console.log(encodedMessage);
127+
// Get raw content as base64url encoded string.
128+
const encodedMessage = Utilities.base64Encode(message.raw);
129+
console.log(encodedMessage);
130+
}
131+
catch(err){
132+
Logger.log(err)
133+
}
112134
}
113-
// [END apps_script_gmail_raw]
135+
// [END gmail_raw]

drive/quickstart/quickstart.gs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
/**
1818
* Lists the names and IDs of up to 10 files.
1919
*/
20-
function listFiles() {
21-
var files = Drive.Files.list({
22-
fields: 'nextPageToken, items(id, title)',
23-
maxResults: 10
24-
}).items;
25-
for (var i = 0; i < files.length; i++) {
26-
var file = files[i];
27-
Logger.log('%s (%s)', file.title, file.id);
20+
function listFiles () {
21+
try{
22+
const files = Drive.Files.list({
23+
fields: 'nextPageToken, items(id, title)',
24+
maxResults: 10
25+
}).items;
26+
for (let i = 0; i < files.length; i++) {
27+
const file = files[i];
28+
Logger.log('%s (%s)', file.title, file.id);
29+
}
30+
}
31+
catch(err){
32+
//TODO(developer)-Handle Files.list() exception
33+
Logger.log('failed with error %s',err.toString());
2834
}
2935
}
3036
// [END drive_quickstart]

gmail/markup/Code.gs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
// [START apps_script_gmail_markup]
1+
// [START gmail_send_email_with_markup]
22
/**
3-
* Tests the schema.
3+
* Send an email with schemas in order to test email markup.
44
*/
55
function testSchemas() {
6-
var htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent();
6+
try{
7+
const htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent();
78

8-
MailApp.sendEmail({
9-
to: Session.getActiveUser().getEmail(),
10-
subject: 'Test Email markup - ' + new Date(),
11-
htmlBody: htmlBody,
12-
});
9+
MailApp.sendEmail({
10+
to: Session.getActiveUser().getEmail(),
11+
subject: 'Test Email markup - ' + new Date(),
12+
htmlBody: htmlBody,
13+
});
14+
}
15+
catch(err){
16+
Logger.log(err)
17+
}
1318
}
14-
// [END apps_script_gmail_markup]
19+
// [END gmail_send_email_with_markup]

gmail/markup/mail_template.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<head>
33
<script type="application/ld+json">
44
{
5-
"@context": "http://schema.org",
6-
"@type": "EmailMessage",
7-
"description": "Check this out",
5+
"@context": "https://schema.org",
6+
"@type": "EmailMessage",
7+
"description": "Check this out",
88
"potentialAction": {
99
"@type": "ViewAction",
10-
"target": "https://www.youtube.com/watch?v=eH8KwfdkSqU"
10+
"target": "https://www.youtube.com/watch?v=eH8KwfdkSqU"
1111
}
1212
}
1313
</script>
@@ -17,4 +17,4 @@
1717
This a test for a Go-To action in Gmail.
1818
</p>
1919
</body>
20-
</html>
20+
</html>

gmail/quickstart/quickstart.gs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright Google LLC
2+
* Copyright Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,24 @@
1515
*/
1616
// [START gmail_quickstart]
1717
/**
18-
* Lists the labels in the user's account.
18+
* Lists all labels in the user's mailbox
1919
*/
2020
function listLabels() {
21-
var response = Gmail.Users.Labels.list('me');
22-
if (response.labels.length == 0) {
23-
Logger.log('No labels found.');
24-
} else {
21+
try{
22+
const response = Gmail.Users.Labels.list('me');
23+
if (response.labels.length == 0) {
24+
Logger.log('No labels found.');
25+
return;
26+
}
2527
Logger.log('Labels:');
26-
for (var i = 0; i < response.labels.length; i++) {
27-
var label = response.labels[i];
28+
for (let i = 0; i < response.labels.length; i++) {
29+
const label = response.labels[i];
2830
Logger.log('- %s', label.name);
2931
}
3032
}
33+
catch(err){
34+
//TODO(developer)-Handle Labels.list() exceptions
35+
Logger.log('failed with error %s',err.toString());
36+
}
3137
}
3238
// [END gmail_quickstart]

0 commit comments

Comments
 (0)