在对话框关闭时收到Sidebar的通知

时间:2018-05-12 15:58:18

标签: google-apps-script

我有自定义侧边栏,我想调用自定义对话框。当用户关闭对话框时,有没有办法通知侧边栏?

在侧边栏中,我使用google.script.run

调用code.gs文件中的方法
google.script.run
.withSuccessHandler(editDone)
.withFailureHandler(errorHandler)
.showTemplatedDialog();

showTemplatedDialog是code.gs中的一个函数,它显示对话框:

    function showDialog() {
        var htmlTemplate = HtmlService.createTemplateFromFile(dialogName);
        htmlTemplate.dataFromServerTemplate = dialogData;

        var html = htmlTemplate.evaluate()
                  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
                  .setWidth(width)
                  .setHeight(height);

        var ui = SpreadsheetApp.getUi();
        ui.showModalDialog(html, title);    
      }
    }

问题是在显示对话框时调用了withSuccessHandler函数,而不是在用户关闭对话框时调用,并且似乎没有任何方法可以通过侧边栏通知用户做出的选择

对话框是否有办法与边栏进行通信?

1 个答案:

答案 0 :(得分:3)

感谢Sandy的提示,我确实使用了sessionStorage,但我使用的是事件处理程序而不是轮询。

在我的侧边栏中,我补充道:

$(window).on("storage", function(e) {
  if (e.originalEvent.storageArea === sessionStorage) {
    var message = sessionStorage.getItem('message');
    $('#sample').html(message);
  }
});

然后在我的对话框中,在关闭之前,我写入sessionStorage:

$('#btnClose').on("click", function() {
  sessionStorage.setItem('message', 'This is the story' + new Date());
  google.script.host.close();
});