Convert range to associative array
Thu Feb 03 2022 17:04:19 GMT+0000 (Coordinated Universal Time)
Saved by
@MakiProductions
/*
Range to Associative Array
PASS: Range with headers in the first row
RETURNS: Associantive array using header row as property names
Skips rows with nothing in the date column so we don't import the totals
*/
function rangeToAssocArray(rng) {
var result = [{}];
// var sheet = SpreadsheetApp.getActiveSheet();
// var rng = sheet.getRange(1, 1, sheet.getLastRow(), 8);
var rngData = rng.getValues();
if (rngData.length > 1) {
for (let r = 1; r < rngData.length; r++) {
if (rngData[r][0] != "") {
result.push({});
for (let c = 0; c < rngData[0].length; c++) {
result[r][rngData[0][c]] = rngData[r][c];
}
}
}
result.shift();
} else {
//result = null;
}
return result;
}
content_copyCOPY
This was lifted from a project, so it might not be generalized yet
Comments