-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
230 lines (213 loc) · 6.66 KB
/
index.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
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
const path = require('path');
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const {OAuth2Client} = require('google-auth-library');
const Promise = require('promise');
/**
* Class that contains methods to write to google sheetsKey
*
* Use the constructor with the key of a given document.
* https://docs.google.com/spreadsheets/d/<very long key to use in constuctor>/edit
*/
class GoogleSheetWrite {
/**
* [constructor description]
* @param {[type]} key String from https://docs.google.com/spreadsheets/d/<very long key to use in constuctor>/edit
*/
constructor(key) {
this.SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
this.TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
this.TOKEN_PATH = this.TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json';
this.SECRET_PATH = path.join('.', 'client_secret.json');
if (!fs.existsSync(this.SECRET_PATH)) {
throw new Error('credentials.json does not exists, please create a desktop API token on https://developers.google.com/sheets/api/quickstart/nodejs (see \'Authorizing requests with OAuth 2.0\').');
}
this.sheetsKey = key;
this.promise = new Promise(resolve => {
const content = fs.readFileSync(path.resolve('.', 'client_secret.json'));
this.authorize(JSON.parse(content), () => {
console.log('Authorization successful!');
resolve();
});
});
}
/**
* Get authorization and call the method that reads the sheet.
*
* @param {String} range Range in sheet, ex: C1:D
* @return {Promise} a Promise that solves when data is written
*/
read(range) {
// Load client secrets from a local file.
return new Promise((resolve, reject) => {
this.promise.then(() => {
fs.readFile('client_secret.json', (err, content) => {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Google Sheets API.
this.authorize(JSON.parse(content), auth => {
return this.readSheet(auth, range)
.then(response => resolve(response))
.catch(error => reject(error));
});
});
});
});
}
/**
* Get authorization and call the method that updates the sheet.
*
* Note: it does not verify if data size is same or compatible as range.
* @param {[any]} values Data to be written to range
* @param {String} range Range in sheet, ex: C1:D
* @return {Promise} a Promise that solves when data is written
*/
write(values, range) {
// Load client secrets from a local file.
return new Promise((resolve, reject) => {
this.promise.then(() => {
fs.readFile('client_secret.json', (err, content) => {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Google Sheets API.
this.authorize(JSON.parse(content), auth => {
return this.updateSheet(auth, values, range)
.then(response => resolve(response))
.catch(error => reject(error));
});
});
});
});
}
/**
* Updates the sheet with the new data
* @param {[type]} auth authorization
* @param {[type]} range range where to write
* @return {[Promise]} a Promise that solves when data is written
*/
readSheet(auth, range) {
const sheets = google.sheets({version: 'v4', auth});
// Write to sheet
const request = {
auth,
spreadsheetId: this.sheetsKey,
range
};
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.get(request, (err, response) => {
if (err) {
reject(err);
} else {
resolve(response.data.values);
}
});
});
}
/**
* Updates the sheet with the new data
* @param {[type]} auth authorization
* @param {[type]} values data to be written
* @param {[type]} range range where to write
* @return {[Promise]} a Promise that solves when data is written
*/
updateSheet(auth, values, range) {
const sheets = google.sheets({version: 'v4', auth});
// Write to sheet
const request = {
auth,
spreadsheetId: this.sheetsKey,
valueInputOption: 'USER_ENTERED',
range,
resource: {
values
}
};
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.update(request, err => {
if (err) {
reject(err);
} else {
resolve('INFO:: Finished writing to: ' + range);
}
});
});
}
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
authorize(credentials, callback) {
const clientSecret = credentials.installed.client_secret;
const clientId = credentials.installed.client_id;
const redirectUrl = credentials.installed.redirect_uris[0];
const oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(this.TOKEN_PATH, (err, token) => {
if (err) {
this.getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
getNewToken(oauth2Client, callback) {
const authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline', // eslint-disable-line camelcase
scope: this.SCOPES
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', code => {
rl.close();
oauth2Client.getToken(code, (err, token) => {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
this.storeToken(token);
callback(oauth2Client);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
storeToken(token) {
try {
fs.mkdirSync(this.TOKEN_DIR);
} catch (error) {
if (error.code !== 'EEXIST') {
throw error;
}
}
fs.writeFileSync(this.TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + this.TOKEN_PATH);
}
}
module.exports = GoogleSheetWrite;