-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
178 lines (138 loc) · 5.68 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var params = "";
var newAccount = false;
var textRaw = "";
var textAccount = "Unknown Account";
var account = "Unknown Account";
var messageText = "Unknown changes";
var changeCount = 0;
function doPost(request) {
params = request.parameters;
newAccount = false;
// make sure this came from us (Slack authentication token)
if (params.token == "HxQjg2tCqOIzVIxX1v7OUMD3") {
segmentText();
// don't do anything if there wasn't a semicolon
if (changeCount > 0) {
// this is the Ad History folder in Google Drive
var folder = DriveApp.getFolderById("0B7vBh6lfLDj6V2ZMVjhIMmVfOHc");
var files = folder.getFiles();
// see if a file already exists by this name
var testString = account + " - Ad History";
while (files.hasNext()) {
var file = files.next();
// regex test to make sure the search is case insensitive
var string1 = testString;
var string2 = file;
var regex = new RegExp('^' + string1 + '$', 'i');
// if it does, open it, we'll use this one (doc)
if (regex.test(string2)) {
var doc = DocumentApp.openById(file.getId());
var body = doc.getBody();
var text = body.editAsText();
var link = file.getUrl();
break;
}
// if there isn't a file with the Account Name, make a new one
if (!files.hasNext()) {
var doc = DocumentApp.create(testString);
var body = doc.getBody();
var text = body.editAsText();
// this is the title of the document
body.insertParagraph(0, account).setAttributes(style3);
body.appendParagraph("").setAttributes(style1);
newAccount = true;
}
}
// make an array of changes
var changes = new Array();
for (var i = 0; i < changeCount; i++) {
changes[i] = messageText[i] || "No change provided";
}
// convert Slack usernames into real names
var editorRaw = String(params.user_name);
var editor = "Unknown User";
if (editorRaw === "dj") {
editor = "Daniel James";
}
else if (editorRaw === "jwilson") {
editor = "Jack Wilson";
}
else if (editorRaw === "jlundy") {
editor = "Justin Lundy";
}
else if (editorRaw === "kmckillop") {
editor = "Kevin McKillop";
}
// add a timestamp
var date = Utilities.formatDate(new Date(), "America/Toronto", "EEEE, MMMM d, yyyy");
var time = Utilities.formatDate(new Date(), "America/Toronto", "h:mm a");
// this is the slightly grey line above the changes
var textToAppend = editor + ' made the following edits on ' + date + " at " + time + "\n";
body.appendParagraph(textToAppend).setAttributes(style1);
// add the changes as bullets
for (var i = 0; i < changes.length; i++) {
body.appendListItem(changes[i]).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes(style2);
}
// new line and horizontal rule to make it cleaner
text.appendText('\n');
doc.appendHorizontalRule();
text.appendText('\n');
// we're done editing
doc.saveAndClose();
// if we created a new document, we need to move it into the Ad History folder by copying and
// then deleting it because Google Apps Script doesn't have a move function for some stupid reason
if (newAccount === true) {
var files = DriveApp.getRootFolder().getFiles();
while (files.hasNext()) {
var file = files.next();
// regex test to make sure the search is case insensitive
var string1 = testString;
var string2 = file;
var regex = new RegExp('^' + string1 + '$', 'i');
if (regex.test(string2)) {
folder.addFile(file);
var pull = DriveApp.getRootFolder();
pull.removeFile(file);
break;
}
}
// so we can link to the document in the confirmation
var link = file.getUrl();
}
// send a confirmation into Slack
postResponse("history", params.user_name, newAccount, account, link);
}
} else {
return;
}
}
function segmentText() {
// assign the raw text to a variable
textRaw = String(params.text);
// split it into Account - Changes;
// textAccount = textRaw.split(/\s{1}-\s{1}/g);
var indexOfDash = textRaw.indexOf(" - ");
account = textRaw.slice(0, indexOfDash);
// account name (left of the " - ")
// account = textAccount[0] || "No account found with this name";
messageText = textRaw.slice(indexOfDash + 3).split(/\s*;\s*/g);
// changes (split by semicolons)
// messageText = textAccount[1].split(/\s*;\s*/g);
// the number of semicolons
changeCount = (textAccount[1].match(/;/g) || []).length;
}
var style1 = {};// style example 1
style1[DocumentApp.Attribute.FONT_SIZE] = 10;
style1[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.CALIBRI;
style1[DocumentApp.Attribute.FOREGROUND_COLOR] = "#555555";
style1[DocumentApp.Attribute.BOLD] = false;
var style2 = {};// style example 2
style2[DocumentApp.Attribute.FONT_SIZE] = 10;
style2[DocumentApp.Attribute.FONT_FAMILY] =DocumentApp.FontFamily.PROXIMA_NOVA;
style2[DocumentApp.Attribute.FOREGROUND_COLOR] = "#111111";
style2[DocumentApp.Attribute.BOLD] = false;
var style3 = {};// style example 3
style3[DocumentApp.Attribute.FONT_SIZE] = 30;
style3[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.ARIAL;
style3[DocumentApp.Attribute.FOREGROUND_COLOR] = "#000000";
style3[DocumentApp.Attribute.BOLD] = true;