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

108
Views
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 answers
Answer question

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 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!