Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayatbiralem/f98cd260941a882076c0b4d20849eed5 to your computer and use it in GitHub Desktop.
Save hayatbiralem/f98cd260941a882076c0b4d20849eed5 to your computer and use it in GitHub Desktop.
Parse published (public) Google Spreadsheet JSON data into Javascript array
// Thanks to this gist: [Sample HTML/JS to parse a Google Spreadsheet](https://gist.github.com/terrywbrady/a03b25fe42959b304b1e)
var id = '1g3471DBNQEg0v_ByMBx1gNDx5cmlN5Ikn3f1DgJtQY8';
var jsonEndPoint = 'https://spreadsheets.google.com/feeds/cells/'+id+'/1/public/values?alt=json';
var parseGoogleSpreadsheetJsonIntoArray = function (response){
// Check the markup hasn't changed
if(response.feed && response.feed.entry && response.feed.entry[0] && response.feed.entry[0].gs$cell) {
var headings = [];
var data = [];
response.feed.entry.forEach(function(item){
var cell = item.gs$cell;
if(cell.row === "1") {
// First row
headings.push(cell.$t);
} else {
var colIndex = parseInt(cell.col) - 1;
var rowIndex = parseInt(cell.row) - 1;
if(!Array.isArray(data[rowIndex])) {
data[rowIndex] = [];
}
data[rowIndex][ headings[colIndex] ] = cell.$t;
}
});
// console.log(headings);
console.log(data);
}
return data;
};
(function(){
$(document).ready(function(){
$.ajax( jsonEndPoint )
.done(function(response) {
console.log(response);
var data = parseGoogleSpreadsheetJsonIntoArray(response);
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment