Skip to content

Instantly share code, notes, and snippets.

@NoCtrlZ1110
Created March 19, 2024 17:28
Show Gist options
  • Save NoCtrlZ1110/40fe3d29c084af0acc533ecb2dc4a75d to your computer and use it in GitHub Desktop.
Save NoCtrlZ1110/40fe3d29c084af0acc533ecb2dc4a75d to your computer and use it in GitHub Desktop.
Convert a google sheet to json
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