当我选择另一个列表框时填充列表框

时间:2014-09-10 14:24:34

标签: google-apps-script

对不起我的代码,我在本例中尝试简化我的解释。 列表框的信息取决于我在其他listBox中选择,但它无法工作。 抱歉我的英语不好。

这里我创建了我的表单;

function doGet(){
  var app = UiApp.createApplication().setTitle('Request Form');
  var flow = app.createFlowPanel().setStyleAttribute("textAlign", "center").setWidth("900px");

      var lreason = app.createLabel('Select reason :');
      var listreason= app.createListBox().setName("listreason").addItem("").addItem("SALUD").addItem("MOTIVO PERSONAL");
      var lfactor = app.createLabel('Select Factor :');
      var listfactor= app.createListBox().setName("listfactor").addItem("Select reason first").setId(listfactor);


  var handlerr = app.createServerValueChangeHandler('factors');
  handlerr.addCallbackElement(flow);
  listreason.addChangeHandler(handlerr);   
  var test1 = app.createLabel('nada'); 

  var gridd = app.createGrid(4,5);

      gridd.setWidget(2,0,lreason)
      .setWidget(2,1,listreason)
      .setWidget(2,3,lfactor)
      .setWidget(2,4,listfactor)
      .setWidget(3,0,test1.setId('test1'));

    flow.add(gridd);
  app.add(flow);  
  return app;  
}

//////////////////这里创建填充其他listBox的函数

function factors(e){
var app=UiApp.getActiveApplication(); 

var var1=e.parameter.listreason;
app.getElementById('test1').setText(var1);  

switch (var1) {

    case "SALUD":

var ss = SpreadsheetApp.openById('1pBayt_tB84E3wKhb-H5tVZJolx04F-Uq-Iom8O3KVHs');
var list = ss.getSheetByName('Salud');

 var lastRow = list.getLastRow();

var templ = app.createListBox();  
var values = list.getRange(2,1,lastRow-1,1).getValues();
for (var i in values){
templ.addItem(values[i][0].toString());
}
app.getElementById('listfactor').addItem(templ);

  break;

    case "MOTIVO ESCOLAR":

var ss = SpreadsheetApp.openById('1pBayt_tB84E3wKhb-H5tVZJolx04F-Uq-Iom8O3KVHs');
var list = ss.getSheetByName('MotivoEscolar');

 var lastRow = list.getLastRow();
var templ = app.createListBox();  

var values = list.getRange(2,1,lastRow-1,1).getValues();
for (var i in values){
templ.addItem(values[i][0].toString());
}
app.getElementById('listfactor').addItem(templ);
  break;

    default: 

app.getElementById('test1').setText("empty");  

    break

}
    return app; 
}

1 个答案:

答案 0 :(得分:0)

您使用的此代码有误(请参阅代码中的注释):

for (var i in values){
templ.addItem(values[i][0].toString());
}
app.getElementById('listfactor').addItem(templ);// you can not add a litBox as an item in another listBox

你应该在循环中简单地添加一个项目...移动你的代码如下:

for (var i in values){
//templ.addItem(values[i][0].toString()); // this new list box is useless since you populate the existing one
app.getElementById('listfactor').addItem(values[i][0].toString());
}