-
Notifications
You must be signed in to change notification settings - Fork 0
/
SheetGetter.js
46 lines (43 loc) · 1.17 KB
/
SheetGetter.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
// See https://flaviocopes.com/google-api-authentication/
const { google } = require('googleapis');
class SheetGetter {
constructor(sheetId, creds) {
let scopes = [
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/drive.readonly'
];
this.jwt = new google.auth.JWT(creds.client_email, null, creds.private_key, scopes);
this.sheetId = sheetId;
}
getSheet(sheetName) {
return new Promise((resolve, reject) => {
const sheets = google.sheets({version: 'v4', auth: this.jwt });
sheets.spreadsheets.values.get({
spreadsheetId: this.sheetId,
range: sheetName
}, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
getLastModified() {
return new Promise((resolve, reject) => {
const drive = google.drive({version: 'v3', auth: this.jwt });
drive.files.get({
fileId: this.sheetId,
fields: 'modifiedTime'
}, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
}
module.exports = { SheetGetter };