-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppScript.js
31 lines (29 loc) · 1.24 KB
/
AppScript.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
var sheetName = 'html-to-sheet';
var scriptProp = PropertiesService.getScriptProperties();
function intialSetup() {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
scriptProp.setProperty('key', activeSpreadsheet.getId());
}
function doPost(e) {
var lock = LockService.getScriptLock();
lock.tryLock(10000);
try {
var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'));
var sheet = doc.getSheetByName(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow() + 1;
var newRow = headers.map(function (header) {
return header === 'timestamp' ? new Date() : e.parameter[header] || '';
});
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]);
return ContentService.createTextOutput(
JSON.stringify({ result: 'success', row: nextRow })
).setMimeType(ContentService.MimeType.JSON);
} catch (error) {
return ContentService.createTextOutput(
JSON.stringify({ result: 'error', error: error.toString() })
).setMimeType(ContentService.MimeType.JSON);
} finally {
lock.releaseLock();
}
}