Last active
June 10, 2016 14:09
-
-
Save Max-Makhrov/2b2aaf45662452fbd5a5a7db5f5b828d to your computer and use it in GitHub Desktop.
trying to export Json data from Google Sheets
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
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> | |
<script> | |
$(document).ready(function() { | |
getSheetData(); | |
}); | |
function getSheetData() { | |
var spreadsheetID = '1i_eJ1XyQXATMCanAZl55z_WAs_Lj0rad-lUnK7jhxz8'; | |
var worksheetID = 'od6'; | |
var url = 'https://spreadsheets.google.com/feeds/list/'; | |
url += spreadsheetID; | |
url += '/'; | |
url += worksheetID; | |
url += '/public/values?alt=json'; | |
var counter = 0; | |
$.getJSON(url, function(data) { | |
var myImportedData = []; | |
var items = []; | |
var item = []; | |
$.each(data.feed.entry, function(key, val) { | |
// row by row | |
//alert(val.gsx$city1.$t); //"gsx$city1":{"$t":"Pittsburg"} | |
var newRow = []; | |
var headers = []; | |
counter++; | |
$.each(val, function(index, value) { | |
if (index.indexOf('gsx') > -1) { | |
if (counter == 1) { | |
headers.push(index.replace('gsx$', '')); | |
} | |
$.each(value, function(index, value) { | |
newRow.push(value); | |
}); | |
} | |
}); | |
// headers | |
if (counter == 1) { | |
myImportedData.push(headers); | |
} | |
myImportedData.push(newRow); | |
}); | |
makeTable($('#myTest'), myImportedData); | |
}); | |
} | |
// makeTable | |
// found here: | |
// http://www.htmlgoodies.com/beyond/css/working_w_tables_using_jquery.html | |
function makeTable(container, data) { | |
var table = $("<table/>").addClass('table table-striped'); | |
$.each(data, function(rowIndex, r) { | |
var row = $("<tr/>"); | |
$.each(r, function(colIndex, c) { | |
row.append($("<t" + (rowIndex == 0 ? "h" : "d") + "/>").text(c)); | |
}); | |
table.append(row); | |
}); | |
return container.append(table); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment