-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-script.js
54 lines (42 loc) · 1.55 KB
/
google-script.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
//
// Original version from:
// https://medium.com/@dmccoy/how-to-submit-an-html-form-to-google-sheets-without-google-forms-b833952cc175
//
/* eslint-disable no-var */
/*globals PropertiesService, LockService, SpreadsheetApp, ContentService*/
function doGet(e) {
return handleResponse(e);
}
var SHEET_NAME = 'RESULT_LIST';
var SCRIPT_PROP = PropertiesService.getScriptProperties();
function handleResponse(e) {
// https://gsuite-developers.googleblog.com/2011/10/concurrency-and-google-apps-script.html
var lock = LockService.getPublicLock();
lock.waitLock(30000);
try {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName(SHEET_NAME);
// We'll assume header is in row 1 but you can override with header_row in GET/POST data
var headRow = e.parameter.header_row || 1;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow() + 1; // get next row
var row = [];
headers.forEach(function(header) {
if (header === 'TIMESTAMP') {
row.push(Date.now());
} else {
row.push(e.parameter[header]);
}
});
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
return ContentService.createTextOutput(
JSON.stringify({ result: 'success', row: nextRow })
).setMimeType(ContentService.MimeType.JSON);
} catch (e) {
return ContentService.createTextOutput(
JSON.stringify({ result: 'error', error: e })
).setMimeType(ContentService.MimeType.JSON);
} finally {
lock.releaseLock();
}
}