Search code examples
google-apps-scriptgoogle-sheetstriggersgoogle-forms

onEdit() function does not triggered when change was made by automatic script


my onEdit() function calling to other function when there is a change in the sheet.

if the user makes the change all works fine. but if the change was made by google-form (the form fill some cells with the answers it gets) onEdit() does not trigger.

do I miss something?


Solution

  • Yes, you forgot to read the documentation for the simple triggers:

    onOpen(e) runs when a user opens a spreadsheet, document, or form that he or she has permission to edit.
    onEdit(e) runs when a user changes a value in a spreadsheet.
    onInstall(e) runs when a user installs an add-on.
    doGet(e) runs when a user visits a web app or a program sends an HTTP GET request to a web app.
    doPost(e) runs when a program sends an HTTP POST request to a web app.

    and installed triggers:

    Even though installable triggers offer more flexibility than simple triggers, they are still subject to several restrictions:

    • They do not run if a file is opened in read-only (view or comment) mode.

    • Script executions and API requests do not cause triggers to run. For example, calling FormResponse.submit() to submit a new form response does not cause the form's submit trigger to run.

    • Installable triggers always run under the account of the person who created them. For example, if you create an installable open trigger, it will run when your colleague opens the document (if your colleague has edit access), but it will run as your account. This means that if you create a trigger to send an email when a document is opened, the email will always be sent from your account, not necessarily the account that opened the document. However, you could create an installable trigger for each account, which would result in one email sent from each account.

    • A given account cannot see triggers installed from a second account, even though the first account can still activate those triggers.

    Consider what would happen if your onEdit(e) was activated by programmatic changes, such as if your onEdit function alters the spreadsheet values...

    In your situation, where you want form submission to activate your on edit function, You will need to install a form submission trigger (there is no simple trigger for form submissions).

    An example function to receive your form submission trigger:

    function giveMeAnInstalledFormSubmitTrigger(formSubmitEventObject) {
      if(!formSubmitEventObject) throw new Error("You called this from the Script Editor");
      var newEventObject = /* do something with the formSubmitEventObject */;
      // Call the on edit function explicitly
      onEdit(newEventObject);
    }
    

    You can read more about the event objects that triggered functions receive in the Apps Script documentation: https://developers.google.com/apps-script/guides/triggers/events