I’ve very novice to appscript and coding in general. I found this code online somewhere for moving rows between sheets whether or not a checkbox in a column is checked. I just changed a few things such as the names of the sheets and the column its looking at.
There is something wrong with it. Right now it seems to be moving the rows over to the other sheet, but when I uncheck the box to move it back, it sometimes disappears. Or it will move back and a different row will be deleted.
Also, lines 3 and 4 are no longer in use. It says “unused declaration for mainSheet’ and the same for ‘sheetToMoveTheRow’ I’m a bit baffled by this because when the name of the function was onEdit it didn’t give me the error, but I changed the function name to moveChecked and since then it has given me the issue.
If you could help me fix this code, I’d greatly appreciate it!
function moveChecked() {
var mainSheet = "To Do";
var sheetToMoveTheRow = "Completed";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = SpreadsheetApp.getActiveRange();
if(sheet.getName() == "To Do" && range.getColumn() == 1 && range.getValue() == true) {
var row = range.getRow();
var numColumns = sheet.getLastColumn();
var targetSheet = ss.getSheetByName("Completed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(row, 1, 1, numColumns).moveTo(target);
sheet.deleteRow(row);}
else if(sheet.getName() == "Completed" && range.getColumn() == 1 && range.getValue() == false) {
var row = range.getRow();
var numColumns = sheet.getLastColumn();
var targetSheet = ss.getSheetByName("To Do");
const aVals = targetSheet.getRange(1,1,targetSheet.getLastRow(),1).getValues();
const targetRow = aVals.filter(String).length + 1;
var target = targetSheet.getRange(targetRow , 1);
sheet.getRange(row, 1, 1, numColumns).moveTo(target);
sheet.deleteRow(row);}
}
I previously changed the name of the function from onEdit to moveChecked and I also deleted the triggers associated with it.