@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
package/lib/googleApi.js
CHANGED
|
@@ -1,460 +1,326 @@
|
|
|
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
|
-
|
|
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
|
-
return
|
|
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
|
-
return
|
|
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
|
-
|
|
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
|
-
"range": {
|
|
327
|
-
"sheetId": 0,
|
|
328
|
-
"dimension": "COLUMNS",
|
|
329
|
-
"startIndex": 0,
|
|
330
|
-
"endIndex": 1
|
|
331
|
-
},
|
|
332
|
-
"properties": {
|
|
333
|
-
"pixelSize": 160
|
|
334
|
-
},
|
|
335
|
-
"fields": "pixelSize"
|
|
336
|
-
},
|
|
337
|
-
}
|
|
338
|
-
]
|
|
339
|
-
})];
|
|
340
|
-
case 4:
|
|
341
|
-
_c.sent();
|
|
342
|
-
console.log('do batch update 2');
|
|
343
|
-
return [4 /*yield*/, cli.doBatchUpdate(id, {
|
|
344
|
-
"requests": [
|
|
345
|
-
{
|
|
346
|
-
"updateCells": {
|
|
347
|
-
"fields": "*",
|
|
348
|
-
"range": {
|
|
349
|
-
"sheetId": 0,
|
|
350
|
-
"startColumnIndex": 0,
|
|
351
|
-
"endColumnIndex": 10,
|
|
352
|
-
"startRowIndex": 0,
|
|
353
|
-
"endRowIndex": 10
|
|
354
|
-
},
|
|
355
|
-
"rows": [
|
|
356
|
-
{
|
|
357
|
-
"values": [
|
|
358
|
-
{
|
|
359
|
-
"userEnteredFormat": {
|
|
360
|
-
"backgroundColor": {
|
|
361
|
-
"blue": 10,
|
|
362
|
-
"green": 10,
|
|
363
|
-
"red": 255
|
|
364
|
-
},
|
|
365
|
-
"borders": {
|
|
366
|
-
"bottom": {
|
|
367
|
-
"style": "SOLID",
|
|
368
|
-
"width": 8,
|
|
369
|
-
"color": {
|
|
370
|
-
"blue": 0,
|
|
371
|
-
"green": 255,
|
|
372
|
-
"red": 0
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
},
|
|
377
|
-
"userEnteredValue": { "stringValue": "strstsdfasdf" }
|
|
378
|
-
},
|
|
379
|
-
{
|
|
380
|
-
"userEnteredValue": { "stringValue": "col1" }
|
|
381
|
-
}
|
|
382
|
-
]
|
|
383
|
-
}
|
|
384
|
-
]
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
]
|
|
388
|
-
})];
|
|
389
|
-
case 5:
|
|
390
|
-
upres = _c.sent();
|
|
391
|
-
console.log(upres);
|
|
392
|
-
console.log('append 1');
|
|
393
|
-
return [4 /*yield*/, cli.append({
|
|
394
|
-
id: id,
|
|
395
|
-
range: "'Sheet1'!A1:B2"
|
|
396
|
-
}, [
|
|
397
|
-
['aaa', 'bbb1']
|
|
398
|
-
])];
|
|
399
|
-
case 6:
|
|
400
|
-
appres = _c.sent();
|
|
401
|
-
console.log('append res');
|
|
402
|
-
console.log(appres);
|
|
403
|
-
console.log('read');
|
|
404
|
-
return [4 /*yield*/, cli.read({
|
|
405
|
-
id: id,
|
|
406
|
-
range: 'A1:B4'
|
|
407
|
-
})];
|
|
408
|
-
case 7:
|
|
409
|
-
rres = _c.sent();
|
|
410
|
-
console.log('read res');
|
|
411
|
-
console.log(rres);
|
|
412
|
-
sheet = cli.getSheetOps(id);
|
|
413
|
-
sheet.doBatchUpdate({
|
|
414
|
-
"requests": [
|
|
415
|
-
{
|
|
416
|
-
"updateDimensionProperties": {
|
|
417
|
-
"range": {
|
|
418
|
-
"sheetId": 0,
|
|
419
|
-
"dimension": "COLUMNS",
|
|
420
|
-
"startIndex": 0,
|
|
421
|
-
"endIndex": 1
|
|
422
|
-
},
|
|
423
|
-
"properties": {
|
|
424
|
-
"pixelSize": 100
|
|
425
|
-
},
|
|
426
|
-
"fields": "pixelSize"
|
|
427
|
-
},
|
|
428
|
-
}
|
|
429
|
-
]
|
|
430
|
-
});
|
|
431
|
-
return [4 /*yield*/, sheet.append('A:B', [['c', 'D']])];
|
|
432
|
-
case 8:
|
|
433
|
-
_c.sent();
|
|
434
|
-
_b = (_a = console).log;
|
|
435
|
-
return [4 /*yield*/, sheet.read('A1:B4')];
|
|
436
|
-
case 9:
|
|
437
|
-
_b.apply(_a, [_c.sent()]);
|
|
438
|
-
return [2 /*return*/];
|
|
439
|
-
}
|
|
440
|
-
});
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
exports.test = test;
|
|
444
|
-
//test(true).catch(err => {
|
|
445
|
-
///console.log(err)
|
|
446
|
-
//console.log(err.response.data);
|
|
447
|
-
//})
|
|
448
|
-
/*
|
|
449
|
-
async function test2() {
|
|
450
|
-
const creds = getClientCredsByEnv('gzperm');
|
|
451
|
-
await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
|
|
452
|
-
}
|
|
453
|
-
console.log('invoking test2')
|
|
454
|
-
test2().catch(err => {
|
|
455
|
-
console.log('error');
|
|
456
|
-
//console.log(err);
|
|
457
|
-
console.log(err.response.text || err.response.data);
|
|
458
|
-
})
|
|
459
|
-
*/
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getTokenFromCode = getTokenFromCode;
|
|
8
|
+
exports.getClient = getClient;
|
|
9
|
+
exports.getClientCredsByEnv = getClientCredsByEnv;
|
|
10
|
+
exports.getClientByEnv = getClientByEnv;
|
|
11
|
+
exports.test = test;
|
|
12
|
+
//import axios, { Method } from 'axios';
|
|
13
|
+
const httpRequest_1 = require("./httpRequest");
|
|
14
|
+
const util_1 = require("./util");
|
|
15
|
+
;
|
|
16
|
+
//old, does not need refresh token
|
|
17
|
+
async function getTokenFromCode(creds, code, redirect_uri) {
|
|
18
|
+
const { client_id, client_secret } = creds;
|
|
19
|
+
const dataStr = (0, util_1.getFormData)({
|
|
20
|
+
client_secret,
|
|
21
|
+
client_id,
|
|
22
|
+
code,
|
|
23
|
+
redirect_uri,
|
|
24
|
+
grant_type: 'authorization_code'
|
|
25
|
+
});
|
|
26
|
+
const tokenBody = await (0, httpRequest_1.doHttpRequest)({
|
|
27
|
+
url: 'https://oauth2.googleapis.com/token',
|
|
28
|
+
method: 'POST',
|
|
29
|
+
data: dataStr,
|
|
30
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
31
|
+
}).then(r => {
|
|
32
|
+
return r;
|
|
33
|
+
});
|
|
34
|
+
return tokenBody;
|
|
35
|
+
}
|
|
36
|
+
function betterErr(desc) {
|
|
37
|
+
return err => {
|
|
38
|
+
const webRsp = err.response;
|
|
39
|
+
if (webRsp) {
|
|
40
|
+
throw {
|
|
41
|
+
desc,
|
|
42
|
+
...(0, util_1.pick)(webRsp, ['data', 'status', 'statusText', 'headers', 'config.url'])
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
throw err;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async function doRefresh(creds) {
|
|
49
|
+
const { refresh_token, client_id, client_secret } = creds;
|
|
50
|
+
if (!client_id || !client_secret)
|
|
51
|
+
throw `doRefresh needs client_id and client_secret in creds`;
|
|
52
|
+
if (!refresh_token)
|
|
53
|
+
throw `missing refresh_token from creds`;
|
|
54
|
+
const dataStr = (0, util_1.getFormData)({
|
|
55
|
+
client_secret,
|
|
56
|
+
client_id,
|
|
57
|
+
refresh_token,
|
|
58
|
+
grant_type: 'refresh_token'
|
|
59
|
+
});
|
|
60
|
+
const refreshBody = await (0, httpRequest_1.doHttpRequest)({ url: 'https://oauth2.googleapis.com/token', data: dataStr,
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" } }).then(r => {
|
|
63
|
+
return r.data;
|
|
64
|
+
}).catch(betterErr(`refreshToken https://oauth2.googleapis.com/token`));
|
|
65
|
+
const { access_token, expires_in, token_type } = refreshBody;
|
|
66
|
+
const doOp = (op, id, postFix, data) => {
|
|
67
|
+
const url = `https://sheets.googleapis.com/v4/spreadsheets/${id}${postFix}`;
|
|
68
|
+
return (0, httpRequest_1.doHttpRequest)({
|
|
69
|
+
url,
|
|
70
|
+
headers: {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
"Authorization": `Bearer ${access_token}`,
|
|
73
|
+
},
|
|
74
|
+
method: op,
|
|
75
|
+
data,
|
|
76
|
+
}).then(r => {
|
|
77
|
+
return (r.data);
|
|
78
|
+
}).catch(betterErr(`doOps error ${url}`));
|
|
79
|
+
};
|
|
80
|
+
const doPost = (id, postFix, data) => doOp('POST', id, postFix, data);
|
|
81
|
+
const doBatchUpdate = async (id, data) => doPost(id, ':batchUpdate', data);
|
|
82
|
+
const append = async ({ id, range }, data, opts) => {
|
|
83
|
+
if (!opts) {
|
|
84
|
+
opts = {};
|
|
85
|
+
}
|
|
86
|
+
if (!opts.valueInputOption)
|
|
87
|
+
opts.valueInputOption = 'USER_ENTERED';
|
|
88
|
+
return await doPost(id, `/values/${range}:append?${(0, util_1.getFormData)(opts)}`, { values: data });
|
|
89
|
+
};
|
|
90
|
+
const read = async ({ id, range }) => doOp('GET', id, `/values/${range}`);
|
|
91
|
+
return {
|
|
92
|
+
access_token,
|
|
93
|
+
expires_on: new Date().getTime() + (expires_in * 1000 - 2000),
|
|
94
|
+
token_type,
|
|
95
|
+
doBatchUpdate,
|
|
96
|
+
append,
|
|
97
|
+
read,
|
|
98
|
+
getSheetOps: id => {
|
|
99
|
+
const getInfo = () => doOp('GET', id, '');
|
|
100
|
+
const createSheet = async (sheetId, title) => {
|
|
101
|
+
return doBatchUpdate(id, {
|
|
102
|
+
requests: [
|
|
103
|
+
{
|
|
104
|
+
addSheet: {
|
|
105
|
+
properties: {
|
|
106
|
+
sheetId,
|
|
107
|
+
title,
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
const sheetInfo = async () => {
|
|
115
|
+
const sheetInfos = await getInfo();
|
|
116
|
+
return sheetInfos.sheets.map(s => {
|
|
117
|
+
const props = s.properties;
|
|
118
|
+
return {
|
|
119
|
+
sheetId: props.sheetId,
|
|
120
|
+
title: props.title,
|
|
121
|
+
index: props.index, //not important,
|
|
122
|
+
...props.gridProperties, //rowCount, columnCount
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
doBatchUpdate: data => doBatchUpdate(id, data),
|
|
128
|
+
append: (range, data, ops) => append({ id, range }, data, ops),
|
|
129
|
+
read: range => read({ id, range }),
|
|
130
|
+
sheetInfo,
|
|
131
|
+
createSheet,
|
|
132
|
+
updateValues: (range, values, opts) => {
|
|
133
|
+
if (!opts) {
|
|
134
|
+
opts = {
|
|
135
|
+
valueInputOption: 'USER_ENTERED'
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (!opts.valueInputOption)
|
|
139
|
+
opts.valueInputOption = 'USER_ENTERED';
|
|
140
|
+
return doOp('PUT', id, `/values/${encodeURIComponent(range)}?${(0, util_1.getFormData)(opts)}`, {
|
|
141
|
+
values,
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
addSheet: async (title) => {
|
|
145
|
+
const sheetsInfo = await sheetInfo();
|
|
146
|
+
//input YYYY, sheetId,
|
|
147
|
+
const found = sheetsInfo.find(s => s.title === title);
|
|
148
|
+
if (found) {
|
|
149
|
+
return {
|
|
150
|
+
found,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
let newId = 0;
|
|
154
|
+
for (;; newId++) {
|
|
155
|
+
const existing = sheetsInfo.find(s => s.sheetId === newId);
|
|
156
|
+
if (existing)
|
|
157
|
+
continue;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
return createSheet(newId.toString(), title);
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const clients = {};
|
|
167
|
+
async function getClient(creds) {
|
|
168
|
+
const name = creds.client_id;
|
|
169
|
+
let client = clients[name];
|
|
170
|
+
const now = new Date().getTime();
|
|
171
|
+
if (!client || client.expires_on <= now) {
|
|
172
|
+
client = await doRefresh(creds);
|
|
173
|
+
if (!client)
|
|
174
|
+
return null;
|
|
175
|
+
clients[name] = client;
|
|
176
|
+
}
|
|
177
|
+
return client;
|
|
178
|
+
}
|
|
179
|
+
function getClientCredsByEnv(envName) {
|
|
180
|
+
const creds = {
|
|
181
|
+
client_id: process.env[`google_${envName}_client_id`],
|
|
182
|
+
client_secret: process.env[`google_${envName}_client_secret`],
|
|
183
|
+
refresh_token: process.env[`google_${envName}_refresh_token`],
|
|
184
|
+
};
|
|
185
|
+
return creds;
|
|
186
|
+
}
|
|
187
|
+
async function getClientByEnv(envName) {
|
|
188
|
+
const creds = getClientCredsByEnv(envName);
|
|
189
|
+
return getClient(creds);
|
|
190
|
+
}
|
|
191
|
+
async function test(d) {
|
|
192
|
+
const cli = await getClientByEnv('gzperm');
|
|
193
|
+
const ops = await cli.getSheetOps('1u_AR8y7iCRPGyDhdOb1cHhjL-vclCIxuLkMhIxd08mU');
|
|
194
|
+
console.log('update val');
|
|
195
|
+
const rrr = await ops.updateValues('Sheet1!G18:G18', [['1']]);
|
|
196
|
+
console.log('update done');
|
|
197
|
+
console.log(rrr);
|
|
198
|
+
if (d)
|
|
199
|
+
return;
|
|
200
|
+
if (!cli)
|
|
201
|
+
return console.log('failed to get client');
|
|
202
|
+
const id = '1MO27odjCsxk6MWL0DygubU53hrtt3OB8SEnqjpUHJ-U';
|
|
203
|
+
if (d)
|
|
204
|
+
return;
|
|
205
|
+
console.log('do batch update');
|
|
206
|
+
await cli.doBatchUpdate(id, {
|
|
207
|
+
"requests": [
|
|
208
|
+
{
|
|
209
|
+
"updateDimensionProperties": {
|
|
210
|
+
"range": {
|
|
211
|
+
"sheetId": 0,
|
|
212
|
+
"dimension": "COLUMNS",
|
|
213
|
+
"startIndex": 0,
|
|
214
|
+
"endIndex": 1
|
|
215
|
+
},
|
|
216
|
+
"properties": {
|
|
217
|
+
"pixelSize": 160
|
|
218
|
+
},
|
|
219
|
+
"fields": "pixelSize"
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
]
|
|
223
|
+
});
|
|
224
|
+
console.log('do batch update 2');
|
|
225
|
+
const upres = await cli.doBatchUpdate(id, {
|
|
226
|
+
"requests": [
|
|
227
|
+
{
|
|
228
|
+
"updateCells": {
|
|
229
|
+
"fields": "*",
|
|
230
|
+
"range": {
|
|
231
|
+
"sheetId": 0,
|
|
232
|
+
"startColumnIndex": 0,
|
|
233
|
+
"endColumnIndex": 10,
|
|
234
|
+
"startRowIndex": 0,
|
|
235
|
+
"endRowIndex": 10
|
|
236
|
+
},
|
|
237
|
+
"rows": [
|
|
238
|
+
{
|
|
239
|
+
"values": [
|
|
240
|
+
{
|
|
241
|
+
"userEnteredFormat": {
|
|
242
|
+
"backgroundColor": {
|
|
243
|
+
"blue": 10,
|
|
244
|
+
"green": 10,
|
|
245
|
+
"red": 255
|
|
246
|
+
},
|
|
247
|
+
"borders": {
|
|
248
|
+
"bottom": {
|
|
249
|
+
"style": "SOLID",
|
|
250
|
+
"width": 8,
|
|
251
|
+
"color": {
|
|
252
|
+
"blue": 0,
|
|
253
|
+
"green": 255,
|
|
254
|
+
"red": 0
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
"userEnteredValue": { "stringValue": "strstsdfasdf" }
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
"userEnteredValue": { "stringValue": "col1" }
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
]
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
]
|
|
270
|
+
});
|
|
271
|
+
console.log(upres);
|
|
272
|
+
console.log('append 1');
|
|
273
|
+
const appres = await cli.append({
|
|
274
|
+
id,
|
|
275
|
+
range: `'Sheet1'!A1:B2`
|
|
276
|
+
}, [
|
|
277
|
+
['aaa', 'bbb1']
|
|
278
|
+
]);
|
|
279
|
+
console.log('append res');
|
|
280
|
+
console.log(appres);
|
|
281
|
+
console.log('read');
|
|
282
|
+
const rres = await cli.read({
|
|
283
|
+
id,
|
|
284
|
+
range: 'A1:B4'
|
|
285
|
+
});
|
|
286
|
+
console.log('read res');
|
|
287
|
+
console.log(rres);
|
|
288
|
+
const sheet = cli.getSheetOps(id);
|
|
289
|
+
sheet.doBatchUpdate({
|
|
290
|
+
"requests": [
|
|
291
|
+
{
|
|
292
|
+
"updateDimensionProperties": {
|
|
293
|
+
"range": {
|
|
294
|
+
"sheetId": 0,
|
|
295
|
+
"dimension": "COLUMNS",
|
|
296
|
+
"startIndex": 0,
|
|
297
|
+
"endIndex": 1
|
|
298
|
+
},
|
|
299
|
+
"properties": {
|
|
300
|
+
"pixelSize": 100
|
|
301
|
+
},
|
|
302
|
+
"fields": "pixelSize"
|
|
303
|
+
},
|
|
304
|
+
}
|
|
305
|
+
]
|
|
306
|
+
});
|
|
307
|
+
await sheet.append('A:B', [['c', 'D']]);
|
|
308
|
+
console.log(await sheet.read('A1:B4'));
|
|
309
|
+
}
|
|
310
|
+
//test(true).catch(err => {
|
|
311
|
+
///console.log(err)
|
|
312
|
+
//console.log(err.response.data);
|
|
313
|
+
//})
|
|
314
|
+
/*
|
|
315
|
+
async function test2() {
|
|
316
|
+
const creds = getClientCredsByEnv('gzperm');
|
|
317
|
+
await getTokenFromCode(creds, '4/xxxx', 'http://localhost:3000');
|
|
318
|
+
}
|
|
319
|
+
console.log('invoking test2')
|
|
320
|
+
test2().catch(err => {
|
|
321
|
+
console.log('error');
|
|
322
|
+
//console.log(err);
|
|
323
|
+
console.log(err.response.text || err.response.data);
|
|
324
|
+
})
|
|
325
|
+
*/
|
|
460
326
|
//# sourceMappingURL=googleApi.js.map
|