-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppScriptDB.gs
108 lines (89 loc) · 2.96 KB
/
AppScriptDB.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
function doPost(e) {
try {
const pairTeamMatch = getTeamAndMatch();
const newEntryTeamMatch = [parseInt(e.parameters["team"][0]), parseInt(e.parameters["match"][0])];
let repeated = false;
for (let i = 0; i < pairTeamMatch.length; i++){
if (pairTeamMatch[i][0] == newEntryTeamMatch[0] && pairTeamMatch[i][1] == newEntryTeamMatch[1]){
repeated=true
break;
}
}
if(!repeated){
record_data(e);
}
var response = {
"result":"success",
"repeated": repeated
};
} catch(error) {
var response = {
"result":"error",
"error": String(error)
};
}
return ContentService
.createTextOutput(JSON.stringify(response))
.setMimeType(ContentService.MimeType.JSON);
}
function getTeamAndMatch() {
const sheet = SpreadsheetApp.getActiveSheet();
const data = sheet.getRange(1,2,sheet.getLastRow(),2).getValues();
data.shift();
return data;
}
function record_data(e) {
var lock = LockService.getDocumentLock();
lock.waitLock(30000);
try {
var sheet = SpreadsheetApp.getActiveSheet();
var oldHeader = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var newHeader = oldHeader.slice();
var fieldsFromForm = getDataColumns(e.parameters);
var currentdate = new Date();
var datetime = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
var row = [datetime];
for (var i = 1; i < oldHeader.length; i++) {
var field = oldHeader[i];
var output = getFieldFromData(field, e.parameters);
row.push(output);
var formIndex = fieldsFromForm.indexOf(field);
if (formIndex > -1) {
fieldsFromForm.splice(formIndex, 1);
}
}
for (var i = 0; i < fieldsFromForm.length; i++) {
var field = fieldsFromForm[i];
var output = getFieldFromData(field, e.parameters);
row.push(output);
newHeader.push(field);
}
var nextRow = sheet.getLastRow() + 1; // get next row
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
if (newHeader.length > oldHeader.length) {
sheet.getRange(1, 1, 1, newHeader.length).setValues([newHeader]);
}
}
catch(error) {
Logger.log(error);
}
finally {
lock.releaseLock();
return;
}
}
function getDataColumns(data) {
return Object.keys(data).filter(function(column) {
return !(column === 'formDataNameOrder' || column === 'formGoogleSheetName' || column === 'formGoogleSendEmail' || column === 'honeypot');
});
}
function getFieldFromData(field, data) {
var values = data[field] || '';
var output = values.join ? values.join(', ') : values;
return output;
}