-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.gs
57 lines (42 loc) · 1.59 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
// Filter all travel invoice from GOJEK, for example:
// Matches: from:([email protected])
// Do this: Skip Inbox, Apply label "yourlabel"
// Change two necessary variables below:
// 1. yourlabel for email will be pulled
var mailLabel = "Invoice/Gojek/Travel";
// 2. allowed email for security reason
var adminMail = "[email protected]";
// Add Spreadsheet's UI menu
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Pull mails").addItem("Get travel details", "getMsg").addToUi();
}
/*
function test(){
const travelThread = GmailApp.getUserLabelByName(mailLabel).getThreads();
Logger.log(travelMessage = travelThread[0].getMessages()[1].getPlainBody());
} */
// Get mails
function getMsg() {
// security
if (Session.getActiveUser().getEmail() != adminMail) {
Browser.msgBox("Please Login as a valid users!");
return;
}
// index mails suitable to given label and their threads
var travelThread = GmailApp.getUserLabelByName(mailLabel).getThreads();
for(var i = 0; i < travelThread.length; i++) {
var travelMessage = travelThread[i].getMessages();
for(var j = 0; j < travelMessage.length; j++) {
var travelDetail = travelMessage[j];
// define fileds column
var datetime = travelDetail.getDate();
var subject = travelDetail.getSubject();
var sender = travelDetail.getFrom();
var content = travelDetail.getPlainBody();
// append to new rows
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeSheet.appendRow([datetime, sender, subject, content]);
}
}
}