I'm getting this error when I try to run my code to had a custom menu. My code:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Menu')
.addItem('Candles','callCandles')
.addToUi();
}
You are not supposed to manually execute a trigger function. This particular one is activated when you open the spreadsheet.
You can see the menu Candles created on top of the menu bar. You just need to refresh the spreadsheet page and you will see it. Please read carefully the official documentation:
https://developers.google.com/apps-script/guides/triggers
When running the onOpen trigger or any function calling Class Ui from the Google Apps Script editor, it will fail if the script was opened from a link, from https://script.google.com, etc. but not from the container even if the container was opened later. It might also happen when somehow the document editor UI and the Google Apps Script Editor lost the connection between the active document, form, slide or spreadsheet and the script.
Usually this is fixed by closing the script and open it again from its container. As of September 2022, this is done from the Extensions menu.
Very rarely it might be needed to restart the web browser, and even more rarely it will be required to restart the computer.
Related
I need help. I'm trying to make a backup of my worksheet that makes it take the data from the worksheet and put it in another worksheet but it's giving this error!
SpreadsheetApp.getUi() can't be used in stand alone projects ejectuted from the script editor, it only works when the stand alone project is used as a Google Editor Add-On.
I know that your script is an stand alone project because the screenshot shows the share button.
In order to be able to use SpreadsheetApp.getUi() you should,
test it as Editor Add-on
use it as Editor Add-on
use it from a script contained in a spreadsheet
Related
"SpreadsheetApp.getUi() cannot be called from this context"
Google Script Cannot call FormApp.getUi() from this context
Exception: Cannot call SpreadsheetApp.getUi() from this context. (line 2, file "Code")
I would like to read the Active spreadsheet and add menu items based on that. Below is the code in OnOpen(e) trigger:
function onOpen(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
//add menus based on values of sheet
}
I am getting below exception:
[Exception: You do not have permission to perform that action.]
I have gone through the documentation and found that the AuthMode is None in OnOpen trigger which does not allow access to any services that require authorization.
Please suggest if there is any other way to accomplish my task.
Thanks.
It's not possible to access the spreadsheet unless the owner or editor of the spreadsheet uses the add-on in that spreadsheet. As written in the official document,
However, because an editor add-on automatically runs its onOpen(e) function to add menu items when a document opens, the behavior above adds some complexity to Apps Script's authorization rules. After all, users wouldn't be comfortable with an add-on accessing personal data every time they open a document.
The add-on needs to be enabled for the document for the addon to get access to the document. When it's enabled, onOpen() runs in AuthMode.LIMITED, where you get access to the bound document. This "enabled" state is caused by
Getting an add-on from the store while using that document, or
Using a previously installed add-on in that document
In all other cases, add on runs in AuthMode.NONE. In this mode, You are only able to add menu items without access to any data including the bound spreadsheet. Add Menu items to request access to such data. Then, after getting AuthMode.FULL, the rest of the workflow can be done.
References:
The complete lifecycle
I recently created and published a Google-Docs add-on, but have run into an issue when trying to download and use it with another account.
The Issue:
When I first install it in a document, the menu items show up and everything is working fine.
However, when I open up a new document, and try to use the menu, all the items have disappeared and I'm left with "Help." (see screenshot)
The only way to fix this issue is to click Add-ons --> Manage Add-ons --> click use in document, and then reload the page. (screenshots)
However, I would like to set the add-on to be used in every document by default, so that users won't have to go through this process and reload every time they create a new document. (I am also open to any other solution that allows the menu items to be shown)
Minimal Reproducible Example:
This is the code I use to create the menu upon installing or opening.
function onInstall(e) {
onOpen(e);
}
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Function 1', 'functionOne')
.addSeparator()
.addItem('Function 2','functionTwo')
.addItem('Function 3', 'functionThree')
.addToUi();
}
The logs show the following error:
You do not have permission to call getUserProperties.
Some relevant info: whenever the script runs, it checks whether the user has saved any settings (which would be in user properties), and if there are no settings, it sets them to default. Is it possible that I cannot access user properties from new documents?
Let me know if I should provide any other information, such as the name of the add-on
You should review Editor add-on authorization. Briefly, add-ons run on limited authorization mode when they are not enabled for the active document (form, spreadsheet, presentation). To check the authorization mode use authMode property of the open event object before calling any method that requires full authorization mode to run.
Related
Can I test DocumentProperties in Google Apps Scripts? (read this first)
Google Apps Script - AuthMode - no menus are visible after installation
add-on with LIMITED auth cannot open sidebar
How I can start a HtmlService directly on file start (onOpen)?
Trouble understanding authmodes
I have a google sheet that has custom scripts in it. It is used as a template and is replicated for each project we have. In order to have the sheet be updated in the future with new scripts, I created a "Library" document and published the scripts as a library.
Everything works fine for me the owner of the document. If I update the library script sheet document it shows up correctly on an older sheet document. However other people in the company do not see the update.
Here is the script in the normal document. (I've removed other functions not relevant to the issue)
// all functions can be found in Customer_notes_Library spreadsheet https://docs.google.com/spreadsheets/d/*********
function onOpen() {
CustomerLibrary.onOpen();
}
Here is the script in the Library Template
devmode:false
// This will run when sheet is opened and add a drop down menu for us to add custom global functions. We have added a Sonicwall Setup Menu option
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Realm Custom Scripts')
.addItem('DHCP Bash Script', 'runBuildDHCPBashScript')
.addItem('IP Adress Input', 'runBuildDHCPIpAdressInput')
.addItem('Wire Device Input', 'runBuildDHCPIpAdressInput')
.addItem('Client Music Request', 'runClientMusicFormRequest')
.addToUi();
}
These drop-down menus are not updating for other users but is updating for me the owner.
I believe the issue is the onOpen function and there needs to be some way for the user to reauthorize access to the sheet that has the library in it after the scripts in the library have been updated.
I see someone had a similar issue here. But I'm unable to follow the solution.
https://productforums.google.com/forum/#!topic/docs/BJd8zRYD1qU;context-place=forum/docs
I also read that setting devmode to false might be helpful, not sure if I am doing that correctly or not.
Any help and if need further detail please let me know.
I've created a number of email scripts for our company's internal usage. Usually I create the script, save and then when I open the spreadsheet I can go to the Tools > Script Manager and run the script.
It is not showing up - only Script Editor and Script Gallery.
The script is not a "stand alone" script - I created it from the spreadsheet's Script Editor.
Is this something that has been disabled in the new Google spreadsheets? Or is there something else I need to code in the script to have it work?
Thank you.
As others have pointed out, it's been removed, but it's pretty easy to create a custom menu to call your method from:
Let's say you have a method myCustomMethod that you want to run from Google Sheets. Open the Script Editor and add the following code:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Run custom method', 'myCustomMethod')
.addToUi();
}
Reload your sheet and you should see a new top-level menu next to Help, called "Custom Menu".
If you are using the new version of Sheets then the "Script Manager" is no longer a feature now as the New Sheets uses "Add-ons". You may find the resource on the link:
https://support.google.com/docs/answer/2942256?hl=en
I don't remember a 'Script Manager' setting, but I always run it from the script itself, so maybe I just wasn't looking. It has apparently been removed for some time so you may instead need to consider creating a custom menu to run any function of the script.
Edit: I had a dig about an old version of sheets, and I could confirm this was in the old version:
But has been removed from the new version of sheets.