如何在Google App脚本中创建弹出框?

时间:2015-01-22 12:47:53

标签: javascript html google-apps-script

基本上这是我的问题:

如何使用带有HTML服务的Google App脚本创建弹出窗口或对话框。

我在Google Script文件中进行了大量验证(与HTML文件相关),我只需要快速告诉用户错误,然后他们就可以调整自己的表单并继续...这是我的尝试验证到目前为止。我试图只使用javascript alert()函数,但它什么也没做。

if(date == ""){
     alert("please select a date");
   }else if(teacher == ""){
     alert("please put your name");
   }else if(group == ""){
     alert("Please enter a group");
   }else if(notes == ""){
     alert("Please write where the tech is being used in the notes");
   }else if(events.length != 0){
     window.alert("It appears that the slot your trying to book is taken");
   }else{

我希望我能以这种方式进行验证,而不必进行全新的验证方法(最有可能来自HTML文件)

1 个答案:

答案 0 :(得分:0)

如果您的验证脚本(if elseif语句)在Code.gs文件中,则警报功能将不起作用。

您要做的是使用.withSuccessHandler将该错误消息发送到html脚本中的google.script.run函数,然后从那里执行警告。
由于警报是从html(而不是.gs文件)中调用的,因此可以正常工作。

让我们假设您上面的脚本位于一个名为validateForm()的函数中,并且您在表单提交按钮上调用该脚本,代码将像这样的东西:

HTML文件

// some codes and scripts here

Google.script.run.withSuccessHandler(showAlert).validateForm(this.form);

function showAlert(stringFromCodeGs) {
  if (stringFromCodeGs != false) {
   alert(stringFromCodeGs);
}

GS文件

function validateForm(formObject) {
if(formObject.date == ""){
     return "please select a date";
   }else if(formObject.teacher == ""){
     return "please put your name";
   }
   // your other else if here
   }else{
     //your code to process form here
      return false;
   }

它的作用是将错误消息抛回(返回)到google.script.run,然后将其用作成功处理程序的参数 - 在本例中,showAlert()函数自动调用通过.withSuccessHandler()。

希望这会有所帮助。