Skip to content

Instantly share code, notes, and snippets.

@deepakness
Created July 19, 2023 14:56
Show Gist options
  • Save deepakness/067d58881a290872006c8711115267b9 to your computer and use it in GitHub Desktop.
Save deepakness/067d58881a290872006c8711115267b9 to your computer and use it in GitHub Desktop.
Apps Script to deploy a Google Sheets file as a JSON web app.
// Create JSON URL
function doGet(req) {
var values = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange().getValues();
var output = [];
for (var i = 0; i < values.length; i++) {
var row = {};
if (i == 0) { // skip the header row
continue;
}
for (var j = 0; j < values[0].length; j++) {
if (!values[i][j] || values[i][j] === '') { // skip if cell is undefined or empty
continue;
}
row[values[0][j]] = values[i][j]; // use the value from the header row as the property name
}
// Only add the row to the output if it has data
if(Object.keys(row).length > 0){
output.push(row);
}
}
return ContentService.createTextOutput(JSON.stringify({ data: output })).setMimeType(ContentService.MimeType.JSON);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment