计算两个特定行之间的行

时间:2015-07-24 05:47:37

标签: google-apps-script google-sheets

我试图计算工作表上两个特定行之间的行数。这是它的样子:

500
501
5011
502
600

所以在这种情况下,在500和600之间有3行。我想这样做的原因是,如果在" 500"之间添加或删除了行。和" 600",我不需要更新脚本。

对此有任何建议。

1 个答案:

答案 0 :(得分:0)

If there will never be another "500" or "600" between the first and the last number you could use something like this.

function myFunction() {
 var ss = SpreadsheetApp.getActive();
 var sheet = ss.getSheetByName("Sheet1");
 var data = sheet.getRange(1,1,sheet.getLastRow()).getValues();
 for (row in data)
 {
   //To no include the row with the 500 you need to add +1
   if (data[row] == 500) var first = row+1
    if (data[row] == 600) var last = row
 }
 var distance = last - first
}

Final answer in var distance

If there might be other "500" or "600" but the "600" will always be the last row you can simply replace the

 if (data[row] == 600) var last = row

by

 var last = sheet.getLastRow()

Hope this helps If unclear ask question and I will reply in the comments

相关问题