2
votes

I've been struggling for a few days now trying to get a script to work. At its most basic level my function contains this:

function onEdit(e) {

var changedRange = e.source.getActiveRange()

}

It seems this is a perfectly legitimate use according to Google's docs: https://developers.google.com/apps-script/guides/triggers/events

This code however produces this error:

TypeError: Cannot read property "source" from undefined. (line 18, file "Code")

I've added a trigger for onEdit as well but this still does not work in the script. Am I missing something?

2
Are you trying to run this script from the script editor (via the 'play' button ?)JPV

2 Answers

3
votes

I created a new spreadsheet, added your code, and edited a cell, and the script executed with no errors. But, as JPV suggested in the comments, if you run the code inside of the script editor with the RUN button, it will produce an error. Running the function inside of the editor doesn't allow the 'e', event object to be passed into the function. Therefore, it's undefined.

0
votes

The most common problem is debugging or running code from app editor. In these modes you simply do not have event. But sometimes it happens when you add some code which requires additional permissions. You will be able to fix it in the next way:

  1. Save your code
  2. Launch all triggers in run mode
  3. Remove your triggers and add them one more time (If still doesn't work)