如何在另一个函数(UI表单)之后运行函数?

时间:2014-01-22 06:22:34

标签: google-apps-script

我的问题是我需要按下buttonGet按钮才能运行function test (e),但只有在执行了三个函数(validateName (e), validateVk (e), validateEmail (e))之后才会出现新的数据,表格将由function test (e)。 所有这一切都应该通过点击buttonGet完成,是否可能? 谢谢你的时间!

function doGet() {

            ...

              var handler = app.createServerHandler('_processSubmittedData').addCallbackElement(panel); 
              var handler1 = app.createServerHandler('refresh').addCallbackElement(panel); 
              var h1 = app.createServerHandler('validateEmail').addCallbackElement(panel);

              var h2 = app.createServerHandler('validateVk').validateMatches(vk, 'vk.com').addCallbackElement(panel); 
              var h21 = app.createClientHandler().validateNotMatches(vk, 'vk.com')
              .forTargets(empty6)
              .setVisible(true);

              var h3 = app.createServerHandler('validateName').validateLength(fname, 2, 100).addCallbackElement(panel); 
              var h31 = app.createClientHandler().validateNotLength(fname, 2, 100)
              .forTargets(empty1)
              .setVisible(true);


              button.addMouseUpHandler(handler);
          buttonGet.addClickHandler(h1)
        .addClickHandler(h2)
        .addClickHandler(h21)
        .addClickHandler(h3)
        .addClickHandler(h31)
        .addClickHandler(handler1);

        ...

              return app;
            }

function refresh(e) {
              ...

            }

function validateEmail(e){
              var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
              var app = UiApp.getActiveApplication();
              var email = e.parameter.email;
              Logger.log(email);
              if(emailPattern.test(email) == false) {
                app.getElementById('empty4');
                 }
              else {
                    app.getElementById('empty4').setText("Email принят!").setStyleAttribute('color', '#339900').setVisible(true)
                    app.getElementById('empty41').setText("ок");
                   }
              return app;
            }
function validateVk (e) {
              var app = UiApp.getActiveApplication();
              app.getElementById('empty6').setText('VK принят!').setStyleAttribute('color', '#339900').setVisible(true);
              app.getElementById('empty61').setText("ок");
              return app
            }
function validateName (e) {
              var app = UiApp.getActiveApplication();
              app.getElementById('empty1').setText('Здравствуйте, ' + e.parameter.fname + '!').setStyleAttribute('color', '#339900').setVisible(true);
              app.getElementById('empty11').setText("ок");
              return app
            }
function test(e) {
              var app = UiApp.getActiveApplication();
              var L1 = e.parameter.empty41
              var L2 = e.parameter.empty61
              var L3 = e.parameter.empty11
              if (L1 == 'ок' && L2 == 'ок' && L3 == 'ок') {
                app.getElementById('cb1').setVisible(true);
                app.getElementById('terms').setVisible(true);
              }
              return app
            }

1 个答案:

答案 0 :(得分:2)

我认为最好的方法是使用addChangeHandler直接检查每个表单元素。 addChangeHandler可以检查是否所有字段都已正确填写,只有在完成后才能按下提交按钮。
这是一个有效的例子:

function doGet(){
 var app = UiApp.createApplication();
  var handler = app.createServerHandler("testData");
  var panel = app.createHorizontalPanel();
  var txt = app.createTextBox().setId("elementToTest").setName("elementToTest").addChangeHandler(handler);
  handler.addCallbackElement(txt);
  var but = app.createButton("submit").setId("submitButton").setEnabled(false);
  panel.add(txt).add(but);
  app.add(panel);
  return(app);
}

function testData(e){
  var app = UiApp.getActiveApplication();
  var val = e.parameter.elementToTest;
  if(val=="test"){
    app.getElementById("submitButton").setEnabled(true);
  }
  return(app);
}

我希望我正确回答了你的问题。

哈罗德

相关问题