通过 Google 应用脚本中的警报按钮关闭侧边栏

Close a Sidebar from alert button in Google App Script

我有一个电子表格,我在其中打开一个包含订单详细信息的侧边栏,然后是一个询问用户是否准备好发送邮件的警告。如果用户选择取消我想停止整个脚本并关闭侧边栏。 google.script.host.close 给出了 Cannot read 属性 "script" from undefined 错误。 我怎样才能简单地关闭侧边栏而无需手动关闭侧边栏?

// 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;
        }

没有直接的方法可以从警报 window 中关闭边栏。但是,您可以使用变通方法。

  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()
    
  • 您想关闭已经打开的侧边栏。
  • 你想通过侧边栏脚本之外的其他功能来实现以上。

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

修改后的脚本:

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

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 设置主侧边栏的存在。使用这个,当主侧边栏没有打开时,可以防止临时侧边栏打开。

参考文献:

如果此解决方法不是您想要的,我很抱歉。

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

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(); 在脚本的其余部分完成之前执行的问题。