Google Apps脚本示例 - 如何更改单选按钮的列表框依赖值的值

时间:2015-04-01 23:18:26

标签: javascript google-apps-script listbox radio-button

我对Google Apps脚本有疑问。我想创建一个基本表单,其中表单包含文本框,单选按钮,列表框和文件上载。我的问题在这里,我想根据选择的单选按钮更改列表框的值。

例如:

//variable radio button
var valRadio1 = app.createRadioButton("radio", "One").setName("One").setId("One");
var valRadio2 = app.createRadioButton("radio", "Two").setName("Two").setId("Two");

//arrays of listbox
var item1 = ["item1","item2"];
var item2 = ["item3","item4"];

//variable listbox
var listBox = app.createListBox().setId("box").setName("box");

如果我选择valRadio1,那么listBox将加载item1的值,如果我选择valRadio2,则listBox将加载item2的值。那么你能向我解释一下我的问题的解决方案吗?你能给我一些例子来解决我的问题吗?非常感谢你们所有人都可以帮助我:D。

注意:我仍在使用UI服务,所以请用这种方式向我解释:D。

1 个答案:

答案 0 :(得分:0)

我建议在折旧时使用基于UiApp的HTML服务。

但是,以下答案可能会对您的需求有所帮助。为了实现这一点,您必须使用每个单选按钮附加click事件处理程序。

function doGet(e) {
  var app = UiApp.createApplication().setTitle('Title'); 
  var panel = app.createVerticalPanel();
  var lb = app.createListBox(true)
                 .setId('lbId')
                 .setName('lbName')
                 .setHeight(60)
                 .setWidth(60);

  var valRadio1 = app.createRadioButton("Radio", "one").setName('one').setId('one');
  var valRadio2 = app.createRadioButton("Radio", "two").setName('two').setId('two');

  var handler1 = app.createServerChangeHandler('one');
  handler1.addCallbackElement(panel);  
  valRadio1.addClickHandler(handler1);

  var handler2 = app.createServerChangeHandler('two');
  handler2.addCallbackElement(panel);  
  valRadio2.addClickHandler(handler2);

  panel.add(valRadio1);      
  panel.add(valRadio2);  

  panel.add(lb);
  app.add(panel);  

  return app;
}

function one(e){
  var app = UiApp.getActiveApplication(); 
  app.getElementById('two').setValue(false);
  var lb = app.getElementById('lbId');
  lb.clear();

  var item1 = ["item1","item2"];

  for(var i=0;i<item1.length;i++) {
    lb.addItem(item1[i]);
  }

  return app;
}

function two(e){
  var app = UiApp.getActiveApplication(); 
  app.getElementById('one').setValue(false);
  var lb = app.getElementById('lbId');
  lb.clear();

  var item2 = ["item3","item4"];

  for(var i=0;i<item2.length;i++) {
    lb.addItem(item2[i]);
  }

  return app;
}

有关详细信息,请参阅depreciated documentation of UiApp.