如何从列表框选择中启用或禁用文本框和按钮

时间:2014-04-14 10:22:48

标签: google-apps-script google-sheets

我正在尝试使用验证功能在选择列表框值时启用或禁用文本框和按钮。请参阅下面的示例代码。任何帮助将不胜感激。

function exampleListBox(){
var app = UiApp.createApplication().setTitle('Example').setHeight(100).setWidth(250);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var panel = app.createFormPanel();
var grid = app.createGrid(5, 3);
var listbox = app.createListBox().setId('lb1').addItem('None').addItem('Item1').addItem('Item2').addItem('Item3').addItem('Item4');
var textbox = app.createTextBox().setId('tb1').setText('None').setEnabled(false);
var pressme = app.createButton('press me').setId('btn1').setEnabled(false);

var chandler1 = app.createClientHandler()
.validateMatches(listbox, 'Item1, Item3')
.forTargets(textbox).setEnabled(true)
.validateNotMatches(listbox, 'None, Item1, Item4')
.forTargets(textbox).setEnabled(false);

grid.setWidget(0, 0, listbox)
.setWidget(0, 1, textbox).addClickHandler(chandler1)
.setWidget(1, 1, pressme);

app.add(grid)
app.add(panel) 
ss.show(app);
}

1 个答案:

答案 0 :(得分:0)

我认为您必须将客户端处理程序添加到列表或整个网格中。此外,您不能在单个客户端handlere中组合这两个函数,我会为每个所需的效果创建一个(启用和禁用)

最后,validateMatches方法中的参数是正则表达式,而不是简单的字符串连接,因此如果您希望条件在“Item1”或“Item3”上为true,则应使用验证的正则表达式关于这2个值。 这是一个示例代码,它不能完全按照它应该如何工作但显示正确方式的开始......

function exampleListBox(){
var app = UiApp.createApplication().setTitle('Example').setHeight(100).setWidth(250);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var panel = app.createFormPanel();
var grid = app.createGrid(5, 3);
var listbox = app.createListBox().setId('lb1').addItem('None').addItem('Item1').addItem('Item2').addItem('Item3').addItem('Item4');
var textbox = app.createTextBox().setId('tb1').setText('None').setEnabled(false);
var pressme = app.createButton('press me').setId('btn1').setEnabled(false);

var chandler1 = app.createClientHandler()
.validateMatches(listbox, 'Item1')
.forTargets(textbox,pressme).setEnabled(true);
var chandler2 = app.createClientHandler()
.validateMatches(listbox, 'None')
.forTargets(textbox,pressme).setEnabled(false);

  grid.addClickHandler(chandler1).addClickHandler(chandler2)
grid.setWidget(0, 0, listbox)
.setWidget(0, 1, textbox)
.setWidget(1, 1, pressme);

app.add(grid)
app.add(panel) 
ss.show(app);
}

我对正则表达式不够好,无法在validateMatches中实现多条件,但我相信你会在某处找到它。如果没有,请使用JavaScript Tag询问SO,这些家伙真是太棒了^^