javascript - 如何获取列表框中所选项目的索引?

标签 javascript google-apps-script google-sites

我想获取 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/

相关文章:

themes - 如何创建自定义的 Google 协作平台主题?

Javascript 类作为对象的子对象

javascript - 如何在 Material UI 中制作复选框 "Badge"?

javascript - ReactJS Component.render() 函数的适当返回类型是什么?

google-apps-script - 如何使用脚本在 Google 表格中生成范围 ID 超链接 #rangeID=XXXXXXXXXX?

javascript - 如何将文档中特定单词的首字母大写?

xml - 有没有办法在谷歌网站上呈现 XML 数据

javascript - dotVVM 将值从数据绑定(bind)的 DOM 元素传递到 JavaScript 变量以进行可视化

google-apps-script - 控制哪个用户在谷歌表格中运行应用程序脚本

html - 使用嵌入代码时,新谷歌网站上的额外滚动条