@gzhangx/googleapi 0.0.76 → 0.0.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/google/drive.d.ts +15 -15
- package/lib/google/drive.js +54 -45
- package/lib/google/drive.js.map +1 -1
- package/lib/google/googleApiServiceAccount.d.ts +129 -132
- package/lib/google/googleApiServiceAccount.js +411 -551
- package/lib/google/googleApiServiceAccount.js.map +1 -1
- package/lib/googleApi.d.ts +58 -58
- package/lib/googleApi.js +325 -459
- package/lib/googleApi.js.map +1 -1
- package/lib/httpRequest.d.ts +24 -25
- package/lib/httpRequest.js +138 -184
- package/lib/httpRequest.js.map +1 -1
- package/lib/index.d.ts +24 -24
- package/lib/index.js +62 -55
- package/lib/index.js.map +1 -1
- package/lib/jwt.d.ts +11 -11
- package/lib/jwt.js +45 -46
- package/lib/jwt.js.map +1 -1
- package/lib/msGraph/msExcell.d.ts +47 -47
- package/lib/msGraph/msExcell.js +69 -157
- package/lib/msGraph/msExcell.js.map +1 -1
- package/lib/msGraph/msauth.d.ts +91 -91
- package/lib/msGraph/msauth.js +309 -533
- package/lib/msGraph/msauth.js.map +1 -1
- package/lib/msGraph/msdir.d.ts +124 -125
- package/lib/msGraph/msdir.js +171 -263
- package/lib/msGraph/msdir.js.map +1 -1
- package/lib/msGraph/types.d.ts +4 -4
- package/lib/msGraph/types.js +2 -2
- package/lib/tests/gtest.d.ts +1 -1
- package/lib/tests/gtest.js +170 -234
- package/lib/tests/gtest.js.map +1 -1
- package/lib/tests/mstest.d.ts +1 -1
- package/lib/tests/mstest.js +184 -269
- package/lib/tests/mstest.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/util.d.ts +9 -9
- package/lib/util.js +95 -96
- package/lib/util.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,552 +1,412 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
//rootUrl = 'https://accounts.google.com/o/oauth2/v2/auth';
|
|
3
|
-
//opts = { access_type: 'offline', scope: 'https://www.googleapis.com/auth/spreadsheets', response_type: 'code', client_id: 'client_id', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob' }
|
|
4
|
-
//return rootUrl + '?' + querystring.stringify(opts);
|
|
5
|
-
//'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&response_type=code&client_id=client_id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob'
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
var
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return (0,
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
if (
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
message: "bad data found for id ".concat(id, " sheet ").concat(sheetName),
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
columns = ret.values[0];
|
|
416
|
-
dataRow = ret.values.slice(1);
|
|
417
|
-
data = dataRow.map(function (d) {
|
|
418
|
-
return columns.reduce(function (acc, column, i) {
|
|
419
|
-
acc[column] = d[i];
|
|
420
|
-
return acc;
|
|
421
|
-
}, {});
|
|
422
|
-
});
|
|
423
|
-
return [2 /*return*/, {
|
|
424
|
-
message: 'OK',
|
|
425
|
-
data: data,
|
|
426
|
-
}];
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
function readData(sheetName, readSize, offset) {
|
|
432
|
-
if (offset === void 0) { offset = { row: 0, col: 0 }; }
|
|
433
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
434
|
-
var ret, message;
|
|
435
|
-
return __generator(this, function (_a) {
|
|
436
|
-
switch (_a.label) {
|
|
437
|
-
case 0: return [4 /*yield*/, getSheetRange(sheetName, readSize, offset)];
|
|
438
|
-
case 1:
|
|
439
|
-
sheetName = _a.sent();
|
|
440
|
-
return [4 /*yield*/, read({ id: id, range: sheetName })];
|
|
441
|
-
case 2:
|
|
442
|
-
ret = _a.sent();
|
|
443
|
-
message = ret.values ? 'OK' : "bad data found for id ".concat(id, " sheet ").concat(sheetName);
|
|
444
|
-
return [2 /*return*/, {
|
|
445
|
-
message: message,
|
|
446
|
-
data: ret.values,
|
|
447
|
-
}];
|
|
448
|
-
}
|
|
449
|
-
});
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
var updateValues = function (range, values, opts) { return __awaiter(_this, void 0, void 0, function () {
|
|
453
|
-
return __generator(this, function (_a) {
|
|
454
|
-
switch (_a.label) {
|
|
455
|
-
case 0:
|
|
456
|
-
if (!opts) {
|
|
457
|
-
opts = {
|
|
458
|
-
valueInputOption: 'USER_ENTERED'
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
if (!opts.valueInputOption)
|
|
462
|
-
opts.valueInputOption = 'USER_ENTERED';
|
|
463
|
-
return [4 /*yield*/, doOp('PUT', id, "/values/".concat(encodeURIComponent(range), "?").concat((0, util_1.getFormData)(opts)), {
|
|
464
|
-
values: values,
|
|
465
|
-
})];
|
|
466
|
-
case 1: return [2 /*return*/, (_a.sent())];
|
|
467
|
-
}
|
|
468
|
-
});
|
|
469
|
-
}); };
|
|
470
|
-
function autoUpdateValues(sheetName, values, offset, opts) {
|
|
471
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
472
|
-
var writeSize, range;
|
|
473
|
-
return __generator(this, function (_a) {
|
|
474
|
-
switch (_a.label) {
|
|
475
|
-
case 0:
|
|
476
|
-
if (!values || !values.length)
|
|
477
|
-
return [2 /*return*/, null];
|
|
478
|
-
writeSize = {
|
|
479
|
-
col: values[0].length,
|
|
480
|
-
row: values.length,
|
|
481
|
-
};
|
|
482
|
-
return [4 /*yield*/, getSheetRange(sheetName, writeSize, offset)];
|
|
483
|
-
case 1:
|
|
484
|
-
range = _a.sent();
|
|
485
|
-
return [4 /*yield*/, updateValues(range, values, opts)];
|
|
486
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
487
|
-
}
|
|
488
|
-
});
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
return {
|
|
492
|
-
doBatchUpdate: function (data) { return doBatchUpdate(id, data); },
|
|
493
|
-
appendRowCols: appendRowCols,
|
|
494
|
-
append: function (range, data, ops) { return append({ id: id, range: range }, data, ops); },
|
|
495
|
-
read: function (range) { return read({ id: id, range: range }); },
|
|
496
|
-
clear: clear,
|
|
497
|
-
sheetInfo: sheetInfo,
|
|
498
|
-
createSheet: createSheet,
|
|
499
|
-
autoCreateSheet: autoCreateSheet,
|
|
500
|
-
updateValues: updateValues,
|
|
501
|
-
autoUpdateValues: autoUpdateValues,
|
|
502
|
-
readDataByColumnName: readDataByColumnName,
|
|
503
|
-
readData: readData,
|
|
504
|
-
deleteSheet: deleteSheet,
|
|
505
|
-
deleteSheetByName: deleteSheetByName,
|
|
506
|
-
getSheetRange: getSheetRange,
|
|
507
|
-
addSheet: function (title) { return __awaiter(_this, void 0, void 0, function () {
|
|
508
|
-
var sheetsInfo, found, newId, existing;
|
|
509
|
-
return __generator(this, function (_a) {
|
|
510
|
-
switch (_a.label) {
|
|
511
|
-
case 0: return [4 /*yield*/, sheetInfo()];
|
|
512
|
-
case 1:
|
|
513
|
-
sheetsInfo = _a.sent();
|
|
514
|
-
found = sheetsInfo.find(function (s) { return s.title === title; });
|
|
515
|
-
if (found) {
|
|
516
|
-
return [2 /*return*/, {
|
|
517
|
-
found: found,
|
|
518
|
-
}];
|
|
519
|
-
}
|
|
520
|
-
newId = 0;
|
|
521
|
-
for (;; newId++) {
|
|
522
|
-
existing = sheetsInfo.find(function (s) { return s.sheetId === newId; });
|
|
523
|
-
if (existing)
|
|
524
|
-
continue;
|
|
525
|
-
break;
|
|
526
|
-
}
|
|
527
|
-
return [2 /*return*/, createSheet(newId.toString(), title)];
|
|
528
|
-
}
|
|
529
|
-
});
|
|
530
|
-
}); },
|
|
531
|
-
};
|
|
532
|
-
}
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
exports.getClient = getClient;
|
|
536
|
-
//test(true).catch(err => {
|
|
537
|
-
///console.log(err)
|
|
538
|
-
//console.log(err.response.data);
|
|
539
|
-
//})
|
|
540
|
-
/*
|
|
541
|
-
async function test2() {
|
|
542
|
-
const creds = getClientCredsByEnv('gzperm');
|
|
543
|
-
await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
|
|
544
|
-
}
|
|
545
|
-
console.log('invoking test2')
|
|
546
|
-
test2().catch(err => {
|
|
547
|
-
console.log('error');
|
|
548
|
-
//console.log(err);
|
|
549
|
-
console.log(err.response.text || err.response.data);
|
|
550
|
-
})
|
|
551
|
-
*/
|
|
1
|
+
"use strict";
|
|
2
|
+
//rootUrl = 'https://accounts.google.com/o/oauth2/v2/auth';
|
|
3
|
+
//opts = { access_type: 'offline', scope: 'https://www.googleapis.com/auth/spreadsheets', response_type: 'code', client_id: 'client_id', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob' }
|
|
4
|
+
//return rootUrl + '?' + querystring.stringify(opts);
|
|
5
|
+
//'https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&response_type=code&client_id=client_id&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob'
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
23
|
+
var ownKeys = function(o) {
|
|
24
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
25
|
+
var ar = [];
|
|
26
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
return ownKeys(o);
|
|
30
|
+
};
|
|
31
|
+
return function (mod) {
|
|
32
|
+
if (mod && mod.__esModule) return mod;
|
|
33
|
+
var result = {};
|
|
34
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
35
|
+
__setModuleDefault(result, mod);
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
})();
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.getClient = getClient;
|
|
41
|
+
const httpRequest_1 = require("../httpRequest");
|
|
42
|
+
const util_1 = require("../util");
|
|
43
|
+
//import jwt from 'jsonwebtoken';
|
|
44
|
+
const jwt = __importStar(require("../jwt"));
|
|
45
|
+
;
|
|
46
|
+
//old, does not need refresh token
|
|
47
|
+
function betterErr(desc) {
|
|
48
|
+
return err => {
|
|
49
|
+
const webRsp = err.response;
|
|
50
|
+
if (webRsp) {
|
|
51
|
+
throw {
|
|
52
|
+
desc,
|
|
53
|
+
//...pick(webRsp, ['data', 'status', 'statusText', 'headers', 'config.url'])
|
|
54
|
+
data: webRsp.data,
|
|
55
|
+
status: webRsp.status,
|
|
56
|
+
statusText: webRsp.statusText,
|
|
57
|
+
headers: webRsp.headers,
|
|
58
|
+
url: webRsp.config?.url,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
throw err;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function getClient(creds) {
|
|
65
|
+
if (!creds.token) {
|
|
66
|
+
if (!creds.private_key || !creds.private_key_id)
|
|
67
|
+
throw `doRefresh needs private_key and private_key_id in creds`;
|
|
68
|
+
if (!creds.client_email)
|
|
69
|
+
throw `missing client_email from creds`;
|
|
70
|
+
}
|
|
71
|
+
const curClientData = {
|
|
72
|
+
expirationTime: 0,
|
|
73
|
+
curToken: '',
|
|
74
|
+
};
|
|
75
|
+
const getToken = () => {
|
|
76
|
+
if (creds.token)
|
|
77
|
+
return creds.token;
|
|
78
|
+
const curTime = Math.floor(Date.now() / 1000);
|
|
79
|
+
if (curClientData.curToken && curClientData.expirationTime < curTime) {
|
|
80
|
+
return curClientData.curToken;
|
|
81
|
+
}
|
|
82
|
+
curClientData.curToken = jwt.signRs256(creds.private_key, {
|
|
83
|
+
iss: creds.client_email,
|
|
84
|
+
sub: creds.client_email,
|
|
85
|
+
exp: 3600,
|
|
86
|
+
aud: 'https://sheets.googleapis.com/',
|
|
87
|
+
kid: creds.private_key_id,
|
|
88
|
+
});
|
|
89
|
+
/*
|
|
90
|
+
curClientData.curToken = jwt.sign({
|
|
91
|
+
//"iss": cred.client_email,
|
|
92
|
+
//"sub": cred.client_email,
|
|
93
|
+
"aud": "https://sheets.googleapis.com/",
|
|
94
|
+
//"iat": unixTime,
|
|
95
|
+
//"exp": unixTime
|
|
96
|
+
|
|
97
|
+
"typ": "JWT",
|
|
98
|
+
"kid": creds.private_key_id,
|
|
99
|
+
}, creds.private_key, {
|
|
100
|
+
"algorithm": "RS256",
|
|
101
|
+
//"type": "JWT",
|
|
102
|
+
expiresIn: 3600,
|
|
103
|
+
issuer: creds.client_email,
|
|
104
|
+
subject: creds.client_email,
|
|
105
|
+
//"kid": cred.private_key_id
|
|
106
|
+
});
|
|
107
|
+
*/
|
|
108
|
+
curClientData.expirationTime = curTime + 3600 - 100;
|
|
109
|
+
return curClientData.curToken;
|
|
110
|
+
};
|
|
111
|
+
const rootUrl = 'https://sheets.googleapis.com/v4/spreadsheets';
|
|
112
|
+
const doOp = (op, id, postFix, data) => {
|
|
113
|
+
const url = `${rootUrl}/${id}${postFix}`;
|
|
114
|
+
return (0, httpRequest_1.doHttpRequest)({
|
|
115
|
+
url,
|
|
116
|
+
headers: {
|
|
117
|
+
"Content-Type": "application/json",
|
|
118
|
+
"Authorization": `Bearer ${getToken()}`,
|
|
119
|
+
},
|
|
120
|
+
method: op,
|
|
121
|
+
data,
|
|
122
|
+
}).then(r => {
|
|
123
|
+
const data = r.data;
|
|
124
|
+
if (data && data.error) {
|
|
125
|
+
data.error.url = url;
|
|
126
|
+
throw data.error;
|
|
127
|
+
}
|
|
128
|
+
return data;
|
|
129
|
+
}).catch(betterErr(`doOps error ${url}`));
|
|
130
|
+
};
|
|
131
|
+
const createTopNewSheet = (data) => {
|
|
132
|
+
const url = rootUrl;
|
|
133
|
+
return (0, httpRequest_1.doHttpRequest)({
|
|
134
|
+
url,
|
|
135
|
+
headers: {
|
|
136
|
+
"Content-Type": "application/json",
|
|
137
|
+
"Authorization": `Bearer ${getToken()}`,
|
|
138
|
+
},
|
|
139
|
+
method: 'POST',
|
|
140
|
+
data,
|
|
141
|
+
}).then(r => {
|
|
142
|
+
return (r.data);
|
|
143
|
+
}).catch(betterErr(`doOps error ${url}`));
|
|
144
|
+
};
|
|
145
|
+
const doPost = (id, postFix, data) => doOp('POST', id, postFix, data);
|
|
146
|
+
const doBatchUpdate = async (id, data) => doPost(id, ':batchUpdate', data);
|
|
147
|
+
const append = async ({ id, range }, data, opts) => {
|
|
148
|
+
if (!opts) {
|
|
149
|
+
opts = {};
|
|
150
|
+
}
|
|
151
|
+
if (!opts.valueInputOption)
|
|
152
|
+
opts.valueInputOption = 'USER_ENTERED';
|
|
153
|
+
return await doPost(id, `/values/${range}:append?${(0, util_1.getFormData)(opts)}`, { values: data });
|
|
154
|
+
};
|
|
155
|
+
const read = async ({ id, range }) => (await doOp('GET', id, `/values/${range}`));
|
|
156
|
+
const ret = {
|
|
157
|
+
//access_token,
|
|
158
|
+
//expires_on: new Date().getTime() + (expires_in * 1000 - 2000),
|
|
159
|
+
//token_type,
|
|
160
|
+
getToken,
|
|
161
|
+
doBatchUpdate,
|
|
162
|
+
append,
|
|
163
|
+
read,
|
|
164
|
+
createTopNewSheet,
|
|
165
|
+
getSheetOps: id => {
|
|
166
|
+
const getInfo = () => doOp('GET', id, '');
|
|
167
|
+
const clear = async (sheetName, offset, clearRange) => {
|
|
168
|
+
const range = await getSheetRange(sheetName, clearRange, offset);
|
|
169
|
+
return await doOp('POST', id, `/values/${range}:clear`);
|
|
170
|
+
};
|
|
171
|
+
const createSheet = async (sheetId, title) => {
|
|
172
|
+
return doBatchUpdate(id, {
|
|
173
|
+
requests: [
|
|
174
|
+
{
|
|
175
|
+
addSheet: {
|
|
176
|
+
properties: {
|
|
177
|
+
sheetId,
|
|
178
|
+
title,
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
]
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
const deleteSheet = async (sheetId) => {
|
|
186
|
+
return doBatchUpdate(id, {
|
|
187
|
+
requests: [
|
|
188
|
+
{
|
|
189
|
+
deleteSheet: {
|
|
190
|
+
sheetId,
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
]
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
const deleteSheetByName = async (title) => {
|
|
197
|
+
const sheets = await sheetInfo();
|
|
198
|
+
const sheet = sheets.find(s => s.title === title);
|
|
199
|
+
if (sheet) {
|
|
200
|
+
return await deleteSheet(sheet.sheetId);
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
message: 'not found',
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
const sheetInfo = async () => {
|
|
207
|
+
const sheetInfos = await getInfo();
|
|
208
|
+
if (sheetInfos.error) {
|
|
209
|
+
throw sheetInfos.error;
|
|
210
|
+
}
|
|
211
|
+
return sheetInfos.sheets.map(s => {
|
|
212
|
+
const props = s.properties;
|
|
213
|
+
return {
|
|
214
|
+
sheetId: props.sheetId,
|
|
215
|
+
title: props.title,
|
|
216
|
+
index: props.index, //not important,
|
|
217
|
+
...props.gridProperties, //rowCount, columnCount
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
/// create sheet and deduct sheet Id from existing
|
|
222
|
+
const autoCreateSheet = async (title) => {
|
|
223
|
+
const sheets = await sheetInfo();
|
|
224
|
+
const sheet = sheets.find(s => s.title === title);
|
|
225
|
+
if (sheet)
|
|
226
|
+
return {
|
|
227
|
+
message: 'found'
|
|
228
|
+
};
|
|
229
|
+
const maxId = sheets.reduce((acc, s) => {
|
|
230
|
+
if (s.sheetId > acc) {
|
|
231
|
+
acc = s.sheetId;
|
|
232
|
+
}
|
|
233
|
+
return acc;
|
|
234
|
+
}, 0) + 1;
|
|
235
|
+
return await createSheet(maxId.toString(), title);
|
|
236
|
+
};
|
|
237
|
+
async function getSheetRange(sheetName, readSize = { row: 0, col: 0 }, offset = { row: 0, col: 0 }) {
|
|
238
|
+
if (sheetName.indexOf('!') < 0) {
|
|
239
|
+
sheetName = sheetName.trim();
|
|
240
|
+
const sheetInfos = await sheetInfo();
|
|
241
|
+
const info = sheetInfos.find(s => s.title === sheetName);
|
|
242
|
+
if (!info) {
|
|
243
|
+
throw {
|
|
244
|
+
message: `Error get sheet info for ${id} ${sheetName}`,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
if (!readSize.col)
|
|
248
|
+
readSize.col = info.columnCount;
|
|
249
|
+
if (offset.col !== 0 && !offset.col)
|
|
250
|
+
offset.col = 0;
|
|
251
|
+
if (offset.row !== 0 && !offset.row) {
|
|
252
|
+
throw 'Offset.row must be specified';
|
|
253
|
+
}
|
|
254
|
+
const endCol = readSize.col + offset.col;
|
|
255
|
+
const appendOps = {
|
|
256
|
+
row: 0,
|
|
257
|
+
col: 0,
|
|
258
|
+
};
|
|
259
|
+
if (endCol > info.columnCount) {
|
|
260
|
+
appendOps.col = endCol - info.columnCount;
|
|
261
|
+
}
|
|
262
|
+
if (!readSize.row)
|
|
263
|
+
readSize.row = info.rowCount;
|
|
264
|
+
const endRow = readSize.row + offset.row;
|
|
265
|
+
if (endRow > info.rowCount) {
|
|
266
|
+
appendOps.row = endRow - info.rowCount;
|
|
267
|
+
}
|
|
268
|
+
if (appendOps.col || appendOps.row) {
|
|
269
|
+
await appendRowCols(info.sheetId, appendOps);
|
|
270
|
+
}
|
|
271
|
+
return `'${sheetName}'!${(0, util_1.xcelPositionToColumnName)(offset.col)}${1 + offset.row}:${(0, util_1.xcelPositionToColumnName)(endCol)}${endRow}`;
|
|
272
|
+
}
|
|
273
|
+
return sheetName;
|
|
274
|
+
}
|
|
275
|
+
async function appendRowCols(sheetId, ap) {
|
|
276
|
+
const requests = [];
|
|
277
|
+
if (ap.col) {
|
|
278
|
+
requests.push({
|
|
279
|
+
appendDimension: {
|
|
280
|
+
sheetId,
|
|
281
|
+
dimension: 'COLUMNS',
|
|
282
|
+
length: ap.col,
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
if (ap.row) {
|
|
287
|
+
requests.push({
|
|
288
|
+
appendDimension: {
|
|
289
|
+
sheetId,
|
|
290
|
+
dimension: 'ROWS',
|
|
291
|
+
length: ap.row,
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return doBatchUpdate(id, { requests });
|
|
296
|
+
}
|
|
297
|
+
async function readDataByColumnName(sheetName, readSize = { row: 0, col: 0 }, offset = { row: 0, col: 0 }) {
|
|
298
|
+
sheetName = await getSheetRange(sheetName, readSize, offset);
|
|
299
|
+
const ret = await read({ id, range: sheetName });
|
|
300
|
+
if (!ret.values) {
|
|
301
|
+
throw {
|
|
302
|
+
message: `bad data found for id ${id} sheet ${sheetName}`,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
const columns = ret.values[0];
|
|
306
|
+
const dataRow = ret.values.slice(1);
|
|
307
|
+
const data = dataRow.map(d => {
|
|
308
|
+
return columns.reduce((acc, column, i) => {
|
|
309
|
+
acc[column] = d[i];
|
|
310
|
+
return acc;
|
|
311
|
+
}, {});
|
|
312
|
+
});
|
|
313
|
+
return {
|
|
314
|
+
message: 'OK',
|
|
315
|
+
data,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
async function readData(sheetName, readSize, offset = { row: 0, col: 0 }) {
|
|
319
|
+
sheetName = await getSheetRange(sheetName, readSize, offset);
|
|
320
|
+
const ret = await read({ id, range: sheetName });
|
|
321
|
+
const message = ret.values ? 'OK' : `bad data found for id ${id} sheet ${sheetName}`;
|
|
322
|
+
return {
|
|
323
|
+
range: sheetName,
|
|
324
|
+
message,
|
|
325
|
+
values: ret.values,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
const updateValues = async (range, values, opts) => {
|
|
329
|
+
if (!opts) {
|
|
330
|
+
opts = {
|
|
331
|
+
valueInputOption: 'USER_ENTERED'
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
if (!opts.valueInputOption)
|
|
335
|
+
opts.valueInputOption = 'USER_ENTERED';
|
|
336
|
+
return (await doOp('PUT', id, `/values/${encodeURIComponent(range)}?${(0, util_1.getFormData)(opts)}`, {
|
|
337
|
+
values,
|
|
338
|
+
}));
|
|
339
|
+
};
|
|
340
|
+
async function autoUpdateValues(sheetName, values, offset, opts) {
|
|
341
|
+
if (!values || !values.length)
|
|
342
|
+
return null;
|
|
343
|
+
const writeSize = {
|
|
344
|
+
col: values[0].length,
|
|
345
|
+
row: values.length,
|
|
346
|
+
};
|
|
347
|
+
const range = await getSheetRange(sheetName, writeSize, offset);
|
|
348
|
+
return await updateValues(range, values, opts);
|
|
349
|
+
}
|
|
350
|
+
const getSheetOpsReturn = {
|
|
351
|
+
doBatchUpdate: data => doBatchUpdate(id, data),
|
|
352
|
+
appendRowCols,
|
|
353
|
+
append: async (origRange, data, ops) => {
|
|
354
|
+
let range = origRange;
|
|
355
|
+
if (range.indexOf('!') < 0) {
|
|
356
|
+
range = await getSheetRange(range);
|
|
357
|
+
}
|
|
358
|
+
return await append({ id, range }, data, ops);
|
|
359
|
+
},
|
|
360
|
+
read: range => read({ id, range }),
|
|
361
|
+
clear,
|
|
362
|
+
sheetInfo,
|
|
363
|
+
createSheet,
|
|
364
|
+
autoCreateSheet,
|
|
365
|
+
updateValues,
|
|
366
|
+
autoUpdateValues,
|
|
367
|
+
readDataByColumnName,
|
|
368
|
+
readData,
|
|
369
|
+
deleteSheet,
|
|
370
|
+
deleteSheetByName,
|
|
371
|
+
getSheetRange,
|
|
372
|
+
addSheet: async (title) => {
|
|
373
|
+
const sheetsInfo = await sheetInfo();
|
|
374
|
+
//input YYYY, sheetId,
|
|
375
|
+
const found = sheetsInfo.find(s => s.title === title);
|
|
376
|
+
if (found) {
|
|
377
|
+
return {
|
|
378
|
+
found,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
let newId = 0;
|
|
382
|
+
for (;; newId++) {
|
|
383
|
+
const existing = sheetsInfo.find(s => s.sheetId === newId);
|
|
384
|
+
if (existing)
|
|
385
|
+
continue;
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
return createSheet(newId.toString(), title);
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
return getSheetOpsReturn;
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
return ret;
|
|
395
|
+
}
|
|
396
|
+
//test(true).catch(err => {
|
|
397
|
+
///console.log(err)
|
|
398
|
+
//console.log(err.response.data);
|
|
399
|
+
//})
|
|
400
|
+
/*
|
|
401
|
+
async function test2() {
|
|
402
|
+
const creds = getClientCredsByEnv('gzperm');
|
|
403
|
+
await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
|
|
404
|
+
}
|
|
405
|
+
console.log('invoking test2')
|
|
406
|
+
test2().catch(err => {
|
|
407
|
+
console.log('error');
|
|
408
|
+
//console.log(err);
|
|
409
|
+
console.log(err.response.text || err.response.data);
|
|
410
|
+
})
|
|
411
|
+
*/
|
|
552
412
|
//# sourceMappingURL=googleApiServiceAccount.js.map
|