进度自动完成后关闭模态

时间:2021-03-12 11:56:06

标签: google-apps-script google-slides-api google-slides

我想使用 GAS 在 Google Slide 中显示进度或加载屏幕。 下面的脚本工作正常,但我不知道如何删除 x 关闭图标以及如何在我的过程完成后自动关闭模式(我在内部使用 GAS)。

show_progress.gs

var htmlOutput = HtmlService
                    .createHtmlOutput('<p>Loading...</p><style>.modal-dialog-title-close{display:none;}</style')
                .setWidth(250)
                .setHeight(300);

SlidesApp.getUi().showModalDialog(htmlOutput, 'Genera progetto');

...............
.............
..................
..............

google.script.host.close();

1 个答案:

答案 0 :(得分:0)

修改后的脚本

这是一个使用 setTimeout(callback, time)

的例子
function myFunction() {

  var html = `
  <p>Loading...</p>
  <script>
    setTimeout(google.script.host.close(), 3000);
  </script>
  `

  var htmlOutput = HtmlService
                .createHtmlOutput(html)
                .setWidth(250)
                .setHeight(300);
  SlidesApp.getUi().showModalDialog(htmlOutput, 'Genera progetto');
}

如您所见,google.script.host.close() 需要作为 html 服务的一部分在客户端调用。当 3000 毫秒过去时,它作为回调函数被调用。

不过,我不确定您的“流程”,以及您计划如何触发它。理想情况下,您从客户端 html 触发该过程,其完成方式如下:

google.script.run.doSomething();

.gs 文件中的函数如下所示:

function doSomething() {
   Utilities.sleep(10000);
   return "Complete";
}

如果你想在某件事完成后让它发生,那么从客户端 html 你:

function onSuccess(return){
    console.log(return);
    google.script.host.close();
}

google.script.run.withSuccessHandler(onSuccess).doSomething()

所以你的完整函数可能看起来像这样:

function myFunction() {

  var html = `
  <p>Loading...</p>
  <script>
    function onSuccess(return){
        console.log(return);
        google.script.host.close();
    }
    google.script.run.withSuccessHandler(onSuccess).doSomething()
  </script>
  `

  var htmlOutput = HtmlService
                .createHtmlOutput(html)
                .setWidth(250)
                .setHeight(300);
  SlidesApp.getUi().showModalDialog(htmlOutput, 'Genera progetto');
}

记住,从客户端开始“Genera progetto”的过程听起来更好。否则就无法从 .gs 文件与客户端 html 通信。

参考资料

相关问题