-
Notifications
You must be signed in to change notification settings - Fork 0
/
recruitment_submission_trigger.js
285 lines (244 loc) · 8.79 KB
/
recruitment_submission_trigger.js
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
// Copyright (c) Tyler Swann and Monash DeepNeuron 2024
class BranchMetadata {
/** Constructs Span with name and given range.
*
* @param {string} sheet_name - Name of branch
* @param {Number} span_start - Start index of Span
* @param {Number} span_end - Last index of Span
* @param {Number} analytics_cidx - The (column) index
* of the 'ID Map &
* Analytics' sheet
* that needs to be
* marked
*/
constructor(
sheet_name = "Default",
span_start = 0,
span_end = 0,
analytics_cidx = undefined
) {
this.name = sheet_name;
this.start = span_start;
this.end = span_end;
this.analytics_cidx = analytics_cidx;
}
}
const metadata = {
"Common": new BranchMetadata("Common", 6, 25),
"AI": new BranchMetadata("AI", 32, 39, 4),
"HPC": new BranchMetadata("HPC", 40, 47, 5),
"Marketing, Design and Publications": new BranchMetadata(
"Marketing",
48,
55,
6
),
"Industry Team": new BranchMetadata("Industry", 56, 58, 7),
"Events Team": new BranchMetadata("Events", 63, 68, 8),
"People and Culture Officer": new BranchMetadata("P&C", 69, 73, 9),
"Law & Ethics Committee": new BranchMetadata("L&E", 26, 31, 10),
"Outreach Team": new BranchMetadata("Outreach", 59, 62, 11),
"Training Team": new BranchMetadata("Training", 74, 79, 12),
"Branch Switch Indexes": [25, 31, 39, 47, 55, 58, 62, 68, 73, 79],
"Unique Count Cell": "A4",
};
/** Transforms sheet cell data to appropriate format for
* processing. This mostly involves transforming
* Array data back into a comma separated string.
*
* @param {Object} data - Data from cell to transform
* @returns {Object}
*/
function transform_response(data) {
if (Array.isArray(data)) {
return data.join(", ");
}
return data;
}
/** Computes ID hash from input string.
* Uses the MD5 hash algorithm.
*
* @param {string} input_str
* @returns {string}
*/
function id_hash(input_str) {
return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input_str)
.map((char) => "0" + ((char + 256) % 256).toString(16).slice(-2)) // Turn 8-bit (signed) numbers to char
.join("");
}
/** Updates sheet with applicants general details
* and responses to branch specific questions.
*
* @param {string} id - Anonymous ID of applicant
* @param {Array<Array<Object>>} responses - Array of branch specific and generic responses to form
* @param {Sheet} sheet - Sheet to edit
*/
function add_or_update_entry(id, responses, sheet) {
const num_rows = sheet.getLastRow();
const new_entry_row = num_rows + 1;
const id_column = sheet
.getRange(2, 1, num_rows, 1)
.getValues()
.flatMap((row) => row);
const existing_entry_idx = id_column.indexOf(id);
if (existing_entry_idx > -1) {
// +2 for offsetting from the 1-based indexing of G-Sheets
// and the fact we are going from the second row down due
// to the first having the questions.
sheet
.getRange(existing_entry_idx + 2, 2, 1, responses[0].length)
.setValues(responses);
return;
}
sheet
.getRange(new_entry_row, 2, 1, responses[0].length)
.setValues(responses);
sheet.getRange(new_entry_row, 1).setValue(id);
}
/** Adds or updates the 'ID Map & Analytics' sheet with info from
* applicants responses.
*
* @param {string} id - Anonymous ID of applicant
* @param {boolean} applied - Value to update in branch column for applicant
* @param {Number} analytics_cidx - Column to update
* @param {Sheet} analytics_sheet - The sheet to add the analytical
* data to
*/
function update_analytics(id, name, email, analytics_cidx, analytics_sheet) {
const num_rows = analytics_sheet.getLastRow();
const new_entry_row = num_rows + 1;
const id_column = analytics_sheet
.getRange(2, 1, num_rows, 1)
.getValues()
.flatMap((row) => row);
const existing_entry_idx = id_column.indexOf(id);
if (existing_entry_idx > -1) {
// +2 for offsetting from the 1-based indexing of G-Sheets
// and the fact we are going from the second row down due
// to the first having the questions.
analytics_sheet
.getRange(existing_entry_idx + 2, analytics_cidx, 1, 1)
.setValue("TRUE");
return;
}
// update total unique count
const unique_count_cell = analytics_sheet.getRange(
metadata["Unique Count Cell"]
);
const current = unique_count_cell.getValue();
unique_count_cell.setValue(current + 1);
// Initialise new row with 'FALSE'
analytics_sheet
.getRange(new_entry_row, 2, 1, 11)
.setValues([
[
name,
email,
"FALSE",
"FALSE",
"FALSE",
"FALSE",
"FALSE",
"FALSE",
"FALSE",
"FALSE",
"FALSE",
],
]);
analytics_sheet
.getRange(new_entry_row, analytics_cidx, 1, 1)
.setValue("TRUE");
analytics_sheet.getRange(new_entry_row, 1).setValue(id);
}
/** Determines which branches have applications
* and forwards responses to each branch
* anonymised by ID hash.
*
* @param {string} id - Anonymous ID hash
* @param {Array<Object>} responses - Array of responses
* @param {Spreadsheet} workbook - The active workbook
*/
function splice_across_branches(id, responses, workbook) {
console.log("Splice");
const common_metadata = metadata["Common"];
const common_responses = responses.slice(
common_metadata.start,
common_metadata.end
);
// Construct lock to prevent data race on responses splicing
const script_lock = LockService.getScriptLock();
console.log(`Before Lock: ${id}`);
// Attempt to gain the this scripts lock, log timeout/wait time.
let total_wait_time = 0;
while (!script_lock.tryLock(20000)) {
total_wait_time += 20000;
console.log(
`Service to ${id} could not obtain lock after 20s (Total wait: ${total_wait_time})`
);
}
console.log(`Servicing now ${id}`);
console.log(metadata["Branch Switch Indexes"].map((idx) => responses[idx]));
// Looping through response fields marking which branches
// applicant is applying to
for (const idx of metadata["Branch Switch Indexes"]) {
const branch = responses[idx];
console.log("branch: ", branch);
if (branch !== "" && branch !== "No") {
const branch_span = metadata[branch];
const branch_responses = responses.slice(
branch_span.start,
branch_span.end
);
const combined_responses = [
common_responses.concat(branch_responses),
];
// Append
add_or_update_entry(
id,
combined_responses,
workbook.getSheetByName(branch_span.name)
);
// Update analytics for branch
update_analytics(
id,
responses[2].slice(0) + " " + responses[3].slice(0), // have to slice others array entries stringify to "[object Object]"
responses[1],
metadata[branch].analytics_cidx,
workbook.getSheetByName("ID Map & Analytics")
);
}
SpreadsheetApp.flush();
}
script_lock.releaseLock();
console.log(`Finished service to ${id}`);
}
/** Entry point of Form submission trigger
*
* @param {Event} e - The Event object for form submission.
*/
function recruitment_application_submit_trigger(e) {
// Form object
const form = e.source;
// Destination/responses spreadsheet and ID
const workbook_id = form.getDestinationId();
const workbook = SpreadsheetApp.openById(workbook_id);
// Responses sheet
const response_sheet = workbook.getSheetByName("Form responses 1");
/**
* Last row with data in response sheet.
* Has to come from sheet directly and not
* the FormResponse object from `e` as
* the later only has the responses and not
* the empty cells needed to copy the full
* range in `dup_with_id()`.
*/
const latest_row = response_sheet.getLastRow();
// Response that fired the trigger
const responses = response_sheet
.getRange(`A${latest_row}:CB${latest_row}`)
.getValues()[0]
.map(transform_response);
// Anonymising ID used for application marking.
const id = id_hash(responses[1] + responses[4]);
splice_across_branches(id, responses, workbook);
}