r/GoogleAppsScript • • 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.

3 Upvotes

11 comments sorted by

2

u/WicketTheQuerent 9d ago

The easiest way to handle this is by using a specific function for each trigger type.

timeDrivenHandler(e){
  // do something

  // Call here the function that should be done for both triggers
}
onEditHandler(e){
  // do something else

  // Call here the function that should be done for both triggers
}

1

u/VAer1 9d ago

Thanks, I just made new function copyingStockDataAnalysisBySpreadsheetEdit earlier. As long as it works, it is fine to me.

Two sets of similar code.

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.

0

u/VAer1 9d ago edited 9d ago

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.

Anyway, how can I re-write line 21 IF condition to limit it to time-driven trigger?

1

u/mommasaidmommasaid 9d ago

(I edited my reply after you read it.)

0

u/VAer1 9d ago

I'm not a technical person. I just wrote all the code (putting together from online source) for my own use.

Let me simplify my question. Please disregard lines 1–16 and assume that code doesn't exist.

How can I write an IF statement to check whether the function is being run by a time-driven trigger? It should exclude From spreadsheet - On edit

1

u/mommasaidmommasaid 9d ago

Globbed into one if statement I believe this will work...

if (e?.triggerUid && ScriptApp.getProjectTriggers().find(t => t.getUniqueId() == e.triggerUid).getEventType() === ScriptApp.EventType.CLOCK) {
 // time driven trigger
}

1

u/VAer1 9d ago

Thanks so much.

1

u/mommasaidmommasaid 9d ago

> Anyway, how can I re-write line 21 IF condition to limit it to time-driven trigger?

Generally you shouldn't need to do this, but to answer the question explicitly, since the Trigger object isn't passed directly in, to do this robustly I believe you'd have to get all the project triggers, find the Trigger object with the ID that matches e.triggerUId, and use Trigger.getEventType() to see if it's ScriptApp.EventType.CLOCK

1

u/VAer1 9d ago

Thanks. I will try to figure out.

1

u/VAer1 9d ago

Anyway, I just copy some code and make a new function copyingStockDataAnalysisBySpreadsheetEdit

As long as it runs correctly, it will be fine.