Google Apps 脚本:从菜单打开文档

Google Apps Script: open document from menu

我知道,无法使用 Google Apps 脚本自动打开文档。但是有没有办法从自定义菜单打开文档?从菜单中 select 一个项目并直接打开一个特定的文档比打开一个对话框更舒服,我必须在对话框中单击 link 到文档。

稍微考虑一下,侧边栏可以充当一种菜单,您可以强制它在打开时显示,并提供在侧边栏关闭时返回的功能。这可能满足您的需求,因为它提供了一种菜单项和 select 一个 link 的能力。使用 Dialogs and Sidebars in G Suite Documents 页面中的示例,下面的代码将允许您了解对话框场景以及边栏菜单项,并在打开时打开边栏菜单项:

在你的 Code.gs 文件中,放置:

function onOpen() {

  showSidebar();

  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Dialog')
      .addItem('Open', 'openDialog')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('dialog');
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showModalDialog(html, 'Dialog title');
}


function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('sidebar')
      .setTitle('My custom sidebar')
      .setWidth(300);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}

接下来,创建 2 个 HTML 文件,"dialog" 和 "sidebar",并向每个文件添加以下内容,以供显示: 在 dialog.html 中放置以下内容:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hello, World!
    <input type="button" value="Close"
        onclick="google.script.host.close()" />
  </body>
</html>

并在 sidebar.html 中放置:

Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />
<br/>
<a href="https://drive.google.com/open?id=16HHKXBMmWXh5NBZRAnFDiwGrZ">Open My Sample File here</a>
<br/>
<a href="https://drive.google.com/open?id=16HHKXBMmWXh5NBZRAnFDiwGrZ" target="_blank">Open My Sample File in a new tab</a>

如果您在编辑器中 运行 onOpen(),或者保存并关闭文件,然后重新打开,您应该在文件中获得边栏。用您合适的项目替换 HTML 中的 Hello World 项目。请注意,提供的 links 将不起作用,因为我删除了 URL.

的一部分