Created
August 5, 2013 11:40
-
-
Save Rich-Harris/6155289 to your computer and use it in GitHub Desktop.
Google Spreadsheets parser
This file contains 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
(function ( global ) { | |
'use strict'; | |
var GoogleSpreadsheetsParser; | |
GoogleSpreadsheetsParser = function ( data ) { | |
this.data( data ); | |
}; | |
GoogleSpreadsheetsParser.prototype = { | |
data: function ( data ) { | |
if ( !arguments.length ) { | |
return this._data; | |
} | |
this._data = data; | |
this._csvDirty = this._arrayDirty = this._jsonDirty = true; | |
return this; | |
}, | |
array: function () { | |
var data, entry, rows, i, cell, cellRef, colId, rowId, cellContent; | |
if ( this._arrayDirty ) { | |
data = this._data; | |
if ( !data || !data.feed || !data.feed.entry ) { | |
return []; | |
} | |
entry = data.feed.entry; | |
rows = []; | |
i = entry.length; | |
while ( i-- ) { | |
cell = entry[i]; | |
cellRef = cell.title.$t; | |
colId = cellRef.substr( 0,1 ).charCodeAt( 0 ) - 65; | |
rowId = cellRef.substring( 1 ) - 1; | |
if ( !rows[ rowId ] ) { | |
rows[ rowId ] = []; | |
} | |
if ( !rows[ rowId ][ colId ] ) { | |
cellContent = cell.content.$t; | |
try { | |
rows[ rowId ][ colId ] = JSON.parse( cellContent ); | |
} catch ( err ) { | |
rows[ rowId ][ colId ] = cellContent; | |
} | |
} | |
} | |
this._array = rows; | |
this._arrayDirty = false; | |
} | |
return this._array; | |
}, | |
json: function () { | |
var rows, headers, mapped; | |
if ( this._jsonDirty ) { | |
rows = this.array(); | |
headers = rows.shift(); | |
this._json = rows.map( function ( row ) { | |
var record = {}, i; | |
i = row.length; | |
while ( i-- ) { | |
if ( i in row ) { | |
record[ headers[i] ] = row[i]; | |
} | |
} | |
return record; | |
}); | |
this._jsonDirty = false; | |
} | |
return this._json; | |
}, | |
csv: function () { | |
var array, mapped, stringified; | |
if ( this._csvDirty ) { | |
// TODO accommodate quoted data, alternative delimiters etc. The JSON.stringify | |
// thing is just a quick hack. Doesn't work with holey arrays etc | |
array = this.array(); | |
mapped = array.map( function ( row ) { | |
stringified = JSON.stringify( row ); | |
return stringified.substr( 1, stringified.length - 2 ); | |
}); | |
this._csv = mapped.join( '\n' ); | |
this._csvDirty = false; | |
} | |
return this._csv; | |
} | |
}; | |
// export as CommonJS module... | |
if ( typeof module !== 'undefined' && module.exports ) { | |
module.exports = GoogleSpreadsheetsParser; | |
} | |
// ... or as AMD module... | |
else if ( typeof define !== 'undefined' && define.amd ) { | |
define( function () { return GoogleSpreadsheetsParser; }); | |
} | |
// ... or as browser global | |
else { | |
global.GoogleSpreadsheetsParser = GoogleSpreadsheetsParser; | |
} | |
}( this )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment