-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-get.gs
90 lines (72 loc) · 2.2 KB
/
api-get.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
function doGet(request) {
var output = ContentService.createTextOutput();
var data = {};
var sheet = "Sheet1";
var pid = Number(request.parameters.product_id).toFixed(0);
var id = "1hNjadGLn_Oipa3c_ljKDKhvJMCj5BsYiiS_1oGb-0Ss";
var cell = request.parameters.cell;
var ss = SpreadsheetApp.openById(id);
if (sheet) {
if (cell) {
data = ss.getSheetByName(sheet).getRange(cell).getValue();
} else {
data = readData_(ss, sheet, pid);
}
} else {
// Grab all sheets except those with a name
// that starts with an underscore
ss.getSheets().forEach(function(oSheet, iIndex) {
var sName = oSheet.getName();
if (! sName.match(/^_/)) {
data[sName] = readData_(ss, sName);
}
})
}
if(data && data != "undefined" && Object.keys(data).length > 0){
data.result = "success";
var result = JSON.stringify(data);
output.setContent(result);
output.setMimeType(ContentService.MimeType.JSON);
}
else {
var seterror = JSON.stringify({"result":"error"});
output.setContent(seterror);
output.setMimeType(ContentService.MimeType.JSON);
}
return output;
}
function readData_(ss, sheetname, pid) {
if (typeof properties == "undefined") {
properties = getHeaderRow_(ss, sheetname);
properties = properties.map(function(p) { return p.replace(/\s+/g, '_'); });
}
var rows = getDataRows_(ss, sheetname);
var data = [];
for (var r = 0, l = rows.length; r < l; r++) {
var row = rows[r];
var record = {};
for (var p in properties) {
Logger.log(row[p]);
record[properties[p]] = convert_(row[p]);
}
data.push(record);
}
return getByValue2(data, pid);
}
function convert_(value) {
if (value === "true") return true;
if (value === "false") return false;
return value;
}
function getDataRows_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
}
function getHeaderRow_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0];
}
function getByValue2(arr, value) {
var result = arr.filter(function(o){return o.product_id == value;} );
return result? result[0] : null; // or undefined
}