Skip to content

Commit

Permalink
Upgraded xml2js to fix problem with reading newlines, also added tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Oct 18, 2013
1 parent d0fd8c8 commit 6093d41
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules/
test
.env
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ This library uses [googleclientlogin](https://github.com/Ajnasz/GoogleClientLogi
- batch requests for cell based updates
- modifying worksheet/spreadsheet properties
- getting list of available spreadsheets for an authenticated user
- add some testing
## Links
Expand Down
26 changes: 13 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ module.exports = function( ss_key, auth_id ){
var google_auth;
var self = this;

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.");
}
Expand Down Expand Up @@ -41,7 +47,7 @@ module.exports = function( ss_key, auth_id ){
self.makeFeedRequest( ["worksheets", ss_key], 'GET', null, function(err, data, xml) {
if ( err ) return cb( err );
var ss_data = {
title: data.title["#"],
title: data.title["_"],
updated: data.updated,
author: data.author,
worksheets: []
Expand Down Expand Up @@ -143,16 +149,10 @@ module.exports = function( ss_key, auth_id ){
}

if ( body ){
var parser = new xml2js.Parser(xml2js.defaults["0.1"]);
parser.on("end", function(result) {
xml_parser.parseString(body, function(err, result){
if ( err ) return cb( err );
cb( null, result, body );
});

parser.on("error", function(err) {
cb( err );
});

parser.parseString(body);
} else {
if ( err ) cb( err );
else cb( null, true );
Expand All @@ -166,7 +166,7 @@ module.exports = function( ss_key, auth_id ){
var SpreadsheetWorksheet = function( spreadsheet, data ){
var self = this;
self.id = data.id.substring( data.id.lastIndexOf("/") + 1 );
self.title = data.title["#"];
self.title = data.title["_"];
self.rowCount = data['gs:rowCount'];
self.colCount = data['gs:colCount'];

Expand Down Expand Up @@ -195,13 +195,13 @@ var SpreadsheetRow = function( spreadsheet, data, xml ){
} else {
if (key == "id") {
self[key] = val;
} else if (val['#']) {
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'];
self['_links'][ link['$']['rel'] ] = link['$']['href'];
});
}
}
Expand Down
27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
{
"author": "Theo Ephraim <theozero@gmail.com> (http://theoephraim.com)",
"name": "google-spreadsheet",
"description": "Google Spreadsheet Data API for Node.js -- has functionality to read, edit, and create rows",
"version": "0.2.4",
"keywords": ["google", "spreadsheet", "spreadsheets", "gdata", "api"],
"description": "Google Spreadsheet Data API for Node.js -- can read, write, edit, and delete rows",
"version": "0.2.5",
"keywords": [
"google",
"spreadsheet",
"spreadsheets",
"gdata",
"api"
],
"homepage": "https://github.com/theoephraim/node-google-spreadsheets",
"repository": {
"type": "git",
"url": "git://github.com/theoephraim/node-google-spreadsheets.git"
},
"main": "index.js",
"engines": {
"node": ">= 0.4.0"
"node": "~0.4.0"
},
"dependencies": {
"request": ">= 2.0.2",
"xml2js": ">= 0.1.9",
"request": "~2.0.2",
"xml2js": "~0.2.8",
"googleclientlogin": "0.2.x"
},
"devDependencies": {
"nodeunit": "~0.8.1",
"async": "~0.2.9"
},
"scripts": {
"test": "node_modules/.bin/nodeunit test/spreadsheet_test.js"
}
}
}
107 changes: 107 additions & 0 deletions test/spreadsheet_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict';

/*
These tests use the test spreadsheet accessible at https://docs.google.com/spreadsheet/ccc?key=0Araic6gTol6SdEtwb1Badl92c2tlek45OUxJZDlyN2c#gid=0
In order to allow other devs to test both read and write funcitonality, the doc must be public read/write which means if someone feels like it, they could mess up the sheet which would mess up the tests. Please don't do that...
*/

var async = require('async');

var GoogleSpreadsheet = require("../index.js");
var doc = new GoogleSpreadsheet('0Araic6gTol6SdGtyUVAzQmVLM0lxUWlBMkNraWVubUE');
var sheet;

exports.node_google_spreadsheet = {
test_info: function(test){
test.expect(2);
doc.getInfo( function(err, sheet_info){
// even with public read/write, I think sheet author should stay constant
test.equal( sheet_info.author.email, 'theozero@gmail.com', 'can read sheet info from google doc');

sheet = sheet_info.worksheets[0];
test.equal( sheet.title, 'Sheet1', 'can read sheet names from doc');

test.done();
});
},
check_auth_env_vars: function(test){
test.expect(1);
var auth_set = process.env.GOOGLE_ACCOUNT != null && process.env.GOOGLE_PASSWORD != null;
test.ok(auth_set, 'Please set env variables GOOGLE_ACCOUNT and GOOGLE_PASSWORD to run tests' );
test.done(false);
},
check_init_auth: function(test){
doc.setAuth(process.env.GOOGLE_ACCOUNT, process.env.GOOGLE_PASSWORD, function(err){
test.done();
})
},
clear_sheet: function(test){
sheet.getRows(function(err, rows){
if ( rows.length == 0 ) test.done();
async.each( rows, function(row, cb){
row.del(cb);
}, function( cb ){
test.done();
})
})
},
check_delete: function(test){
test.expect(1);
async.waterfall([
function read(cb){
sheet.getRows( cb );
},
function check(rows, cb){
test.equal( rows.length, 0, 'sheet should be empty after delete calls');
cb();
}
], function(err){
if (err) console.log(err);
test.done();
});
},
basic_write_and_read: function(test){
test.expect(2);
async.waterfall([
function write(cb){
sheet.addRow({ key: 'test-key', val: 'test-val' }, function(){
cb();
});
},
function read(cb){
sheet.getRows( cb );
},
function check(rows, cb){
test.equal( rows[0].key, 'test-key', 'newly written value should match read value');
test.equal( rows[0].val, 'test-val', 'newly written value should match read value');
cb();
}
], function(err){
if (err) console.log(err);
test.done();
});
},
check_newlines_read: function(test){
test.expect(2);
async.waterfall([
function write(cb){
sheet.addRow({ key: "Newline\ntest", val: "Double\n\nnewline test" }, function(){
cb();
});
},
function read(cb){
sheet.getRows( cb );
},
function check(rows, cb){
// this was an issue before with an older version of xml2js
test.ok( rows[1].key.indexOf("\n") > 0, 'newline is read from sheet');
test.ok( rows[1].val.indexOf("\n\n") > 0, 'double newline is read from sheet');
cb();
}
], function(err){
if (err) console.log(err);
test.done();
});
}
};

0 comments on commit 6093d41

Please sign in to comment.