Class: Axlsx::AutoFilter
- Inherits:
-
Object
- Object
- Axlsx::AutoFilter
- Defined in:
- lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
Overview
This class represents an auto filter range in a worksheet
Instance Attribute Summary collapse
-
#range ⇒ String
The range the autofilter should be applied to.
-
#worksheet ⇒ Object
readonly
Returns the value of attribute worksheet.
Instance Method Summary collapse
-
#add_column(col_id, filter_type, options = {}) ⇒ FilterColumn
Adds a filter column.
-
#apply ⇒ Object
actually performs the filtering of rows who's cells do not match the filter.
-
#columns ⇒ SimpleTypedList
A collection of filterColumns for this auto_filter.
-
#defined_name ⇒ String
the formula for the defined name required for this auto filter This prepends the worksheet name to the absolute cell reference e.g.
-
#initialize(worksheet) ⇒ AutoFilter
constructor
creates a new Autofilter object.
-
#to_xml_string(str = '') ⇒ String
serialize the object.
Constructor Details
#initialize(worksheet) ⇒ AutoFilter
creates a new Autofilter object
9 10 11 12 13 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 9 def initialize(worksheet) raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet) @worksheet = worksheet end |
Instance Attribute Details
#range ⇒ String
The range the autofilter should be applied to. This should be a string like 'A1:B8'
20 21 22 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 20 def range @range end |
#worksheet ⇒ Object (readonly)
Returns the value of attribute worksheet.
15 16 17 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 15 def worksheet @worksheet end |
Instance Method Details
#add_column(col_id, filter_type, options = {}) ⇒ FilterColumn
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.
44 45 46 47 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 44 def add_column(col_id, filter_type, = {}) columns << FilterColumn.new(col_id, filter_type, ) columns.last end |
#apply ⇒ Object
actually performs the filtering of rows who's cells do not match the filter.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 51 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 ⇒ SimpleTypedList
A collection of filterColumns for this auto_filter
34 35 36 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 34 def columns @columns ||= SimpleTypedList.new FilterColumn end |
#defined_name ⇒ String
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
26 27 28 29 30 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 26 def defined_name return unless range Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name) }) end |
#to_xml_string(str = '') ⇒ String
serialize the object
70 71 72 73 74 75 76 |
# File 'lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb', line 70 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 |