Google Apps Script supports triggers ( Triggers ), also called activators, which pass events ( Events ) to trigger functions to fire/activate functions. Unfortunately, the development environment does not allow functions to be tested without passing parameters, so it is not possible to simulate an event in this way. When attempted, it results in an error like:
ReferenceError: 'e' is not defined.
One can treat the event as an optional parameter and insert a default value into the trigger function using the techniques of "Is there a better way to do optional function parameters in Javascript?" . But this introduces the risk that a lazy programmer (Who said me?) could leave that code behind with unintended side effects.
Surely there are better ways.
References
Based on How can I test a trigger function in GAS? by Mogsdad 2014-11-14 19:40:55Z edit
Write a test function that passes a mock event to the trigger function. Here is an example that tests a trigger function
onEdit()
. This passes an event object with all the information described for "Spreadsheet Edit Events" in Understanding Events .To use it, set an interrupt on your objective function
onEdit
, select the functiontest_onEdit
and clickDebug
.If you're curious, this was written to test the conditional on three cells function
onEdit
for Google Spreadsheet .Below is a function for form submit events in the spreadsheet. This builds a mock event by reading data from the form submit. This was originally written for Getting TypeError in onFormSubmit trigger? .
Tips
When simulating events, take care to match the document event objects as precisely as possible.
If you want to validate the documentation, you can log the events received from the trigger function.
valores
will skip blank responses (in "new forms" + "new spreadsheet"). The methodfilter(Boolean)
is used to simulate this behavior.* A cell formatted as "plain text" will preserve the date as a string, and is not a good idea.
References
Mogsdad 's reply edit 2014-12-03 17:16:57Z.