简体   繁体   中英

TypeError: Cannot read property 'source' of undefined onEdit - Google Apps Script

I am working on the following code. I'm getting the following error message:

TypeError: Cannot read property 'source' of undefined onEdit @ Code.gs:6

Code:

function onEdit(event) {
// assumes source data in sheet named calls
// target sheet of move to named Completed
// getColumn with drop-downs is currently set to column 7 or F
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();

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

Explanation:

This function is executed automatically when the user edits the sheet.

  • You are not supposed to execute this manually as you do right now and hence you are getting an error.

  • In particular, the block of code you have inside the if statement will be executed upon edits in column 7 (column G) in the sheet Calls and when the edited value is CSR Transfer .

Code snippet:

Take full advantage of the event object :

function onEdit(event) {
  const ss = event.source;
  const s = ss.getActiveSheet();
  const r = event.range;
  if(s.getName() == "Calls" && r.getColumn() == 7 && r.getValue() == "CSR Transfer") {
    const row = r.getRow();
    const numColumns = s.getLastColumn();
    const targetSheet = ss.getSheetByName("CSR Transfer");
    const target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

Make sure the code is saved, the sheet names: Calls and CSR Transfer exist and that you edit column G in Calls with the value CSR Transfer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM