-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.gs
100 lines (83 loc) · 2.68 KB
/
agent.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
/**
* doGet() handles HTTP GET request.
*
* Expected optional parameters:
* x - Google Spreadsheet ID
* l - location index (zero-based sheet index)
* a - accommodation index (zero-based row index)
* s - status (value for 2nd column)
* n - note (staus cell note)
*/
function doGet(e) {
const response = { };
const sheetId = e?.parameter?.x;
if (sheetId) {
const spreadsheet = SpreadsheetApp.openById(sheetId);
const sheets = spreadsheet.getSheets();
const getAccommodationData = (values, notes) => {
const data = { no: values[0] };
if (values[1]) {
data.status = values[1];
if (notes[1])
data.note = notes[1];
}
return data;
};
const getLocationData = (sheet) => {
const data = { name: sheet.getName(), accommodations: [] };
const range = sheet.getRange(1, 1, sheet.getLastRow(), 2);
const rules = range.getDataValidations();
const notes = range.getNotes();
const values = range.getDisplayValues();
for (let i = 0; i < values.length; ++i) {
data.accommodations.push(getAccommodationData(values[i], notes[i]))
if (!data.statuses) {
const rule = rules[i][1];
if (rule && rule.getCriteriaType() == SpreadsheetApp.DataValidationCriteria.VALUE_IN_LIST)
data.statuses = rule.getCriteriaValues()[0];
}
}
return data;
};
const sheetIndex = e.parameter.l;
if (sheetIndex !== undefined) {
const sheet = sheets[+sheetIndex];
const rowIndex = e.parameter.a;
if (rowIndex !== undefined) {
if (e.parameter.s !== undefined || e.parameter.n !== undefined) {
const range = sheet.getRange(+rowIndex + 1, 2);
if (e.parameter.s !== undefined)
range.setValue(e.parameter.s);
if (e.parameter.n !== undefined)
range.setNote(e.parameter.n || null);
}
const range = sheet.getRange(+rowIndex + 1, 1, 1, 2);
const notes = range.getNotes();
const values = range.getDisplayValues();
Object.assign(response, getAccommodationData(values[0], notes[0]));
}
else
Object.assign(response, getLocationData(sheet));
}
else {
Object.assign(response, { name: spreadsheet.getName(), locations: [] });
for (const sheet of sheets)
response.locations.push(getLocationData(sheet));
}
}
//Logger.log(response);
return ContentService.createTextOutput()
.setMimeType(MimeType.JSON)
.setContent(JSON.stringify(response));
}
function test() {
doGet({
parameter: {
x: '1D0RcPMdFOBB0U_KVWNbxtxJMlPSS53WgfdzvAgrwFuc',
l: 0,
a: 0,
s: '',
n: ''
}
});
}