简体   繁体   中英

Google Scripts for Sheets - onEdit and “source”

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?

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM