-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
58 lines (45 loc) · 1.69 KB
/
Code.gs
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
function filterInbox() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Criteria")
let lastrow = sheet.getLastRow()
// Build an array with [[From, Subject], [From, Subject], ...]
filter_array = sheet.getRange(2, 1, lastrow - 1, 2).getValues()
let threads = GmailApp.getInboxThreads()
console.log(`${GmailApp.getInboxUnreadCount()} unread in the inbox`)
// get all the messages for the current batch of threads
var messages = GmailApp.getMessagesForThreads(threads);
messages.forEach(function (message) {
// console.log(`${message.length} messages in this thread.`)
let from_address = function (message) {
if (message[0].getFrom().match(/<(.+)>/) == null) {
return message[0].getFrom()
} else {
return message[0].getFrom().match(/<(.+)>/)[1]
}
}(message)
let subject = message[0].getSubject()
// let body = message[0].getBody().substring(0,100)
// https://spreadsheet.dev/arrays-apps-script
console.log(`from: ${from_address}`)
console.log(`subject: ${subject}`)
let if_statement = filter_array.filter(
function (e) {
return 2 == e.filter(
function (f) {
return from_address.toLowerCase().match(f.toLowerCase()) || subject.toLowerCase().match(f.toLowerCase())
}
).length
}
).length
console.log(if_statement)
if (if_statement > 0) {
// Loop over all messages in this thread to delete
message.forEach(function (msg) {
let id = msg.getId()
console.log(`Moved to trash: ${id}`)
GmailApp.getMessageById(id).moveToTrash()
GmailApp.getMessageById(id).star()
})
}
})
}