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

109
Visualizações
For loop in the column

I'm having trouble with loops in a column. My goal is to start another function if any cell in the column B has a value.

Here my code that I tried to readapt thanks to the previous answers.

  let startRow = 2;
  let lastRow = sheet.getLastRow();
  let numRows = lastRow  - startRow;
  let rng = sheet.getRange(2,2).getValues();
  
  for (var i = 0; i < numRows; i++){
    
   let cell = rng[i];

    if (cell !== '') {     
      
      //otherfunction();      
      
    }
  }

The problem is that the other function doesn't start

What I doing wrong?

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Explanation:

Issue 1:

  • getRange(2,2) specifies only a single cell (B2) but because you use getValues you get a 2D array in the form [[x]].

Therefore when you call rng[i] you get something like [x] which is, again, an array. Therefore, cell !== '' will always be false.

Issue 2:

It seems you want to iterate over column B but you in your code you only consider a single cell, therefore the for loop is not used and in fact rng has only one row (as I explained before).

Solution:

function myFunction() {
  
  let ss = SpreadsheetApp.getActive();
  let sheet = ss.getSheetByName("Sheet1");
  let values = sheet.getRange('B2:B'+sheet.getLastRow()).getValues().flat();
  
  for (var i = 0; i < values.length; i++){
    
   let cell = values[i];

   if (cell !== '') {     
      
      //otherfunction();      
      
    }
  }
}
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