Apps Script persistent variable across all scripts? - google-apps-script

I am trying to get a variable to work in all instances of all scripts across my user account to prevent unwanted scripts in different documents running if the variable is set. I've managed to do this by creating a file on Google Drive but it's too slow really.
I know about PropertiesService but according to the documentation this seems to only be specific to the current script or document. Is there any fast way to create a persistent variable accessible from anywhere in the google account via apps script?

There are several options
Use a project to create installable triggers instead of simple triggers,
Use a library,
Create a Workspace add-on
Create an Editor Add-on
On any of the above use the Properties Service and getScriptProperties instead of copies of the same script.

Related

How can I set an app script on a Google Sheet via the API?

Is there a way to set an App Script on a new Google Sheet via the Google Sheets API? We use pygsheets as our Python API Client for Google Sheets. Since we are using the onSelectionChange trigger to populate certain cells with a date, we would like to setup the app script after we have populated rows in the spreadsheet.
The Google Sheets API doesn't have a method to do this. You can use the Apps Script API instead, but there are a few caveats which may or may not work with your use case:
You can use projects.create to create a new Apps Script project and attach it to a Sheets file by specifying the parentId field, which is the Sheet's ID. However, there are no methods to attach a preexisting script to a Sheet. You can still use projects.updateContent to add the required script files, though.
You cannot unbind the script once it has been attached.
This API does not work with service accounts, and the user running it needs to explicitly allow it in their settings page. This is for security reasons, since allowing a malicious app to freely write scripts on your account could be catastrophic, but it adds more steps that your users would need to take in order to run your app.

Create a macro in Google Sheets using Google App Script

I want to programmatically create a macro and call the same from another stand-alone app script project.
One of the approaches I have used is to create the project using the projects.create API and called the function in that project using scripts.run.
The problem with this approach is that I've to manually set the GCP project for the newly created project, as the script and calling application must share the same GCP project as mentioned here.
I couldn't find any resource to perform it programmatically.
Basically, I want to execute the App script code provided by the user dynamically from my existing Google Sheets add-on.
Any suggestion or help would be great.
Thanks.
Not sure I understand correctly, but lets assume you want to do the following:
Create a Function (your Macro) in a Apps Script Project
Use the Function in the above Project from multiple Google Spreadsheets
In that case you can do multiple things, my suggestions would be
Create a Apps Script Project
Enter your Function (your Macro) in that Project
Use that Project as a Library
Wherever (in a Spreadsheet) you need this Function (your Macro), add the Library and create a Function which calls the Library Function.
One alternative, if you really want a App which runs as Addon, then see here the reference for that.
Reference
Library

Publish as a private add-on to avoid granting permission on copies of gsheets

I have a similar question to both of the questions below. I have a script bound to a google sheets, and I use this sheet as template. However I'm looking for an option to not have to grant permission each time I copy the file.
From reading the answers in the questions below, I understand I have to publish a standalone script as an add-on.
However, reading this answer, I see that I need to create a Cloud Platform Dashboard and all bunch of stuff which looks pretty messy to me, such as google reviewing process. Again, it is only for personal use...
Is there a way I can privately publish it as an add-on, without having to go through all the process?
Thank you
What is the best way to create Container-bound Scripts that can be cloned?
Grant permissions on open for first time for a bound script in Google Sheets
If you don't want spreadsheet hook triggers like onEdit or button or anything else, You can use a standalone script.
A standalone script can be written, which loops through your spreadsheets doing what's needed based on a time trigger.
Adding to the already existing answer
Publishing a private add-on does not require going through the Google Review process, especially since it is for personal use only.
Therefore, the situations below do not require verification:
If you want to deploy the add-on solely for internal use which means that the add on will be used only by people in your Google Workspace or Cloud Identity organization.
If you want to use the add-on domain wide which means that the add on will be used only by Google Workspace enterprise users within the domain.
For the whole list of exceptions from the verification process, you can check this here.
Reference
OAuth API verification FAQs.

How can I Find All Uses of a Google Script Library?

I have a Google Script library that is used by at least 100 other scripts (some that are bound to spreadsheets/documents, some that are not). How can I find all of these client scripts that reference my library script?
More specifically, I need to be able to add a new feature into the library that requires new permissions that I (the user) must grant. The client scripts won't run if I just add this feature to the library without granting the permissions to each of the client scripts. So ultimately, I need to give this new permission to each of the clients. And if I knew what scripts were actually using this library, I could do that manually for each one. But I need to URL's or ID's or something for each of those scripts.
Answer:
Unfortunately this is not possible to do.
More Information
It is possible to get a list of standalone Scripts from your Drive, though scripts bound to a file can not be searched for using regular searching methods.
It is possible, using the help of this Google Account page to get a list of all the Apps that have access to your account, though only files you have authorised will appear here, and apps which are not just those created by you in Apps Script will appear there (for example, other add-ons or even Android Apps bound to your account appear here).
A Partial Workaround:
Using Google Apps Script, you can list all Apps Script Projects that you own with help of the MimeType enumeration GOOGLE_APPS_SCRIPT
var scripts = DriveApp.getFilesByType(MimeType.GOOGLE_APPS_SCRIPT);
var arr =[ ];
while (scripts.hasNext()) {
var script = scripts.next();
arr.push(script)
}
Logger.log(arr);
Or even just searching for type:script in Drive, however this only returns a list of scripts that are not bound to a file.
You can then use regular Google Drive search terms to find which of these files contain, for example, a unique method name that the library uses. I am aware this isn't a perfect solution and you would still have to look for projects bound to a file using the above webpage.
Feature Request:
It appears that back in 2014 a feature request for this was made on Google's Issue Tracker, though I would suggest creating another feature request for this here as it was marked as a duplicate of another issue. You can make a feature request under the correct Apps Script component here.
References:
Google Apps Script - Enum MimeType
Google Drive Search Query Terms
Apps with access to your account
Google's Issue Tracker
Feature Request: Listing and searching for container bound scripts
Create an Apps Script Feature Request

How to make set of scripts available for all my Google Spreadsheets?

Excel supports the concept of a PERSONAL.XLS file which contains my personal scripts, which I want to be able to use on any spreadsheet/workbook that I open.
How do I do similar in Google Docs, i.e. have a set of scripts which, regardless of which Google Spreadsheet I open, will cause the onOpen event to be run and will thus add my extra menu?
You cannot do that in Google Spreadsheets. However the best practice is to
Write a standalone script with your onOpen and other functions.
Publish this as a library
Write a shell onOpen in each of the spreadsheets that you create
manually. :( This shell function will call the library's onOpen.
However, if you create a copy of a spreadsheet that has an associated script, then the copy will also have the script in it.
1) You can open your Script and from the Publish menu select "Test as add-on".
2) Now under "Configure New Test" you can select a document and then run the add-on for that doc.
Unfortunately you have to do this for each document one at a time. I would really love it if there was a way to tell a script to be available for all my spreadsheets, or at least have an easy way to install a personal add-on on a per-sheet basis just like you can install an add-on from the marketplace, kinda like a personal marketplace.
Although not recommended, You can also copy set of scripts to all other google spreadsheets programmatically using AppsScriptApi