0
votes

I am quite new to coding and am running into an issue I can't work around. I've seen similar questions in the forums but can't seem to work out my issue based on those answers. I'm attempting to set up a script that will move a line to another sheet when "Closed" is entered into a specific column. I keep getting the following error when I run the script from my GoogleSheet:

"TypeError: Cannot read property "source" from undefined."

This is the code I am working with:

 function onEdit(event) {
      // assumes source data in sheet named Open
      // target sheet of move to named Closed
      // test column with yes is col 8 or H
  var ss = SpreadsheetApp.getActiveSheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Open" && r.getColumn() == 8 && r.getValue() == "Closed") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Closed");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

Any assistance would be greatly appreciated!

1
If you are running this from the Script Editor, the function receives no arguments. Only when you activate the simple trigger, by editing a cell in the bound spreadsheet, will a function with the on edit trigger receive the edit event object. Review Apps Script trigger documentation - tehhowch

1 Answers

0
votes

I’ve never used google script before but from the looks of things “.source” is not being recognised, and that is because you’ve used it as a function of event; which when you’ve declared it as a pass you haven’t given the type and a name. Try “event e”, and then use e.source?