edit-google-spreadsheet
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -1,11 +0,16 @@ | ||
var GoogleClientLogin = require('googleclientlogin').GoogleClientLogin; | ||
var GoogleOAuthJWT = require('google-oauth-jwt'); | ||
//client auth helper | ||
module.exports = function(usr, pw, done) { | ||
if(!usr || !pw || !done) return; | ||
module.exports = function(opts, done) { | ||
if(opts.username && opts.password) | ||
clientLogin(opts.username, opts.password, done); | ||
else if(opts.oauth) | ||
oauthLogin(opts.oauth, opts.useHTTPS, done); | ||
}; | ||
function clientLogin(username, password, done) { | ||
var googleAuth = new GoogleClientLogin({ | ||
email: usr, | ||
password: pw, | ||
email: username, | ||
password: password, | ||
service: 'spreadsheets', | ||
@@ -16,9 +21,22 @@ accountType: GoogleClientLogin.accountTypes.google | ||
googleAuth.on(GoogleClientLogin.events.error, function(e) { | ||
done("Google Auth Error: " + e.message); | ||
done("Google Client Login Error: " + e.message); | ||
}); | ||
//success - next step | ||
googleAuth.on(GoogleClientLogin.events.login, function() { | ||
done(null, googleAuth.getAuthId()); | ||
done(null, {type : 'GoogleLogin', token : googleAuth.getAuthId()}); | ||
}); | ||
googleAuth.login(); | ||
}; | ||
} | ||
function oauthLogin(oauth, useHTTPS, done) { | ||
if(!oauth.scopes) | ||
oauth.scopes = ['http'+useHTTPS+'://spreadsheets.google.com/feeds']; | ||
GoogleOAuthJWT.authenticate(oauth, function (err, token) { | ||
if(err) | ||
done("Google OAuth Error: " + err); | ||
else | ||
done(null, {type : 'Bearer', token : token}); | ||
}); | ||
} |
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
@@ -11,14 +10,9 @@ | ||
//public api | ||
exports.create = function(opts) { | ||
var spreadsheet = new Spreadsheet(); | ||
if(!opts.callback) | ||
throw "Missing callback"; | ||
if(!opts.username) | ||
return opts.callback("Missing 'username'"); | ||
if(!opts.password) | ||
return opts.callback("Missing 'password'"); | ||
if(!(opts.username && opts.password) && !opts.oauth) | ||
return opts.callback("Missing authentication information"); | ||
if(!opts.spreadsheetId && !opts.spreadsheetName) | ||
@@ -29,2 +23,9 @@ return opts.callback("Missing 'spreadsheetId' or 'spreadsheetName'"); | ||
var spreadsheet = new Spreadsheet(); | ||
//default to http's' when undefined | ||
opts.useHTTPS = opts.useHTTPS === undefined || opts.useHTTPS ? 's' : ''; | ||
spreadsheet.protocol += opts.useHTTPS; | ||
//add to spreadsheet | ||
_.extend(spreadsheet, _.pick( opts, | ||
@@ -36,3 +37,3 @@ 'spreadsheetId', 'spreadsheetName', | ||
spreadsheet.log('Logging into Google...'.grey); | ||
auth(opts.username, opts.password, function(err, token) { | ||
auth(opts, function(err, token) { | ||
if(err) return opts.callback(err); | ||
@@ -42,3 +43,2 @@ spreadsheet.log('Logged into Google'.green); | ||
}); | ||
}; | ||
@@ -49,2 +49,3 @@ | ||
this.token = null; | ||
this.protocol = 'http'; | ||
this.reset(); | ||
@@ -86,3 +87,3 @@ } | ||
method: 'GET', | ||
url: 'https://spreadsheets.google.com/feeds/'+type+'sheets'+spreadsheetUrlId+'/private/full?alt=json', | ||
url: this.protocol+'://spreadsheets.google.com/feeds/'+type+'sheets'+spreadsheetUrlId+'/private/full?alt=json', | ||
headers: this.authHeaders | ||
@@ -116,4 +117,10 @@ }, function(err, response, body) { | ||
this.token = token; | ||
var authorizationHeader; | ||
if (token.type == 'GoogleLogin'){ | ||
authorizationHeader = 'GoogleLogin auth=' + token.token; | ||
} else { | ||
authorizationHeader = 'Bearer ' + token.token; | ||
} | ||
this.authHeaders = { | ||
'Authorization': 'GoogleLogin auth=' + token, | ||
'Authorization': authorizationHeader, | ||
'Content-Type': 'application/atom+xml', | ||
@@ -126,3 +133,3 @@ 'GData-Version': '3.0', | ||
Spreadsheet.prototype.baseUrl = function() { | ||
return 'http://spreadsheets.google.com/feeds/cells/' + this.spreadsheetId + '/' + this.worksheetId + '/private/full'; | ||
return this.protocol+'://spreadsheets.google.com/feeds/cells/' + this.spreadsheetId + '/' + this.worksheetId + '/private/full'; | ||
}; | ||
@@ -320,3 +327,2 @@ | ||
Spreadsheet.prototype.receive = function(callback) { | ||
if(!this.token) | ||
@@ -326,4 +332,3 @@ return callback("No authorization token. Use auth() first."); | ||
var _this = this; | ||
// get some stuff | ||
// get whole spreadsheet | ||
request({ | ||
@@ -334,11 +339,20 @@ method: 'GET', | ||
}, function(err, response, body) { | ||
//body is error | ||
if(response.statusCode != 200) | ||
err = ''+body; | ||
if(err) return callback(err, null); | ||
//show error | ||
if(err) | ||
return callback(err, null); | ||
var result = JSON.parse(body); | ||
var result; | ||
try { | ||
result = JSON.parse(body); | ||
} catch(e) { | ||
return callback("JSON Parse Error: " + e); | ||
} | ||
if(!result.feed) { | ||
err = "Error Reading Spreadsheet"; | ||
_this.log( | ||
@@ -355,2 +369,9 @@ err.red.underline + | ||
var info = { | ||
spreadsheetId: _this.spreadsheetId, | ||
worksheetId: _this.worksheetId, | ||
worksheetTitle: result.feed.title.$t || null, | ||
worksheetUpdated: result.feed.updated.$t || null, | ||
authors: result.feed.author && result.feed.author.map(function(author) { | ||
return { name: author.name.$t, email: author.email.$t }; | ||
}), | ||
totalCells: entries.length, | ||
@@ -386,2 +407,1 @@ totalRows: 0, | ||
}; | ||
{ | ||
"name": "edit-google-spreadsheet", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"dependencies": { | ||
@@ -8,4 +8,5 @@ "colors": "~0.6.0-1", | ||
"lodash": "~1.2.1", | ||
"request": "~2.21.0" | ||
"request": "~2.21.0", | ||
"google-oauth-jwt": "0.0.4" | ||
} | ||
} |
@@ -5,4 +5,2 @@ ## Node - Edit Google Spreadsheet | ||
*Warning: There have been API changes since last release. See below.* | ||
#### Install | ||
@@ -15,3 +13,3 @@ ``` | ||
Create sheet: | ||
Create sheet with client login: | ||
@@ -34,2 +32,19 @@ ``` js | ||
Create sheet with OAuth: | ||
``` js | ||
var Spreadsheet = require('edit-google-spreadsheet'); | ||
Spreadsheet.create({ | ||
debug: true, | ||
oauth : { | ||
email: 'some-id@developer.gserviceaccount.com', | ||
keyFile: 'private-key.pem' | ||
}, | ||
spreadsheetName: 'node-edit-spreadsheet', | ||
worksheetName: 'Sheet1', | ||
callback: sheetReady | ||
}); | ||
``` | ||
Update sheet: | ||
@@ -56,3 +71,3 @@ | ||
spreadsheet.receive(function(err, rows) { | ||
spreadsheet.receive(function(err, rows, info) { | ||
if(err) throw err; | ||
@@ -66,3 +81,3 @@ console.log("Found rows:", rows); | ||
#### More Examples | ||
#### More `add` Examples | ||
@@ -96,8 +111,8 @@ Batch edit: | ||
Named cell references: | ||
Formula building with named cell references: | ||
``` js | ||
spreadsheet.add({ | ||
3: { | ||
4: { name: "a", val: 42 }, | ||
5: { name: "b", val: 21 }, | ||
4: { name: "a", val: 42 }, //'42' though tagged as "a" | ||
5: { name: "b", val: 21 }, //'21' though tagged as "b" | ||
6: "={{ a }}+{{ b }}" //forumla adding row3,col4 with row3,col5 => '=D3+E3' | ||
@@ -109,3 +124,2 @@ } | ||
#### API | ||
@@ -120,26 +134,46 @@ | ||
##### spreadsheet.`receive( callback( err , rows , info ) )` | ||
Recieves the entire spreadsheet. The `rows` object returned is in the same object format as the cells you `add()`, so `add(rows)` will be valid. The `info` object looks like `{ totalRows: 1, totalCells: 1, lastRow: 3, nextRow: 4 }`. | ||
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: | ||
``` | ||
{ | ||
spreadsheetId: 'ttFmrFPIipJimDQYSFyhwTg', | ||
worksheetId: 'od6', | ||
worksheetTitle: 'Sheet1', | ||
worksheetUpdated: '2013-05-31T11:38:11.116Z', | ||
authors: [ { name: 'jpillora', email: 'dev@jpillora.com' } ], | ||
totalCells: 1, | ||
totalRows: 1, | ||
lastRow: 3, | ||
nextRow: 4 | ||
} | ||
``` | ||
#### Options | ||
##### debug | ||
If `true`, will display colourful console logs outputing current actions | ||
##### `callback` | ||
Function returning the authenticated Spreadsheet instance. | ||
##### username password | ||
Google account - *Be careful about committing these to public repos* | ||
##### `debug` | ||
If `true`, will display colourful console logs outputing current actions. | ||
##### spreadSheetName spreadsheetId | ||
##### `username` `password` | ||
Google account - *Be careful about committing these to public repos*. | ||
##### `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`* | ||
##### `spreadSheetName` `spreadsheetId` | ||
The spreadsheet you wish to edit. Either the Name or Id is required. | ||
##### workSheetName worksheetId | ||
##### `workSheetName` `worksheetId` | ||
The worksheet you wish to edit. Either the Name or Id is required. | ||
##### callback | ||
Function returning the authenticated Spreadsheet instance | ||
##### `useHTTPS` | ||
Whether to use `https` when connecting to Google (default: `true`) | ||
#### Todo | ||
* OAuth | ||
* Create New Spreadsheets | ||
* Read specific range of cells | ||
* Option to cache auth token in file | ||
@@ -146,0 +180,0 @@ #### FAQ |
17046
377
180
5
+ Addedgoogle-oauth-jwt@0.0.4
+ Addedgoogle-oauth-jwt@0.0.4(transitive)