-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.gs
79 lines (61 loc) · 1.85 KB
/
webhook.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
//You need to enter your own spreadsheetID below. See the documentation for instructions on where to find it.
var spreadsheetID = "1y83FoYH92oc3nnJQgoFCPiKxlNaA6PDrnMYhiqWVo8I";
function doPost(e) {
try {
var sh = SpreadsheetApp.openById(spreadsheetID).getSheets()[0]
sh.setFrozenRows(1);
//sh.appendRow([JSON.stringify(e)]);
if (sh.getLastRow() > 0) {
var titles = sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0];
} else {
var titles = [];
}
if (e.parameter["data.json"]) {
var obj = JSON.parse(e.parameter["data.json"]);
//sh.appendRow([JSON.stringify(obj)]);
var arrNames = [];
for (i = 0; i < titles.length; i++) {
arrNames[i] = "";
}
var flag = false
for (var nam in obj) {
//sh.appendRow(obj[nam]);
var flag1 = true;
for (i = 0; i < titles.length; i++) {
if (titles[i] == ('' + nam).replace("_", " ")) {
arrNames[i] = obj[nam][0];
flag1 = false;
break;
}
}
if (flag1 == true) {
titles.push(('' + nam).replace("_", " "));
arrNames[titles.length - 1] = obj[nam][0];
flag = true;
}
}
var lock = LockService.getScriptLock();
var success = lock.tryLock(10000);
if (!success) {
throw 'Could not obtain lock after 10 seconds.';
}
if (flag == true) {
sh.getRange(1, 1, 1, titles.length).setValues([titles]);
}
if (sh.getLastRow() == 1) {
sh.appendRow(["'"]);
}
sh.appendRow(arrNames);
lock.releaseLock();
} else {
throw "Object not found";
}
return HtmlService.createHtmlOutput('<div>'+ 'Processed successfully!' + '</div>');
//return ContentService.createTextOutput('Processed successfully!');
} catch (e) {
return HtmlService.createHtmlOutput('<div>'+ e + '</div>');
//return ContentService.createTextOutput(e);
}
}
function doNothing(){
}