Created
November 9, 2013 12:55
-
-
Save openhoat/7385152 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GoogleSpreadsheet = require('google-spreadsheet') | |
, Profess = require('profess') | |
, config, profess, spreadsheet, errorHandller; | |
config = { | |
gdocKey: process.argv[2] || '0AilC0U4Eb0tjdGEwR1RDTlRrbnVHbUVBWjBSVHk5OVE', // Sample doc https://docs.google.com/a/octo.com/spreadsheet/ccc?key=0AilC0U4Eb0tjdGEwR1RDTlRrbnVHbUVBWjBSVHk5OVE&usp=drive_web#gid=0 | |
googleLogin: process.argv[3], | |
googlePwd: process.argv[4], | |
worksheetIndex: 0 | |
}; | |
profess = new Profess(); | |
errorHandller = profess.handleError(function (err) { | |
console.error('Error :', err.toString()); | |
}); | |
profess. | |
do(function () { | |
spreadsheet = new GoogleSpreadsheet(config.gdocKey); | |
if (config.googleLogin) { | |
spreadsheet.setAuth(config.googleLogin, config.googlePwd, errorHandller); | |
} else { | |
profess.next(); | |
} | |
}). | |
then(function (data) { | |
spreadsheet.getInfo(errorHandller); | |
}). | |
then(function (sheetInfo) { | |
var worksheet; | |
if (!sheetInfo.worksheets || !sheetInfo.worksheets[config.worksheetIndex]) { | |
return errorHandller('No worksheet!'); | |
} | |
worksheet = sheetInfo.worksheets[config.worksheetIndex]; | |
worksheet.getRows(errorHandller); | |
}). | |
then(function (rows) { | |
var jsonDoc = []; | |
rows.forEach(function (row) { | |
var jsonRow, resultRow; | |
jsonRow = JSON.parse(JSON.stringify(row)); // Keep only json valid datas | |
resultRow = {}; | |
Object.keys(jsonRow).filter(function (key) { // Filter metadata names to drop | |
return ['_xml', 'id', 'title', 'content', '_links'].indexOf(key) === -1; | |
}).forEach(function (key) { // Keep only real datas | |
resultRow[key] = jsonRow[key]; | |
}); | |
jsonDoc.push(resultRow); | |
}); | |
profess.next(jsonDoc); | |
}). | |
then(function (doc) { | |
console.log('resulting json doc :', JSON.stringify(doc, null, 4)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Build : npm init && npm install google-spreadsheet profess --save
Run : node gdoc-to-json [gdoc_key] [[google_login] [google_pwd]]