Sergei I.
Sergei I.

Reputation: 3

For loop with an if else statement Google-apps-script

I have sheet. In column A I have simbol ☑ in some cells. I need that active cell moves to cell after last cell with ☑ simbol. Now code doesn't work as I want. Please help me to change code. Thanks.

Here is my code

 function LastCellInCollA() {
  var sheet = SpreadsheetApp.getActiveSheet()
  var cell = sheet.getRange(2, 1, 1, 1);
  var i
  sheet.setCurrentCell(cell);  
  for (i = 2; i < 146; i++)
    var valuei = sheet.getRange(i, 1, 1, 1).getValues();
  {
    if (valuei = "☑️") 
    {  
      sheet.setCurrentCell(sheet.getRange(i, 1, 1, 1));
      i = i + 1;
        }
  }
}

Upvotes: 0

Views: 581

Answers (1)

ADW
ADW

Reputation: 4247

Is that checkmark an ASCII symbol or are you using the Google Sheet checkbox?

If you are using the Google Sheet checkbox, then the following script may work for you.

The recommended way is to not hit the sheet in each loop. So the script does the math and goes directly to the relevant cell instead of going row-by-row.

function LastCellInCollA() {
  var sheet = SpreadsheetApp.getActiveSheet()
  var data = sheet.getRange('A2:A').getValues();
  var d = 0;
  while (data[d][0] == true) { d++; } // If you are using ASCII symbol, replace 'true' with the symbol
  var lastRow = d + 2;
  var cell = sheet.getRange(lastRow, 1);
  sheet.setCurrentCell(cell);
}

Upvotes: 1

Related Questions