-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdfData.gs
143 lines (126 loc) · 3.25 KB
/
fdfData.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* FDF Data Extraction
* By Eric Lin
*
* This script SHOULD live in a Google Spreadsheet's Script Editor
*
* It will scan the user's emails for anything from ServiceLink and FDF related
* And put it in the spreadsheet accordingly
*/
function extractData() {
var restart = true;
var sheet = SpreadsheetApp.getActiveSheet();
var sheetHeader = sheet.getRange(1, 1, 1, 8);
if(sheetHeader.isBlank())
{
sheetHeader.setValues([["FDF Number", "Date Submitted", "Department", "Requested Action", "Short Description", "Start Date", "Attachment Name", "Attachment URL"]]);
}
var labelList = GmailApp.getUserLabels();
var labelExists = false;
for (var i=0; i<labelList.length; i++)
{
if(labelList[i].getName() === "Processed FDFs")
{
labelExists = true;
}
}
if (labelExists && restart)
{
GmailApp.getUserLabelByName("Processed FDFs").deleteLabel();
restart = false;
labelExists = false;
}
if (!labelExists)
{
GmailApp.createLabel("Processed FDFs");
}
var label = GmailApp.getUserLabelByName("Processed FDFs");
var threads = GmailApp.search("!label:processed-fdfs from:([email protected]) FDF");
//var threads = GmailApp.search("!label:processed-fdfs from:([email protected]) FDF");
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var from = messages[j].getFrom();
if(messages[j].getFrom().match("[email protected]"))
//if(messages[j].getFrom().match("[email protected]"))
{
var msg = messages[j].getBody();
var sub = messages[j].getSubject();
var dat = messages[j].getDate();
var numFDF = extractFDFNumber(sub);
var dept = extractDepartmentName(msg);
var action = extractRequestedAction(msg);
var desc = extractShortDescription(msg);
var start = extractStartDate(msg);
var attachName = extractAttachmentName(msg);
var attachURL = extractAttachmentURL(msg);
sheet.appendRow([numFDF, dat, dept, action, desc, start, attachName, attachURL]);
}
}
threads[i].addLabel(label);
}
}
function extractFDFNumber(subject) {
return subject.match(/\d+/)[0];
}
function extractDepartmentName(body) {
try{
var name = body.match(/Department Name:\s*(.*?)<\/div>/)[1];
return name;
}
catch(err)
{
return "";
}
}
function extractRequestedAction(body) {
try{
var action = body.match(/Requested Action:\s*(.*?)<\/div>/)[1];
return action;
}
catch(err)
{
return "";
}
}
function extractShortDescription(body) {
try{
var desc = body.match(/Short Description:\s*(.*?)<\/div>/)[1];
return desc;
}
catch(err)
{
return "";
}
}
function extractStartDate(body) {
try{
var start = body.match(/Start Date:\s*(.*?)<\/div>/)[1];
return start;
}
catch(err)
{
return "";
}
}
function extractAttachmentName(body) {
try{
var name = body.match(/Attachment:\s*<a\s*href=".*?>(.*?)<\/a><\/div>/)[1];
return name;
}
catch(err)
{
return "";
}
}
function extractAttachmentURL(body) {
try{
var url = body.match(/Attachment:\s*<a\s*href="(.*?)"\s*.*?<\/div>/)[1];
return url;
}
catch(err)
{
return "";
}
}