0

I was trying to figure out how to send variables to a function that's being run off time-driven (clock) trigger. There's a method by storing the variables in the PropertyStore in the trigger function and then recall them in the actual function that does whatever task you need.

Please see this question where it's discussed in length. How to set a time-driven (clock) trigger for a function that needs parameters sent to it?

The issue with this method is that it has a limitation.

It is that if I would like to trigger this function twice I would have an issue since I already stored a VALUE for the KEY I created for the function for the first time I triggered it, I cannot go ahead and store another VALUE for the function to use the second time because it's already taken.

For example if I Trigger my function to run tommorow at 2pm I send the variable into a property let's say as a pair 'NAME':'John'. Now if I would like to set a trigger for the same function to run at 3pm but this time I want the 'NAME' to be Steven I cannot do that since it is already set to 'John'

All the code can be found by clicking the link above, it was posted in that question and this is sort of a follow up question. I was advised to give it its own question by moderators.

jack
  • 330
  • 3
  • 12
  • Hello, can you share the code you are using at the moment? – Alessandro Aug 04 '20 at 09:50
  • Follow the link in the question, all the code was posted at that question – jack Aug 04 '20 at 14:12
  • What is the logic behind the names (how and why do they change)? Where do you store the names? Is the name unique for each run or it is a specific property for your trigger? – Alessandro Aug 04 '20 at 15:26
  • the "names" are just an example. My variables are coming from the user's input. I'm working on a messing app, so the user will type in a destination number and say send this sms tomorrow at 2pm. Then if he wants to send another sms to a different number tomorrow at 3pm, I now have the issue outlined above – jack Aug 04 '20 at 21:22

1 Answers1

0

Solution

You should create a Script Property which key is the trigger Uid and value a JSON string with the parameters. In this way each trigger can have its own scope and you can use the same parameter names without collisions.

Look at this answer for the implementation details: https://stackoverflow.com/a/49101767/7453656

Note: The PropertiesService has size limitations, you can use a support Spreadsheet to store your string parameters too.

+---------------+-------------+
|       A       |      B      |
+---------------+-------------+
| trigger-1-Uid | JSON String |
+---------------+-------------+
| trigger-2-Uid | JSON String |
+---------------+-------------+

To access a Spreadsheet values you can easily do this using the Spreadsheet ID.

To get the trigger Uid once it's created via the ScriptApp Class you can obtain the triggerUid property from the returned trigger Object.

References

Time Driven Trigger object

SpreadsheetApp

PropertiesService

Alessandro
  • 2,848
  • 1
  • 8
  • 16