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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SerializedAttributes

#declared_attributes, 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



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

def initialize(options={})
  parse_options options
end

Instance Attribute Details

#blankBoolean

Flag indicating whether to filter by blank.

Returns:

  • (Boolean)


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

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.



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

def calendar_type
  @calendar_type
end

Instance Method Details

#apply(cell) ⇒ Object

Tells us if the row of the cell provided should be filterd 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



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

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



57
58
59
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 57

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.



98
99
100
101
102
103
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 98

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



52
53
54
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 52

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?)



88
89
90
91
92
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 88

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



77
78
79
80
81
82
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 77

def to_xml_string(str = '')
  str << "<filters #{serialized_attributes}>"
  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