Created
July 15, 2023 07:51
-
-
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
This file contains 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
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WARNING this snippet was written by ChatGPT