3
votes

Google Docs allow scripting.

I'm trying to do change the color of a cell when a cell content matches a particular word. My problem is that the onEdit function is not working as specified here: https://developers.google.com/apps-script/guide_events#Simple

Whenever I try to use event.source google apps throw an error: "source of undefined"

function onEdit(event)
{
  Browser.msgBox(event.source.getActiveSheet());
}

Surely this is quite basic, do I do anything wrong here?

1
When I try your code, it works fine. If there is more code than just this in your file, could you add it as well? Also, how are you running this? For an onEdit function, you should test by editing a cell in the spreadsheet, rather than by running it from the Script Editor, since if you run it from the Script Editor, it's missing the context of the triggering event.Jan Kleinert
Right ok. I've obviously misunderstood the interface. Pretty silly of me. I believed that running a script from the script editor basically adds the script into the spreadsheet sandbox. Thanks! You could answer my question then it's answered :)Julian Krispel-Samsel

1 Answers

4
votes

Your code works fine, but for onEdit() functions, make sure you're running them by editing a cell in the spreadsheet, rather than directly from the Script Editor. These built-in simple event handlers need the context of the event to be passed in in order to work properly. If you run them from the Script Editor directly, then no event object is passed in, which is why you get the error.