简体   繁体   中英

Copy Row if Cell Contains X to Different Sheet then delete row.

I have been searching all day trying to find out how to do this and get close but never to a finished state.

What I am trying to do is on active spreadsheet, look at Sheet "Happy". If a cell in Colum G contains "Closed" I want to copy the cells between range A:K for that row to sheet "Sad". Once the copy is done and the data is moved I want to then delete the whole row on sheet "Happy" that the data was copied from.

IF cell = closed, than copy to sheet and delete, else nothing.

I will run this script every 5 min which I can do with triggers.

Any help would be greatly appreciated.

Here is what I tried so far

function Copy() {
 var sss = SpreadsheetApp.getActive()
 var ss = sss.getSheetByName('Happy');
 var range = ss.getRange('A:k'); 
 var data = range.getValues();

 var tss = SpreadsheetApp.getActive(); 
 var ts = tss.getSheetByName('Sad:'); 

 var valuesToCopy = ss.getRange(2,2,100).getValues();
  ts.getRange(2,ts.getLastRows()+1,valuesToCopy.length,1).setValues(valuesToCopy);
  function copyrange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Happy'); //source sheet
  var testrange = sheet.getRange('G:G');
  var testvalue = (testrange.getValues());
  var csh = ss.getSheetByName('Sad'); //destination sheet
  var data = [];
  var j =[];

  //Condition check in G:G; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
  if ( testvalue[i] == 'Closed') {
  data.push.apply(data,sheet.getRange(i+1,1,1,11).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 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;
}
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM