使用可变变量来定义循环中的范围。

huangapple go评论103阅读模式
英文:

Use changing variable to define range in loop

问题

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.

  1. var sourcesheet = ss.getSheetByName("sheet name");
  2. var nameRowN = starting rows already in an array
  3. var sor = number of rows in each array already calculated and put in an array
  4. var ranges = [];
  5. for (var m = 0; m < sourcesheet.length; m++) {
  6. ranges = sourcesheet.getRange(nameRowN[m],5,sor[m],29).getValues();
  7. }

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:

  1. ranges = sourcesheet.getRange(nameRowN[1],5,sor[1],29).getValues();
英文:

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.

  1. var sourcesheet = ss.getSheetByName(&quot;sheet name&quot;);
  2. var nameRowN = starting rows already in an array
  3. var sor = number of rows in each array already calculated and put in an array
  4. var ranges = [];
  5. for (var m = 0; m &lt; sourcesheet.length; m++) {
  6. ranges = sourcesheet.getRange(nameRowN[m],5,sor[m],29).getValues();
  7. }

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:

  1. ranges = sourcesheet.getRange(nameRowN[1],5,sor[1],29).getValues();

答案1

得分: 1

如果您的目标是迭代所有包含内容的行 - 您可以使用方法getLastRow()

示例:

  1. var lastRow = sourcesheet.getLastRow();
  2. for (var m = 0; m < lastRow; m++) {
  3. // 执行您想要的操作
  4. }

另一种可能性是与getValues()length一起使用getDataRange()

示例:

  1. var rangeLength = spreadsheet.getDataRange().getValues().length;
  2. for (var m = 0; m < rangeLength; m++) {
  3. // 执行您想要的操作
  4. }
英文:

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

Sample:

  1. var lastRow=sourcesheet.getLastRow();
  2. for (var m = 0; m &lt; lastRow; m++) {
  3. //do what you want
  4. }

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

Sample:

  1. var rangeLength=spreadsheet.getDataRange().getValues().length;
  2. for (var m = 0; m &lt; rangeLength; m++) {
  3. //do what you want
  4. }

huangapple
  • 本文由 发表于 2020年1月6日 22:34:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定