I know, there ist no way to automatically open a document with Google Apps Script. But is there a way to open a document from a custom menu? It would be more comfortable to select an item from the menu and directly open a specific document than open a dialog box where I have to click a link to the document.
1 Answers
Thinking about this a little, a Sidebar COULD act as a sort of menu, and you could force it to display on open as well as provide the ability to get the sidebar back if it is closed. That may be viable for your needs as it provides a sort of menu item and the ability to just select a link. Using the Examples from the Dialogs and Sidebars in G Suite Documents page, the below code will allow for the Dialog Box scenario you are aware of as well as the Sidebar menu item and opens the sidebar menu item on open:
In the Code.gs file you, place:
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);
}
Next, create 2 HTML files, "dialog" and "sidebar" and add the following to each, for displaying purposes: In dialog.html place the following:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Hello, World!
<input type="button" value="Close"
onclick="google.script.host.close()" />
</body>
</html>
and in sidebar.html place this:
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>
If you run onOpen() in the editor, or save and close the files, then reopen, you should get the sidebar in your file. Replace the Hello World items in the HTML with your appropriate items. Note that the links provided will not work as I removed a portion of the URL.