我知道,无法使用 Google Apps 脚本自动打开文档。但是有没有办法从自定义菜单打开文档?从菜单中选择一个项目并直接打开特定文档比打开一个对话框(在对话框中必须单击文档链接)更方便。
最佳答案
稍微考虑一下,侧边栏可以充当一种菜单,您可以强制它在打开时显示,并提供在侧边栏关闭时恢复侧边栏的功能。这可能适合您的需求,因为它提供了一种菜单项和仅选择链接的能力。使用 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.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 项替换为您适当的项。请注意,由于我删除了 URL 的一部分,因此提供的链接将不起作用。
关于javascript - 谷歌应用脚本: open document from menu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45676827/