Skip to content

Instantly share code, notes, and snippets.

@phamhuy361995
Forked from NoCtrlZ1110/sheet2json.gs
Created June 3, 2024 08:33
Show Gist options
  • Save phamhuy361995/e5df1d32958f66a0d313dde819f6f38c to your computer and use it in GitHub Desktop.
Save phamhuy361995/e5df1d32958f66a0d313dde819f6f38c 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