Class: Axlsx::Filters

Inherits:
Object
  • Object
show all
Includes:
OptionsParser, SerializedAttributes
Defined in:
lib/axlsx/workbook/worksheet/auto_filter/filters.rb

Overview

When multiple values are chosen to filter by, or when a group of date values are chosen to filter by, this object groups those criteria together.

Defined Under Namespace

Classes: DateGroupItem, Filter

Constant Summary collapse

CALENDAR_TYPES =

Allowed calendar types

%w(gregorian gregorianUs gregorianMeFrench gregorianArabic hijri hebrew taiwan japan thai korea saka gregorianXlitEnglish gregorianXlitFrench none).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SerializedAttributes

included, #serialized_attributes, #serialized_element_attributes, #serialized_tag

Methods included from OptionsParser

#parse_options

Constructor Details

#initialize(options = {}) ⇒ Filters

Note:

The recommended way to interact with filter objects is via AutoFilter#add_column

Creates a new Filters object

Examples:

ws.auto_filter.add_column(0, :filters, :blank => true, :calendar_type => 'japan', :filter_items => [100, 'a'])

Parameters:

  • options (Hash) (defaults to: {})

    Options used to set this objects attributes and create filter and/or date group items

  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Array] (Hash)

    a customizable set of options



20
21
22
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 20

def initialize(options = {})
  parse_options options
end

Instance Attribute Details

#blankBoolean

Flag indicating whether to filter by blank.

Returns:

  • (Boolean)


31
32
33
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 31

def blank
  @blank
end

#calendar_typeObject

Calendar type for date grouped items. Used to interpret the values in dateGroupItem. This is the calendar type used to evaluate all dates in the filter column, even when those dates are not using the same calendar system / date formatting.



37
38
39
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 37

def calendar_type
  @calendar_type
end

Instance Method Details

#apply(cell) ⇒ Object

Tells us if the row of the cell provided should be filtered as it does not meet any of the specified filter_items or date_group_items restrictions. TODO implement this for date filters as well!

Parameters:

  • cell (Cell)

    The cell to test against items



44
45
46
47
48
49
50
51
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 44

def apply(cell)
  return false unless cell

  filter_items.each do |filter|
    return false if cell.value == filter.val
  end
  true
end

#date_group_itemsObject

the date group values in this filters object



59
60
61
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 59

def date_group_items
  @date_group_items ||= []
end

#date_group_items=(options) ⇒ Object

Note:

This can be specified, but will not be applied to the date

Date group items are date group filter items where you specify the date_group and a value for that option as part of the auto_filter values in your workbook at this time.



102
103
104
105
106
107
108
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 102

def date_group_items=(options)
  options.each do |date_group|
    raise ArgumentError, "date_group_items should be an array of hashes specifying the options for each date_group_item" unless date_group.is_a?(Hash)

    date_group_items << DateGroupItem.new(date_group)
  end
end

#filter_itemsObject

The filter values in this filters object



54
55
56
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 54

def filter_items
  @filter_items ||= []
end

#filter_items=(values) ⇒ Object

not entirely happy with this. filter_items should be a simple typed list that overrides << etc to create Filter objects from the inserted values. However this is most likely so rarely used...(really? do you know that?)



92
93
94
95
96
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 92

def filter_items=(values)
  values.each do |value|
    filter_items << Filter.new(value)
  end
end

#to_xml_string(str = +'')) ⇒ Object

Serialize the object to xml



79
80
81
82
83
84
85
86
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 79

def to_xml_string(str = +'')
  str << '<filters '
  serialized_attributes(str)
  str << '>'
  filter_items.each { |filter| filter.to_xml_string(str) }
  date_group_items.each { |date_group_item| date_group_item.to_xml_string(str) }
  str << '</filters>'
end