You can write a test function that passes the simulated event to your trigger function. Here is an example that tests the onEdit() trigger function. It passes the event object with all the information described for "Editing Spreadsheets" to the General Event Information .
To use it, set a breakpoint in the onEdit target function, select the test_onEdit function and click Debug .
function test_onEdit() { onEdit({ user : Session.getActiveUser().getEmail(), source : SpreadsheetApp.getActiveSpreadsheet(), range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(), value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(), authMode : "LIMITED" }); }
If you're interested, this was written to test the onEdit function for Google Sheets on three cells .
Here is a test function for spreadsheet form submission events. He creates his simulated event by reading form submission data. Was it originally written to get a TypeError in an onFormSubmit trigger? .
function test_onFormSubmit() { var dataRange = SpreadsheetApp.getActiveSheet().getDataRange(); var data = dataRange.getValues(); var headers = data[0];
Advice
When simulating events, try to match the objects of the documented events as close as possible.
If you want to check the documentation, you can register the received event from your trigger function.
Logger.log( JSON.stringify( e , null, 2 ) );
In spreadsheet form submission events:
- all namedValues are arrays.
- Timestamps are strings, and their format will be localized in the form locale. If you read from a table with default formatting * these are Date objects. If your trigger function depends on the format of the timestamp string (which is a bad idea), make sure you mimic the value correctly.
- If you have columns in your spreadsheet that are not in your form, the method in this script will simulate an “event” with additional values added, which is not what you get from the form view.
- As reported in Issue 4335 , the
values array skips empty responses (in "new forms" + "new sheets"). The filter(Boolean) method is used to model this behavior.
* Formatted in a cell "plain text" will save the date as a string and is not a good idea.
Mogsdad Apr 18 '13 at 17:03 2013-04-18 17:03
source share