summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb4
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb12
-rw-r--r--test/workbook/tc_workbook.rb4
3 files changed, 12 insertions, 8 deletions
diff --git a/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb b/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
index 05da57ef..9fd6be18 100644
--- a/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
+++ b/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb
@@ -47,8 +47,8 @@ module Axlsx
columns.last
end
- # actually performs the filtering of rows who's cells do not
- # match the filter.
+ # actually performs the filtering of rows who's cells do not
+ # match the filter.
def apply
first_cell, last_cell = range.split(':')
start_point = Axlsx::name_to_indices(first_cell)
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index fcd883e1..f034a000 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -598,6 +598,7 @@ module Axlsx
# Returns a sheet node serialization for this sheet in the workbook.
def to_sheet_node_xml_string(str='')
+ add_autofilter_defined_name_to_workbook
str << '<sheet '
serialized_attributes str
str << "r:id='" << rId << "'"
@@ -608,10 +609,8 @@ module Axlsx
# This intentionally does not use nokogiri for performance reasons
# @return [String]
def to_xml_string
- 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
+ add_autofilter_defined_name_to_workbook
+ auto_filter.apply if auto_filter.range
str = '<?xml version="1.0" encoding="UTF-8"?>'
str << worksheet_node
serializable_parts.each do |item|
@@ -795,5 +794,10 @@ module Axlsx
column_info[index] ||= Col.new(index + 1, index + 1)
end
+ def add_autofilter_defined_name_to_workbook
+ return if workbook.defined_names.any?{|dn| dn.name=='_xlnm._FilterDatabase'} || !auto_filter.range
+ workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1
+ end
+
end
end
diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb
index 2a51e4bd..51dd01f4 100644
--- a/test/workbook/tc_workbook.rb
+++ b/test/workbook/tc_workbook.rb
@@ -38,7 +38,7 @@ class TestWorkbook < Test::Unit::TestCase
@wb.add_worksheet(:name=>'foo')
@wb.add_worksheet(:name=>'bar')
assert_equal('foo', @wb.sheet_by_name('foo').name)
-
+
end
def test_date1904
assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
@@ -71,7 +71,7 @@ class TestWorkbook < Test::Unit::TestCase
assert_equal(@wb.worksheets.first, ws, "the worksheet returned is the worksheet added")
assert_equal(ws.name, "bob", "name option gets passed to worksheet")
end
-
+
def test_insert_worksheet
@wb.add_worksheet(:name => 'A')
@wb.add_worksheet(:name => 'B')