0

I am having Data Managing google sheet in that i need to copy and Paste the data to another sheet when criteria match.

When Status in Data sheet Equal to "Done" any of these that row should copy and paste to output sheet with selected column and date stamp. Getting Error "TypeError: Cannot read properties of undefined (reading 'source')". Here is my Code:-

function onEdit(event) {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var s = event.source.getActiveSheet();
   var r = event.source.getActiveRange();

if(s.getName() == "Followup" && r.getColumn() == 7 && r.getValue() == "True") {
   var row = r.getRow();
   var numColumns = s.getLastColumn();
   var targetSheet = ss.getSheetByName("Sheet8");
   var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
   s.getRange(row, 1, 1, numColumns).copyTo(target);
   }
}
  • From `onEdit(event)` and `event.source.getActiveSheet()`, I thought that your script is used with the OnEdit trigger. If my understanding is correct, if you directly run your script with the script editor, I thought that an error like `TypeError: Cannot read properties of undefined (reading 'source')` occurs. How about this? If you want to directly run your script, how about modifying `event.source` to `ss`? – Tanaike Dec 16 '22 at 05:41

1 Answers1

0

Try it this way:

function onEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == "Followup" && e.range.columnStart == 7 && e.value() == "TRUE") {
    let tsh = e.source.getSheetByName("Sheet8");
    let trg = tsh.getRange(tsh.getLastRow() + 1, 1);
    sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).copyTo(trg);
  }
}

Note: you cannot run this from the script editor or a menu unless you use an intermediate function and suppy the event object.

Cooper
  • 59,616
  • 6
  • 23
  • 54