Can anybody see anything wrong in this code
// make new ssheet
var payablesNewDoc=SpreadsheetApp.getActiveSpreadsheet().copy(newName); // NB undocumented function
// Create onEdit trigger for the new Spreadsheet
var onEditTrigger = ScriptApp.newTrigger("onChange")
.forSpreadsheet(payablesNewDoc)
.onEdit()
.create();
Logger.log(onEditTrigger.getHandlerFunction()); // logs "onChange"
Logger.log(onEditTrigger.getEventType()); // logs "ON_EDIT"
It runs without error, and the two log messages seem to indicate that the trigger was created. However when I open the new spreadsheet and make an edit, nothing is fired and the script editor doesn’t show any declared triggers.
because it seems in this instance you are making a copy of the host Sheet and script, the
onEditcan be added as a simple trigger within the template script, i.e. a function so defined:function onEdit() { // do stuff }.This function will be copied along with the Spreadsheet itself.
One improvement you could make if you are likely to have many copies of the same script floating about you can call a handler function hosted in a library elsewhere to keep your triggered code in one place. If you attach the external script as a Library with development mode turned on, this Library will also be attached to any copies you make to the original Spreadsheet. This will allow you amend the triggered code without having to open all the copies
Make a standalone GS script with a handler function that will replicate the functions you originally wished to trigger, you will need to pass the active sheet by Id thusly:
then in your template Spreadsheet add the external script as a library (e.g. named
EditLibrary).within your simple
onEdit()function simply call the Library function instead of anything inline; passing event details as a parameter.that should do what you need with no need to use the triggerBuilder and allowing you amend your code later.