How to make Custom Apps Script Function trigger on edit? - google-apps-script

So I created a Custom Google Apps Script project, and then deployed it as an add-on (for my domain only). However I soon realized that I need to change the trigger to be only onEdit. However that option isn't there for this Apps Script project. How do I get around this? In the picture there is only time-based trigger and calendar trigger. While for a script that's not deployed as an add-on there is an option to make it trigger onEdit

By using the "add trigger" button on the project triggers page we could only create on edit triggers for Bound to spreadsheet (Google Sheets) projects.
The alternative is to create the trigger by using code. For details please checkout https://developers.google.com/apps-script/guides/triggers/installable
it's worthy to note that the documentation in https://developers.google.com/apps-script/overview on the Types of Scripts lists three
Standalone
Bound to G Suited Documents
Web Apps
In this case the screenshot that is included in the question corresponds to a standalone project.

Related

How can I "publish" an editor add-on to be used by just me?

I'm involved in a standards development group where the meeting invitations are sent out, not as calendar invites, but as Word docs. I've written a script that allows me to take the Word doc uploaded to Google Drive, and run the script to create an entry on a Google calendar that I maintain.
At the moment, the script is unpublished, and to run it on the docs I use the "Test as Add-On" function from the script editor. That's a bit cumbersome because I have to go through the selection dialog to select the doc I want to run it on. Additionally, in the new scripts editor, it won't be possible to delete old tests and so my test dialog will end up getting filled up with every run of the script I've ever carried out.
It would be much better if I could have the script available to me, and me only, from the Extensions->Add-Ons menu when I open a doc. The script is not suitable for general publication: it works only on docs with the exact format of these meeting notices, so it should only be run by someone who knows what they're doing, and it accesses a calendar that I own, so I don't want to make it public. But I would like to be able to put it into some kind of "deployed" state so that, as I say, when I open any doc when I'm logged in as me, the script is just available directly from the Add-Ons menu.
Is there any way to achieve this? Or is there an alternative approach possible to this general problem, of having a script that you want to run on multiple documents that you own without having to formally publish it?
One option to "test as add-on" without publishing the script as an editor add-on, is to use installable triggers instead of simple triggers. This triggers should be created by using code in order to avoid having to add any code to each spreadsheet.
If you are determined to take the publishing path you can set your add-on listing to be "unlisted", this means that the add-on will not appear on the Google Workspace Marketplace, people will need to know the installation URL to be able to install the add-on.
Other options:
If you have a Google Workspace account you could publish the add-on privately for your domain. If you don't have this account type or if this is "not good enough", you might make your Google Apps Script add-on publicly installable but limit its core features to be used only by you by making use of a dynamic custom menu. For details please checkout Editor add-on authorization
The dynamic custom menu might based on a setting depending on the user email address. To make this work, as the Session.getActiveUser() requires authorization to run, it can't be executed by simple triggers, but you might implement a workflow to save it or a flag, i.e. add an option to the custom menu to initialize the editor add-on.
Another option is to make the dynamic custom menu based on the spreadsheet structure and/or content.
References
Installable Triggers
Enable and configure the Google Workspace Marketplace SDK
Related
Google Apps Script: Dynamically creating spreadsheet menu items
Publish an add-on privately

Triger a function on a change in doc

I want to run a Google App Script function every time a change is made to my Google Doc. I have found onChange and onEdit triggers which are only available for Google Spreadsheet. I am sure there must be an edit/change trigger for Google Doc as well.
Unfortunately there are no onEdit(), onChange() triggers for Google Docs.
The triggers available for Google Docs are the following:
onOpen() triggers both simple and installable;
time driven triggers;
onInstall() simple triggers.
What you can do instead is to use a time driven trigger so in this way even though the changes are not directly triggering the execution, the trigger will still run how often you want it to.
Last but not least, you can file a Feature Request on Issue Tracker where you specify the details needed.
Reference
Apps Script Trigger;
Google Issue Tracker.

Standalone Script for Google Sheets

I am looking to locally use a Google Apps Script with one of my Google Sheets (Attached to a form) that will be accessible to all emails that have permission to access the sheet, but I don't plan on publishing this script to the public. Do I still need to publish in order to use this script, or will I be fine just adding an onSubmit function for my Google Form as a trigger for my script to run in my Google Sheet? And can I use installable triggers if I plan on operating this script as a standalone only attached to this one Google Sheet?
Do I still need to publish in order to use this script
No: If you own the spreadsheet (or have editor permissions on it), your script can remain private. A script does not need to be bound to a Spreadsheet except if it will use:
Spreadsheet UI (getUi(), custom menus, dialogs and sidebars)
"Active" items (getActiveSpreadsheet(), getActiveSheet(), getActiveRange(), getActiveCell())
...will I be fine just adding an onSubmit function for my Google form as a trigger for my script to run in my Google Sheet?
Yes: Your standalone script can have installable triggers associated with Google Apps items such as spreadsheets, documents and forms.
And can I use installable triggers if I plan on operating this script as a standalone only attached to this one Google Sheet?
Yes: There's nothing special about the 1:1 relationship; the same trigger could be installed for multiple spreadsheets.

Sharing a spreadsheet with script triggers

I've created a stock watch tool that I'd like to share with the public. It consists of an editable Google spreadsheet with an associated function that is designed to run on a regular schedule as a trigger to perform the notifications when applicable.
How can I deploy this so that it's easiest for novice users to take advantage of the tool? Do I have to explain to users how to set up their own triggers?
Or is there a way I can bundle it all together, even with the triggers such that all the user has to do is fill out the spreadsheet? Or does deploying as a webapp apply here at all?
The new add-ons for GAS don't support installable triggers yet ("But keep the restrictions in mind"), but the old sheets still do (well mine still work). But I can't imagine you can deploy anything via the old script gallery anymore.
So I'd update your script to programmatically create the triggers onInstall() and create a demo version of an old spreadsheet that users could take a copy of. Then keep an eye on installable triggers coming to add-ons when you can deploy it as one of them.

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