我想获取 Google Apps 脚本列表框中所选项目的索引,而不是所选项目本身。到目前为止我看到的所有示例都创建了一个服务器处理程序,它通过
获取列表框的值var list1Value = e.parameter.list1;
不过我想获取索引,这样我就可以索引到一个数组中。我尝试使用此解决方案 http://productforums.google.com/forum/#!category-topic/apps-script/services/vXa57-9T6E4 但是我的脚本提示 indexOf 没有被识别
var currentmonth = months.indexOf(e.parameter.list1);
有人知道如何获取索引吗?顺便说一句,如果有区别的话,这是一个在网站而不是电子表格中运行的谷歌应用程序脚本。
最佳答案
另一个答案没有多大意义,因为 Google Apps 脚本是在 Google 的服务器上执行的,而不是在您的浏览器中执行的,并且 indexOf()
在 GAS 中得到了很好的识别。
您应该使用一个包含 listBox 元素的数组,然后使用 listArray.indexOf(listelement);
您将获得所选项目的索引。
例子:
//in the UI construction
var ItemlistArray = ['item1','item2','item3','item4'];// note that this variable definition could be placed outside of the function so it becomes a global variable...
for (n=0,n<ItemlistArray.length;++n){
ListBox.addItem(ItemlistArray[n]
}
//in the Handler function
var item = e.parameter.listBoxName
var ItemlistArray = ['item1','item2','item3','item4'];// if ItemlistArray is global this line can be removed
var index = ItemlistArray.indexOf(item); // if for example the selected item was 'item3', index will be 2
关于javascript - 如何获取列表框中所选项目的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11804081/