4

i am creating google sheets addon, according to google docs

https://developers.google.com/apps-script/guides/triggers#getting_started

onEdit is a simple trigger which doesnt need to be installed

this is my code.gs

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}

function showAlert() {
}


function onEdit() {
  console.log("on edit called")
}


and clicked on test deployment and opened the spreadsheet which i have full access to

screenshot_one

screenshot_two

however onEdit doesnt seem to be called when i edit the values in spreadsheet ( doesnt show on execution logs too), what should be done for simple triggers to work ?

  • You simply have to make an edit in any cell of the spreadsheet. Clicking on the test deployment does nothing as far as this function is concerned – Cooper Feb 02 '22 at 02:23
  • You will also have to go the executions to see the Logger.log() If you also add a paramenter like e and use the code Logger.log(JSON.stringify(e)); then you will be able to see the event object provided by the trigger. – Cooper Feb 02 '22 at 02:27
  • @Cooper i did edit the cells after that ( added this info on the question ), but onEdit didnt show up on executions – Naveen Muthusamy Feb 02 '22 at 02:33
  • Well in that case I'd close the spreadsheet and move to another one and put the script into the script editor save it and try again if it doesn't work get a hold google support if it does then delete the other spreadsheet and keep working in the new one. It should work unless you have other onEdit functions with the same name. – Cooper Feb 02 '22 at 03:06
  • By the way I would be using Logger.log() instead of console.log(). – Cooper Feb 02 '22 at 03:09

1 Answers1

2

Test deployment doesn't help to test Google Workspace Editor Add-ons, they are intended to be used with Google Workspace Add-ons that are a different kind of add-ons.

To test an Editor add-on you might use the now called "Classic Editor" and use Run > Test as add-on. For details see https://developers.google.com/apps-script/add-ons/how-tos/testing-editor-addons.

NOTE: I have not found helpful Run > Test as add-on. I prefer to use a bounded script to do some of the early tests on the bounded spreadsheet, then publish the editor add-on for internal use only (this requires a Google Workspace account) for doing other tests when they are really needed.

It's worthy to mention that Editor add-ons might be installed, enabled or installed and enabled.

The installation is done once by user.

The add-on should be enabled on each spreadsheet (or document, or form, or presentation) before they can be used. In order to be able to enable the add-on there should be a custom menu at least with one option.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    i switched to classic editor and edited the code.gs to create a menu with one item in onOpen trigger, when i do Run > Test as add-on (with configuration set to Installed and enabled), the menu gets created, when i change the values in the cells, onEdit trigger is not called, i couldnt find it on executions. – Naveen Muthusamy Feb 02 '22 at 03:40
  • Did you opened the spreadsheet from the Run > Test as add-on dialog? Have tried this using Chrome in incognito mode with all the extensions disabled and signed in only using one account? – Rubén Feb 02 '22 at 04:11
  • yes i opened the spreadsheet from run > test as add on dialogue, set the installation config to installed and enabled ( i was using firefox developer edition), i tried this with chrome and got the same result. onOpen trigger is called, but onEdit trigger doesnt execute. – Naveen Muthusamy Feb 02 '22 at 04:34
  • 1
    @NaveenMuthusamy I was able to reproduce the problem but as I mentioned in the answer, I have not found helpful using Run > Test as add-on so I prefer to do the early testing on the bounded spreadsheet and sometimes I publish the add-on for internal use for testing. – Rubén Feb 02 '22 at 04:56
  • 1
    By the way, you might be interested in reading https://developers.google.com/apps-script/support#bugs – Rubén Feb 02 '22 at 04:59