Skip to content

Instantly share code, notes, and snippets.

@thisbit
Created July 15, 2023 07:51
Show Gist options
  • Save thisbit/8027e4167d82efc1ddfc84713da7db64 to your computer and use it in GitHub Desktop.
Save thisbit/8027e4167d82efc1ddfc84713da7db64 to your computer and use it in GitHub Desktop.
Google Sheets as JSON, use this with Extensions > App Script in google sheets
function getSheetDataAsJSON() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var headers = values[0];
var jsonData = [];
for (var i = 1; i < values.length; i++) {
var row = values[i];
var rowObj = {};
for (var j = 0; j < headers.length; j++) {
rowObj[headers[j]] = row[j];
}
jsonData.push(rowObj);
}
return JSON.stringify(jsonData);
}
function doGet() {
return ContentService.createTextOutput(getSheetDataAsJSON())
.setMimeType(ContentService.MimeType.JSON);
}
@thisbit
Copy link
Author

thisbit commented Jul 15, 2023

WARNING this snippet was written by ChatGPT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment