edit-google-spreadsheet
Advanced tools
Comparing version 0.1.7 to 0.2.0
@@ -9,2 +9,4 @@ "use strict"; | ||
var util = require("./util"); | ||
var Metadata = require('./metadata'); | ||
var async = require('async'); | ||
@@ -260,7 +262,9 @@ //public api | ||
var row, col, strs = []; | ||
this.maxRow = 0; | ||
this.maxCol = 0; | ||
for(row in this.entries) | ||
for(col in this.entries[row]) { | ||
var obj = this.entries[row][col]; | ||
this.maxRow = Math.max(this.maxRow, row); | ||
this.maxCol = Math.max(this.maxCol, col); | ||
if(typeof obj.val === 'string') | ||
@@ -280,6 +284,9 @@ obj.val = this.getNames(obj); | ||
Spreadsheet.prototype.send = function(callback) { | ||
Spreadsheet.prototype.send = function(options, callback) { | ||
if(typeof options === 'function') | ||
callback = options; | ||
else if(!callback) | ||
callback = function() {}; | ||
if(!callback) callback = function() {}; | ||
if(!this.token) | ||
@@ -297,24 +304,40 @@ return callback("No authorization token. Use auth() first."); | ||
// _this.log(entries.white); | ||
request({ | ||
method: 'POST', | ||
url: this.baseUrl() + '/batch', | ||
headers: this.authHeaders, | ||
body: body | ||
}, function(error, response, body) { | ||
async.series([ | ||
function autoSize(next){ | ||
if(!options || !options.autoSize) | ||
return next(); | ||
_this.metadata(function(err, metadata){ | ||
if(err) return next(err); | ||
if(error) | ||
return callback(error, null); | ||
//no resize needed | ||
if(metadata.rowCount >= _this.maxRow && | ||
metadata.colCount >= _this.maxCol) | ||
next(null); | ||
if(body.indexOf("success='0'") >= 0) { | ||
error = "Error Updating Spreadsheet"; | ||
_this.log(error.red.underline + ("\nResponse:\n" + body)); | ||
} else { | ||
_this.log("Successfully Updated Spreadsheet".green); | ||
//data has been successfully sent, clear it | ||
_this.reset(); | ||
//resize with maximums | ||
metadata.rowCount = Math.max(metadata.rowCount, _this.maxRow); | ||
metadata.colCount = Math.max(metadata.colCount, _this.maxCol); | ||
_this.metadata(metadata, next); | ||
}); | ||
}, | ||
function send(next){ | ||
request({ | ||
method: 'POST', | ||
url: _this.baseUrl() + '/batch', | ||
headers: _this.authHeaders, | ||
body: body | ||
}, function(err, response, body) { | ||
if(err) return next(err); | ||
if(body.indexOf("success='0'") >= 0) { | ||
err = "Error Updating Spreadsheet"; | ||
_this.log(err.red.underline + ("\nResponse:\n" + body)); | ||
} else { | ||
_this.log("Successfully Updated Spreadsheet".green); | ||
//data has been successfully sent, clear it | ||
_this.reset(); | ||
} | ||
next(err); | ||
}); | ||
} | ||
callback(error); | ||
}); | ||
], callback); | ||
}; | ||
@@ -397,3 +420,15 @@ | ||
}); | ||
}; | ||
Spreadsheet.prototype.metadata = function(data, callback){ | ||
var meta = new Metadata(this); | ||
if(typeof data === 'function') { | ||
callback = data; | ||
meta.get(callback); | ||
return; | ||
} else if(!callback){ | ||
callback = function() {}; | ||
} | ||
meta.set(data, callback); | ||
return; | ||
}; |
{ | ||
"name": "edit-google-spreadsheet", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "colors": "~0.6.0-1", |
@@ -76,3 +76,37 @@ ## Node - Edit Google Spreadsheet | ||
``` | ||
#### Metadata | ||
Get metadata | ||
``` js | ||
function sheetReady(err, spreadsheet) { | ||
if(err) throw err; | ||
spreadsheet.metadata(function(err, metadata){ | ||
if(err) throw err; | ||
console.log(metadata); | ||
// { title: 'Sheet3', rowCount: '100', colCount: '20', updated: [Date] } | ||
}); | ||
} | ||
``` | ||
Set metadata | ||
``` js | ||
function sheetReady(err, spreadsheet) { | ||
if(err) throw err; | ||
spreadsheet.metadata({ | ||
title: 'Sheet3' | ||
rowCount: '100', | ||
colCount: '20' | ||
}, function(err, metadata){ | ||
if(err) throw err; | ||
console.log(metadata); | ||
}); | ||
} | ||
``` | ||
***WARNING: all cells outside the range of the new size will be silently deleted*** | ||
#### More `add` Examples | ||
@@ -121,8 +155,16 @@ | ||
##### `Spreadsheet.create( options )` | ||
See [Options](#Options) below | ||
##### spreadsheet.`add( obj | array )` | ||
Add cells to the batch. See examples. | ||
##### spreadsheet.`send( callback( err ) )` | ||
##### spreadsheet.`send( [options,] callback( err ) )` | ||
Sends off the batch of `add()`ed cells. Clears all cells once complete. | ||
* `options.autoSize` | ||
Increase the page size (rows and columns) in order to content fits on it (default `false`). | ||
##### spreadsheet.`receive( callback( err , rows , info ) )` | ||
@@ -145,2 +187,9 @@ Recieves the entire spreadsheet. The `rows` object is an object in the same format as the cells you `add()`, so `add(rows)` will be valid. The `info` object looks like: | ||
##### spreadsheet.`metadata( [data, ] callback )` | ||
Get and set metadata | ||
*Note: when setting new metadata, if `rowCount` and/or `colCount` is left out, | ||
an extra request will be made to retrieve the missing data.* | ||
#### Options | ||
@@ -158,3 +207,3 @@ | ||
##### `oauth` | ||
OAuth configuration object. See [google-oauth-jwt](https://github.com/extrabacon/google-oauth-jwt). *By default `oauth.scopes` is set to `['https://spreadsheets.google.com/feeds']` (`http` if not `useHTTPS`* | ||
OAuth configuration object. See [google-oauth-jwt](https://github.com/extrabacon/google-oauth-jwt#specifying-options). *By default `oauth.scopes` is set to `['https://spreadsheets.google.com/feeds']` (`https` if `useHTTPS`)* | ||
@@ -184,1 +233,7 @@ ##### `spreadSheetName` `spreadsheetId` | ||
Thanks to `googleclientlogin` for easy Google API ClientLogin Tokens | ||
#### References | ||
* https://developers.google.com/google-apps/spreadsheets/ | ||
* https://developers.google.com/google-apps/documents-list/ | ||
21673
9
485
235