使用谷歌工作表脚本在列中查找空单元格

Jub*_*oss 5 javascript for-loop google-sheets google-apps-script

我试图在谷歌表格的特定列中找到一个空单元格。我熟悉 getLastRow(),但它返回整个工作表中的最后一行。我想获取特定列中的第一个空单元格。所以,我使用了 for 循环和 if。但我不知道为什么它不起作用。问题是 for 循环不返回任何内容。我在工作表测试(第 2 行)中得到了 H 列(位置 8)的 10 行。前 5 行已经有内容。稍后将使用另一个代码将数据添加到此单元格。所以,我希望能够在此列中找到第一个空单元格,以便能够放置新数据。

var sheettest = SpreadsheetApp.getActive().getSheetByName("test");
var columntest = sheettest.getRange(4, 8, 10).getValues();  
    for(var i=0; columntest[i]=="" ; i++){
      if (columntest[i] ==""){ 
        var notationofemptycell = sheettest.getRange(i,8).getA1Notation();
        return notationofemptycell 
      }

  }
Run Code Online (Sandbox Code Playgroud)

正如我所说,我想找到该列中的第一个空单元格。只要单元格不为空,我就定义了 for 循环。那么如果单元格为空,它应该返回该单元格的 A1 表示法。似乎 for 循环确实会继续,直到找到空单元格,因为我在调试中没有得到 var "notationofemptycell" 的定义。

Ed *_*son 1

您需要循环遍历columntest 的长度。尝试这个:

function myFunction() {
  var sheettest = SpreadsheetApp.getActive().getSheetByName("test");
  var lr=sheettest.getLastRow()
  var columntest = sheettest.getRange(4, 8, lr).getValues();  
    for(var i=0; i<columntest.length ; i++){
      if (columntest[i] ==""){ 
        var notationofemptycell = sheettest.getActiveCell().getA1Notation();
        Logger.log( notationofemptycell)
        break;//stop when first blank cell on column H os found.
      }
  }
}
Run Code Online (Sandbox Code Playgroud)