0

我正在开发一个 Google Drive Form,它将验证输入并在提交时发送电子邮件。问题是将处理程序附加到 Google 创建的表单控件。一些教程创建控件并将它们添加到 UiApp 的实例中,并在附加处理程序后从函数中返回该对象。我试过在打开表单时调用这样的函数,但它不起作用,但我可以在提交时调用这样的函数。

function sendFormByEmail(e) 
{ 
  var email = "some@email.com"; 
  var subject = "Form Results"; 
  var message = ""; for(var field in e.namedValues) 
  { 
    message += field + ' :: ' + e.namedValues[field].toString() + "\n\n";
  } 
  MailApp.sendEmail(email, subject, message); 

}

但是,我想做客户端和服务器端验证。我突然想到我会看看 html 对控件有什么样的属性,也许我可以通过这种方式找到控件,但我不知道 Google 不会更改 id、类或在任何给定时间为此事命名。这是我最后尝试使用并返回 UiApp 实例的代码,但我更喜欢抓住控制并按照我的方式处理它们。

function doGet(){
  //create app
  var app = UiApp.createApplication();
  //controls
  var txtbAccountNumber = app.createTextBox().setId('tbAccountNumber').setName('tbnAccountNumber');
  var txtbOwnerEmail = app.createTextBox().setId('tbOwnerEmail').setName('tbnOwnerEmail');
  var txtbAccountAdjustment = app.createTextBox().setId('tbAccountAdjustment').setName('tbnAccountAdjustment');
  var btnSubmit = app.createButton("Submit").setEnabled(false);
  var lblEmailFailed = app.createLabel("The email was not valid");
  var lblAccountNumber = app.createLabel("The account number was not valid");
  var lblAccountAdjustment = app.createLabel("The account adjustment was not valid");
  //handler for submit click
  var handler = app.createServerHandler("validateInput")
  .validateEmail(txtbOwnerEmail)
  .validateNumber(txtbAccountNumber)
  .validateNumber(txtbAccountAdjustment)
  .addCallbackElement(txtbAccountNumber)
  .addCallbackElement(txtbOwnerEmail)
  .addCallbackElement(txtbAccountAdjustment);

  //callback control handlers
  var validEmail = app.createClientHandler()
  .validateEmail(txtbOwnerEmail)
  .forTargets(txtbOwnerEmail)
  .setStyleAttribute("color","black")
  .forTargets(btnSubmit)
  .setEnabled(true)
  .forTargets(lblEmailFailed)
  .setVisible(false);

  var validAccountNumber = app.createClientHandler()
  .validateNumber(txtbAccountNumber)
  .forTargets(txtbAccountNumber)
  .setStyleAttribute("color","black")
  .forTargets(btnSubmit)
  .setEnabled(true)
  .forTargets(lblAccountNumber)
  .setVisible(false);

  var validAccountAdjustment = app.createClientHandler()
  .validateNumber(txtbAccountAdjustment)
  .forTargets(txtbAccountAdjustment)
  .setStyleAttribute("color","black")
  .forTargets(btnSubmit)
  .setEnabled(true)
  .forTargets(lblAccountAdjustment)
  .setVisible(false);

  var invalidEmail = app.createClientHandler()
  .validateEmail(txtbOwnerEmail)
  .forTargets(txtbOwnerEmail)
  .setStyleAttribute("color","red")
  .forTargets(btnSubmit)
  .setEnabled(false)
  .forTargets(lblEmailFailed)
  .setVisible(true);

  var invalidAccountNumber = app.createClientHandler()
  .validateNumber(txtbAccountNumber)
  .forTargets(txtbAccountNumber)
  .setStyleAttribute("color","red")
  .forTargets(btnSubmit)
  .setEnabled(false)
  .forTargets(lblAccountNumber)
  .setVisible(true);

  var invalidAccountAdjustment = app.createClientHandler()
  .validateNumber(txtbAccountAdjustment)
  .forTargets(txtbAccountAdjustment)
  .setStyleAttribute("color","red")
  .forTargets(btnSubmit)
  .setEnabled(false)
  .forTargets(lblAccountAdjustment)
  .setVisible(true);

  txtbOwnerEmail.addBlurHandler(validEmail);
  txtbOwnerEmail.addBlurHandler(invalidEmail);
  txtbAccountNumber.addBlurHandler(validAccountNumber);
  txtbAccountNumber.addBlurHandler(invalidAccountNumber);
  txtbAccountAdjustment.addBlurHandler(validAccountAdjustment);
  txtbAccountAdjustment.addBlurHandler(invalidAccountAdjustment);
  btnSubmit.addClickHandler(handler);

  app.add(txtbOwnerEmail);
  app.add(txtbAccountNumber);
  app.add(txtbAccountAdjustment);
  app.add(lblEmailFailed);
  app.add(lblAccountNumber);
  app.add(lblAccountAdjustment);

  return app;
}

function validateInput(e){
 var app = UiApp.getActiveApplication();
  var pattern = /^[a-zA-Z0-9._]+@[a-zA-Z0-9._]+\.[a-zA-Z0-9._]{2,4}/;
  return pattern.test(e.parameter.txtbOwnerEmail);   
}

如何将处理程序和验证字段附加到 Google Doc 生成的表单?

4

1 回答 1

0

那是不可能的。您必须使用 UiApp 或 HtmlService 自己创建一个表单。您目前可以与 Google 表单进行的唯一交互是您可以绑定到 onFormSubmit()。

于 2013-02-07T07:09:19.260 回答