onEdit(e) works only when it wants?

876 views
Skip to first unread message

7 Ideas Digitales

unread,
Apr 7, 2022, 4:29:37 AM4/7/22
to Google Apps Script Community
Hi:
I am new to this environment and I have loved what can be done. However I am with a "minor" problem apparently that I can not identify. While the examples are clear when I use the onEdit(e) event, something is wrong when I modify it and it stops recognizing the "range" property. I explain:
I am controlling the changes in a sheet of my worksheet, for this I am using a simple script:

function onEdit(e) {
  var range = e.range;
  range.setNote('Last modified: ' + new Date());
}


It works. but if I modify it to ask for the previous content or the one that was entered in it, it stops working and I can't call another function that copies some data and modifies another sheet (this one works fine on its own).

function onEdit(e) {
  var range = e.range;
  if (rg.oldvalue == 'Opened') {
    range.setNote('Last modified: ' + new Date());
    //Call to another function
    var x = MakeChangesTo() ;
  };
}


When I run it, it gives me the following error:
TypeError: Cannot read property 'range' of undefined


Any idea why it stops working when I add a couple of lines?

Thanks

Clark Lind

unread,
Apr 9, 2022, 12:41:50 PM4/9/22
to Google Apps Script Community
Unless  rg.oldvalue is a global variable,  you will have to spell everything out:   

function onEdit(e) {
  var range = e.range;
 //  if (rg.oldvalue == 'Opened') {
  if (range.getValue() == 'Opened') {

CBMServices Web

unread,
Apr 9, 2022, 12:52:24 PM4/9/22
to google-apps-sc...@googlegroups.com
The error you are getting is because you are trying to manually run it. The e (event object) parameter is only valid when it is automatically triggered by spreadsheet activity.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b842934f-97cf-482a-bbe7-56194b1f72f9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages