0

更新:添加了 doGet 功能。

我有一个用于注册过程的网络应用程序。我创建了一个电子邮件确认,当用户单击“注册”按钮时会向他们发送一封电子邮件。

我的问题是,如果电子邮件字段留空,我该如何停止该功能的运行?此时,当我运行整个脚本时,如果我不输入电子邮件地址,则单击注册时会显示一条错误消息。我不希望函数抛出错误,我希望仍然能够注册。

function doGet(e) {
 var app = UiApp.createApplication().setTitle('Education Registration');
  app.setStyleAttribute("background", "#DBE8C4");
var panel1 = app.createAbsolutePanel().setId('panel1');
  panel1.setHeight(900);
  panel1.setWidth(1500);

  //Register button

var dateSelection = app.createButton('Register').setSize(140, 40);
var loadingWait = app.createLabel('After clicking Register, please allow 5 - 30 seconds for the webpage to process the request.');
var clickHandler = app.createServerHandler("respondToDateSelection");
  dateSelection.addClickHandler(clickHandler);
  clickHandler.addCallbackElement(panel1);

//Email Handler
var emailHandler = app.createServerHandler("emailConfirmation");
  dateSelection.addClickHandler(emailHandler);
  emailHandler.addCallbackElement(panel1);

  return app;

}


function emailConfirmation(e) {
 var app = UiApp.getActiveApplication();

  app.getElementById('fNameText1');
  app.getElementById('lNameText1');
  app.getElementById('eAddressText1');
  app.getElementById('dataItemsLB');
  app.getElementById('aemailAddressText');

  var fNameText1 = e.parameter.fNameText1;
  var lNameText1 = e.parameter.lNameText1;
  var eAddressText1 = e.parameter.eAddressText1;
  var dataItemsLB = e.parameter.dataItemsLB;
  var aemailAddressText = e.parameter.aemailAddressText;

  var subject = "Class Registration Confirmation - " + fNameText1 + " " + lNameText1;
  var emailBody = "This is an Email Confirmation.";

            MailApp.sendEmail(eAddressText1, subject,
                 emailBody, {cc: aemailAddressText});


  return app;

}
4

1 回答 1

0

有很多方法可以做到这一点,最“优雅”的方法可能是在你的函数中使用客户端处理程序验证器doGet,但你没有显示它......(有一个特定的电子邮件验证器)另一种方法是有一个UI 中的警告标签最初是不可见的,当e.parameter.eAddressText1不是有效的电子邮件或为空时才可见,相同的条件将适用于sendEmail命令,如果无效则跳过它然后返回 UI。

随时发布您的 doGet 函数,以获得更准确的答案。


编辑: 感谢您发布您的代码,虽然它不完整并且需要一些工作才能使其成为一个有趣的示例,但我最终使用此代码来说明 clientHandlers、验证器和 try/catch 的电子邮件地址的使用......

这是代码,我将其更改为在电子表格上工作以避免部署和版本控制,这只是一个测试,仅此而已......

function doGet(e) {
 var app = UiApp.createApplication().setTitle('Education Registration');
  app.setStyleAttribute("background", "#DBE8C4");
  var panel0 = app.createFlowPanel().setId('panel0');
  var panel1 = app.createVerticalPanel().setId('panel1');
  var fNameText1=app.createTextBox().setName('fNameText1').setId('fNameText1');;
  var lNameText1=app.createTextBox().setName('lNameText1').setId('lNameText1');;
  var eAddressText1=app.createTextBox().setName('eAddressText1').setId('eAddressText1').setText('mail')
  var dataItemsLB=app.createTextBox().setName('dataItemsLB').setId('dataItemsLB');;
  var aemailAddressText=app.createTextBox().setName('aemailAddressText').setId('aemailAddressText').setText('mail');
  panel1.add(fNameText1).add(lNameText1).add(eAddressText1).add(dataItemsLB).add(aemailAddressText)
  panel0.add(panel1)

//Register button
  var dateSelection = app.createButton('Register').setSize(140, 40).setId('dateSelection');
  var loadingWait = app.createLabel('After clicking Register, please allow 5 - 30 seconds for the webpage to process the request.').setVisible(false).setId('loadingWait');
  var clickHandler = app.createServerHandler("respondToDateSelection").validateEmail(eAddressText1);
  dateSelection.addClickHandler(clickHandler);
  clickHandler.addCallbackElement(panel0);

//Email Handler
  var emailHandler = app.createServerHandler("emailConfirmation").validateEmail(eAddressText1);
  dateSelection.addClickHandler(emailHandler);
  emailHandler.addCallbackElement(panel1);

//client handlers
  var warning = app.createLabel('Please enter your email where necessary').setId('warning').setVisible(false).setStyleAttribute('background','yellow')
  var clientHandlerwait = app.createClientHandler().forTargets(loadingWait).setVisible(true).validateEmail(eAddressText1)
  var clientHandler1 = app.createClientHandler().validateNotEmail(eAddressText1)
    .forTargets(warning).setVisible(true).forEventSource().setStyleAttribute('color','red')
  var clientHandler2 = app.createClientHandler().validateNotEmail(aemailAddressText)
    .forTargets(warning).setVisible(true).forEventSource().setStyleAttribute('color','red')

  dateSelection.addClickHandler(clientHandlerwait).addClickHandler(clientHandler1).addClickHandler(clientHandler2)

  app.add(panel1.add(dateSelection).add(loadingWait).add(warning))
  SpreadsheetApp.getActive().show(app)
//  return app;
}

function respondToDateSelection(){
return
}

 function emailConfirmation(e) {
  var app = UiApp.getActiveApplication();
   app.getElementById('warning').setVisible(false);
   app.getElementById('dateSelection').setStyleAttribute('color','black')

  var fNameText1 = e.parameter.fNameText1;
  var lNameText1 = e.parameter.lNameText1;
  var eAddressText1 = e.parameter.eAddressText1;
  var dataItemsLB = e.parameter.dataItemsLB;
  var aemailAddressText = e.parameter.aemailAddressText;
  var subject = "Class Registration Confirmation - " + fNameText1 + " " + lNameText1;
  var emailBody = "This is an Email Confirmation.";
    try{
//            MailApp.sendEmail(eAddressText1, subject,emailBody, {cc: aemailAddressText});
        Utilities.sleep(500)// simulate a duration to read the message
        app.getElementById('loadingWait').setText('mail sent').setVisible(true)
}catch(err){
        app.getElementById('loadingWait').setText('error sending mail').setVisible(true)
}
  return app;
}
于 2013-01-09T20:42:29.420 回答