1
votes

Hey I'm a beginner in Google Apps Script. I got an error:

Exception: This script has too many triggers.

Even though I only have 1 trigger which is:

ScriptApp.newTrigger("Status()").timeBased().atDate(2021, 2, 1).create();

I'm actually thinking about using more time-driven triggers but i tried 1 and it has this error. Do you have any idea what is happening?


When the Web App is launched, new triggers are always created. Is there any way to keep the triggers without creating another one when the Web App is visited?

1
Initially, I thought this is a duplicate, but you say you created only one. Are you sure? What does triggers tab say about the number of triggers for the current script? Please, update the question to include how the trigger is set up (installed)Oleg Valter
Whoa I just check it and apparently everytime I try to run the trigger it saved as a trigger. It solved, thank you!Ray
No problem - common enough issue. You don't need to install trigger multiple times, the function should be ran only once, but you figured that out already - good luck!Oleg Valter
@OlegValter Thanks! But hey apparently I just got another issue, so everytime I launch the Google Web App from the link then the triggers are created. That's the cause of my too many triggers error come from. Is there any way to keep the triggers without creating another one when the web app visited?Ray
No problem. Ray, please, update the question to clarify that the issue is being caused by what you've desctibed in the comment above.Oleg Valter

1 Answers

1
votes

Problem

Installing a new trigger each time a Web App is open.

Ensuring only one instance of a trigger is installed

The sample below is a utility for checking whether the installable trigger is already in place or needs to be installed. Configure it to your liking and run each time the Web App is open using doGet trigger.

/**
 * @summary gets or installs a trigger
 * @param {string} callbackName
 * @param {GoogleAppsScript.Script.EventType} type
 * @param {function} installer
 */
const getOrIntallTrigger = (callbackName, type, installer) =>

    /**
     * @returns {?GoogleAppsScript.Script.Trigger}
     */
    () => {

        console.log(`Checking for triggered ${callbackName} function`);

        const ss = SpreadsheetApp.getActiveSpreadsheet();

        const triggers = ScriptApp.getUserTriggers(ss);

        const found = triggers
            .filter(trigger => trigger.getEventType() === type && trigger.getHandlerFunction() === callbackName);

        const [trigger] = found;

        !trigger && installer();

        return trigger;
    };

Notes

  1. For this one to work, you need to have V8 runtime enabled and be comfortable with ES6 syntax.