Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda