How can I show the sidebar for anonymous viewers (or editors)?
I tried using normal and installable triggers:
Normal Triggers:
function onOpen(){
var html = HtmlService.createHtmlOutputFromFile('Page')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
}
This worked with the owner of the spreadsheet but didn't work with anonymous users although anyone with the link has editing permission.
Installable Triggers:
function showSidebar(){
... the same body of the previous 'onOpen' function
}
Then, I bound the function showSidebar to an installable trigger that is called when the spreadsheet is opened.
This didn't work with either the owner or an anonymous user!
Finally, I tried binding the function showSidebar to an image inserted into the spreadsheet but didn't work with an anonymous user. It displays a message saying
Script showSidebar experienced an error
and even if the final method worked well, it will not show the sidebar automatically.
It looks to me that the code is almost the same of https://developers.google.com/apps-script/guides/dialogs#custom_sidebars. That has two files, Code.gs and Page.html. As the OP didn't mentioned the code of Page.html it's very likely that it was missing on his attempt as using the same code as the OP worked just fine if the code of the Page.html file is include and the file is opened by the owner of by an editor.
Regarding running as anonymous user, the onOpen doesn't run when the spreadsheet is opened by an anonymous with view or edit access. There is a report related to this in the Google Apps Issue Tracker
Issue: 5747 Trigger for anonymous user / script for anonymous user
UPDATE:
From an answer by +Samantha to a similar thread in the Google Docs Help Forum
In order for Scripts to run on a Google Sheet, the user must be logged
in and have "can edit" rights. This means that anonymous users will
not be able to run a Script.
If you would like to see this functionality added to Scripts, I
recommend navigating to the Google Developers' Apps Script support
page and pressing the "Send Feedback" button.
Code from the first link
Code.gs
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Custom Menu')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
Page.html
Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />