mrmeyer725
mrmeyer725

Reputation: 1

My Apps Script loop is not looking at more rows than the first

I have a general spreadsheet in which Column B has a number 1-9. I want to copy each row item to the corresponding sheet I have created, sheet 1-9. However, I set my loop to begin at row 5 and continue the length of all rows in the data range but the loop does not continue past row 5 even though more rows have a "1" in column B. Can anyone help to see why my loop is not continuing? Thanks!

function GoodFunction() {

var spreadsheet = SpreadsheetApp.getActive()
var R0sheet = spreadsheet.getSheetByName('S14Allocation')
var R1sheet = spreadsheet.getSheetByName('R1-Allocations')

var ss = spreadsheet;
var s = R0sheet
var targetSheet = R1sheet
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
var r = s.getDataRange();     
var numColumns = s.getLastColumn();
var numRows=s.getLastRow();
for (var i = 5; i <= numRows.length ; i++); {
  if (s.getDataRange().getCell(i,2).getValue() === 1) {
    s.getRange(i,1,1,numColumns).copyTo(target);
  }
}

}

Upvotes: 0

Views: 59

Answers (1)

Fred
Fred

Reputation: 1111

numRows is already a number (from s.getLastRow();) so use i < numRows as the condition in your for loop.

As Serge said in his comment though, the code is very confusing. There's no need for that many variables, and there are more efficient ways of dealing with multiple cells at once.

https://developers.google.com/apps-script/best_practices#batchOperations

Upvotes: 1

Related Questions