Skip to content

Instantly share code, notes, and snippets.

@dsernst
Last active February 21, 2019 04:31
Show Gist options
  • Save dsernst/c63a06888ddd86da7869 to your computer and use it in GitHub Desktop.
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.
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