Home > Software design >  I need to remove the header rows(?) from my filtered and copied data in Google Sheets
I need to remove the header rows(?) from my filtered and copied data in Google Sheets

Time:05-31

EDITED: To include image of data after code runs.

I have a google sheet that is holding some data that is specific to a percentage. When this percentage is reached then that row is no longer needed. I would like to copy it to another sheet to keep it in archive. I have a tick box that will be used to select multiple rows that are no longer needed and I will have a button that will run the script to move the rows. I have tried a few iterations of code but each time an extra row at the top is copied. I have tried various functions such as offset, rowDepth, shift but no luck. (I possibly haven't put the code in the correct place.)

This is a screenshot of the data layout: Layout of data

And this is the code I am currently trying to use:

function create_filter(){
    
   const ss = SpreadsheetApp.getActiveSpreadsheet();
   const sheet1 = ss.getSheetByName("Template");
   const range = sheet1.getRange("A:S");
   const  filter = range.createFilter();
   const  filterCriteria1 = SpreadsheetApp.newFilterCriteria().whenTextEqualTo('TRUE')
   const col1 = 19;

   const add_filter1 =filter.setColumnFilterCriteria(col1,filterCriteria1); 

   Logger.log("Filter has been added.");

   var rangeData = sheet1.getDataRange();
    
   const sheet2 = ss.getSheetByName("Archive");
   
   range.copyTo(sheet2.getRange(3,1));

   filter.remove();
}

What I end up copying: You can see the extra row here

Thanks

CodePudding user response:

Your screenshots suggest that rows Archive!1:2 already contain the headers as static text. You are copying all of range below those two rows. The problem is that range includes the header row. You can try fixing that like this:

   const range = sheet1.getRange('A2:S');

You are overwriting the previous data in Archive each time you run the script. To append data rather than overwrite it, you can use the appendRows_() utility function, like this:

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);
}
  • Related