Skip to content

Commit 101ce15

Browse files
authored
feat: Gmail List messages (#552)
1 parent 9d4f742 commit 101ce15

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

advanced/gmail.gs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function listInboxSnippets() {
4646
pageToken: pageToken
4747
});
4848
if (threadList.threads && threadList.threads.length > 0) {
49-
threadList.threads.forEach(function(thread) {
49+
threadList.threads.forEach(function (thread) {
5050
console.log('Snippet: %s', thread.snippet);
5151
});
5252
}
@@ -90,8 +90,8 @@ function logRecentHistory() {
9090
});
9191
const history = recordList.history;
9292
if (history && history.length > 0) {
93-
history.forEach(function(record) {
94-
record.messages.forEach(function(message) {
93+
history.forEach(function (record) {
94+
record.messages.forEach(function (message) {
9595
if (changed.indexOf(message.id) === -1) {
9696
changed.push(message.id);
9797
}
@@ -101,7 +101,7 @@ function logRecentHistory() {
101101
pageToken = recordList.nextPageToken;
102102
} while (pageToken);
103103

104-
changed.forEach(function(id) {
104+
changed.forEach(function (id) {
105105
console.log('Message Changed: %s', id);
106106
});
107107
} catch (err) {
@@ -130,3 +130,33 @@ function getRawMessage() {
130130
}
131131
}
132132
// [END gmail_raw]
133+
134+
// [START gmail_list_messages]
135+
/**
136+
* Lists unread messages in the user's inbox using the advanced Gmail service.
137+
*/
138+
function listMessages() {
139+
// The special value 'me' indicates the authenticated user.
140+
const userId = 'me';
141+
142+
// Define optional parameters for the request.
143+
const options = {
144+
maxResults: 10, // Limit the number of messages returned.
145+
q: 'is:unread', // Search for unread messages.
146+
};
147+
148+
try {
149+
// Call the Gmail.Users.Messages.list method.
150+
const response = Gmail.Users.Messages.list(userId, options);
151+
const messages = response.messages;
152+
console.log('Unread Messages:');
153+
154+
for (const message of messages) {
155+
console.log(`- Message ID: ${message.id}`);
156+
}
157+
} catch (err) {
158+
// Log any errors to the Apps Script execution log.
159+
console.log(`Failed with error: ${err.message}`);
160+
}
161+
}
162+
// [END gmail_list_messages]

0 commit comments

Comments
 (0)