Search code examples
google-apps-scriptgoogle-sheetstriggers

Moving Rows from one sheet to another based on selected values


I have an Apps Script I am using to move rows from one tab on my sheet to another based on what I select in a specific column.

The code I am using seems to work however it is putting two copies of the line on the second sheet I am pretty new to app script so i may have made an error in the code. I litterally just need the row to move over and delete from the original page.

function onEdit(e){
    var sourceSheet = e.range.getSheet();
    var row = e.range.getRow();
    if(sourceSheet.getSheetName() === 'Submissions'){
        var rowRange = sourceSheet.getRange(row, 1, 1, sourceSheet.getLastColumn());
        var rowValues = rowRange.getValues()[0];
        if(rowValues[16] === "Approved"){
            var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Completed");  
            targetSheet.appendRow(rowValues);  
            sourceSheet.deleteRow(row);
        }
    } 
}

Solution

  • Delete Selected Rows

    function onEdit(e) {
      var sh = e.range.getSheet();
        if(sh.getName() == 'Submissions'){
            var rowRange = sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn());
            var rowValues = rowRange.getValues()[0];
            if(rowValues[16] == "Approved"){
                var tsh = e.source.getSheetByName("Completed");  
                tsh.appendRow(rowValues);  
                sh.deleteRow(e.range.rowStart);
            }
        } 
    }
    

    Perhaps you create and installable trigger with the name onEdit. In this case you are getting two triggers. One the installable version and one from the simple trigger. I also rewrote the function taking advantage of event object.