How to protect the Apps Script code in a Google spreadsheet?
I have written some code for my Google spreadsheet with the script editor.
I want to share this spreadsheet with my clients but I don't want to share the code I have written.
This code adds a menu to my spreadsheet which contains useful functions that should work/run when my clients open this spreadsheet with condition applied that: they shouldn't be able to see its code.
This is easy with Excel, but with Google spreadsheet I don't know. . I have searched a lot on Google but they all gives the idea about how to share a spreadsheet. For example in "View" mode, but in that case there is a problem: my menu function, which adds a menu, is "onOpen" and doesn't start when my clients open it.
How to implement this in Google Spreadsheet?
Solution 1:
Short Answer
Publish your script as an editor add-on or as a Google Workspace add-on. Bear in mind that you could make it private, by selecting unlisted or making it available only for your G Suite / Google Workspace organization.
Explanation
Add-ons were added on 2014. This is better than using a library because there will be nothing shared from your add-on.
Please note that
- it's not possible to access Google Apps Script server-side code by using Chrome Developer Tools or another similar software.
- there are some restrictions for add-ons, study them carefully as maybe this alternative will not work for you.
- nowadays add-ons require a Google Cloud Project and an oAuth consent screen. If the add-on will be shared with external users it will requires an oAuth verification and an app review.
If publishing as add-on is not an option for you, you should rethink your solution considering the use of a web application, using the Apps Script API or the Google Sheets API.
References
- Get code from my google spreadsheet addon after publishing
- Google Apps Script Add-On Source Code/Security
- Build add-ons for Google Docs and Sheets
- Develop Add-ons for Google Sheets, Docs, Slides, Forms, and Gmail
- Developing Add-ons for G Suite G+ Community
Solution 2:
make use of Library the documentation explains how to use it and there are a few interesting post on the subject as well
Solution 3:
Make a special library file containing only the script for your client. Your client must have at least a read-level access. So he is able to see your script. Remove your script and make a new innocent script in that file and save this as a new version. Now your client sees only this new script. Because his application is still working on the old version of your library, the original script will do his job as usual.
Solution 4:
- Create 2 libraries; one is dummy (I call it Lib1) and another core library that has all the secret codes (I call it Lib2).
- Add Lib2 as a library to Lib1 and use in it the necessary methods that are fully implemented in Lib2.
- Share both libraries publicly and set their permission to Viewer.
- From the sharing setting page of Lib1, there is a gear symbol. make sure the following option is unchecked "Viewers and commenters can see the option to download, print, and copy"
- Add Lib1 to your clients' script (maybe a Spreadsheet script).
- Share the script of your client as editor or viewer.
If the client could get Lib1 ID by accessing it from Resources/Libraries menu of his script, he won't be able to access the Resources/Libraries menu of Lib1. He also cannot make another copy of Lib1 because of the unchecked option done in step 4, preventing him from possibly seeing the ID of Lib2.