我正在谷歌应用程序脚本中为谷歌工作表制作自定义功能,我让用户为该功能选择一个大范围(例如,I1:I23) . 我希望脚本遍历范围中的每个单元格并检查内容以获取特定值 . 这是我的代码 . 它不漂亮,但我可以用这个答案解决它 .
function lowerpos(range,value){
var lowest = 0
var cell = //the top cell in the selected range
for ( i=1; i>= range.length;i++){//go through every cell in range
if( cell.value == value){ //if the value of the cell its on is the same as value
lowest = cell //set the variable lowest to the cell's row and column
}
cell = cell + 1 //go to the next cell
}
return lowest
}
此代码逐个通过范围中的单元格检查它们以查看它们的值是否是用户指定的值 . 我希望它返回带有值的最后一个已检查单元格的行和列 .
1 回答
如果范围只有一列,您可以执行以下操作来清理现有的代码函数:
创建变量 cell = range.getCell(i,1);在for循环中,在if语句之前 .
用cell.getValue()替换cell.value;
将 lowest = cell 替换为 lowest = cell.getValue() ;
包括休息;在你的for循环中,在if语句中, lowest = cell.getValue() ;这将在找到完全匹配后退出循环 .