Cannot call SpreadsheetApp.getUi from this context - google-apps-script

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

Related

Running a Macro through multiple Google Sheets without Copying & Pasting the Script

I am very new to Google Apps Script. I created a Macro in Excel with VBA. My process is downloading a CSV that I save to my hard drive, opening it (my PERSONAL workbook is open at all times) and then running my macro which will format the list/perform the steps of my macro. I would then save and manually upload that list back to Google Drive.
This is obviously inefficient and The rest of my team uses Google Drive/ Google Sheets and so I am attempting to teach myself how to build out the same macro in Google Apps Script. I understand the difference between bound and unbound scripts - this is similar to Excel in the sense that if I wanted to run my macro on multiple sheets, I would have to save it to my PERSONAL spreadsheet where I stored all of my macros and then have that sheet open at the same time that I ran the macro on a different spreadsheet.
However, for Google Apps Script, it appears as though you simply cannot run a macro through multiple different workbooks without manually copying and pasting your code to every new spreadsheet. Is this true? I see that it could be added in the app store and published as an add-on but my script is painfully basic, I'm just changing some words and formatting. It seems odd that this cannot be applied to multiple spreadsheets the way that it can in Excel. My goal was to use something like:
function onOpen(e) {
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('List Upload')
.addItem('Format List Upload', 'formatListUpload')
.addToUi();
where formatListUpload would run my formatting script so that my coworkers could just click "Format List Upload" as a menu option and the new spreadsheet they had just opened would be quickly formatted. This seems like base-level functionality for macros/ app script. Is there truly no way that this can be done?
Many, many thanks!
From the question
However, for Google Apps Script, it appears as though you simply cannot run a macro through multiple different workbooks without manually copying and pasting your code to every new spreadsheet. Is this true?
No
Is there truly no way that this can be done?
Instead of using a simple trigger you might use an installable trigger, but this option is limited to create 20 triggers by script / user. To make this work you will need somehow to open the spreadsheet i.e. SpreadsheetApp.openById(spreadsheetId)
Options to avoid having to create an add-on
One option that doesn't requires installable triggers is to submit the spreadsheet having the macro to the templates gallery and use the template to create the new spreadsheets
Another option is use script to create a new spreadsheet and the installable trigger for it.
One more option is to create a new spreadsheet, grab the spreadsheet id, either manually or programmatically, then use it to create the installable trigger for the new spreadsheet.

Exception: Cannot call SpreadsheetApp.getUi() from this context. (line 2, file "Code")

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.

Identifying the spreadsheet to use in Google Script Editor

I am trying to automate a google spreadsheet on google script editor. However, when I try to identify the spreadsheet and select as the one that we are working on, it is written in the documentation cited below (openById) that scripts that use this method require authorization with one or more of the following scopes:
--> https://www.googleapis.com/auth/spreadsheets.currentonly
--> https://www.googleapis.com/auth/spreadsheets
When I add these scopes, my function doesn't run. It only runs when those scopes are not added. Either way, I am met with the error msg that reads: "We are sorry, a server error occurred. Please wait a bit and try again."
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#openbyidid
I tried various methods include OpenByUrl and getActiveSpreadsheet...
function automatedInvoiceEmailing() {
var people = [];
// selecting the spreadsheet (without the bottom line, the function works just fine)
var ss = SpreadsheetApp.openById("1jdn3S1Iv2zDAqF6Hyy3fybKARZJYmg-LJVdUWJJS3LA");
}
Either way, I am met with the error msg that reads: "We are sorry, a server error occurred. Please wait a bit and try again."
I expected the sheet to have been selected
Edit: hmm, when I copied the google sheets and saved the new code, it runs properly!! :)
Since you are working with a bounded script (bound to the sheet you’re working with), the way to get a reference to the sheet is with var ss = SpreadsheetApp.getActiveSheet();, this will provide a reference to the bound document. You would use openById() to access another file.
As for scopes, they are added automatically when you run the script for the first time, it will ask for your permission to access your data, and when you accept, google adds them to the script project automatically.
Here are some quick-start examples on working with sheets and apps script

Apps script library automatic updation on client side (on doc refresh)?

I have
an Apps Script Library 'MyLib'.
template Google Spread Sheet ('MyGSSheet'). Through Script Editor I added the library 'MyLib' to 'MyGSSheet'. I set 'Development mode' 'on'.
users get a 'copy' of this template ('MyGSSheet').
How can I have a setup wherein any changes I make to 'MyLib' get picked up across these copies automatically (once the Spreadsheet is reloaded)?
I thought having 'Development mode' 'on' is all that's needed for this continuous update of the code in all the Spreadsheets.
However, I don't see this happening. The copies aren't picking the latest code.
I also granted 'edit' permission to all users within our company domain.
I am not able to comment so I hope I am contributing enough to justify an answer..
So I tried to reproduce this:
I created a standalone App Script 'MyLib' and wrote a single function:
function myFunction()
{
SpreadsheetApp.getUi().alert("TEST");
}
Next I created a spreadsheet and added a script to it via Tools. I'll call it "Spreadsheet Script".
In the Spreadsheet Script I added the MyLib as a library and turned the development mode 'on'.
Also I added two functions to Spreadsheet Script:
function onOpen(e)
{
myFunction();
}
function myFunction()
{
MyLib.myFunction();
}
Ok, now I shared the Spreadsheet and the MyLib - script to my colleague with edit rights. He opened the spreadsheets and got the alert "TEST".
Now when I modified the alert text from the MyLib-script's myFunction to "TEST 2 " and just saved the file (File/Save, did not save a new version), my colleague saw the changes in the spreadsheet. Then, I made him to take a copy of the spreadsheet( To test the effect of the spreadsheet owner change).
I changed the alert the text to "TEST 3". The change was reflected in his copy of the spreadsheet.
Are you able to reproduce this or does this approach fail to update for the other users? I am choosing this kind of simple example as sometimes the reason might reside in the code too...

Script Manager Not Showing in Google Spreadsheet

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.