-
-
Save nguyentu9/857ec859ba1690bfc5e8ebbb08ce792f 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