Google云端硬盘表格数据验证

时间:2013-02-07 06:43:42

标签: javascript google-app-engine google-drive-api

我正在开发一个Google云端硬盘表单,用于验证输入并在提交后发送电子邮件。问题是将处理程序附加到Google创建的表单控件。一些tutorials创建控件并将它们添加到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,class或者在任何给定时间的名称。这是我最后使用的代码,并返回了一个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生成的表单?

1 个答案:

答案 0 :(得分:0)

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