2
votes

I have been banging my head on this for awhile now...my document bound google app script works great when testing local to the document. I tried to test as publish > Test as Add-on with another sheet. Although my onInstall() and onOpen() simple triggers worked fine, my onEdit function did not.

I created this simple code below to find the root cause of my onEdit issue, but even this simple code does not work. I created a new sheet with a bound script. I then tested in another sheet, again the onInstall() and onOpen() worked fine, the opOpen menu even called my on Edit function, but when editing a cell nothing happens. Any help is appreciated, I would like to get this to work with simple triggers not installable triggers, as this seems like a really simple use case.

function onOpen(e) {
  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Test Menu')
    .addItem('Hello', 'onEdit')
    .addToUi();
}

function onInstall(e) {
  onOpen(e);
}

function onEdit(e) {
  Logger.log('ran onedit!');
  SpreadsheetApp.getUi().alert('Hello');
}
1
The Simple Trigger Restrictions suggest this may be intended behavior. The script must be bound to a Google Sheets, Docs, or Forms file. Which is why it works when Testing as an Add-on using the same spreadsheet it is bound to, but not when using a different spreadsheet. In which, an Installable Trigger would fix the issue - even if it seems like overkill. There is an Open Issue in the bug tracker you can follow.random-parts

1 Answers

2
votes

Thanks I must have missed this simple trigger restriction in the documentation. I implemented as an installable trigger, a little overkill but no biggy.

Ironically "Installable triggers are currently not supported when testing. Functionality that depends on installable triggers will not be testable."

This restriction seems to make testing add-on scripts next to impossible.

function createTriggers() {
  ScriptApp.newTrigger('onMyEdit')
    .forSpreadsheet(SpreadsheetApp.getActive())
    .onEdit()
    .create();
}

function onMyEdit(e) { 
  Logger.log('ran onedit!');
  SpreadsheetApp.getUi().alert('Hello');
}