Created
March 19, 2024 17:28
-
-
Save NoCtrlZ1110/40fe3d29c084af0acc533ecb2dc4a75d to your computer and use it in GitHub Desktop.
Convert a google sheet to json
This file contains hidden or 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
const toJson = (values = []) => { | |
if (!values || values.length <= 1) return []; | |
const [keys, ...data] = values; | |
const result = data.map( | |
(row) => { | |
const object = {}; | |
keys.forEach((key, index) => object[key] = row[index]); | |
return object; | |
} | |
); | |
return JSON.stringify(result); | |
} | |
const doGet = () => { | |
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
const json = toJson(sheet.getDataRange().getValues()); | |
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment