r/GoogleAppsScript • u/VAer1 • 9d ago
Unresolved How can I write script to check whether it was triggered by a time-driven trigger?
Lines 1–16 are from an online source, and I modified the copyStockDataAnalysis function based on the source. However, I think there is something wrong with the IF statement on line 21.
I also have another trigger configured as From spreadsheet - On edit, and it seems that the IF condition on line 21 evaluates to true when the function is executed by the On edit trigger.
This is not the intended behavior. I want the IF statement to execute only when the function is triggered by a time-driven trigger, not by the Spreadsheet On edit trigger.
Could someone please help me correct the condition on line 21?
Edit: I don't use lines 1–16 at all. They are included only as reference code to provide context for this post and have nothing to do with my function copyingStockDataAnalysis itself.
I should not have included lines 1–16 in the original post. Sorry for the confusion! I added them here to explain why I wrote line 21 the way I did.



2
u/WicketTheQuerent 9d ago
The easiest way to handle this is by using a specific function for each trigger type.
2
u/mommasaidmommasaid 9d ago edited 9d ago
The code in lines 1..16 is first checking for the existence of a range in the the event object, which exists for both simple or installed edit events.
You are skipping that step, and checking for a triggerID, but a trigger ID exists in any installed trigger, including installed onEdit which it appears you are using.
So you could first exclude edit events like the original code, but... since you don't seem to want to do anything on edit, I'd just delete the edit trigger, which will avoid a whole bunch of unnecessary processing and logging.
I would also recommend you rename that argument "fun" back to "e" to avoid (even more) confusion.