Triger a function on a change in doc - google-apps-script

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.

Related

Is it possible to create at onEdit/onChange installable trigger for Google Docs in App Script

Goal: Create a installable trigger in App Script for Google Docs which fires when the eventType onEdit is done. (So the trigger listens for the onEdit event and when triggered it calls another function which does X when the user deletes or adds a paragraph)
Background: Google Sheets has an onEdit simple trigger, and the eventType on_edit. The eventType onEdit only supports Google Sheets. Regarding Google Docs the EventTypes and triggerBuilder methods for Google Docs (according to the docs) are only onOpen and Create.
I'm fairly new to App Script so I'm hoping someone knows a hack to get around the fact that the triggerBuilder only supports two methods.
The only available trigger for Google Docs is onOpen().
A solution would be to monitor if the last changed timestamp changes with a trigger for instance like so
DriveApp.getFileById('gdoc id').getLastUpdated()

Installable Trigger or Manifest Trigger for Google Sheets AddOn Making BigQuery Request

I am trying to use Google Big Query in my App Script for a Google Workspace AddOn I have created. In my dev environment this all works fine as a separate function with a static query, however I need to make the queries dynamic based on values users set in particular cells of a particular sheet. To do this I think I need to use onEdit() type functionality.
However the documentation makes clear that I can’t use onEdit to make such requests that require authorisation so I need to use a trigger and this is where I am getting confused.
Looking at the docs for installable triggers this seems to be the way to go. However at the bottom re triggers in add-ons it says I need to follow a different method and points me to Manifest Triggers. However, at the bottom of Manifest Triggers section it then says In addition to manifest triggers, Google Workspace Add-ons can also use Apps Script installable triggers which takes me back to where I came from.
So do I need to use Manifest Triggers for an Add On or can I use an installable trigger via App Script? Or can I use either but certain use cases would suggest one over the other?

How to make Custom Apps Script Function trigger on edit?

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.

onFormSubmit trigger removes when update G-Suite add-on

I want create a standalone add-on for google form which should handle form-submit event with creation a new text file in google drive with content of the form response. And then distrubute this add-on for other users.
My goal is user can install the add-on on google form and it just should work, without additional setup from add-on menu.
While testing I figured out that there are Simple and Installable triggers are available. (onFormSubmit is an installable trigger)
Installable triggers clear each time when add-on is updated on G-Suite Marketplace. Therefore it removes my onFormSubmit trigger, and because of that the installed add-on stops working after update.
onFormSubmit trigger is added inside onInstall Simple trigger this way:
function onInstall() {
var form = FormApp.getActiveForm();
var trigger = ScriptApp.newTrigger('respondToFormSubmit')
.forForm(form)
.onFormSubmit()
.create();
}
I thought to bypass this issue by handling onFormSubmit inside Simple trigger onEdit of the linked spreadsheet. Actually it could check a spreadsheet for new rows and then create a new file. But Simple triggers are without autorization to private services (like google drive), so it's not possible to create a file on google drive inside a simple trigger.
Is there way to keep onFormSubmit trigger after publish a new version of add-on, without its manual re-install?
Well, these all reasonable, but then why onInstall event is not raised again? Or may be is there an event onUpdate to handle add-on update?
UPDATE
Here I read:
Add-on triggers will stop firing in any of the following situations:
If the add-on is uninstalled by the user
If the add-on is disabled in a document (if it is re-enabled, the trigger will become operational again)
If the developer unpublishes the add-on or submits a broken version to the add-on store
I just set next version number in the publish form, click "Update web store draft", and confirm update in new window. Am I doing everything right?
The trigger stops firing even without code modifications, just with new version of add-on. And there are no any log errors in stackdriver console. Could it be a bug?

Force auto-submit a Google Form using app-script?

I am using a code which closes the FORM at specified time. So what happened is some students are failing to submit it before the time limit and their responses are not being recorded.
Is there any way to force auto-submit a google form at specified end-time for all the users who are writing the quiz lets say!
Any ideas are welcome.
You can check this documentation about Installable Triggers which let Apps Script run a function automatically when a certain event occurs. It is similar to simple triggers for Google Apps like onOpen(), but they can respond to additional events, and they behave differently.
There are several installable triggers for Google Apps and you can use an installable form-submit trigger that runs when a user responds to a form.
Be noted that there are two versions of the form-submit trigger, one for Google Forms itself and one for Sheets if the form submits to a spreadsheet.