当对话框和自定义侧边栏都存在时关闭对话框

时间:2016-08-11 09:40:43

标签: google-apps-script google-sheets

我在使用google.script.host.close()命令时遇到问题,因为它正在关闭我的侧边栏而不是对话框。

function clickWeek() {
  setWeekColor('week', '#7FFF00');
  setMonthColor('month', '#d6d6c2');
  setPressColor('press', '#d6d6c2');
  setAllDataColor('allData', '#d6d6c2');
  google.script.run.withSuccessHandler(google.script.host.close).weekAheadCreate();
}

对话框从weekAheadCreate()功能的顶部打开,如下所示:

var htmlOutput = HtmlService
     .createHtmlOutput('<p>Please wait a moment...</p>')
     .setWidth(250)
     .setHeight(80);
 SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Loading');

我有什么想法可以强制google.script.host.close()在对话框而不是侧边栏上执行操作?

1 个答案:

答案 0 :(得分:1)

close()方法关闭当前对话框/侧边栏,并且假设clickWeek()位于侧边栏中,它将关闭它而不是对话框。您需要在对话框中运行close()方法。

在创建对话框时以及withSuccessHandler()检测到成功返回后,您如何触发服务器端功能,然后使用close()关闭对话框。

在创建createHtmlOutputFromFile的对话框时,您需要使用createHtmlOutput,并在html中使用<script>标记。这是一个例子:

<强> code.gs

function createDialog(){
  var htmlOutput = HtmlService
       .createHtmlOutputFromFile('dialog')
       .setWidth(250)
       .setHeight(80);
   SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Loading');
}

function doBusyStuff(){
  // Busy stuff
}

<强> dialog.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    (function() {
      // Runs the busy stuff and closes the dialog using .close() on success
      google.script.run
          .withSuccessHandler(google.script.host.close)
          .doBusyStuff();
    })();
    </script>
  </head>
  <body>
  <p>Please wait a moment...</p>
  </body>
</html>