-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheet-uploader.js
150 lines (141 loc) · 3.77 KB
/
sheet-uploader.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
139
140
141
142
143
144
145
146
147
148
149
150
const {google} = require('googleapis');
const authTokens = {};
function getGoogleAuth(
name,
credentials) {
const cachedAuthToken = authTokens[name];
if (cachedAuthToken) {
return cachedAuthToken;
}
const credential = credentials[name];
if (!credential) {
throw new Error(`could not find google credential with name '${name}`);
}
const auth = new google.auth.JWT(
credential.client_email,
null,
credential.private_key,
[
"https://www.googleapis.com/auth/spreadsheets"
],
null
);
authTokens[name] = auth;
return auth;
}
function getResultType(task) {
const resultType = task.type;
if (!resultType) {
throw new Error(`no result type was set for rule with name '${task.name}'`);
}
const type = resultType.trim().toLocaleLowerCase();
if (
type === 'scalar'
|| type === 'table') {
return type;
}
throw new Error(`unknown result type '${type}' for rule with name '${task.name}'`);
}
function getResultValues(task) {
const type = getResultType(task);
const result = {
values: task.result
}
if (type === 'scalar') {
return result;
} else if (type === 'table') {
result.majorDimension = "ROWS";
return result;
}
throw new Error(`unknown result type '${type}' for rule with name '${task.name}'`);
}
async function getRange(
task,
sheets){
return new Promise((resolve, reject) => {
const payload = {
spreadsheetId: task.googleSheetId,
range: task.googleSheetRange,
};
sheets.spreadsheets.values.get(
payload,
(error, response) => {
if (error) {
reject(error);
return;
}
resolve(response.data.values);
});
});
}
async function clearRange(
task,
sheets) {
const currentData = await getRange(
task,
sheets);
const isNewDataSetLarger = !currentData
|| task.result.length >= currentData.length;
if (isNewDataSetLarger) {
// no point clearing
return;
}
return new Promise((resolve, reject) => {
const payload = {
spreadsheetId: task.googleSheetId,
range: task.googleSheetRange,
};
sheets.spreadsheets.values.clear(
payload,
(error, response) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
}
async function uploadDataForRule(
task,
credentials) {
const auth = getGoogleAuth(
task.googleCredential,
credentials);
const sheets = google.sheets({
version: 'v4',
auth: auth
});
console.log(`sheets: uploading for rule ${task.name}`);
if (task.clearRange) {
await clearRange(
task,
sheets);
}
const resource = getResultValues(task);
return new Promise((resolve, reject) => {
const payload = {
spreadsheetId: task.googleSheetId,
valueInputOption: "USER_ENTERED",
range: task.googleSheetRange,
resource: resource
};
sheets.spreadsheets.values.update(
payload,
(error, response) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
}
async function uploadDataToGoogleSheets(config) {
await Promise.all(config.tasks.map(task => uploadDataForRule(
task,
config.settings.google)));
}
module.exports = {
uploadDataToGoogleSheets: uploadDataToGoogleSheets
}