在Google Apps Script中,可以使用以下代码示例在数组中搜索值:
function searchValueInArray() {
var searchValue = "apple";
var array = ["banana", "apple", "orange", "grape"];
var index = array.indexOf(searchValue);
if (index !== -1) {
Logger.log("Value found at index " + index);
} else {
Logger.log("Value not found");
}
}
在上面的示例中,我们定义了一个searchValue
变量来存储要搜索的值。然后,我们定义了一个array
数组,其中包含一些字符串值。
接下来,我们使用indexOf()
方法在数组中搜索searchValue
的索引。如果找到了值,则indexOf()
方法将返回值的索引,否则返回-1。
最后,我们使用Logger.log()
函数将结果记录在日志中。您可以在Google Apps Script编辑器的“查看”菜单中打开日志来查看结果。
您可以根据需要修改搜索值和数组来适应您的情况。