class Axlsx::AutoFilter

This class represents an auto filter range in a worksheet

Attributes

range[RW]

The range the autofilter should be applied to. This should be a string like ‘A1:B8’ @return [String]

worksheet[R]

Public Class Methods

new(worksheet) click to toggle source

creates a new Autofilter object @param [Worksheet] worksheet

# File lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb, line 12
def initialize(worksheet)
  raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
  @worksheet = worksheet
end

Public Instance Methods

add_column(col_id, filter_type, options = {}) click to toggle source

Adds a filter column. This is the recommended way to create and manage filter columns for your autofilter. In addition to the require id and type parameters, options will be passed to the filter column during instantiation. @param [String] col_id Zero-based index indicating the AutoFilter column to which this filter information applies. @param [Symbol] filter_type A symbol representing one of the supported filter types. @param [Hash] options a hash of options to pass into the generated filter @return [FilterColumn]

# File lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb, line 45
def add_column(col_id, filter_type, options = {})
  columns << FilterColumn.new(col_id, filter_type, options)
  columns.last
end
apply() click to toggle source

actually performs the filtering of rows who’s cells do not match the filter.

# File lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb, line 52
def apply
  first_cell, last_cell = range.split(':')
  start_point = Axlsx::name_to_indices(first_cell)
  end_point = Axlsx::name_to_indices(last_cell)
  # The +1 is so we skip the header row with the filter drop downs
  rows = worksheet.rows[(start_point.last+1)..end_point.last] || []

  column_offset = start_point.first
  columns.each do |column|
    rows.each do |row|
      next if row.hidden
      column.apply(row, column_offset)
    end
  end
end
columns() click to toggle source

A collection of filterColumns for this auto_filter @return [SimpleTypedList]

# File lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb, line 35
def columns
  @columns ||= SimpleTypedList.new FilterColumn
end
defined_name() click to toggle source

the formula for the defined name required for this auto filter This prepends the worksheet name to the absolute cell reference e.g. A1:B2 -> ‘Sheet1’!$A$1:$B$2 @return [String]

# File lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb, line 28
def defined_name
  return unless range
  Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name)})
end
to_xml_string(str='') click to toggle source

serialize the object @return [String]

# File lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb, line 69
def to_xml_string(str='')
  return unless range
  str << "<autoFilter ref='#{range}'>"
  columns.each { |filter_column| filter_column.to_xml_string(str) }
  str << "</autoFilter>"
end