I need to return several columns from an array pulled out of Google Sheets. Currently my code is
let out = data.map(function (row, index) {
return [row[1], row[0], row[36], row[6], row[45], row[46], row[47], row[7]]});
This code works well. But the needs have changed such that the report will have different columns each week. It will always have the columns I need, but the position of those columns will be changing. I can get the column numbers with
const colHeaders = ["Full Name","EID","Email - Work","Management Level"];
...
let sheetHeaders = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues().flat();
let out = [];
for (let i in colHeaders){
out.push(sheetHeaders.indexOf(colHeaders[i]));
}
but I don’t know how to then use the list of header positions to return the full columns.
2
Answers
You can map the array of column numbers to the values in those columns.
ActiveSheet Example: