Trigger not working?

163 views
Skip to first unread message

Andrew Apell

unread,
Aug 4, 2020, 1:48:36 AM8/4/20
to Google Apps Script Community
I have an add-on that a deployed a while ago and I recently made changes to the code that required using a trigger.
So, I created a time driven trigger to fire a certain function every 24 hours.
However, after installing the add-on myself for testing purposes, it seems that the trigger never ran. Interestingly though, the Apps Script log shows that it ran properly without any errors (0%).

So, my question is, could I be doing something wrong? Is it even possible to create a trigger this way?

Alan Wells

unread,
Aug 4, 2020, 8:02:32 AM8/4/20
to Google Apps Script Community

An add-on can create a time-based trigger.
Add-ons (according to the documentation) can only create 1 trigger of each trigger type.
Reasons for the failure that I can think of:
* The code you are using to develop the add-on is in a stand-alone Apps Script file, and your code requires getting the active document.
   You would need to test the trigger from a bound script.
* The execution is failing immediately upon running and therefore you see no results.  Could be an uncaught error.  Put the entire contents of the trigger function in a try/catch block and log the error the Stackdriver.

It should work.  A time based trigger in an add-on is something that Apps Script is capable of.
How reliable is it?  That's another question.
Could it be an Apps Script bug, or a failure at Google?  I don't know.  Maybe.
But try to get some debugging information.
Message has been deleted

Andrew Apell

unread,
Aug 5, 2020, 5:40:13 PM8/5/20
to Google Apps Script Community
So allow me to be direct.
Say I have a published add-on as already mentioned. Then I want to add a trigger that fires once a day to run a custom function. How would I go about doing that?
This is a newbie question but I request as much detail as possible because I don't want to leave anything to chance.

AD:AM

unread,
Aug 5, 2020, 6:16:05 PM8/5/20
to Google Apps Script Community
Andrew, you used the term "custom function", which usually refers to a function that is invoked via a formula in a spreadsheet cell. Is this the case in your example?

Andrew Apell

unread,
Aug 5, 2020, 6:18:29 PM8/5/20
to Google Apps Script Community
Yes, it's a function to read from and write to the user properties.

AD:AM

unread,
Aug 5, 2020, 7:58:53 PM8/5/20
to Google Apps Script Community
OK. I think a custom function invoked in a spreadsheet cell is kind of its own beast, in that, in my experience, you can't force it to run/refresh in the cell simply by running the function via a trigger. The workaround I have usually recommended is for a triggered function to delete the formula from the cell, flush() the ss, and reinstate the formula.

But I may be misunderstanding your situation here - aplogies if so.

HTH
Adam

Andrew Apell

unread,
Aug 6, 2020, 2:03:02 AM8/6/20
to Google Apps Script Community
Maybe I used the wrong word when I said "custom function". It's just a function and it won't directly interact with the spreadsheet.
It's simply dealing with the properties service.

I guess my question is: If I manually create a trigger to run this function, does it affect all users or do I have to create a trigger for each user individually?

Andrew Apell

unread,
Aug 6, 2020, 11:36:06 AM8/6/20
to Google Apps Script Community
Gurus, where are you?

Andrew Apell

unread,
Aug 6, 2020, 12:39:58 PM8/6/20
to Google Apps Script Community
From the documentation: "Add-on installable triggers can't be created manually"

Now, this means I have to create it programatically...

Alan Wells

unread,
Aug 6, 2020, 12:58:20 PM8/6/20
to Google Apps Script Community
A manually created time-base trigger, or even a time-based trigger that was created programmatically,
but with code outside the add-on code, can't run add-on code.
That's because of the "scoped" conditions.
Otherwise, any add-on could run any other add-on's code.
Anyone with an installed add-on could decide that they are going to mess with your add-on,
and just start running your code randomly,  or constantly as an attack.
(if they new the function name)
A time based trigger that is created programmatically from your add-on code only runs for that user.
It won't run universally for everyone who installed the add-on.
Message has been deleted

Andrew Apell

unread,
Aug 6, 2020, 1:07:38 PM8/6/20
to Google Apps Script Community
OK.
So now I want to create a timed based trigger that looks like this:

function ramdomTrigger_() {
  ScriptApp.newTrigger("randomFunction_").timeBased().everyHours(72).create();
}

2 questions:
  1. Should I add this to the onInstall() function?
  2. Does adding the underscore affect the trigger?

Alan Wells

unread,
Aug 6, 2020, 1:21:57 PM8/6/20
to Google Apps Script Community
I would not add it to the onInstall trigger.
You don't want the installation to fail.
I wouldn't put anything in the onInstall trigger that isn't absolutely necessary.

I'm guessing that the underscore won't be a problem.
But I don't know for sure.
Recently I've noticed that add-on function names for triggers don't show up in the Apps Script dashboard.
But you should use the underscore whenever possible to make the function private.

Andrew Apell

unread,
Aug 6, 2020, 1:31:32 PM8/6/20
to Google Apps Script Community
So, if I'm not putting it with onInstall, how do I ensure that the trigger is created by the user?

Alan Wells

unread,
Aug 6, 2020, 1:52:52 PM8/6/20
to Google Apps Script Community
Do you have an onOpen() function?
Do you have something that the user must do, in order for the add-on to work?
Maybe tie it to the payment code?
Does the user need to do something to open PayPal?
Whatever code the user MUST use at some point.

Andrew Apell

unread,
Aug 6, 2020, 2:19:21 PM8/6/20
to Google Apps Script Community
I have all those but when I consider the flow of the code, creating the trigger under onOpen and the like will not be helpful.
To be honest, the creation of the trigger under onInstall looks like the best and only option.
Is this prone to getting errors?

