Class: Axlsx::Filters

Inherits:
Object
  • Object
show all
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

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
23
24
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 20

def initialize(options={})
  options.each do |key, value|
    self.send("#{key}=", value) if self.respond_to? "#{key}="
  end
end

Instance Attribute Details

#blankBoolean

Flag indicating whether to filter by blank.

Returns:

  • (Boolean)


28
29
30
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 28

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.



34
35
36
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 34

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



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

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



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

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.



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

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



50
51
52
# File 'lib/axlsx/workbook/worksheet/auto_filter/filters.rb', line 50

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



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

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



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

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