Skip to content

Instantly share code, notes, and snippets.

@joshm21
Created April 23, 2021 17:48
Show Gist options
  • Save joshm21/30cfe1890534b8634b7c88396e5919eb to your computer and use it in GitHub Desktop.
Save joshm21/30cfe1890534b8634b7c88396e5919eb to your computer and use it in GitHub Desktop.
Google Apps Script - Sheet to Array of Objects
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