Showing sidebar for viewers - google-apps-script

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()" />

Related

How to replace generic icon

I am creating an add-on for Google Forms. The script extracts data from the form and sends the results to the Google spreadsheet. What I would like to do, if possible, is replace the generic add-on icon (refer to the screenshot below. The icon is highlighted) with an icon that I have. How would I achieve it?
The following is where I am creating the add-on menu:
function onOpen(e) {
FormApp.getUi()
.createAddonMenu()
//.createMenu("MOATT Add-On")
.addItem('Show Modal Extract Form Q&A', 'showModal')
.addItem('Show Sidebar Modal Extract Form Q&A', 'showSideBar')
.addItem('Show Modeless Extract Form Q&A', 'showModeless')
.addItem('About MOATT Add-On', 'showAbout')
.addToUi();
}
Google Apps Script have methods to add custom menus but not to change the built-in user interface.
Resources
https://developers.google.com/apps-script/reference/forms/form-app#getui
https://developers.google.com/apps-script/reference/base/ui.html

Problem with google.script.run in custom HTML dialog in G Suite account (but not personal Google account)

To diagnose the problem using google.script.run in a custom HTML dialog, I ran Google’s example script and HTML code for dialog boxes in both my personal and G Suite accounts. Here is Google's example: https://developers.google.com/apps-script/guides/html/communication. The code is shown below.
In my personal account, the code worked perfectly. The custom HTML dialog popped up and the function doSomething created log output.
In my G Suite account, the function doSomething did not run at all. I tried playing with settings, testing as an add-on, publishing to the web, and more, but I could not make the Google's sample code work. I spent an hour on the chat session with Google support.
My guess: I am missing a few authorization and/or settings steps in my G Suite account. Please help! I am new to the G Suite AddOn Marketplace, and would appreciate very specific instructions for next steps. I want to make sure my real script works on my G suite account before submitting it for review. I would like to use custom dialog boxes so that I can collect user input in multiple fields.
Here is a link for a document with screen shot images of my authorization and execution windows. I did NOT get the expected "This app isn't verified" warning in my G Suite account. https://docs.google.com/document/d/1IiXmPE7zB1E-s_Uzzhmc0ggOCkz3qqcy1cqZrezkgSg/edit?usp=sharing
Here is the google script code:
// code.gs
// Use this code for Google Docs, Slides, Forms, or Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index');
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Dialog title');
}
function doSomething(){ // I added from second step of example
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Sheet1');
var cell1 = sheet.getRange("A1");
cell1.setValue("did something");
Logger.log("did something")
};
Here is the HTML code for the file Index.html:
<!DOCTYPE html>"
<html>
<head>
<base target="_top">
</head>
<body>
Hello, World!
<script>
google.script.run.doSomething();
</script>
<input type="button" value="Close"
onclick="google.script.host.close()" />
</body>
</html>
Similar question posed by: Google Apps Script - HTML service not working properly only on G-suite accounts
Did your script created in your personal or your GSuite accout? Please consider that Google Chrome capture as global log in, the account (email) that you use to do the first log in. Google don't mind if from this logged account you logged in others accounts: Google still continuing considering as global loggin the first one account you logged in. In this way, try to do the first log in with your GSuite accout; I hope this will solve the issue and you can run from all your accounts.
(thanks for Hujie Wang's replied to question/60438751/ : that helped me undertood this issue and solve it for my project)

Keep Macro initiating drawing always on the screen

In my Google Sheets I have two drawings, each of which executes a macro. As I scroll through the sheet I would like the "drawing" to follow me so they always appear on the display so I can invoke them immediately. Is there a way to make these "macro initiating drawings" float so they are always on the screen?
At the moment, this is not possible. However, I would like to propose you two alternatives using Google Apps Script.
Creating a menu
You can create a menu in Google Docs, Sheets, Slides or Forms using Google Apps Script.
In order to do so, you must create an onOpen() function containing code for the creation of it. Each of the buttons can execute different scripts that you may choose.
If you would like to create a menu with two options (one for the first drawing, and one for the second one) you could use a code similar to the following:
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom menu')
.addItem('First item', 'yourFirstFunction')
.addItem('Second item', 'yourSecondFunction')
.addToUi();
}
Where yourFirstFunction and yourSecondFunction should be functions declared in your script. It would look like the following:
Creating a sidebar
Sidebars can display HTML content (including images) so they may be more akin to what you are trying to accomplish. In order to create them you will also need to use the onOpen() function to trigger its display:
function onOpen() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
Along with the declaration of the two functions, yourFirstFunction() and yourSecondFunction().
You will also need to create your HTML file. You can create via File>New>HTML File, and for this example we will call it "Page". It will hold the following contents:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<img src="https://drive.google.com/uc?export=view&id=1eUiSE5jLHWQiOTQISbdsTxsOLgoOvxLM" width="250" onclick="google.script.run.yourFirstFunction()"/>
<img src="https://drive.google.com/uc?export=view&id=1pZCYQH1ZkxxL3Uvw8q9aHvp7cc7pWMAS" width="250" onclick="google.script.run.yourSecondFunction()"/>
</body>
</html>
As you can see, it holds two images, each of one calling their respective Google Apps Script functions. This would look like the following (of course you can use the images you please and customise the sidebar as much as you want using HTML5 and CSS3):
References
Custom Menus in G Suite
Custom sidebars

google-apps-script restric menu use/creation to users

I have a sheet with a google apps script that does various things. One of those things is building a menu on open.
I would like this menu to be built only when a certain specific user is acessing the sheet. How can I achieve this?
You cannot have restrictions inside onOpen since the user's email address is not available in any context that allows a script to run without that user's authorization. You can however include checks in the function that runs on clicking menu items.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Menu')
.addItem('First item', 'doSomething')
.addToUi();
}
function doSomething() {
if (Session.getActiveUser().getEmail() !== "abc#example.com") return;
// else do something here
}

Google Sheets: Link Add-on Help item with function

I'm publishing and Add-on for Google Sheets.
This is the function that creates the menu under the Add-ons tab:
function onOpen(e) {
SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi();
}
It is working OK and it also creates a Help menu item.
Now, I have a help function but how do I link it to this item?
For this you can simply add the help function code in a function named "showSidebar" as it mentions in this page.
If this not what you wanted to implement, add it in comments or edit your question.