-
Notifications
You must be signed in to change notification settings - Fork 123
/
api.js
88 lines (83 loc) · 3.72 KB
/
api.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
var gauthkey = 'ENTER API KEY HERE'; // https://developers.google.com/sheets/api/guides/authorizing#APIKey
var request = require('request');
module.exports = function (req, res, next) {
try {
var params = req.query,
api_key = params.api_key || gauthkey,
id = params.id,
sheet = params.sheet,
query = params.q,
useIntegers = params.integers || true,
showRows = params.rows || true,
showColumns = params.columns || true,
url = 'https://sheets.googleapis.com/v4/spreadsheets/' + id + '/values/' + sheet + '?key=' + api_key;
request(url, function (error, response, body) {
if (!id) {
return res.status(response.statusCode).json('You must provide a sheet ID');
}
if (!sheet) {
return res.status(response.statusCode).json('You must provide a sheet name');
}
if (!error && response.statusCode === 200) {
var data = JSON.parse(response.body);
var responseObj = {};
var rows = [];
var columns = {};
if (data && data.values) {
var headings = data.values[0]
for (var i = 1; i < data.values.length; i++) {
var entry = data.values[i];
var newRow = {};
var queried = !query;
for (var j = 0; j < entry.length; j++) {
var name = headings[j];
var value = entry[j];
if (query) {
if (value.toLowerCase().indexOf(query.toLowerCase()) > -1) {
queried = true;
}
}
if (Object.keys(params).indexOf(name) > -1) {
queried = false;
if (value.toLowerCase() === params[name].toLowerCase()) {
queried = true;
}
}
if (useIntegers === true && !isNaN(value)) {
value = Number(value);
}``
newRow[name] = value;
if (queried === true) {
if (!columns.hasOwnProperty(name)) {
columns[name] = [];
columns[name].push(value);
} else {
columns[name].push(value);
}
}
}
if (queried === true) {
rows.push(newRow);
}
}
if (showColumns === true) {
responseObj['columns'] = columns;
}
if (showRows === true) {
responseObj['rows'] = rows;
}
return res.status(200).json(responseObj);
} else {
var data = JSON.parse(response.body);
return res.status(response.statusCode).json(data.error);
}
} else {
var data = JSON.parse(response.body);
return res.status(response.statusCode).json(data.error);
}
});
} catch (error) {
var data = JSON.parse(response.body);
return res.status(response.statusCode).json(data.error);
}
};