Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda