0

I have the following trigger setTrigger() set for when a Google Form is submitted. For testing purposes I created a simple function triggerFunction() that creates a .txt file in my Google Drive.

function setTrigger() {
  ScriptApp.newTrigger('triggerFunction')
  .forForm("formId")
  .onFormSubmit()
  .create();
}

function triggerFunction() {
  let folderId = [folderId];
  let blob = ['This is my blob'];
  DriveApp.getFolderById(folderId).createFile('file.txt',blob);
}

Now when I run the triggerFunction() manually it acts normally. It creates a file.txt in my folder. But when I submit a form on my Google form with formId it creates two separate file.txt files! How do I keep the trigger from running the function twice?!

I have tried the following but it still runs the function twice.

function setTrigger() {
  if (ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "triggerFunction").length == 0) {
    ScriptApp.newTrigger('triggerFunction').forForm("formId").onFormSubmit().create();
  }
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Abhijeet
  • 23
  • 5
  • 1
    Do you have more than one trigger? Look in the triggers page. A few years ago we had a time when we were getting spurious additional triggers. Perhaps they have returned but this is the first I've seen of it recently – Cooper May 13 '22 at 03:28
  • No that's the only trigger I have. – Abhijeet May 13 '22 at 03:48
  • Well perhaps you are actually getting a spurious trigger. Here's a description of the last time I remember seeing a similar thing https://stackoverflow.com/questions/54834837/how-can-i-be-getting-multiple-unwanted-event-blocks-from-the-same-onformsubmit-t/54860085#54860085 – Cooper May 13 '22 at 03:51

0 Answers0