2

我正在使用 Google Apps 脚本编写 GUI。我有一个文本框,例如 txtLastName,我想添加一个客户端处理程序来检测空值。本质上,我想让它成为必填字段。

4

1 回答 1

0

我想有几种可能性,其中之一是使用长度验证器

var clientHandler = app.createClientHandler().validateLength(widget, min, max)

并将其用于setVisible警告消息并最终禁用“提交按钮”...

clientHandlers 上的文档非常明确

这是一个简单形式的示例,用于检查question 1并显示警告消息:

function BuildForm() {
  var text= new Array();
  text =['question 1','question 2','question 3','question 4','question 5','question 6']; 
  var textBox = new Array();    
  var app=UiApp.createApplication().setTitle('Form');
  var panel = app.createVerticalPanel().setId('panel');
  var btn = app.createButton('process');
  var warning = app.createLabel('You forgot to fill in question 1').setVisible(false)
  for (nn=0;nn<text.length;++nn){
    var label = app.createLabel(text[nn]);
    textBox[nn] = app.createTextBox().setName(text[nn].replace(' ',''));
    panel.add(label).add(textBox[nn])
  }
    var cliH1 = app.createClientHandler().validateLength(textBox[0], 0, 1)
        .forTargets(btn).setEnabled(false)
        .forTargets(warning).setVisible(true)
    var cliH2 = app.createClientHandler()
        .forTargets(btn).setEnabled(true)
        .forTargets(warning).setVisible(false)

  var servH = app.createServerHandler('process').addCallbackElement(panel).validateLength(textBox[0], 1, 40)
  btn.addClickHandler(cliH1)
  textBox[0].addClickHandler(cliH2)
  btn.addClickHandler(servH)  
  panel.add(btn).add(warning)
  app.add(panel)
  var doc = SpreadsheetApp.getActive();
  doc.show(app)
}


function process(e){
  var app = UiApp.getActiveApplication()
      Browser.msgBox(e.parameter.question1+'  '+e.parameter.question2+'  '+e.parameter.question3+'  '+e.parameter.question4+'  '+e.parameter.question5+'  '+e.parameter.question6)
      app.close()
      return app
          }

编辑:这是另一个使用另一个验证(validateMatches)并检查所有答案的版本

function BuildForm2() {
  var text= new Array();
  text =['question 1','question 2','question 3','question 4','question 5','question 6']; 
  var textBox = new Array();    
  var app=UiApp.createApplication().setTitle('Form');
  var panel = app.createVerticalPanel().setId('panel');
  var btn = app.createButton('process').setWidth('240');
  var warning = app.createLabel('You forgot to fill at least one question').setVisible(false)
  for (nn=0;nn<text.length;++nn){
    var label = app.createLabel(text[nn]);
    textBox[nn] = app.createTextBox().setName(text[nn].replace(/ /g,''));
    panel.add(label).add(textBox[nn])
  }
  var servH = app.createServerHandler('process').addCallbackElement(panel).validateLength(textBox[0], 1, 40).validateLength(textBox[1], 1, 40).validateLength(textBox[2], 1, 40)
      .validateLength(textBox[3], 1, 40).validateLength(textBox[4], 1, 40).validateLength(textBox[5], 1, 40);
  var cliH = app.createClientHandler().validateMatches(textBox[0],'').validateMatches(textBox[1],'').validateMatches(textBox[2],'').validateMatches(textBox[3],'')
      .validateMatches(textBox[4],'').validateMatches(textBox[5],'')
        .forTargets(warning).setVisible(true)
  btn.addClickHandler(servH).addClickHandler(cliH)  
  panel.add(btn).add(warning)
  app.add(panel)
  var doc = SpreadsheetApp.getActive();
  doc.show(app)
}
于 2012-07-06T15:58:30.643 回答