Google-sheets – If cell equals a value then move row to different Google Spreadsheet

google sheetsgoogle-apps-script

I've got a script that sorts a Google Spreadsheet when a value in a column is changed.

I'd like to add to the script the following functionality:

If the value for column E = "Complete" then the entire row is moved to
a different spreadsheet (that is a different tab in the same Google
Spreadsheet document).

The row would either be inserted, or appended to the bottom of the spreadsheet in the first blank row.

Is this possible?

Best Answer

Yes, I currently do this for a form system once it receives all necessary approvals I move all of the form submission's values to a new sheet that only contains approved submissions.

The following code gets the values of the row you would like to move and copies it (maintaining the original row) to the destination sheet. Assume that the submissionrow variable is the index of the row you want to move and the sheet variable is the source sheet you are checking the values on.

var lastcol = sheet.getLastColumn();
var rowvalues = sheet.getRange(submissionrow,1,1,lastcol).getValues();
var destinationsheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(nameofthedestinationsheet);
var rownum = destinationsheet.getLastRow() + 1;
destinationsheet.getRange(rownum, 1,1,lastcol).setValues(rowvalues);