I have a shared google sheet that I use as a to-do list. I am using Script Editor. I originally had everything moving to an empty row on sheet 1. As more was added I found that the done items need to move to a second sheet. My original code moved the row from any input in column 6/F. I have tried adding a trigger to move column 7/G to sheet 2. Both codes work by themselves, but I cannot seem to combine them.
After researching this site and others, I have tried renaming onEdit, Nesting, using “my function”, recording macros. I receive the same error but the line changes depending on how I edit the code. "TypeError: Cannot read property 'range' of undefined onEdit @ Done.gs:2"
What I want to achieve,
When the task is marked complete it moves to sheet2 (Column 7/G type “yes”) When the status of a task is updated it moves it to an empty row on sheet 1 (Column 6/F words used – ongoing, pending review, later) I am also looking for a basic course to start understanding scripts and macros for google sheets. I realize that I am starting in the middle and making it harder on myself. Thank you I appreciate the feedback and recommendations!
function onEdit(event) {
// assumes source data in sheet named Sheet1
// target sheet of move to named Sheet2
// test column with yes is col 7 or G
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Sheet1" && r.getColumn() == 7 && r.getValue() == "yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet2");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
function onEdit(e) {
const row = e.range.getRow();
const col = e.range.getColumn();
const as = e.source.getActiveSheet();
if(as.getName() == "Sheet1" && col == 6 && row > 1 && !as.getRange(row,col).getValue()=='') {
const row_new = as.getRange(row,1,1,col);
row_new.copyTo(as.getRange(as.getLastRow()+1,1,1,col));
as.deleteRow(row);
}
}