forked from bernot-dev/starrez-google-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapp.js
138 lines (119 loc) · 3.7 KB
/
webapp.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
/* global getSheet */
/* exported doGet doPost */
/**
* Insert a new row into a sheet and delete the last row
* @param {object} options Must contain at least spreadsheetId/spreadsheetUrl
*/
function updateLiveFeed(options) {
var sheet = getSheet(options);
// Insert row just below top row
sheet.insertRowBefore(2);
// Delete last row
var lastRow = sheet.getMaxRows();
sheet.deleteRow(lastRow);
// Retrieve new row as range
var newRow = sheet.getRange("2:2");
// Set values of range
newRow.setValues([options.values]);
}
/**
* Delete a row from a sheet based on matching criteria
* @param {object} options Must contain at least spreadsheetId/spreadsheetUrl
* and matchOn criteria with key and value properties. The key should be the
* column heading in the spreadsheet, and the value should be the value of a
* cell in the column of the key that will result in the row being deleted.
*/
function deleteRowsFromSheet(options) {
var sheet = getSheet(options);
var sheetValues = sheet.getDataRange().getValues();
var [spreadsheetKeys] = sheetValues;
var colsToCheck = [];
var valsToCheck = [];
if (options.matchOn === undefined || options.matchOn.length === 0) {
throw new Error("\"matchOn\" criteria must be defined in options");
}
var index;
options.matchOn.forEach(function setupMatching(criterion) {
Logger.log(criterion);
index = spreadsheetKeys.indexOf(criterion.key);
if (index >= 0) {
colsToCheck.push(index);
valsToCheck.push(criterion.value);
} else {
throw new Error("Failed while trying to match against column that does not exist in sheet: " + criterion.key);
}
});
for (var sheetRow = sheet.getLastRow() - 1; sheetRow > 1; sheetRow -= 1) {
var rowMatch = true;
for (var matchIndex = 0; matchIndex < colsToCheck.length; matchIndex += 1) {
if (
sheetValues[sheetRow][colsToCheck[matchIndex]] !==
valsToCheck[matchIndex]
) {
rowMatch = false;
}
}
if (rowMatch === true) {
sheet.deleteRow(sheetRow + 1);
}
}
}
/**
* Process an incoming request based if action is supported
* @param {object} event The incoming GET or POST data. Must specify and action
* and any required parameters for that action.
*/
function processRequest(event) {
var output = ContentService.createTextOutput();
output.setMimeType(ContentService.MimeType.TEXT);
try {
// Gather options from request
var options;
if (
event !== undefined &&
event.postData !== undefined &&
typeof event.postData.contents === "string"
) {
options = JSON.parse(event.postData.contents);
} else if (
event !== undefined &&
typeof event.parameters === "object" &&
Object.keys(event.parameters).length !== 0
) {
options = event.parameters;
} else {
// Default is to run setup web app
return HtmlService.createHtmlOutputFromFile("index.html");
}
if (typeof options.action === "string") {
if (options.action === "updateLiveFeed") {
updateLiveFeed(options);
} else if (options.action === "deleteRowsFromSheet") {
deleteRowsFromSheet(options);
} else {
throw new Error("Invalid action");
}
} else {
throw new Error("\"action\" must be defined in options");
}
output.setContent("Success!");
} catch (err) {
Logger.log(err);
output.setContent(err);
}
return output;
}
/**
* Receive incoming GET request
* @param {object} event Incoming GET request data
*/
function doGet(event) {
return processRequest(event);
}
/**
* Receive incoming POST request
* @param {object} event Incoming POST request data
*/
function doPost(event) {
return processRequest(event);
}