From be5cbe443d7cea3dd7cfc8aec4d984c34a0fb9bf Mon Sep 17 00:00:00 2001 From: Adam Gardiner Date: Wed, 10 Apr 2013 14:29:11 +0100 Subject: Add support for preserving leading and trailing spaces in cell values --- lib/axlsx/workbook/worksheet/cell_serializer.rb | 10 ++++++++-- test/workbook/worksheet/tc_cell.rb | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index e4c35c3a..b054316b 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -23,6 +23,12 @@ module Axlsx # @param [String] str The string instance this run will be concated to. # @return [String] def run_xml_string(cell, str = '') + t = cell.value.to_s + if t[0, 1] == ' ' || t[-1, 1] == ' ' + t = '' << t << '' + else + t = '' << t << '' + end if cell.is_text_run? data = cell.instance_values.reject{|key, value| value == nil || key == 'value' || key == 'type' } keys = data.keys & Cell::INLINE_STYLES @@ -37,9 +43,9 @@ module Axlsx str << "<" << key.to_s << " val='" << data[key].to_s << "'/>" end end - str << "" << "" << cell.value.to_s << "" + str << "" << t << "" else - str << "" << cell.value.to_s << "" + str << t end str end diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index 208b15e0..9b8cd769 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -275,7 +275,20 @@ class TestCell < Test::Unit::TestCase end doc = Nokogiri::XML(ws.to_xml_string) assert(doc.xpath("//f[@text()='IF(2+2=4,4,5)']")) + end + def test_to_xml_string_with_leading_or_trailing_spaces + # Check that xml:space="preserve" has been added when cell contains leading or trailing spaces + @c.type = :string + @c.value = " a" + c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) + assert(c_xml.xpath("//t/@xml:space='preserve'")) + @c.value = "a " + c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) + assert(c_xml.xpath("//t/@xml:space='preserve'")) + @c.value = "a" + c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) + assert(!c_xml.xpath("//t/@xml:space='preserve'")) end def test_font_size_with_custom_style_and_no_sz -- cgit v1.2.3 From bd6a3ebf91fd0677f3d31ecba4a22b9edb812406 Mon Sep 17 00:00:00 2001 From: Adam Gardiner Date: Mon, 15 Apr 2013 10:49:02 +0100 Subject: Add benchmark for preserve space --- test/benchmark.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/benchmark.rb b/test/benchmark.rb index 2ef82eaf..3f017874 100644 --- a/test/benchmark.rb +++ b/test/benchmark.rb @@ -8,6 +8,7 @@ Axlsx::trust_input = true row = [] input = (32..126).to_a.pack('U*').chars.to_a 20.times { row << input.shuffle.join} +row_with_spaces = row.map{ |v| " #{v}" } times = 3000 Benchmark.bmbm(30) do |x| @@ -69,5 +70,18 @@ Benchmark.bmbm(30) do |x| end end } + + x.report('preserve-spaces') { + p = Axlsx::Package.new + p.workbook do |wb| + wb.add_worksheet do |sheet| + times.times do + sheet << row_with_spaces + end + end + end + s = p.to_stream() + File.open('example_preserve_spaces.xlsx', 'w') { |f| f.write(s.read) } + } end -File.delete("example.csv", "example_streamed.xlsx", "example_shared.xlsx", "example_autowidth.xlsx", "example_noautowidth.xlsx") +File.delete("example.csv", "example_streamed.xlsx", "example_shared.xlsx", "example_autowidth.xlsx", "example_noautowidth.xlsx", "example_preserve_spaces.xlsx") -- cgit v1.2.3 From e76c93bcbf842f01a02a2485873c5eeed3838bf4 Mon Sep 17 00:00:00 2001 From: Adam Gardiner Date: Sat, 27 Apr 2013 16:33:35 +0100 Subject: Revert changes to cell serialization --- lib/axlsx/workbook/worksheet/cell_serializer.rb | 10 ++-------- test/benchmark.rb | 16 +--------------- test/workbook/worksheet/tc_cell.rb | 13 ------------- 3 files changed, 3 insertions(+), 36 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index b054316b..e4c35c3a 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -23,12 +23,6 @@ module Axlsx # @param [String] str The string instance this run will be concated to. # @return [String] def run_xml_string(cell, str = '') - t = cell.value.to_s - if t[0, 1] == ' ' || t[-1, 1] == ' ' - t = '' << t << '' - else - t = '' << t << '' - end if cell.is_text_run? data = cell.instance_values.reject{|key, value| value == nil || key == 'value' || key == 'type' } keys = data.keys & Cell::INLINE_STYLES @@ -43,9 +37,9 @@ module Axlsx str << "<" << key.to_s << " val='" << data[key].to_s << "'/>" end end - str << "" << t << "" + str << "" << "" << cell.value.to_s << "" else - str << t + str << "" << cell.value.to_s << "" end str end diff --git a/test/benchmark.rb b/test/benchmark.rb index 3f017874..2ef82eaf 100644 --- a/test/benchmark.rb +++ b/test/benchmark.rb @@ -8,7 +8,6 @@ Axlsx::trust_input = true row = [] input = (32..126).to_a.pack('U*').chars.to_a 20.times { row << input.shuffle.join} -row_with_spaces = row.map{ |v| " #{v}" } times = 3000 Benchmark.bmbm(30) do |x| @@ -70,18 +69,5 @@ Benchmark.bmbm(30) do |x| end end } - - x.report('preserve-spaces') { - p = Axlsx::Package.new - p.workbook do |wb| - wb.add_worksheet do |sheet| - times.times do - sheet << row_with_spaces - end - end - end - s = p.to_stream() - File.open('example_preserve_spaces.xlsx', 'w') { |f| f.write(s.read) } - } end -File.delete("example.csv", "example_streamed.xlsx", "example_shared.xlsx", "example_autowidth.xlsx", "example_noautowidth.xlsx", "example_preserve_spaces.xlsx") +File.delete("example.csv", "example_streamed.xlsx", "example_shared.xlsx", "example_autowidth.xlsx", "example_noautowidth.xlsx") diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index 9b8cd769..208b15e0 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -275,20 +275,7 @@ class TestCell < Test::Unit::TestCase end doc = Nokogiri::XML(ws.to_xml_string) assert(doc.xpath("//f[@text()='IF(2+2=4,4,5)']")) - end - def test_to_xml_string_with_leading_or_trailing_spaces - # Check that xml:space="preserve" has been added when cell contains leading or trailing spaces - @c.type = :string - @c.value = " a" - c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) - assert(c_xml.xpath("//t/@xml:space='preserve'")) - @c.value = "a " - c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) - assert(c_xml.xpath("//t/@xml:space='preserve'")) - @c.value = "a" - c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) - assert(!c_xml.xpath("//t/@xml:space='preserve'")) end def test_font_size_with_custom_style_and_no_sz -- cgit v1.2.3 From d68df9e5088c7ce23f550968df1280a31f6a8d48 Mon Sep 17 00:00:00 2001 From: Adam Gardiner Date: Sat, 27 Apr 2013 18:01:29 +0100 Subject: Add a preserve_spaces option to worksheet, defaults to true --- lib/axlsx/workbook/worksheet/worksheet.rb | 9 ++++++++- test/workbook/worksheet/tc_worksheet.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 5f650263..36031be7 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -37,6 +37,7 @@ module Axlsx @page_setup = PageSetup.new options[:page_setup] if options[:page_setup] @print_options = PrintOptions.new options[:print_options] if options[:print_options] @header_footer = HeaderFooter.new options[:header_footer] if options[:header_footer] + @preserve_spaces = options.fetch(:preserve_spaces, true) end # The name of the worksheet @@ -327,6 +328,10 @@ module Axlsx auto_filter.range = v end + # Accessor for controlling whether leading and trailing spaces in cells are + # preserved or ignored. The default is to preserve spaces. + attr_accessor :preserve_spaces + # The part name of this worksheet # @return [String] def pn @@ -699,7 +704,9 @@ module Axlsx # Helper method for parsingout the root node for worksheet # @return [String] def worksheet_node - "" % [XML_NS, XML_NS_R] + (@preserve_spaces ? + "" : + "") % [XML_NS, XML_NS_R] end def sheet_data diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index ed8c7cab..c2bb0faa 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -352,6 +352,15 @@ class TestWorksheet < Test::Unit::TestCase assert(schema.validate(doc).map{ |e| puts e.message; e }.empty?, "error free validation") end + def test_to_xml_string_with_preserve_spaces + # Check that xml:space="preserve" has been added when preserve_spaces is set + ws_xml = Nokogiri::XML(@ws.to_xml_string) + assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='preserve'")) + @ws.preserve_spaces = false + ws_xml = Nokogiri::XML(@ws.to_xml_string) + assert(!ws_xml.xpath("//xmlns:worksheet/@xml:space='preserve'")) + end + def test_styles assert(@ws.styles.is_a?(Axlsx::Styles), 'worksheet provides access to styles') end -- cgit v1.2.3