Script Manager Not Showing in Google Spreadsheet - google-apps-script

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.

Related

Hide Apps Script code from editor of a Google Sheet

I hope I'm not again asking a question that's been answered, kindly bear with me if I am doing so.
I basically have been building a Google Sheet to be used by a number of users. I created a number of Script files and everything is working as expected, however editors of the Sheet will be able to see the source code from Script Editor menu option on Sheets.
There is a lot of sensitive information in the scripts that should be hidden from the editing users, how can I restrict them from editing the scripts and also not viewing sensitive information?
I found this answer which suggested publishing the Apps Script project as an Add-on. Unfortunately I am having a tough time comprehending how to go about it (my manifest hasn't been updated) and there are a few more answers that use different approaches.
Note: If there is an easy example showing how to go about Publishing then kindly share as I am struggling with Google's documentation.
My appsscript.json file:
{
"timeZone": "Africa/Maputo",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
Note2: The scripts' entry point is via an installable trigger On editing the spreadsheet
Note3: When switching to Legacy mode and Publish->Deploy from Manifest as in the examples from Google's documentation, I cannot see an option to install the addon - No entry points is indicated.
What would be the best way to achieve this security feature given I have multiple .gs files in my project and want the editors of the Spreadsheet not to have even viewing access of the source code.
Will greatly appreciate any feedback, Thanks.
The behavior you are experiencing seems to be due to the fact that the script is bound to the sheet in question.
A bound script is nothing but a script which was directly created from the spreadsheet itself rather than separately.
What you can do in this situation
Create a new Apps Script project which is not bound to any spreadsheet. A simple script.new in the browser bar will do the trick.
Copy and paste the entire code to the new script.
Replace any instructions similar to:
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Or whichever Apps Script service you are using
To:
SpreadsheetApp.openById("SPREADSHEET_ID").getActiveSheet();
// Or whichever Apps Script service you are using
Since the new script won't be attached to the spreadsheet, you will have to specify exactly which spreadsheet you want to open and work on, hence the use of openById method.
Reinstall the trigger/s
You can install the triggers programmatically too! For your use-case, if you want to use an onEdit, just create the following function. This ends up creating an onEdit trigger and it attaches it to the function you want it to act as the trigger.
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.openById('SPREADSHEET_ID');
ScriptApp.newTrigger('TRIGGER_FUNCTION_NAME')
.forSpreadsheet(ss)
.onEdit()
.create();
}
Remove any code from the old script which was attached to the spreadsheet.
Voilà!
Reference
Container-bound Scripts.
Each Editor has access to all shared codes.
Check this website
To hide code for editors, you can only obfuscate the code.
You cannot protect the code nor any credentials unless you publish the code as an add-on.

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.

When Google document is copied with container-bound script, how to get its installable triggers?

Issue
I have a container-bound script attached to my document.
It has onOpen installable trigger and I need to get the script in newly created documents.
The most effective way I found is to create the new documents by copying from the original document.
The problem is, that the triggers are not copied with it.
Conditions:
Simple triggers are not enough, I need to use action requiring authorization
I don't want to force my users to create manually trigger for each document
I don't mind if the installable trigger is created after clicking button from the programmatically created menu
My thoughts:
I wanted to create it programmatically – there is a problem with the testing environment. I am getting an error:
The add-on attempted an action that is not permitted in Test as add-on
mode. To use this action, you must deploy the add-on.
as I understood, I need to release the project to the store to use this, which I don't want to do.
I don't mind release as an add-on, but the google script IDE only offers to release as web Docs web add-on.
Code:
function onOpen(e){
DocumentApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('Custom menu')
.addItem('Open sidebar automatically', 'createTrigger')
.addToUi();
}
function createTrigger() {
var doc = DocumentApp.getActiveDocument();
ScriptApp.newTrigger('onOpenReal')
.forDocument(doc)
.onOpen()
.create();
}
function onOpenReal(e){
...something requiring authorization...
}
I figured the answer partially:
I think it is not possible to copy the document together with its triggers, but I didn't find any official documentation about that.
But the error:
The add-on attempted an action that is not permitted in Test as add-on
mode. To use this action, you must deploy the add-on.
in the seemingly regular environment was caused by saved test configuration in the Google apps script editor.
To get rid of the error you need to first delete all test configurations in "Test as add-on" option. Until that the system sees all launches as "test" even if opened as a regular user.

google sheets scripts not updating for all users

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.

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...