-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmail.js
60 lines (50 loc) · 1.72 KB
/
gmail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function getUnreadMessagesByLabel(labelName) {
let label = GmailApp.getUserLabelByName(labelName);
if (!label) {
throw new Error(`Label "${labelName}" not found.`);
}
// ラベルに付けられた未読メッセージの総数を取得
let unreadThreadsCount = label.getUnreadCount();
let threadsRead = 0;
let maxThreadsToFetch = 10; // 一度に取得するスレッドの最大数
let unreadMessages = [];
let start = 0;
// すべての未読メッセージを取得するまでループ
while (threadsRead < unreadThreadsCount) {
let threads = label.getThreads(start, maxThreadsToFetch);
for (let thread of threads) {
if (thread.isUnread()) {
threadsRead++;
let messages = thread.getMessages();
for (let message of messages) {
if (message.isUnread()) {
unreadMessages.push(message);
}
}
}
}
start += maxThreadsToFetch;
}
return unreadMessages;
}
function markMessagesAsRead(messages) {
messages.forEach(message => {
let messageId = message.getId();
let messageToMarkRead = GmailApp.getMessageById(messageId);
messageToMarkRead.markRead();
});
}
function printAllGmailLabels() {
// Gmailのすべてのラベルを取得
let labels = GmailApp.getUserLabels();
// ラベルが存在しない場合
if (labels.length === 0) {
console.log("No labels found in Gmail.");
return;
}
// 各ラベルの名前をコンソールに出力
console.log("Gmail Labels:");
labels.forEach(label => {
console.log(label.getName());
});
}