Skip to content
Browse files

Finished and Specced Auto Filter with Filters (standard) filter support

This means you can add autofilters that will restrict the visible rows
based on a specified value.
  • Loading branch information...
1 parent 000423e commit a1f64c74f44c3ce2c32061d1adf9baab94aef4ba @randym committed
View
3 lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
@@ -54,7 +54,8 @@ def apply
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]
+ rows = worksheet.rows[(start_point.last+1)..end_point.last] || []
+
column_offset = start_point.first
columns.each do |column|
rows.each do |row|
View
8 test/workbook/worksheet/auto_filter/tc_auto_filter.rb
@@ -4,9 +4,10 @@ class TestAutoFilter < Test::Unit::TestCase
def setup
ws = Axlsx::Package.new.workbook.add_worksheet
- 3.times { ws.add_row [1,2,3] }
+ 3.times { |index| ws.add_row [1*index,2*index,3*index] }
@auto_filter = ws.auto_filter
@auto_filter.range = 'A1:C3'
+ @auto_filter.add_column 0, :filters, :filter_items => [1]
end
def test_defined_name
@@ -29,4 +30,9 @@ def test_add_column
end
end
+ def test_applya
+ assert_equal nil, @auto_filter.worksheet.rows.last.hidden
+ @auto_filter.apply
+ assert_equal true, @auto_filter.worksheet.rows.last.hidden
+ end
end
View
28 test/workbook/worksheet/auto_filter/tc_filters.rb
@@ -2,15 +2,13 @@
class TestFilters < Test::Unit::TestCase
def setup
- @filters = Axlsx::Filters.new(:filter_items => [1, 'a'], :date_group_items =>[ { :date_time_grouping => :year, :year => 2012 } ] , :blank => true)
+ @filters = Axlsx::Filters.new(:filter_items => [1, 'a'],
+ :date_group_items =>[ { :date_time_grouping => :year, :year => 2011, :month => 11, :day => 11, :hour => 0, :minute => 0, :second => 0 } ] ,
+ :blank => true)
end
- def test_initialize
- assert_equal Axlsx::Filters::CALENDAR_TYPES.first, @filters.calendar_type
- end
-
- def blank
- assert_equal false, @filters.blank
+ def test_blank
+ assert_equal true, @filters.blank
assert_raise(ArgumentError) { @filters.blank = :only_if_you_want_it }
@filters.blank = true
assert_equal true, @filters.blank
@@ -31,6 +29,22 @@ def test_date_group_items
assert @filters.date_group_items.is_a?(Array)
assert_equal 1, @filters.date_group_items.size
end
+
+ def test_apply_is_false_for_matching_values
+ keeper = Object.new
+ def keeper.value; 'a'; end
+ assert_equal false, @filters.apply(keeper)
+ end
+ def test_apply_is_true_for_non_matching_values
+ hidden = Object.new
+ def hidden.value; 'b'; end
+ assert_equal true, @filters.apply(hidden)
+ end
+
+ def test_to_xml_string
+ doc = Nokogiri::XML(@filters.to_xml_string)
+ assert_equal(1, doc.xpath('//filters[@blank="true"]').size)
+ end
end

0 comments on commit a1f64c7

Please sign in to comment.
Something went wrong with that request. Please try again.