Home > Mobile >  How to copy the filter itself to another sheet within the same google spreadsheet?
How to copy the filter itself to another sheet within the same google spreadsheet?

Time:10-06

I got a filter in previous tabs, I can see the first row in those tabs with a dropdown that I can select/filter. Then I create a tab within the same spreadsheet and wanted to use the exact same first row as previous tabs. So I copied the first row to the new tab, but only text is copied but not the filter itself. Thanks.

CodePudding user response:

You need to use a script. Here it is (Replace Sheet1 and Sheet2 with your tab names):

const SRC = 'Sheet1'
const DST = 'Sheet2'

function copyFilter() {
  const ss = SpreadsheetApp.getActive()
  const shSrc = ss.getSheetByName(SRC)
  const shDst = ss.getSheetByName(DST)

  const existingFilter = shDst.getFilter()
  if(existingFilter){
    existingFilter.remove()
  }
  const headerRange = shSrc.getDataRange().offset(0,0,1)
  const filterRange = shDst.getRange(headerRange.getA1Notation())
  filterRange.setValues(headerRange.getValues())
  filterRange.createFilter()
}
  • Related