Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip Iteration Google Apps Script

I have a very simple For Loop in Google Apps Script to check some conditions in a Google Sheet. What I want is to add another condition, if it is met, then I want to skip current iteration and move on to next. This is pretty easy in VBA, but I am not sure how to do it on JavaScript.

Current code:

for (var i=1 ; i<=LR ; i++)
    {
     if (Val4 == "Yes")
      {
       // Skip current iteration...   <-- This is the bit I am not sure how to do
      }
     elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
      {
        // Do something..
       }
      else
      {
       // Do something else...
      }

    }
like image 758
Oday Salim Avatar asked Sep 04 '25 03:09

Oday Salim


1 Answers

continue statement can be used to continue to next itteration :

for (var i=1 ; i<=LR ; i++)
{
  if (Val4 == "Yes")
  {
    continue; // Skip current iteration... 
  }
  // Do something else...
}

In your sample case, leaving the if block empty will achieve the same result:

for (var i=1; i <= LR; i++)
{
  if (Val4 == "Yes")
  {

  }
  elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
  {
    // Do something..
  }
  else
  {
    // Do something else...
  }
}
like image 160
Slai Avatar answered Sep 06 '25 01:09

Slai