0

I would like to extract multiple ranges within a range on a spreadsheet. The number of rows always changing. This is what I'm aiming to do. The start row and the number of rows will always be different.

var sourcesheet = ss.getSheetByName("sheet name");
var nameRowN = starting rows already in an array
var sor = number of rows in each array already calculated and put in an array

 var ranges = [];
   for (var m = 0; m < sourcesheet.length; m++) {
   ranges = sourcesheet.getRange(nameRowN[m],5,sor[m],29).getValues();
   }

So ranges should include multiple mini arrays. The for loop goes through m, then m+1 and so on. Am I on the right track? Is that possible to achieve what I'm looking for this way?

Edit: I can get the range outside of the loop and replace m variable:

ranges = sourcesheet.getRange(nameRowN[1],5,sor[1],29).getValues();
2
  • 1
    Sheet Class has no length property. Commented Jan 6, 2020 at 18:29
  • I had no idea, thank you! I'm quite a beginner with scripts, trying to teach myself while working. Can I use a range instead? Commented Jan 7, 2020 at 8:03

1 Answer 1

1

If your goal is to iterate through all rows with contents - you can use the method getLastRow().

Sample:

var lastRow=sourcesheet.getLastRow();
for (var m = 0; m < lastRow; m++) {
  //do what you want
}

Another possibility would be to use getDataRange() in conjunction with getValues() and length.

Sample:

var rangeLength=spreadsheet.getDataRange().getValues().length;
for (var m = 0; m < rangeLength; m++) {
  //do what you want
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The second method you mentioned was working just as I needed it to. Now it seems so simple yet I couldn't figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.