我正在尝试在Google表格的特定列中找到一个空单元格。我熟悉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
}
}
正如我所说,我想在该列中找到第一个空单元格。我定义了for循环,只要单元格不为空即可。那么如果该单元格为空,则应返回该单元格的A1表示法。似乎for循环一直进行到找到空单元格为止,因为在调试中我没有得到var“ notationofemptycell”的定义。
您需要遍历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.
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句