Converting Values of Google Spreadsheet to Object using Google Apps Script

Gists

This is a sample script for converting the values of Google Spreadsheet to an object using Google Apps Script.

Sample script

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);
}
  • When this script is run, the above sample image can be retrieved.
  • In this sample script, the 1st row of the sheet is used as the header row.

 Share!