Skip to content

Instantly share code, notes, and snippets.

@vanpariyar
Created March 22, 2022 09:04
Show Gist options
  • Select an option

  • Save vanpariyar/a4442ccb19e5810d46eb7821b2843f1b to your computer and use it in GitHub Desktop.

Select an option

Save vanpariyar/a4442ccb19e5810d46eb7821b2843f1b to your computer and use it in GitHub Desktop.
Converting Google Sheets cell values to an object array with heading keys using Google Apps Script
  • Converting Google Sheets cell values to an object array with heading keys using Google Apps Script
  • Google sheet to json object
function myFunction() {
  const sheetName = "Sheet1";
  const [headers, ...rows] = SpreadsheetApp.getActiveSpreadsheet()
    .getSheetByName(sheetName)
    .getDataRange()
    .getValues();
  const res = rows.map((r) =>
    headers.reduce((o, h, j) => Object.assign(o, { [h]: r[j] }), {})
  );
  console.log(res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment