I am trying to pass to a function one parameter, coming from an array like:
const array=[1,2,2,3]
I do not want to introduce manually every time my list of parameters, so I am fetching the data I want from a spreadsheet, getting the unique values of the column. These would be my desired array.
However, I get this error in function generate_Form_links(af_col):
TypeError: Cannot read property 'forEach' of undefined
Here I get the column I want:
function getByName(colName) {
const data=SpreadsheetApp.openById('1jogNOhdamVMXAlGyYOn9RXA2vNbrzNNJBUGhbL-EhTU').getSheets()[0]
const [hA, ...vs]=data.getRange('E1:E13').getValues();
let idx={};
hA.forEach((h,i) => {idx[h]=i;});
let oA=[];
oA=vs.map(r=>r[idx[colName]]);
Logger.log(oA.join('\n'))
}
var af_col=getByName('AF')
what could be wrong??
My function where I pass the parameter is:
function generate_Form_links(af_col){
af_col.forEach(function(e, index){
console.log(e, index)
createForm(e) //other function that create forms for each element of the given array
})
}
Perhaps this simple example will be of some value.
Sheet0:
| COL1 | COL2 | COL3 | COL4 | COL5 |
|---|---|---|---|---|
| 1,1 | 1,2 | 1,3 | 1,4 | 1,5 |
| 2,1 | 2,2 | 2,3 | 2,4 | 2,5 |
| 3,1 | 3,2 | 3,3 | 3,4 | 3,5 |
| 4,1 | 4,2 | 4,3 | 4,4 | 4,5 |
The Code to extract all values in COL3
function getByName(colName) {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const [hA, ...vs] = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getValues();
let idx = {};
hA.forEach((h, i) => { idx[h] = i; });
let oA = [];//output array
oA = vs.map(r => r[idx['COL3']]);
Logger.log(oA.join('\n'))
}
Execution log
4:50:57 PM Notice Execution started
4:50:58 PM Info 1,3
2,3
3,3
4,3
4:50:58 PM Notice Execution completed