-
-
Save phamhuy361995/e5df1d32958f66a0d313dde819f6f38c to your computer and use it in GitHub Desktop.
Convert a google sheet to json
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
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