簡體   English   中英

如何在列表框中獲取所選項的索引?

[英]How to get the index of the selected item in a list box?

我想在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM