'Google Apps Script trigger function runs twice
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();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|