Since my entire Google Doc add-on would be included in a sidebar, I'd like to have it appear by a simple single click on the main menu. However, all examples I see require first clicking the main menu, and then some sort of submenu, e.g. as below.
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('My Add-On')
.addItem('My Add-On sidebar', 'openSidebar')
.addToUi();
}
Instead, I'd like to do something like below, in principle (though it cannot work this way).
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('My Add-On', 'openSidebar') // this doesn't actually work this way
.addToUi();
}
So that the sidebar would open directly when clicking on the main menu.
Is that possible somehow?
(There seems to be some sort of a workaround for Spreadsheets described here, but doesn't seem to apply for Docs.)
CodePudding user response:
Opening a sidebar with a click on the menu name is not possible, as it's not possible to assign a function to the menu.
Regarding the referred "workaround" (opening a sidebar by using simple trigger) that might work for the document owner but not for document editors due to changes made by Google due to security concerns, but you might make that each editor creates their own installable open trigger instead. Please bear in mind that you should include measures to prevent trigger collisions, i.e. check if the effective user is the same as the active user.
Resources
- https://issuetracker.google.com/issues/69238694#comment7
- https://developers.google.com/apps-script/guides/triggers/installable
CodePudding user response:
Launching a sidebar
function launchSidebar() {
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('FileName').setTitle("App Title"))
}
function onOpen(e) {
launchSidebar();
}
function mymenu() {
SpreadsheetApp.getUi().createMenu("MyMenu")
.addItem("Launch SideBar","launchSidebar")
.addToUi()
}
}
function onOpen() {
mymenu();
}
You will find that sometimes the sidebar my not open so having an item in the menu to launch it for you is convenient.