Google Apps Script supports Triggers, that pass Events to trigger functions. Unfortunately, the development environment will let you test functions with no parameter passing, so you cannot simulate an event that way. If you try, you get an error like:
ReferenceError: 'e' is not defined.
One could treat the event like an optional parameter, and insert a default value into the trigger function using any of the techniques from "Is there a better way to do optional function parameters in JavaScript?". But that introduces a risk that a lazy programmer (hands up if that's you!) will leave that code behind, with unintended side effects.
Surely there are better ways?
2017 Update: Debug the Event objects with Stackdriver Logging for Google Apps Script. From the menu bar in the script editor, goto:
View > Stackdriver Logging
to view or stream the logs.console.log() will write
DEBUG
level messagesExample onEdit():
Example onFormSubmit():
Example onChange():
Then check the logs in the Stackdriver UI labeled as the
message
string to see the outputYou can write a test function that passes a simulated event to your trigger function. Here's an example that tests an
onEdit()
trigger function. It passes an event object with all the information described for "Spreadsheet Edit Events" in Understanding Events.To use it, set your breakpoint in your target
onEdit
function, select functiontest_onEdit
and hitDebug
.If you're curious, this was written to test the
onEdit
function for Google Spreadsheet conditional on three cells.Here's a test function for Spreadsheet Form Submission events. It builds its simulated event by reading form submission data. This was originally written for Getting TypeError in onFormSubmit trigger?.
Tips
When simulating events, take care to match the documented event objects as close as possible.
If you wish to validate the documentation, you can log the received event from your trigger function.
In Spreadsheet form submission events:
values
array skips over blank answers (in "new Forms" + "new Sheets"). Thefilter(Boolean)
method is used to simulate this behavior.*A cell formatted "plain text" will preserve the date as a string, and is not a Good Idea.