Tuesday 5 December 2023

Check for existing Form Submit Triggers

The following Google Apps Script is designed to check existing user triggers of a Google Sheet for any onFormSubmit triggers. Any that are found are then deleted and a new onFormSubmit trigger is created.

Check existing user triggers of a Google Sheet
Check existing user triggers of a Google Sheet

The Code

We start by getting the active spreadsheet as in this instance that will be the Response Sheet connected to our Google Form. We use this to get an array of all existing triggers for the user running the code:

// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();

// get all existing Triggers
var triggers = ScriptApp.getUserTriggers(ss);

Next we loop through these triggers and get the event type so we can narrow down the search to onFormSubmit's only:

// get Trigger event type
var triggerEventType = trigger.getEventType();

// check if onFormSubmit event type
if (triggerEventType == ScriptApp.EventType.ON_FORM_SUBMIT) {

If one is found then we simply delete it:

// delete existing Trigger
ScriptApp.deleteTrigger(trigger);

At the very end we can setup our new onFormSubmit trigger to run whatever function we want:

// create new onFormSubmit Trigger
ScriptApp.newTrigger('YOUR FUNCTION NAME HERE')
  .forSpreadsheet(SpreadsheetApp.getActive())
  .onFormSubmit()
  .create();


Download

Check for existing Form Submit Triggers download (please use 'File' > 'Make a copy' for your own version).


No comments:

Post a Comment