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
Related
Is it possible to add an html element directly into google sheets cells using google apps script? I have a tasks list in my minitask.html that I would like to add into my google sheet. I have a large merged cell where I would like to add the element (B39:I71). I haven't been able to find a way to add it directly into my merged cell.
The best solution I have found that gets the task list somewhat on to my sheet is by adding a sidebar of the element.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Tasks')
.addItem('Mini Tasks', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('minitask')
.setTitle('Mini Tasks');
SpreadsheetApp.getUi()
.showSidebar(html);
}
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
}
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()" />
I keep getting the above error message after using code supplied by Google for creating custom menus in Google Docs. The code can be found under the top heading titled "Custom menus in Google Docs, Sheets, or Forms" at the following link. The left hand side of the column says that the code is for "Google Docs, Forms, or new Sheets," but it doesn't work in Google Docs.
Had the same problem.
Got error: API-method 'getUi' is only available in new version of Google Spreadsheets.
In a document be sure to use DocumentApp everywhere.
Also in the addSubMenu.
Try this:
function onOpen() {
DocumentApp.getUi()
.createMenu('My Menu')
.addItem('My Menu Item', 'myFunction')
.addSeparator()
.addSubMenu(DocumentApp.getUi().createMenu('My Submenu')
.addItem('One Submenu Item', 'mySecondFunction')
.addItem('Another Submenu Item', 'myThirdFunction'))
.addToUi();
}
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.