Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
How do I extract the unique values of a column to use them as a parameter in a function?

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  
  })
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!