Skip to content

Instantly share code, notes, and snippets.

@cms-jakes
Last active May 27, 2018 15:41
Show Gist options
  • Select an option

  • Save cms-jakes/4d6b96b0c2dea62e4a28db7d5f4765c8 to your computer and use it in GitHub Desktop.

Select an option

Save cms-jakes/4d6b96b0c2dea62e4a28db7d5f4765c8 to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////////////
//The function takes two inputs:
//url = Spreadhsheet URL
//sheetName = name of sheet in Spreadsheet you want to convert to json
//The resulting json objects will be a named based on text in the cells of row 1 with spaces removed
////////////////////////////////////////////////////////////////////////
function getSpreadsheetData(ssURL,sheetName){
var ss = SpreadsheetApp.openByUrl(ssURL);
var sheet = ss.getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var sheetArray = [];
var headers = data[0]
var jsonArray = [];
for(var i=1; i<data.length; i++) {
var line = data[i];
var json = new Object();
for(var j=0; j<headers.length; j++) {
var header = headers[j].replace(/\s/g, '');
json[header] = line[j];
}
jsonArray.push(json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment