Home > Software engineering >  How can I delete my filtered rows after copying to new sheet?
How can I delete my filtered rows after copying to new sheet?

Time:06-02

I am requiring a script that deletes my filtered rows after copying them to a new sheet.

This is the code I have to filter and copy to a new sheet:

function archiveData() {
  const ss = SpreadsheetApp.getActive();
  const values = ss.getRange('Template!A3:S')
    .getValues()
    .filter(row => row[18]); // column S
    const targetSheet = ss.getSheetByName('Archive');
  appendRows_(targetSheet, values, 1);
}

It makes use of appendRows_() I have tried the following code but no avail:

const ws = ss.getSheetByName("Template");  
const colSData = ws.getRange('S3:S').getValues();  

for(let i =colSData.length-1;i>=0;i--){   
   if(colSData[i][0] == true){     
   //console.log(colSData);     
   ws.deleteRows(i 1);   
   } 
 }

CodePudding user response:

In your script, as a simple modification, how about the following modification?

From:

ws.deleteRows(i 1);   

To:

ws.deleteRows(i   3);
  • In your script, the values are retrieved with const colSData = ws.getRange('S3:S').getValues();. By this, I thought that ws.deleteRows(i 1); might be required to be modified to ws.deleteRows(i 3);.

  • By the way, in your script, when const colSData = ws.getRange('S3:S').getValues(); is modified to const colSData = ws.getRange('S3:S' ws.getLastRow()).getValues();, the process cost might be a bit reduced.

  • Related