-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.js
73 lines (62 loc) · 2.22 KB
/
template.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
const JSON = require('JSON');
const sendHttpRequest = require('sendHttpRequest');
const encodeUriComponent = require('encodeUriComponent');
const getGoogleAuth = require('getGoogleAuth');
const spreadsheetId = data.url.replace('https://docs.google.com/spreadsheets/d/', '').split('/')[0];
const requestUrl = getUrl();
const auth = getGoogleAuth({
scopes: ['https://www.googleapis.com/auth/spreadsheets']
});
return sendGetRequest();
function sendGetRequest() {
let params = {
headers: {'Content-Type': 'application/json', },
method: 'GET'
};
if (data.authFlow === 'own') {
params.authorization = auth;
}
return sendHttpRequest(requestUrl, params).then(successResult => {
let bodyParsed = JSON.parse(successResult.body);
if (successResult.statusCode >= 200 && successResult.statusCode < 400) {
if (data.type === 'cell') {
return bodyParsed.values[0][0];
}
if (data.type === 'object') {
return bodyParsed.values.reduce((acc, curr) => {
acc[curr[0]] = curr[1];
return acc;
}, {});
}
return bodyParsed.values;
} else {
return '';
}
});
}
function getUrl() {
if (data.authFlow === 'stape') {
const containerKey = data.containerKey.split(':');
const containerZone = containerKey[0];
const containerIdentifier = containerKey[1];
const containerApiKey = containerKey[2];
const containerDefaultDomainEnd = containerKey[3] || 'io';
return (
'https://' +
enc(containerIdentifier) +
'.' +
enc(containerZone) +
'.stape.' +
enc(containerDefaultDomainEnd) +
'/stape-api/' +
enc(containerApiKey) +
'/v1/spreadsheet/auth-proxy?spreadsheetId=' + spreadsheetId +
'&range=' + enc(data.type === 'cell' ? data.cell : data.range)
);
}
return 'https://content-sheets.googleapis.com/v4/spreadsheets/'+spreadsheetId+'/values/'+enc(data.type === 'cell' ? data.cell : data.range);
}
function enc(data) {
data = data || '';
return encodeUriComponent(data);
}