Andrew Apell

unread,
Aug 6, 2020, 2:22:30 PM8/6/20
to Google Apps Script Community
Just seen this in the official documentation:

"The trigger (onInstall) won't run when a user installs the add-on from the G Suite Marketplace website"

This is a problem for me... I will have to find another way 

On Thursday, 6 August 2020 at 20:52:52 UTC+3 aj.a...@gmail.com wrote:

Afaan Naqvi

unread,
Feb 20, 2024, 5:40:24 AMFeb 20
to Google Apps Script Community
Was there a solution here?
I see pretty clearly that the documentation still states: "The trigger won't run when a user installs the add-on from the Google Workspace Marketplace website."
And so I don not understand how to implement the onInstall() --> onOpen() --> menu creation flow for my addon.

Any help would be greatly appreciated. Like I'll go to the beach and write your name in the sand.

Andrew Apell

unread,
Feb 20, 2024, 6:46:26 AMFeb 20
to Google Apps Script Community
If you follow this setup, then your menu will always appear:

function onOpen(e) {
// Menu creation code
};

function onInstall(e) {
onOpen(e)
};

However, if you need something to run at the moment of installation, then I think you are out of options. I see no other solution except persuading your users to install your add-on via the spreadsheet menu.

Afaan Naqvi

unread,
Feb 20, 2024, 7:32:51 AMFeb 20
to Google Apps Script Community
But when a user installs the App from the Google Workspace (through the Sheets Extensions menu) the Sheet is already "open" and so the onOpen() function will not run. And because the onInstall() function does not run when installing from the Google Workspace Marketplace, it will also not subsequently run the onOpen() function.

I can't believe I have learned JavaScript, built my first app, made it through the viciously difficult OAuth Verification process, the crazy CASA scan and subsequent survey, and this is where my app will go to die. Such a GD shame.

Andrew Apell

unread,
Feb 20, 2024, 8:15:09 AMFeb 20
to Google Apps Script Community
Why do you think that "onOpen" will not run if the sheet is already open? I have triggered to "onOpen" function to run multiple times when the sheet is already open... sort of like refreshing it.
So, if the user installs your project via the spreadsheet menu, and your code is written following the example I gave earlier, then "onInstall" will cause "onOpen" and any other function you add there to run.

Afaan Naqvi

unread,
Feb 20, 2024, 9:33:35 AMFeb 20
to Google Apps Script Community
My understanding is that the onOpen() function runs when the user opens the document , per the docs.
The Authorization lifecycle page also states that the onInstall() function should be used to create the menu, by calling the onOpen() function (but that contradicts the onInstall() documentation)

At the moment however my Add-On isn't even working (i.e. populating the Extension menu) when I test even with the "Installed" and "Enabled" options both checked, which it was previously, so at the moment I can't even test, and am hoping that at least that will start working again magically as it did for someone higher up in this thread after a few hours.

I did have my onOpen() and onInstall() functions per your suggested setup all along, and got the following email from the Marketplace SDK review (with this screenshot showing only the Help item populate in the menu)

"In order to address the issues listed below, you will need to make changes in the Google Workspace Marketplace SDK settings.

Menu - Menu options not shown after App is installed. Please ensure that the add-on correctly uses onInstall() and onOpen() to populate its menu. The menu items populate when the add-on is first installed and when a different file is opened. See Editor add-on authorization. , https://developers.google.com/workspace/add-ons/concepts/editor-auth-lifecycle
If your published add-on fails to add its menu items, look in the browser's JavaScript console to see if an error was thrown, then examine your script to see whether the onOpen(e) function or global variables call services that aren't allowed in AuthMode.NONE.
This can be a possible implementation issue why the menu options are not populated. When your onOpen(e) function runs, the entire script is loaded and any global statements are executed. These statements execute under the same authorization mode as onOpen(e) and fail if the mode prohibits them. This prevents onOpen(e) from running.
Be sure that you are correctly implementing the Triggers and the AuthMode for the Editor Add-ons so that the menu is opened correctly.
Please make sure that you comply with the following:
- The add-ons script project includes an onInstall(e) function that populates the menu, usually by calling onOpen(e) (https://developers.google.com/apps-script/guides/triggers#onopene).
- To follow the limitations of the AuthMode.NONE (https://developers.google.com/gsuite/add-ons/concepts/editor-auth-lifecycle#authorization_modes), the script project's global code and the AuthMode.NONE path of the onOpen(e) function should not contain calls to services that require authentication. Make sure you correctly handle the AuthMode.NONE state, and move any code that handles initializing the token into AuthMode.LIMITED.
You can find more information about this on this link (https://developers.google.com/gsuite/add-ons/concepts/editor-auth-lifecycle#opening).
As part of the review process we do not check on the code itself so we cannot give advice directly on the code implementation. Please check for these recommendations and go through the testing of the Add-on on your side by checking the testing options (https://developers.google.com/workspace/add-ons/how-tos/testing-editor-addons) and the OAuth Testing modes and let us know if this solves the issue."

Afaan Naqvi

unread,
Feb 22, 2024, 9:13:01 AMFeb 22
to Google Apps Script Community
Ok this was not Google after all.
I was selecting the radio button for my test in the testing dialogue and then clicking the SHEET_NAME in the same row as the radio button, instead of the "Execute" button at the very top of the testing dialogue.
My app is approved and should be on the Marketplace later today I am told.

Thanks for the help and guidance.

Andrew Apell

unread,
Feb 22, 2024, 9:19:07 AMFeb 22
to Google Apps Script Community
Wunderbar!
Reply all
Reply to author
Forward
0 new messages