本文介绍了我如何与其兄弟(侧边栏)通信页面(模态对话框)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的脚本转换为插件.其中一个需求是配置一个模板,所以我编写了一个侧边栏来启动一个字段选择器.由于侧边栏没有足够的空间供选择器使用,我必须通过在服务器端调用此代码从我从侧边栏创建的模式对话框中启动它:

I'm converting a script of mine to be an add on. One of the needs is to configure a template, so I have programmed a sidebar that launchs a field picker. As the sidebar does not have enough room for the picker, I have to launch it from a modal dialog that I create from the sidebar, by calling this code in the server side:

var html = HtmlService.createHtmlOutputFromFile('TemplatePicker.html')
  .setWidth(600).setHeight(425);
SpreadsheetApp.getUi().showModalDialog(html, 'Select the file with the template');

我的问题是,一旦用户选择了文件,当我获得所选文件的 ID 时,我无法将该 ID 传递给侧边栏.我尝试调用 someJSFunctionOfSidebar(id)parent.someJSFunctionOfSidebar(id),但没有奏效,所以我最终结束将值传递给服务器端并重新加载侧边栏从那里开始,但速度很慢,产生的效果很难看.

My problem is that once the user picks the file, when I have the id of the chosen file, I'm not able to pass that id to the sidebar. I tried invoking someJSFunctionOfSidebar(id) and parent.someJSFunctionOfSidebar(id), but it didn't work, so I finally ended passing the value to the server side and reloading the sidebar from there, but it's very slow and the resulting effect is ugly.

我的问题是:

有没有办法将客户端级别的值从使用 SpreadsheetApp.getUi().showModalDialog 创建的模态对话框传递给其父级?也许它不是真正的父级,这就是它不工作的原因.

Is there a way to pass a value at client level from a modal dialog created with SpreadsheetApp.getUi().showModalDialog to its parent? Perhaps it's not really its parent and that's the reason for it not working.

推荐答案

对 - 这里实际上没有 DOM 父/子关系在起作用.侧边栏和模式对话框都是从服务器端脚本启动的,并且是独立的.不过,它们都可以与服务器通信,因此您可以使用存储转发技术将结果从选择器发送到侧边栏.

Right - there isn't actually a DOM parent / child relationship in play here. Both the sidebar and the modal dialog were launched from server-side scripts, and are independent. They can both communicate with the server, though, so you can use a store-and-forward technique to get the result from your picker to the sidebar.

基本思路:

  • 侧边栏将在请求选择器启动后立即开始轮询服务器以获取选择器的结果.
  • 选择器的结果将发送到服务器,使用 google.script.run.
  • 服务器将临时存储结果 - 这可以像全局变量一样简单,具体取决于您的情况.
  • 一旦有结果,下一次投票就会检索它.

看看如何从附加组件中轮询 Google 文档,了解轮询器的基本概念.

Have a look at How to poll a Google Doc from an add-on for the basic idea of a poller.

这篇关于我如何与其兄弟(侧边栏)通信页面(模态对话框)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:57