Created
April 23, 2021 17:48
-
-
Save joshm21/30cfe1890534b8634b7c88396e5919eb to your computer and use it in GitHub Desktop.
Google Apps Script - Sheet to Array of Objects
This file contains 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 sheetToObjects = (sheet, headerRow=1) => { | |
const arrayToObject = (array,headers) => { | |
return array.reduce((obj,value,index) =>{ | |
return {...obj,[headers[index]]: value} | |
}, {}) | |
} | |
const sheetValues = sheet.getDataRange().getValues() | |
const dataValues = sheetValues.slice(headerRow-1) | |
const headers = dataValues.shift() | |
return dataValues.map(row => arrayToObject(row,headers)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment