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.
Related
Iv'e written a custom google sheet function using Google Apps Script, and i would like to share it with another spreadsheet user, but i do not want to share the functions source code.
I've converted the Google Apps Script to a project, but now i have no idea how to link the project back to my sheet, so the function will work again.
I also do not want to publish the AddIn to the marketplace.
Test As Addin also doesn't work, the sheet is opened in a new tab, but the cells with the custom function says #NAME?.
What am i missing?
Explanation / issues:
The error #NAME? indicates that you are trying to use a function
that does not exist. As you also mentioned, this function does not
belong to the active spreadsheet but on a different project.
Unfortunately, it is not possible to share a custom function with other spreadsheets directly.
Possible Workarounds:
You can create an add-on.
Another workaround solution would be to create a library. That is a great alternative and the documentation is quite straightforward.
Related:
Creating add ons in Google Apps Script
How to call library function as custom function from spreadsheet formula?
I have been searching for a solution to a Sheet Protection issue, I have a Sheet that includes automated scripts to write data into the Sheet below from some fields at the top. I want the user to be able to provide the data in the fields at the top and then to run a script that adds the data below. The script to add the data works fine for the owner of the sheet if Protection is enabled, but fails for any user that has Edit rights as the Protection cannot be cleared by script for the data to be written to the bottom of the sheet.
https://developers.googleblog.com/2015/02/control-protected-ranges-and-sheets-in.html
I found this code from a Google blog post a few years ago and have tried variations without success....is this type of approach just not possible with Google Sheets and Google Script ?
Unfortunately, that is not currently possible to run a bound script as the owner without using a workaround. Scripts can only be run as the owner when they use Triggers or when you make a standalone Script Web App. You can see this for more information.
This should bypass fooling around with scripting protection ranges!
The Workaround!
There is a way you can get around this by creating a web app so that your bound script talks to the web app which runs on the spreadsheet. See this answer for more information.
So the process looks like this: User clicks a button that runs a function on a bound script. This function makes a web call to a web app that can run a function as the person who created the script.
I would also recommend you pay attention to a comment by Augustine C:
...you may also find it helpful to have a shared secret key saved in your spreadsheet and then verify it using the backend webapp script, or to perhaps verify that the recipient of the email is, in fact, also an editor of your Google Sheet.
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...
I have a system (I'm the only user of this system, and that's not expected to change, ever) in which spreadsheets are copied from a template file. This template has a script, which populates a few menus to perform some operations. So each new copy of the template spreadsheet has its own copy of the script. The problem with this is that every time a new spreadsheet is used, the user (me) has to authorize the execution of the script. This didn't use to be that bad, but the authorization process has recently become way more annoying (see for example https://developers.google.com/apps-script/images/unverified-app-ui.gif). Given that I'm the only user of these spreadsheets, I think this process is unnecessary and wish to get around it.
I have tried to get around this by extracting the code into a standalone script file and publishing as an add on (https://developers.google.com/apps-script/quickstart/docs) but actually publishing it requires me to pay 5 dollars, and I think this is ridiculous given that I am the only user.
Any other ideas?
Thanks in advance.
AFAIK, that's the intended behavior. Check guide to the authorization lifecycle for add-ons.
Add-on automatically runs its onOpen(e) function to add menu items when a document opens — but to protect users' data, Apps Script restricts what the onOpen(e) function can do.
Note that only published add-ons can be in AuthMode.NONE.
The concept of authorization modes applies to all Apps Script executions.
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.