Create a gist now

Instantly share code, notes, and snippets.

@oodavid /index.html
Last active Dec 18, 2015

Convert Google Spreadsheeets to JSON
<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title>Google Spreadsheet to JSON...</title>
<script src=""></script>
<script src=""></script>
// Which file to load?
var csv = '';
// How many rows to skip (if you have anything ABOVE the column definitions)
var skip = 2;
// Map your spreadsheet columns to JSON keys
var column_keys = [
{ 'column': 'Date', 'key': 'date' },
{ 'column': 'Meal', 'key': 'meal' },
{ 'column': 'Notes', 'key': 'comments' }
// When your ready, load the CSV...
$.get(csv, function(data){
// Format the data into arrays
data = $.csv.toArrays(data);
// Skip columns
for(var s=0; s<skip; s++){
// We translate the columns into json keys
// The remote columns must match exactly to make sure that the structure is exactly as we expect it to be!
for(var t=0, tl=column_keys.length; t<tl; t++){
if(column_keys[t]['column'] != data[0][t]){
alert('== Error - Spreadheet Titles ==\nLOCAL "' + column_keys[t]['column'] + '" != REMOTE "' + data[0][t] + '"');
return false;
// Splendid, remove the remote titles
// Loop the data...
var rows = [];
for(var d=0, dl=data.length; d<dl; d++){
var datum = data[d];
// If we hit the "DO NOT IMPORT" columns we're done...
if(datum[0] == "DO NOT IMPORT" || !datum[0] || datum[0] == ""){
// Format it into a structured object
var row = {};
for(var t=0, tl=column_keys.length; t<tl; t++){
row[column_keys[t]['key']] = datum[t];
// Set the row
// And output
$('textarea').text('var rows = ' + JSON.stringify(rows, null, '\t') + ';');
body {font-family: sans-serif; text-align: center;}
textarea {width: 600px; height: 400px; display: block; margin: 10px auto; white-space: pre; word-wrap: normal; overflow-x: scroll;}
<h1>Google Spreadsheet to JSON...</h1>
<p>Turns <a href="" target="_blank">this spreadsheet</a>... into this JSON object...</p>
<p>See this <a href=""> article</a> for more info</p>

This comment has been minimized.

Show comment
Hide comment

hueitan Mar 4, 2015

This doesn't work for me =(

hueitan commented Mar 4, 2015

This doesn't work for me =(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment