forked from theoephraim/node-google-spreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
663 lines (560 loc) · 20.8 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
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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
var async = require("async");
var request = require("request");
var xml2js = require("xml2js");
var http = require("http");
var querystring = require("querystring");
var _ = require('lodash');
var GoogleAuth = require("google-auth-library");
var GOOGLE_FEED_URL = "https://spreadsheets.google.com/feeds/";
var GOOGLE_AUTH_SCOPE = ["https://spreadsheets.google.com/feeds"];
var REQUIRE_AUTH_MESSAGE = 'You must authenticate to modify sheet data';
// The main class that represents a single sheet
// this is the main module.exports
var GoogleSpreadsheet = function( ss_key, auth_id, options ){
var self = this;
var google_auth = null;
var visibility = 'public';
var projection = 'values';
var auth_mode = 'anonymous';
var auth_client = new GoogleAuth();
var jwt_client;
options = options || {};
var xml_parser = new xml2js.Parser({
// options carried over from older version of xml2js
// might want to update how the code works, but for now this is fine
explicitArray: false,
explicitRoot: false
});
if ( !ss_key ) {
throw new Error("Spreadsheet key not provided.");
}
// auth_id may be null
setAuthAndDependencies(auth_id);
// Authentication Methods
this.setAuthToken = function( auth_id ) {
if (auth_mode == 'anonymous') auth_mode = 'token';
setAuthAndDependencies(auth_id);
}
// deprecated username/password login method
// leaving it here to help notify users why it doesn't work
this.setAuth = function( username, password, cb ){
return cb(new Error('Google has officially deprecated ClientLogin. Please upgrade this module and see the readme for more instrucations'))
}
this.useServiceAccountAuth = function( creds, cb ){
if (typeof creds == 'string') {
try {
creds = require(creds);
} catch (err) {
return cb(err);
}
}
jwt_client = new auth_client.JWT(creds.client_email, null, creds.private_key, GOOGLE_AUTH_SCOPE, null);
renewJwtAuth(cb);
}
function renewJwtAuth(cb) {
auth_mode = 'jwt';
jwt_client.authorize(function (err, token) {
if (err) return cb(err);
self.setAuthToken({
type: token.token_type,
value: token.access_token,
expires: token.expiry_date
});
cb()
});
}
this.isAuthActive = function() {
return !!google_auth;
}
function setAuthAndDependencies( auth ) {
google_auth = auth;
if (!options.visibility){
visibility = google_auth ? 'private' : 'public';
}
if (!options.projection){
projection = google_auth ? 'full' : 'values';
}
}
// This method is used internally to make all requests
this.makeFeedRequest = function( url_params, method, query_or_data, cb ){
var url;
var headers = {};
if (!cb ) cb = function(){};
if ( typeof(url_params) == 'string' ) {
// used for edit / delete requests
url = url_params;
} else if ( Array.isArray( url_params )){
//used for get and post requets
url_params.push( visibility, projection );
url = GOOGLE_FEED_URL + url_params.join("/");
}
async.series({
auth: function(step) {
if (auth_mode != 'jwt') return step();
// check if jwt token is expired
if (google_auth.expires > +new Date()) return step();
renewJwtAuth(step);
},
request: function(result, step) {
if ( google_auth ) {
if (google_auth.type === 'Bearer') {
headers['Authorization'] = 'Bearer ' + google_auth.value;
} else {
headers['Authorization'] = "GoogleLogin auth=" + google_auth;
}
}
headers['Gdata-Version'] = '3.0';
if ( method == 'POST' || method == 'PUT' ) {
headers['content-type'] = 'application/atom+xml';
}
if (method == 'PUT' || method == 'POST' && url.indexOf('/batch') != -1) {
headers['If-Match'] = '*';
}
if ( method == 'GET' && query_or_data ) {
var query = "?" + querystring.stringify( query_or_data );
// replacements are needed for using structured queries on getRows
query = query.replace(/%3E/g,'>');
query = query.replace(/%3D/g,'=');
query = query.replace(/%3C/g,'<');
url += query;
}
request( {
url: url,
method: method,
headers: headers,
body: method == 'POST' || method == 'PUT' ? query_or_data : null
}, function(err, response, body){
if (err) {
return cb( err );
} else if( response.statusCode === 401 ) {
return cb( new Error("Invalid authorization key."));
} else if ( response.statusCode >= 400 ) {
var message = _.isObject(body) ? JSON.stringify(body) : body.replace(/"/g, '"');
return cb( new Error("HTTP error "+response.statusCode+" ("+http.STATUS_CODES[response.statusCode])+") - "+message);
} else if ( response.statusCode === 200 && response.headers['content-type'].indexOf('text/html') >= 0 ) {
return cb( new Error("Sheet is private. Use authentication or make public. (see https://github.com/theoephraim/node-google-spreadsheet#a-note-on-authentication for details)"));
}
if ( body ){
xml_parser.parseString(body, function(err, result){
if ( err ) return cb( err );
cb( null, result, body );
});
} else {
if ( err ) cb( err );
else cb( null, true );
}
})
}
});
}
// public API methods
this.getInfo = function( cb ){
self.makeFeedRequest( ["worksheets", ss_key], 'GET', null, function(err, data, xml) {
if ( err ) return cb( err );
if (data===true) {
return cb(new Error('No response to getInfo call'))
}
var ss_data = {
id: data.id,
title: data.title,
updated: data.updated,
author: data.author,
worksheets: []
}
var worksheets = forceArray(data.entry);
worksheets.forEach( function( ws_data ) {
ss_data.worksheets.push( new SpreadsheetWorksheet( self, ws_data ) );
});
self.info = ss_data;
self.worksheets = ss_data.worksheets;
cb( null, ss_data );
});
}
// NOTE: worksheet IDs start at 1
this.addWorksheet = function( opts, cb ) {
// make opts optional
if (typeof opts == 'function'){
cb = opts;
opts = {};
}
cb = cb || _.noop;
if (!this.isAuthActive()) return cb(new Error(REQUIRE_AUTH_MESSAGE));
var defaults = {
title: 'Worksheet '+(+new Date()), // need a unique title
rowCount: 50,
colCount: 20
};
var opts = _.extend({}, defaults, opts);
// if column headers are set, make sure the sheet is big enough for them
if (opts.headers && opts.headers.length > opts.colCount) {
opts.colCount = opts.headers.length;
}
var data_xml = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006"><title>' +
opts.title +
'</title><gs:rowCount>' +
opts.rowCount +
'</gs:rowCount><gs:colCount>' +
opts.colCount +
'</gs:colCount></entry>';
self.makeFeedRequest( ["worksheets", ss_key], 'POST', data_xml, function(err, data, xml) {
if ( err ) return cb( err );
var sheet = new SpreadsheetWorksheet( self, data );
self.worksheets = self.worksheets || [];
self.worksheets.push(sheet);
sheet.setHeaderRow(opts.headers, function(err) {
cb(err, sheet);
})
});
}
this.removeWorksheet = function ( worksheet_id, cb ){
if (!this.isAuthActive()) return cb(new Error(REQUIRE_AUTH_MESSAGE));
self.makeFeedRequest( ["worksheets", ss_key, worksheet_id], 'DELETE', cb );
}
this.getRows = function( worksheet_id, opts, cb ){
// the first row is used as titles/keys and is not included
// opts is optional
if ( typeof( opts ) == 'function' ){
cb = opts;
opts = {};
}
var query = {}
if ( opts.offset ) query["start-index"] = opts.offset;
else if ( opts.start ) query["start-index"] = opts.start;
if ( opts.limit ) query["max-results"] = opts.limit;
else if ( opts.num ) query["max-results"] = opts.num;
if ( opts.orderby ) query["orderby"] = opts.orderby;
if ( opts.reverse ) query["reverse"] = 'true';
if ( opts.query ) query['sq'] = opts.query;
self.makeFeedRequest( ["list", ss_key, worksheet_id], 'GET', query, function(err, data, xml) {
if ( err ) return cb( err );
if (data===true) {
return cb(new Error('No response to getRows call'))
}
// gets the raw xml for each entry -- this is passed to the row object so we can do updates on it later
var entries_xml = xml.match(/<entry[^>]*>([\s\S]*?)<\/entry>/g);
// need to add the properties from the feed to the xml for the entries
var feed_props = _.clone(data.$);
delete feed_props['gd:etag'];
var feed_props_str = _.reduce(feed_props, function(str, val, key){
return str+key+'=\''+val+'\' ';
}, '');
entries_xml = _.map(entries_xml, function(xml){
return xml.replace('<entry ', '<entry '+feed_props_str);
});
var rows = [];
var entries = forceArray( data.entry );
var i=0;
entries.forEach( function( row_data ) {
rows.push( new SpreadsheetRow( self, row_data, entries_xml[ i++ ] ) );
});
cb(null, rows);
});
}
this.addRow = function( worksheet_id, data, cb ){
var data_xml = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">' + "\n";
Object.keys(data).forEach(function(key) {
if (key != 'id' && key != 'title' && key != 'content' && key != '_links'){
data_xml += '<gsx:'+ xmlSafeColumnName(key) + '>' + xmlSafeValue(data[key]) + '</gsx:'+ xmlSafeColumnName(key) + '>' + "\n"
}
});
data_xml += '</entry>';
self.makeFeedRequest( ["list", ss_key, worksheet_id], 'POST', data_xml, cb );
}
this.getCells = function (worksheet_id, opts, cb) {
// opts is optional
if (typeof( opts ) == 'function') {
cb = opts;
opts = {};
}
// Supported options are:
// min-row, max-row, min-col, max-col, return-empty
var query = _.assign({}, opts);
self.makeFeedRequest(["cells", ss_key, worksheet_id], 'GET', query, function (err, data, xml) {
if (err) return cb(err);
if (data===true) {
return cb(new Error('No response to getCells call'))
}
var cells = [];
var entries = forceArray(data['entry']);
var i = 0;
entries.forEach(function( cell_data ){
cells.push( new SpreadsheetCell( self, worksheet_id, cell_data ) );
});
cb( null, cells );
});
}
};
// Classes
var SpreadsheetWorksheet = function( spreadsheet, data ){
var self = this;
self.url = data.id;
self.id = data.id.substring( data.id.lastIndexOf("/") + 1 );
self.title = data.title;
self.rowCount = parseInt(data['gs:rowCount']);
self.colCount = parseInt(data['gs:colCount']);
self['_links'] = [];
links = forceArray( data.link );
links.forEach( function( link ){
self['_links'][ link['$']['rel'] ] = link['$']['href'];
});
self['_links']['cells'] = self['_links']['http://schemas.google.com/spreadsheets/2006#cellsfeed'];
self['_links']['bulkcells'] = self['_links']['cells']+'/batch';
function _setInfo(opts, cb) {
cb = cb || _.noop;
var xml = ''
+ '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">'
+ '<title>'+(opts.title || self.title)+'</title>'
+ '<gs:rowCount>'+(opts.rowCount || self.rowCount)+'</gs:rowCount>'
+ '<gs:colCount>'+(opts.colCount || self.colCount)+'</gs:colCount>'
+ '</entry>';
spreadsheet.makeFeedRequest(self['_links']['edit'], 'PUT', xml, function(err, response) {
if (err) return cb(err);
self.title = response.title;
self.rowCount = parseInt(response['gs:rowCount']);
self.colCount = parseInt(response['gs:colCount']);
cb();
});
}
this.resize = _setInfo;
this.setTitle = function(title, cb) {
_setInfo({title: title}, cb);
}
// just a convenience method to clear the whole sheet
// resizes to 1 cell, clears the cell, and puts it back
this.clear = function(cb) {
var cols = self.colCount;
var rows = self.colCount;
self.resize({rowCount: 1, colCount: 1}, function(err) {
if (err) return cb(err);
self.getCells(function(err, cells) {
cells[0].setValue(null, function(err) {
if (err) return cb(err);
self.resize({rowCount: rows, colCount: cols}, cb);
});
})
});
}
this.getRows = function(opts, cb){
spreadsheet.getRows(self.id, opts, cb);
}
this.getCells = function(opts, cb) {
spreadsheet.getCells(self.id, opts, cb);
}
this.addRow = function(data, cb){
spreadsheet.addRow(self.id, data, cb);
}
this.bulkUpdateCells = function(cells, cb) {
var entries = cells.map(function (cell, i) {
cell._needsSave = false;
return "<entry>\n <batch:id>" + cell.batchId + "</batch:id>\n <batch:operation type=\"update\"/>\n <id>" + self['_links']['cells']+'/'+cell.batchId + "</id>\n <link rel=\"edit\" type=\"application/atom+xml\"\n href=\"" + cell._links.edit + "\"/>\n <gs:cell row=\"" + cell.row + "\" col=\"" + cell.col + "\" inputValue=\"" + cell.valueForSave + "\"/>\n </entry>";
});
var data_xml = "<feed xmlns=\"http://www.w3.org/2005/Atom\"\n xmlns:batch=\"http://schemas.google.com/gdata/batch\"\n xmlns:gs=\"http://schemas.google.com/spreadsheets/2006\">\n <id>" + self['_links']['cells'] + "</id>\n " + entries.join("\n") + "\n </feed>";
spreadsheet.makeFeedRequest(self['_links']['bulkcells'], 'POST', data_xml, function(err, data) {
if (err) return cb(err);
// update all the cells
var cells_by_batch_id = _.indexBy(cells, 'batchId');
if (data.entry && data.entry.length) data.entry.forEach(function(cell_data) {
cells_by_batch_id[cell_data['batch:id']].updateValuesFromResponseData(cell_data);
});
cb();
});
}
this.del = function(cb){
spreadsheet.makeFeedRequest(self['_links']['edit'], 'DELETE', null, cb);
}
this.setHeaderRow = function(values, cb) {
if (!values) return cb();
if (values.length > self.colCount){
return cb(new Error('Sheet is not large enough to fit '+values.length+' columns. Resize the sheet first.'));
}
self.getCells({
'min-row': 1,
'max-row': 1,
'min-col': 1,
'max-col': self.colCount,
'return-empty': true
}, function(err, cells) {
if (err) return cb(err);
_.each(cells, function(cell) {
cell.value = values[cell.col-1] ? values[cell.col-1] : '';
});
self.bulkUpdateCells(cells, cb);
});
}
}
var SpreadsheetRow = function( spreadsheet, data, xml ){
var self = this;
self['_xml'] = xml;
Object.keys(data).forEach(function(key) {
var val = data[key];
if(key.substring(0, 4) === "gsx:") {
if(typeof val === 'object' && Object.keys(val).length === 0) {
val = null;
}
if (key == "gsx:") {
self[key.substring(0, 3)] = val;
} else {
self[key.substring(4)] = val;
}
} else {
if (key == "id") {
self[key] = val;
} else if (val['_']) {
self[key] = val['_'];
} else if ( key == 'link' ){
self['_links'] = [];
val = forceArray( val );
val.forEach( function( link ){
self['_links'][ link['$']['rel'] ] = link['$']['href'];
});
}
}
}, this);
self.save = function( cb ){
/*
API for edits is very strict with the XML it accepts
So we just do a find replace on the original XML.
It's dumb, but I couldnt get any JSON->XML conversion to work reliably
*/
var data_xml = self['_xml'];
// probably should make this part more robust?
data_xml = data_xml.replace('<entry>', "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>");
Object.keys( self ).forEach( function(key) {
if (key.substr(0,1) != '_' && typeof( self[key] == 'string') ){
data_xml = data_xml.replace( new RegExp('<gsx:'+xmlSafeColumnName(key)+">([\\s\\S]*?)</gsx:"+xmlSafeColumnName(key)+'>'), '<gsx:'+xmlSafeColumnName(key)+'>'+ xmlSafeValue(self[key]) +'</gsx:'+xmlSafeColumnName(key)+'>');
}
});
spreadsheet.makeFeedRequest( self['_links']['edit'], 'PUT', data_xml, cb );
}
self.del = function( cb ){
spreadsheet.makeFeedRequest( self['_links']['edit'], 'DELETE', null, cb );
}
}
var SpreadsheetCell = function( spreadsheet, worksheet_id, data ){
var self = this;
function init() {
self.id = data['id'];
self.row = parseInt(data['gs:cell']['$']['row']);
self.col = parseInt(data['gs:cell']['$']['col']);
self.batchId = 'R'+self.row+'C'+self.col;
self['_links'] = [];
links = forceArray( data.link );
links.forEach( function( link ){
self['_links'][ link['$']['rel'] ] = link['$']['href'];
});
self.updateValuesFromResponseData(data);
}
self.updateValuesFromResponseData = function(_data) {
// formula value
var input_val = _data['gs:cell']['$']['inputValue'];
// inputValue can be undefined so substr throws an error
// still unsure how this situation happens
if (input_val && input_val.substr(0,1) === '='){
self._formula = input_val;
} else {
self._formula = undefined;
}
// numeric values
if (_data['gs:cell']['$']['numericValue'] !== undefined) {
self._numericValue = parseFloat(_data['gs:cell']['$']['numericValue']);
} else {
self._numericValue = undefined;
}
// the main "value" - its always a string
self._value = _data['gs:cell']['_'] || '';
}
self.setValue = function(new_value, cb) {
self.value = new_value;
self.save(cb);
};
self._clearValue = function() {
self._formula = undefined;
self._numericValue = undefined;
self._value = '';
}
self.__defineGetter__('value', function(){
return self._value;
});
self.__defineSetter__('value', function(val){
if (!val) return self._clearValue();
var numeric_val = parseFloat(val);
if (!isNaN(numeric_val)){
self._numericValue = numeric_val;
self._value = val.toString();
} else {
self._numericValue = undefined;
self._value = val;
}
if (typeof val == 'string' && val.substr(0,1) === '=') {
// use the getter to clear the value
self.formula = val;
} else {
self._formula = undefined;
}
});
self.__defineGetter__('formula', function() {
return self._formula;
});
self.__defineSetter__('formula', function(val){
if (!val) return self._clearValue();
if (val.substr(0,1) !== '=') {
throw new Error('Formulas must start with "="');
}
self._numericValue = undefined;
self._value = '*SAVE TO GET NEW VALUE*';
self._formula = val;
});
self.__defineGetter__('numericValue', function() {
return self._numericValue;
});
self.__defineSetter__('numericValue', function(val) {
if (val === undefined || val === null) return self._clearValue();
if (isNaN(parseFloat(val)) || !isFinite(val)) {
throw new Error('Invalid numeric value assignment');
}
self._value = val.toString();
self._numericValue = parseFloat(val);
self._formula = undefined;
});
self.__defineGetter__('valueForSave', function() {
return xmlSafeValue(self._formula || self._value);
});
self.save = function(cb) {
self._needsSave = false;
var edit_id = 'https://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full/R'+self.row+'C'+self.col;
var data_xml =
'<entry><id>'+self.id+'</id>'+
'<link rel="edit" type="application/atom+xml" href="'+self.id+'"/>'+
'<gs:cell row="'+self.row+'" col="'+self.col+'" inputValue="'+self.valueForSave+'"/></entry>'
data_xml = data_xml.replace('<entry>', "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>");
spreadsheet.makeFeedRequest( self['_links']['edit'], 'PUT', data_xml, function(err, response) {
if (err) return cb(err);
self.updateValuesFromResponseData(response);
cb();
});
}
self.del = function(cb) {
self.setValue('', cb);
}
init();
return self;
}
module.exports = GoogleSpreadsheet;
//utils
var forceArray = function(val) {
if ( Array.isArray( val ) ) return val;
if ( !val ) return [];
return [ val ];
}
var xmlSafeValue = function(val){
if ( val == null ) return '';
return String(val).replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
var xmlSafeColumnName = function(val){
if (!val) return '';
return String(val).replace(/[\s_]+/g, '')
.toLowerCase();
}