Last active
February 21, 2019 04:31
-
-
Save dsernst/c63a06888ddd86da7869 to your computer and use it in GitHub Desktop.
getSpreadsheetData() utility function takes the name of a worksheet, then grabs all the data on the page and gives you back an array of objects, with the first row as property names.
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
function getSpreadsheetData(sheetName) { | |
// This function gives you an array of objects modeling a worksheet's tabular data, where the first items — column headers — become the property names. | |
var arrayOfArrays = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName || 'Sheet1').getDataRange().getValues(); | |
var headers = arrayOfArrays.shift(); | |
return arrayOfArrays.map(function (row) { | |
return row.reduce(function (memo, value, index) { | |
if (value) { | |
memo[headers[index]] = value; | |
} | |
return memo; | |
}, {}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment