【问题标题】:Close a Sidebar from alert button in Google App Script从 Google App 脚本中的警报按钮关闭侧边栏
【发布时间】:2019-01-07 06:39:17
【问题描述】:

我有一个电子表格,我在其中打开一个带有订单详细信息的侧边栏,然后出现一个警告,询问用户是否准备好发送邮件。如果用户选择取消,我想停止整个脚本并关闭侧边栏。 google.script.host.close 给出了无法从未定义的错误中读取属性“脚本”。 我如何无需手动关闭侧边栏?

// Display a modal dialog box in sidebar with custom HtmlService content to preview the order.
   var htmlOutput = HtmlService.createHtmlOutput('<h1>'+ supplier + '</h1><br/>' +  previewOrder)
   .setTitle('Order Details');
   SpreadsheetApp.getUi().showSidebar(htmlOutput);

// now also show an alert asking if you want to send the mail
   var ui = SpreadsheetApp.getUi();
   var response = ui.alert('Confirm Sending','You are about to send this order to '+  supplier + ' (' + emailAddress + ') - are you sure?', ui.ButtonSet.YES_NO_CANCEL);

        // Process the user's response.
        if (response == ui.Button.YES) {
          var subject = "Order for Tomorrow ";
          MailApp.sendEmail(emailAddress,subject + dayname + " - " + Utilities.formatDate(tomorrow, "GMT+2", "d MMM") + "" ,emailBody, {to: emailAddress,cc: ccEmailAddress, htmlBody: emailBody});          

          }
        else  if (response == ui.Button.NO) {
          //if user chooses NO then ignore and continue the loop
        } else {
      //close the sidebar
          SpreadsheetApp.getUi().google.script.host.close();
          //cancel the script
          return;
        }

【问题讨论】:

  • 为什么不在侧边栏上放一个按钮来预览订单并使用对话框客户端来预览订单,然后当他们取消对话框时,您可以同时关闭侧边栏和对话框.我经常使用 JQuery UI - Dialog,它们相当容易使用。如果您需要来自服务器的数据,您可以使用 google.script。使用成功处理程序运行以获取数据。

标签: google-apps-script web-applications sidebar


【解决方案1】:

我发现这个解决方案有效。

function example(formObject) {
  function onSucces(e) {
      google.script.host.close();
  }

  // Inside the sidebar html  
  var runner = google.script.run.withSuccessHandler(onSucces)
    .formHandlerFunction(formObject);

  return;
}  

使用 withSuccessHandler 为我解决了问题。 我遇到了在我的脚本的其余部分完成之前执行google.script.host.close(); 的问题。

【讨论】:

    【解决方案2】:
    • 您要关闭已打开的侧边栏。
    • 您想通过不是侧边栏脚本的其他功能来实现上述功能。

    如果我的理解是正确的,那么这个解决方法怎么样?在此解决方法中,侧边栏被临时侧边栏覆盖而关闭。我认为您的情况有几种解决方法。因此,请将此视为其中之一。

    修改脚本:

    当这反映到您的脚本中时,修改后的脚本如下。

    var ui = SpreadsheetApp.getUi();
    var response = ui.alert('Confirm Sending','You are about to send this order to '+  supplier + ' (' + emailAddress + ') - are you sure?', ui.ButtonSet.YES_NO_CANCEL);
    
    // Process the user's response.
    if (response == ui.Button.YES) {
      var subject = "Order for Tomorrow ";
      MailApp.sendEmail(emailAddress,subject + dayname + " - " + Utilities.formatDate(tomorrow, "GMT+2", "d MMM") + "" ,emailBody, {to: emailAddress,cc: ccEmailAddress, htmlBody: emailBody});
    }
    else  if (response == ui.Button.NO) {
      //if user chooses NO then ignore and continue the loop
    } else {
      //close the sidebar
    
      var html = HtmlService.createHtmlOutput("<script>google.script.host.close();</script>"); // Added
      SpreadsheetApp.getUi().showSidebar(html); // Added
    
      //cancel the script
      return;
    }
    

    注意:

    • 在未打开主侧边栏的情况下运行此修改后的脚本时,临时侧边栏会打开片刻。如果您不想这样做,例如,请在打开主侧边栏时使用 PropertiesService 设置主侧边栏的存在。使用这个,当主侧边栏没有打开时,可以防止临时侧边栏被打开。

    参考资料:

    如果此解决方法不是您想要的,对不起。

    【讨论】:

    • 非常感谢 - 如此简单的解决方法可以完成工作!
    • @user2894986 很高兴您的问题得到了解决。也谢谢你。
    【解决方案3】:

    没有直接的方法可以从警报窗口关闭侧边栏。但是,您可以使用解决方法。

    1. 当用户点击取消时,在服务器端设置一个属性。

       PropertiesService.getDocumentProperties().setProperty("CLOSED", "CLOSED");
      
    2. 在边栏中,设置一个每秒轮询此属性值的计时器。如果返回值为 CLOSED,则关闭侧边栏。

      // Server side
      function checkClosedStatus() {
         var props = PropertiesService.getDocumentProperties();
         var value = props.getProperty("CLOSED");
         if (value === "CLOSED") {
           props.deleteProperty("CLOSED");
         }
         return value;
      }
      
      // Inside the sidebar html
      
      google.script
        .run
        .withSuccessHandler(function(e) {
               if (e === "CLOSED") 
                  google.script.host.close();
       })
       .checkClosedStatus()
      

    【讨论】:

    • 谢谢。我目前正在从一个脚本运行整个事情并且没有单独的 Sidebar.html...我可以在我的脚本中包含你的代码的最后一点吗?
    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2020-12-23
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多