1

I made a similar request a few days ago using the following link:

How to copy a specific row from one sheet to another (Google Apps Script)

Now I have new requirements for which I need your help:

  1. the script should copy specific rows that have value "No" in column T from one sheet to another sheet. It is important that it copies the values and the formatting.

  2. the complete row should be copied.

  3. after successful copying, the row should be deleted in the sourcesheet.

Can you please help me? Thank you very much!

Below you can find @Marios solution so far. At this point again many thanks!

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName("[Overview] All_Cases");
  const tarSheet = ss.getSheetByName("OPS_FUNNEL");
  const data = srcSheet.getDataRange().getValues().filter(r=>r[21]=='No').map(r => [r[0]]);
  var tarlast = tarSheet.getRange("A:A").getValues().filter(String).length;
  
  if (data.length>0){
    tarSheet.getRange(tarlast+1,1,data.length,1).setValues(data);  
  }
Marios
  • 26,333
  • 8
  • 32
  • 52

1 Answers1

2

Explanation:

You can store the deleted row indexes inside the filter function itself and then use the classical approach to delete rows backwards.

Solution:

Column T has an array index of 19, not 21. Be careful with this.

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName("[Overview] All_Cases");
  const tarSheet = ss.getSheetByName("OPS_FUNNEL");
  const rowsD = [];
  const data = srcSheet.getDataRange().getValues()
               .filter((r,i)=>{
                       let val = r[19]=='No';
                       if(val){
                          rowsD.push(i+1);
                          return val;
                       }                      
                });
  if (data.length>0){
    tarSheet.getRange(tarSheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
    for (var i = rowsD.length - 1; i>=0; i--) {
      srcSheet.deleteRow(rowsD[i]); 
    }
  };
}
Marios
  • 26,333
  • 8
  • 32
  • 52
  • @user14523871 You need to find the index of that column. For example column C is in the third position. But JavaScript arrays start their indexes from `0`. So you need to use the number `2`. (0 index is column A, 1 index is column B, 2 index is column C). One way to find out the index of the column if that column is too far in the right, is to simply create a formula. Put in cell A1 the number 0, in cell B1 a formula: `=A1+1` and drag it all the way to the left until `BL`. So you will see the index you need to use. – Marios Feb 09 '21 at 10:29