我试图解决在GAS中创建嵌套列表框的不可能性。我创建了一些数组来填充列表框,并使用for循环将它们连接到各自的列表框。
数组
var TicketTypeArray=["TICKETTYPE1","TICKETTYPE2","TICKETTYPE3","TICKETTYPE4","TICKETTYPE5","TICKETTYPE6","TICKETTYPE7","TICKETTYPE8","TICKETTYPE9","TICKETTYPE10","TICKETTYPE11","TICKETTYPE12","TICKETTYPE13","TICKETTYPE14"];
var DemandedByArray=["DEMANDEDBY1","DEMANDEDBY1"];
var AnalystArray=["ANALYST1","ANALYST2","ANALYST3","ANALYST4","ANALYST5","ANALYST6","ANALYST7","ANALYST8","ANALYST9"];
var StatusType1Array=["STATUS1","STATUS2","STATUS3"];
var StatusType2Array=["STATUS1","STATUS2","STATUS3"];
var StatusType3Array=["STATUS1","STATUS2","STATUS3"];
我尝试使用if else循环将下一个列表框嵌套到另一个列表框中:
if (TicketTypeListBox="TICKETTYPE1")
{
for(var i=0; i<StatusType1Array.length; i++)
{
StatusListBox.addItem(appRegistro.createLabel(StatusType1Array[i])).setItemText(i, StatusType1Array[i]);
}
}
else if (TicketTypeListBox="TICKETTYPE2")
{
for(var i=0; i<StatusType2Array.length; i++)
{
StatusListBox.addItem(appRegistro.createLabel(StatusType2Array[i])).setItemText(i, StatusType2Array[i]);
}
}
else
{
StatusListBox.addItem("Teste");
}
TicketTypeListBox是:
var TicketTypeListBox = appRegistro.createListBox().setId('TicketType').setName('TicketType');
for(var i=0; i<TicketTypeArray.length; i++)
{
TicketTypeListBox.addItem(appRegistro.createLabel(TicketTypeArray[i])).setItemText(i, TicketTypeArray[i]);
}
要显示面板,请使用以下代码:
panel.add(DataLabel);
panel.add(DataTextBox);
panel.add(TicketIDLabel);
panel.add(TicketIDTextBox);
panel.add(TicketTypeLabel);
panel.add(TicketTypeListBox);
panel.add(DemandedByLabel);
panel.add(DemandedByListBox);
panel.add(AnalystLabel);
panel.add(AnalystListBox);
panel.add(StatusLabel);
panel.add(StatusListBox);
appRegistro.add(panel);
return appRegistro
现在,当我在Google协作平台中运行脚本时,我收到错误消息“无法找到方法添加(字符串)”。在进行debuging时,它会将错误定位在TicketTypeListBox的行中。
panel.add(TicketTypeListBox);
我该怎么办?
答案 0 :(得分:1)
此错误即将发生,因为在if
块中,您使用了赋值运算符而不是相等比较器,它将字符串分配给列表框。
if (TicketTypeListBox = "ACCESS CONTROL")
使用
if (TicketTypeListBox == "ACCESS CONTROL")
同样修改其他地方。
我非常确定它仍然无效,因为无法将列表框与字符串进行比较。要创建嵌套列表框,您必须在lisbox上使用更改处理程序。这是一个相同的快速示例。
var lbArray1 = ['item1', 'item2', 'item3'];
var item1Array = ['item11', 'item12', 'item13'];
var item2Array = ['item21', 'item22', 'item23'];
var item3Array = ['item31', 'item32', 'item33', 'item34'];
function doGet(e){
var app = UiApp.createApplication();
var vPanel = app.createVerticalPanel();
app.add(vPanel);
var lb1 = app.createListBox().setName('lb1').setId('lb1');
for(var i in lbArray1){lb1.addItem(lbArray1[i])};
var lb2 = app.createListBox().setName('lb2').setId('lb2');
//load the 2nd lb with items dependent on first one
for(var i in item1Array){lb2.addItem(item1Array[i])};
vPanel.add(lb1).add(lb2);
//use change handler to dynamically change the 2nd listbox based on the selection of first listbox
var changeHandler1 = app.createServerHandler('updateListBox_').addCallbackElement(vPanel);
lb1.addChangeHandler(changeHandler1);
return app;
}
function updateListBox_(e){
var app = UiApp.getActiveApplication();
//first listbox item selected
var lb1Item = e.parameter.lb1;
var lb2 = app.getElementById('lb2');
lb2.clear();//remove all items from lb2
if(lb1Item == 'item1'){
for(var i in item1Array){lb2.addItem(item1Array[i])};
}
else if(lb1Item == 'item2'){
for(var i in item2Array){lb2.addItem(item2Array[i])};
}
else if(lb1Item == 'item3'){
for(var i in item3Array){lb2.addItem(item3Array[i])};
}
return app;
}