I am trying to move a row of data from one sheet to another, when a specific value is recorded in a specific column.
I have found and adapted the following code to use in the Apps Script:
function onEdit(event) {
// assumes source data in sheet named Responses
// target sheet of move to named Completed
// test column with yes/no is col Y or 25
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Responses" && r.getColumn() == 25 && r.getValue() == "yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I have tested this on a copy of this data and it has worked fine. However, when using the original, nothing happens, when yes is entered in column Y. I have listed below some reasons why I think this may be occurring:
- This is a shared sheet, I am not the owner. The owner has granted permissions
- The responses sheet is connected with a Google Form
P.S. The following error appears when the script is executed from the editor, I don't believe this is an issue based on the type of trigger, it also appeared on my test and had no impact
TypeError: Cannot read property 'source' of undefined onEdit @ Code.gs:6
I would appreciate any suggestions on workarounds!