Skip to content

Commit

Permalink
Add auto_filter defined name in Axlsx::Worksheet#to_xml_string (refs
Browse files Browse the repository at this point in the history
…randym#285)

The way ot was done previously (adding the defined name on
`Axlsx::Worksheet#auto_filter=`) meant that it was added only when
using this method. But it's possible to assign range on the auto-created
`AutoFilter` with:

```ruby
workheet.auto_filter.range = "A1:B2"
```

In this case the defined name was never added to the workbook.
  • Loading branch information
JonathanTron committed Feb 26, 2014
1 parent d7b4ae4 commit 450d5e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/axlsx/workbook/worksheet/worksheet.rb
Expand Up @@ -367,7 +367,6 @@ def name=(name)
def auto_filter=(v)
DataTypeValidator.validate "Worksheet.auto_filter", String, v
auto_filter.range = v
workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1
end

# Accessor for controlling whether leading and trailing spaces in cells are
Expand Down Expand Up @@ -609,7 +608,10 @@ def to_sheet_node_xml_string(str='')
# This intentionally does not use nokogiri for performance reasons
# @return [String]
def to_xml_string
auto_filter.apply if auto_filter.range
if auto_filter.range
auto_filter.apply
workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1
end
str = '<?xml version="1.0" encoding="UTF-8"?>'
str << worksheet_node
serializable_parts.each do |item|
Expand Down
11 changes: 11 additions & 0 deletions test/workbook/worksheet/tc_worksheet.rb
Expand Up @@ -514,7 +514,18 @@ def test_auto_filter
assert_raise(ArgumentError) { @ws.auto_filter = 123 }
@ws.auto_filter.range = "A1:D9"
assert_equal(@ws.auto_filter.range, "A1:D9")
@ws.to_xml_string
assert(@wb.defined_names.any?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
end

def test_auto_filter_assign
assert(@ws.auto_filter.range.nil?)
assert(@wb.defined_names.none?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
assert_raise(ArgumentError) { @ws.auto_filter = 123 }
@ws.auto_filter = "A1:D9"
assert_equal(@ws.auto_filter.range, "A1:D9")
@ws.to_xml_string
assert(@wb.defined_names.any?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
end

def test_sheet_pr_for_auto_filter
Expand Down

0 comments on commit 450d5e7

Please sign in to comment.