簡體   English   中英

如何在Google App腳本中創建一個彈出框?

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

基本上這是我的問題:

如何使用帶有HTML服務的Google App腳本創建彈出窗口或對話框。

我正在Google腳本文件(適用於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文件)

如果您的驗證腳本(如果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()。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM