如何在独立的谷歌应用程序脚本中显示UI?

时间:2018-05-31 05:59:42

标签: google-apps-script google-sheets

在构建绑定的Google Apps脚本时,我可以使用以下函数向用户显示ui。

SpreadsheetApp.getUi().showModalDialog(html, 'Title');
SpreadsheetApp.getActive().show(ui);

但是当我尝试在独立脚本中运行这些函数时,我收到以下错误:

Cannot call SpreadsheetApp.getUi() from this context.

我可以找到一种在运行独立的谷歌应用脚​​本时显示用户界面的方法。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

这可以通过将您的脚本发布为Web应用程序并在doGet函数中返回html输出文件来实现。

将以下内容添加到您的脚本

function doGet(e){
  var html = HtmlService.createHtmlOutputFromFile('file.html')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  return html;
}

然后转到 发布 - >部署为网络应用