简体   繁体   English

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

[英]How do I create a pop up box in Google App script?

Basicly this is my question: 基本上这是我的问题:

How do I, using Google App script w/ HTML service create a Pop up or dialog box. 如何使用带有HTML服务的Google App脚本创建弹出窗口或对话框。

I am doing lots of validation in a Google Script file (as appose to HTML file) and I just need to quickly tell the user the error, then they can adjust their form and carry on... this is my attempt at validation so far. 我正在Google脚本文件(适用于HTML文件)中进行很多验证,我只需要快速告诉用户错误,然后他们就可以调整其形式并继续进行...这是我到目前为止的验证尝试。 I've tried to just use the javascript alert() function but it does nothing. 我试图只使用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{

Im hoping I can do this validation this way and not have to do a whole new method of validation (most likely from within the HTML file) 我希望我可以通过这种方式进行验证,而不必执行全新的验证方法(很可能来自HTML文件)

If your validation script (if elseif statements) is in the Code.gs file, the alert function will not work. 如果您的验证脚本(如果elseif语句)在Code.gs文件中,则警报功能将不起作用。

What you want to do is to throw that error message to your google.script.run function in your html script using the .withSuccessHandler, then do the alert from there. 您要做的是使用.withSuccessHandler将错误消息抛出到html脚本中的google.script.run函数中,然后从那里进行警报。
Since the alert is called from within html (not in the .gs file), it will work. 由于警报是从html(而不是.gs文件)中调用的,因此它将起作用。

Lets assume that the script you have above is in a function called validateForm() and you are calling that on your form submit button, the code will looke like something like this: 假设上面的脚本在一个名为validateForm()的函数中,并且您正在表单提交按钮上调用它,则代码将类似于以下内容:

HTML file HTML文件

// some codes and scripts here

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

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

GS file 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;
   }

What it does is that it throws (return) the error message back to your google.script.run, which will then use it as a parameter for the success handler - in this example, the showAlert() function, automatically called by the .withSuccessHandler(). 它的作用是将错误消息返回(返回)到您的google.script.run,然后将其用作成功处理程序的参数-在此示例中,是showAlert()函数,由自动调用。 withSuccessHandler()。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM