Search code examples
google-apps-scripttriggersgoogle-forms

Google Form Submit trigger activates multiple times on submit


I have a script that runs perfectly fine on its own. When I add a trigger from filling out a form, however, it triggers up to five times in a row!

The code is too long to copy here but the script:

  • Takes the input of a form
  • Populates some other fields on another page with the last row of the contact page
  • Crawls a master drive and makes a list of documents
  • Creates a new folder
  • Copies the files from the list into a new structure in the new folder
  • Emails the person who filled out the form
  • Passes that person ownership of the folder

I've toyed with Locks (getScriptLock) which sort of works some of the time b a) I don't really understand it and b) it still manages to break free and do second run sometimes. This is cluttering up my drive and my inbox with errors from running into the lock and folders from triggering extra around the lock. I tried having a hardcoded spot on the spreadsheet that gets filled the first time that row on the contact sheet is run through the script and theoretically chokes it off and prevents it from running again... but that doesn't work either.

Has anyone heard of a script triggering multiple times from a form input?


Solution

  • Ok, doing further research this behavior has been seen in google forms > sheets since at least 2015. It seems to relate to server load/time of day--after business hours it only occurs 1 in 10 times, but during them it is something like 60%.

    Locks didn't really work, although I do have a user lock on one portion of the script.

    What I have done is writing to the sheet at different times and checking whether those writes have taken place. Just one isn't enough to stop the behavior, I am throwing errors and terminating extra scripts in four different places and according to the logs they are all being tripped when it gets bad (4-8 instances).