How to Defined property source?

708 views
Skip to first unread message

Alive Nata

unread,
May 24, 2021, 5:33:17 AM5/24/21
to Google Apps Script Community
Hi,

I want to ask to define property source because script get error if running.
TypeError: Cannot read property 'source' of undefined

but if save the script and im testing on google sheet, the script success.

I want to ask why every running get error?

Alan Wells

unread,
May 24, 2021, 8:00:41 AM5/24/21
to Google Apps Script Community
It's difficult to know what your situation is, but because the property name is "source" I could guess that the function is being passed an event object.
And that the function is being run by an event trigger.
If the function is run directly from the code editor, then there is no event object.  There was no event to trigger the function to run.  You triggered the function to run, not an event.  In that case, because there is no event object, it is undefined.  The property "source" can't be read from undefined.

Alive Nata

unread,
May 24, 2021, 11:27:57 PM5/24/21
to Google Apps Script Community
So I just ignore the error in the code editor?
because there is no 'source' in the event object.

If I look in the code editor event spreadshseet in
var activeSheet = e.source.getActiveSheet();
  if (activeSheet.getName() == "Designer_Data","Quotation_Data","Quotation_Rev_Data","Drafter_Layout","Drafter_TD")

below is my full code 

function onEdit(e) {
  var activeSheet = e.source.getActiveSheet();
  if (activeSheet.getName() == "Designer_Data","Quotation_Data","Quotation_Rev_Data","Drafter_Layout","Drafter_TD") { // Put the name of the specific worksheet between the quotation marks. For multiple sheets put the sheet name between the quotes(" ") followed by a comma and so on.
    var aCell = e.source.getActiveCell(), col = aCell.getColumn();
    if (col == 10) { // Number of the column with checkboxes
      var dateCell = aCell.offset(0,4); // In offset, put number of rows away (- is up, + is down) and number of columns away (- is left, + is right)
      if (aCell.getValue() === true) {
        var newDate = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
        dateCell.setValue(newDate);
      } else {
        dateCell.setValue("");
      }
    }
  }

Alan Wells

unread,
May 25, 2021, 9:01:53 AM5/25/21
to Google Apps Script Community
If you need the event object to test your code, then you'll either need to test the code by actually editing a Sheet cell,
or you'd need to call the onEdit(e) function from another function with a dummy object that you created in order to run it from the code editor.
You can't ignore the error message because the code is failing.

Alive Nata

unread,
May 27, 2021, 3:53:49 AM5/27/21
to Google Apps Script Community
Yes, I was created dummy data in
https://docs.google.com/spreadsheets/d/1xOOnnq1ay0kS9z2Bb74gBJ8bMTagvHBpUymDwzuCFmQ/edit#gid=853877289 

Then, I'm testing in worksheet the trigger running normal but if debug at editor code getting error :v. So, I just texting script code then save without debug script code.
Reply all
Reply to author
Forward
0 new messages