如何在列表框中获取所选项的索引?

时间:2012-08-03 22:23:08

标签: 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);

任何人都对如何获取索引有个好主意?顺便说一下,这是一个谷歌应用程序脚本运行在不在电子表格中的网站,如果这有所作为。

1 个答案:

答案 0 :(得分:1)

另一个答案没有多大意义,因为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