-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_upload.gs
357 lines (310 loc) · 11.1 KB
/
bulk_upload.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Copyright (C) 2020, Sol Boucher
// Copyright (C) 2020, The Vanguard Campaign Corps Mods (vanguardcampaign.org)
// @OnlyCurrentDoc
// Session.getEffectiveUser().getEmail()
const USE_STAGING = true;
const REQUESTS_SHEET = 'Requests';
const TYPES_SHEET = 'Request types';
const TYPE_COLUMN = 1;
const FILENAME_COLUMN = 2;
const CLAIM_COLUMN = 3;
const DONE_COLUMN = 4;
const ALLOWLIST = {
'contact[cell]': true,
'contact[optOut]': true,
// Type 'Absentee ballot survey':
'question[Do they see the link?]': true,
'question[Finished?]': true,
'question[Have they requested an absentee ballot yet?]': true,
'question[Want additional help?]': true,
'question[Will they actually vote in-person?]': true,
// Type 'HubDialer voter ID':
'Absentee Ballot': true,
'Date': true,
'Phone Number': true,
'Support': true,
'Status': true,
// Type 'Persuasion Campaign':
'question[RO Ask_1]': true,
'question[RO Ask_2]': true,
'question[RO Ask_3]': true,
'question[RO Ask_4]': true,
'question[RO Ask_5]': true,
'question[VBM Issues?]': true,
'question[Vote by mail?]': true,
'question[Will they vote for Jamaal?]': true,
// Type 'Third Outreach':
'question[Will you reach out to 3-5 voters?]': true,
// Type 'Undecided phonebank':
'First Call Date': true,
'Pledge': true,
// Type 'Virtual town hall':
'question[Will this person attend?]': true,
'tags': true,
// Type 'Westchester absentee ballot request':
'DT_ADDED': transform(addColumnBoolean, 'DT_ADDED', 'DT_ADDED,ADDED'),
'DT_REQUEST': transform(addColumnBoolean, 'DT_REQUEST', 'DT_REQUEST,REQUEST'),
'DT_RETURN': transform(addColumnBoolean, 'DT_RETURN', 'DT_RETURN,RETURN'),
'DT_MAILED': transform(addColumnBoolean, 'DT_MAILED', 'DT_MAILED,MAILED'),
// Type 'WFP voter ID':
'question[In District?]': true,
'question[VBM Issues?_1]': true,
'question[Vote by mail?_1]': true,
'question[Voter Disposition]': true,
// Type 'WFP voter ID consolidated':
'cell': true,
'date_updated': transform(truncateColumnSpace, 'date_updated'),
'question_response': true,
};
const TAGS = [
'Absentee - Will get to it later',
'Angry/Refused',
'Engel Supporter',
'Going to Attend',
'Maybe',
'Moved',
'Petition',
'Spanish',
'Supporter/Not going to attend',
'Trump supporter',
// The last tag is special: if set, all others will be cleared!
'Wrong Number',
];
function doEdit(ed) {
var range = ed.range;
if(range.getSheet().getName() != REQUESTS_SHEET
|| range.getNumRows() != 1 || range.getNumColumns() != 1)
return;
else if(range.getColumn() == DONE_COLUMN) {
var claim = range.getSheet().getRange(range.getRow(), CLAIM_COLUMN);
if(claim.getValue() == '')
claim.setValue('??');
return;
} else if(range.getColumn() != CLAIM_COLUMN)
return;
else if(!ed.value) {
range.insertCheckboxes();
return;
} else if(!range.isChecked())
return;
var self = 'self';
var target = '';
if(USE_STAGING) {
self += '_staging';
target = ' target="_blank"';
}
var href = PropertiesService.getScriptProperties().getProperty(self) + '?record=' + range.getRow();
var link = HtmlService.createHtmlOutput('<a href="' + href + '"' + target + '>' + href + '</a>');
SpreadsheetApp.getUi().showModalDialog(link, 'Click to download');
}
function doGet(ter) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName(REQUESTS_SHEET);
var row = ter.parameter.record;
var filename = sheet.getRange(row, FILENAME_COLUMN).getValue();
var type = sheet.getRange(row, TYPE_COLUMN).getValue();
var vanid = lookup(TYPES_SHEET, type);
if(!vanid)
return HtmlService.createHtmlOutput('Unsupported request type: \'' + type + '\'')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
var csv = get(PropertiesService.getScriptProperties().getProperty('downloader') + '?'
+ 'access_token=' + ScriptApp.getOAuthToken() + '&'
+ 'tracker=' + doc.getId() + '&'
+ 'record=' + row);
sheet.getRange(row, CLAIM_COLUMN).removeCheckboxes();
if(filename.endsWith('.txt'))
csv = csv.replace(/^([A-Z]*[0-9]+)[^\n]+[A-Z](\d{4})?(\d{2})?(\d{2})?[^\n]*$/mg, '$1,$2-$3-$4');
if(csv.indexOf(',') == -1)
return HtmlService.createHtmlOutput(csv)
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
if(filename.endsWith('.txt'))
return serve(filename + '.csv', 'countyId,date\n' + csv);
// Start with a good 'ol dos2unix.
csv = csv.replace(/\r/g, '');
// Terminate the file with a newline if there isn't one already.
if(!csv.endsWith('\n'))
csv += '\n';
// Add a dummy column on non-Spoke files to force the comma replacement
// logic to apply to strings in the last column
// TODO: In the future, we should not treat the last column specially
if(!csv.match(/^[^\n]+,tags\n/)) {
csv = csv.replace(/\n/g, ",\n");
}
if(vanid.primary_key) {
if(vanid.no_header) {
var numcols = csv.replace(/\n.*/, '').replace(/"[^"]*"/g, '').replace(/[^,]+/g, '').length + 1;
var lblcols = new Array(numcols).fill(undefined).map(function(ign, index) {
return String.fromCharCode('A'.charCodeAt(0) + index);
});
csv = lblcols.join(',') + '\n' + csv;
}
var missing = [];
for(var heading in vanid)
if(heading != 'no_header' && heading != 'primary_key')
if(vanid[heading])
ALLOWLIST[vanid[heading]] = transform(function(value) {
return addColumnBoolean(truncateColumnSpace(value));
}, vanid[heading], heading + heading.replace(/.*_/, ','));
else {
missing.push(heading);
missing.push(heading.replace(/.*_/, ''));
}
if(missing.length) {
var nays = missing.map(function() {
return false;
}).join(',');
ALLOWLIST[vanid.primary_key] = transform(function() {
return nays;
}, vanid.primary_key, missing.join(','));
}
vanid = vanid.primary_key;
}
// Remove double-double quotes since they compromise the comma replacement logic
csv = csv.replace(/""/g, '');
// And for my next trick, I'll disappear all strings except the last one.
// This assumes that the tags column is the very last one!
csv = csv.replace(/"([^"]+)",/g, function(match, quoted) {
return quoted.replace(/,|\n/g, ';') + ',';
});
// Compile a list of the columns we'll be preserving. Note that we always "keep" vanId.
var preserve = csv.match(/^.*$/m)[0].split(',');
for(var idx = 0; idx < preserve.length; ++idx) {
var action = ALLOWLIST[preserve[idx]];
if(action)
preserve[idx] = [idx, action];
}
preserve = Object.fromEntries(preserve.filter(Array.isArray));
// Time to actually remove the unwanted columns, then add the vanId one.
var vanidx = -1;
var unkeyed = 0;
var records = 0;
var datadump = '';
csv = csv.replace(/^[^"\n]+/mg, function(line) {
var fields = line.split(',');
var line = [];
for(var idx = 0; idx < fields.length; ++idx) {
var transform = preserve[String(idx)];
if(typeof transform == 'function')
line.push(transform(fields[idx]));
else if(transform)
line.push(fields[idx]);
}
line = line.join(',');
if(vanidx == -1) {
vanidx = fields.indexOf(vanid);
return 'primary_key,' + line;
}
var id = fields[vanidx];
if(id)
id = id.replace(/^.+(\d{10})$/, '$1');
else if(fields[0]) {
id = '0';
++unkeyed;
datadump += '<br>' + JSON.stringify({ fields: fields, vanidx: vanidx, id: id });
}
++records;
return id + ',' + line;
});
if(unkeyed == records)
return HtmlService.createHtmlOutput('Data file missing primary key column: \'' + vanid + '\':' + datadump)
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
var date = filename.match(/[0-9]{4}[-_][0-9]{2}[-_][0-9]{2}/);
if(date)
date = date[0].replace(/_/g, '-');
if(!csv.match(/^[^\n]+,tags\n/)) {
if(date) {
var labelled = false;
csv = csv.replace(/\n/g, function() {
if(!labelled) {
labelled = true;
return ',date\n';
}
return ',' + date + '\n';
});
}
return serve(filename, csv);
}
// Add a date column parsed from the filename and explode tags into multiple columns.
var skipped = false;
csv = csv.replace(/tags$/m, 'date,tag[' + TAGS.join('],tag[') + ']');
csv = csv.replace(/,([^,\n]+|"[^"]+")?$/mg, function(match, stripped) {
if(!skipped) {
skipped = true;
return match;
} else if(!stripped)
return ',' + date + ','.repeat(TAGS.length);
var repl = ',' + date;
var tags = stripped.replace(/"/g, '').split(',');
if(tags.includes(TAGS[TAGS.length - 1]))
return repl + ','.repeat(TAGS.length) + 'true';
for(var tag of TAGS) {
repl += ',';
if(tags.includes(tag))
repl += 'true';
}
return repl;
});
return serve(filename, csv);
}
function transform(transform, oldlabel, newlabel = oldlabel) {
return function(value) {
if(value == oldlabel)
return newlabel;
return transform(value);
}
}
function addColumnBoolean(value) {
return value + ',' + Boolean(value);
}
function truncateColumnSpace(value) {
return value.split(' ')[0];
}
function lookup(table, key, fallback = null) {
if(!key)
return fallback;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(table);
if(sheet.getLastRow() < 2)
return fallback;
var cell = sheet.getRange(2, 1, sheet.getLastRow() - 1)
.createTextFinder(key)
.matchEntireCell(true)
.findNext();
if(!cell)
return fallback;
var cols = sheet.getLastColumn();
if(cols == 2)
return sheet.getRange(cell.getRow(), 2).getValue();
else {
var result = {};
for(var col = 2; col <= cols; ++col)
result[sheet.getRange(1, col).getValue()] = sheet.getRange(cell.getRow(), col).getValue();
return result;
}
}
function get(resource, authorization) {
if(authorization)
authorization = {
headers: {
Authorization: 'Bearer ' + authorization,
},
};
return UrlFetchApp.fetch(resource, authorization).getContentText();
}
function serve(filename, contents, mime = ContentService.MimeType.CSV) {
return ContentService.createTextOutput(contents)
.setMimeType(mime)
.downloadAsFile(filename.replace(/\(|\)|!|#/g, ''));
}