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

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

问题描述:

我想所选项目的索引在谷歌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); 

人对如何获得该指数是一个好主意?顺便说一句,这是一个谷歌应用程序脚本运行在网站不在电子表格,如果这有所作为。

+0

什么是'months'在你的脚本? – Bergi 2012-08-04 13:38:25

+0

我有一个电子表格,我正在抽出几个月时间。这只是标准的一月,二月 – Mathitis2Software 2012-08-04 14:48:01

+0

'VAR phoneBillSpreadsheet = SpreadsheetApp.openById(SPREADSHEET_ID); VAR片= phoneBillSpreadsheet.getSheets(); months = sheets [0] .getRange(1,2,1,11).getValues();' phoneBillData = sheets [0] .getRange(3,2,6,11).getValues();' – Mathitis2Software 2012-08-04 14:53:12

对方回答,因为谷歌Apps Script是谷歌的服务器上执行,而不是在你的浏览器和indexOf()在气井的认可并没有太大的意义。

您应该使用数组使用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 
+0

请您详细说明UI构建部分?您不使用“选择单元格”→验证→条件→项目列表构建项目列表。 – Jonny 2014-07-17 09:31:24

+0

对不起,我没有访问我的电脑......我在霍利迪的,但很快回来:) – 2014-07-18 12:19:40