Google-sheets – How to move a row into another sheet

google sheetsgoogle-apps-script

In Google Sheets, I need to hide rows if column H says, "Completed"

My preference would be to cut and copy the row to a new sheet if column F reads "Completed", but sounds more difficult.

If you have the time to explain this, it would be great.

Currently, I have a Query function on another sheet which will copy a row if that rows column F reads, "completed", but if I then delete the row off of the original sheet, then of course it disappears.

Best Answer

use this script which will check H column and when it finds Completed it will move the whole row (A:Z) from Sheet1 into Sheet2 and then delete row from Sheet1

function copyrange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1'); //source sheet
  var testrange = sheet.getRange('H:H'); //range to check
  var testvalue = (testrange.getValues());
  var csh = ss.getSheetByName('Sheet2'); //destination sheet
  var data = [];
  var j =[];

  //Condition check in H:H; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
  if ( testvalue[i] == 'Completed') {
  data.push.apply(data,sheet.getRange(i+1,1,1,25).getValues());
  //Copy matched ROW numbers to j
  j.push(i);
 }
 }
//Copy data array to destination sheet

 csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);

//Delete matched rows in the source sheet
  for (i=0;i<j.length;i++){
  var k = j[i]+1;
  sheet.deleteRow(k);

//Alter j to account for deleted rows
  if (!(i == j.length-1)) {
  j[i+1] = j[i+1]-i-1;
}
}
}