Skip to content

Instantly share code, notes, and snippets.

@Zakarialabib
Created May 15, 2023 12:06
Show Gist options
  • Save Zakarialabib/4db3cf219e25506fe5a309177d746c0e to your computer and use it in GitHub Desktop.
Save Zakarialabib/4db3cf219e25506fe5a309177d746c0e to your computer and use it in GitHub Desktop.
Export json google sheet
function doGet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var range = sheet.getDataRange();
var values = range.getValues();
var headers = values[0];
var data = values.slice(1);
var json = {};
for (var col = 0; col < headers.length; col++) {
var header = headers[col];
json[header] = [];
for (var row = 0; row < data.length; row++) {
if (data[row][col] != "") {
json[header].push(data[row][col]);
}
}
}
var output = ContentService.createTextOutput(JSON.stringify(json));
output.setMimeType(ContentService.MimeType.JSON);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment