如何获取列表框中所选项目的索引? [英] How to get the index of the selected item in a list box?

查看:121
本文介绍了如何获取列表框中所选项目的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

另一个答案doesn因为Google Apps脚本是在Google的服务器上执行的,而不是在您的浏览器中执行的,并且在$ GAS中已经很好地识别了 indexOf()


你应该使用一个包含listBox元素的数组,然后使用 listArray.indexOf(listelement); 你将得到所选的索引

例子:

  //在UI构建中

var ItemListArray = ['item1','item2','item3','item4']; //注意,这个变量定义可以放在函数之外,因此它变成了一个全局变量。 。
for(n = 0,n< ItemlistArray.length; ++ n){
ListBox.addItem(ItemlistArray [n]
}

// in Handler函数

var item = e.parameter.listBoxName
var ItemlistArray = ['item1', 'item2','item3','item4']; //如果ItemlistArray是全局的,可以删除这一行
var index = ItemlistArray.indexOf(item); //例如,如果选择的项目是'item3',索引将是2


I want to get the index of the selected item in a Google Apps Script list box not the selected item itself. All of the examples I've seen so far create a server handler that gets the value of the list box through

var list1Value = e.parameter.list1;

I want to get the index though so I can index into an array. I tried to use this solution http://productforums.google.com/forum/#!category-topic/apps-script/services/vXa57-9T6E4 but my script complained that indexOf wasn't recognized

var currentmonth = months.indexOf(e.parameter.list1);

Anyone have a good idea on how to get the index? By the way this is a google apps script running in Sites not in Spreadsheets if that makes a difference.

解决方案

The other answer doesn't make much sense since Google Apps Script is executed on Google's server, not in your browser and indexOf() is well recognized in GAS.

You should use an array with the elements of you listBox and then using listArray.indexOf(listelement); you'll get the index of the selected item.

example :

//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

这篇关于如何获取列表框中所选项目的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