From e49aee3e2f700569a519c5346746d239a05383cf Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 28 Apr 2013 12:55:46 +0900 Subject: Refactored and renamed space preservation preserve_spaces has been moved to the workbook and renamed xml_space as that provides a good reference for people trying to figure out what it does, and let's the author specify space preservation for serializations using the shared strings table as well as the default inline serialization in cells. --- README.md | 8 +++++++- examples/2010_comments.rb | 17 +++++++++++++++++ lib/axlsx/version.rb | 2 +- lib/axlsx/workbook/shared_strings_table.rb | 14 +++++++++++--- lib/axlsx/workbook/workbook.rb | 20 +++++++++++++++++++- lib/axlsx/workbook/worksheet/worksheet.rb | 11 +++++++---- lib/schema/sml.xsd | 3 +++ test/workbook/tc_shared_strings_table.rb | 6 ++++++ test/workbook/tc_workbook.rb | 17 +++++++++++++++++ test/workbook/worksheet/tc_worksheet.rb | 9 --------- 10 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 examples/2010_comments.rb diff --git a/README.md b/README.md index cb69e4fb..20f67088 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,10 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem, #Change log --------- +- **April.??.13**:1.37 + - Added space preservation for cell text. This will allow whitespace + in cell text both when using shared strings and when serializing + directly to the cell. - **April.24.13**:1.3.6 - Fixed LibreOffice/OpenOffice issue to properly apply colors to lines in charts. @@ -271,7 +275,9 @@ air and our feet on the ground. [nibus](https://github.com/nibus) - For patching sheet name uniqueness. -[scambra](https://github.com/scambra) - for keeping our lines in line! +[scambra](https://github.com/scambra) - For keeping our lines in line! + +[agardiner](https://github.com/agardiner) - For the preservation of space. #Copyright and License ---------- diff --git a/examples/2010_comments.rb b/examples/2010_comments.rb new file mode 100644 index 00000000..6a7bedf8 --- /dev/null +++ b/examples/2010_comments.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby -w -s +# -*- coding: utf-8 -*- +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" + +#```ruby +require 'axlsx' +p = Axlsx::Package.new +p.workbook.add_worksheet(:name => 'Excel 2010 comments') do |sheet| + sheet.add_row ['Cell with visible comment'] + sheet.add_row + sheet.add_row + sheet.add_row ['Cell with hidden comment'] + + sheet.add_comment :ref => 'A1', :author => 'XXX', :text => 'Visibile' + sheet.add_comment :ref => 'A4', :author => 'XXX', :text => 'Hidden', :visible => false +end +p.serialize('excel_2010_comment_test.xlsx') diff --git a/lib/axlsx/version.rb b/lib/axlsx/version.rb index 546f601d..6c9907ab 100644 --- a/lib/axlsx/version.rb +++ b/lib/axlsx/version.rb @@ -1,5 +1,5 @@ module Axlsx # The current version - VERSION = "1.3.6" + VERSION = "1.3.7" end diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 43e8a1a8..f1bee3ae 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -26,10 +26,16 @@ module Axlsx # @see Cell#sharable attr_reader :unique_cells + # The xml:space attribute + # @see Workbook#xml_space + attr_reader :xml_space + # Creates a new Shared Strings Table agains an array of cells # @param [Array] cells This is an array of all of the cells in the workbook - def initialize(cells) + # @param [Symbol] xml_space The xml:space behavior for the shared string table. + def initialize(cells, xml_space=:preserve) @index = 0 + @xml_space = xml_space @unique_cells = {} @shared_xml_string = "" shareable_cells = cells.flatten.select{ |cell| cell.plain_string? } @@ -40,8 +46,10 @@ module Axlsx # Serializes the object # @param [String] str # @return [String] - def to_xml_string - '' << @shared_xml_string << '' + def to_xml_string(str='') + str << '" : - "") % [XML_NS, XML_NS_R] + "" % [XML_NS, XML_NS_R] end def sheet_data diff --git a/lib/schema/sml.xsd b/lib/schema/sml.xsd index fa396e80..f3994e0a 100644 --- a/lib/schema/sml.xsd +++ b/lib/schema/sml.xsd @@ -13,6 +13,8 @@ + + @@ -2213,6 +2215,7 @@ + diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb index e3c9bf7b..7a333f4f 100644 --- a/test/workbook/tc_shared_strings_table.rb +++ b/test/workbook/tc_shared_strings_table.rb @@ -24,6 +24,12 @@ class TestSharedStringsTable < Test::Unit::TestCase assert_equal(sst.unique_count, 4) end + def test_uses_workbook_xml_space + assert_equal(@p.workbook.xml_space, @p.workbook.shared_strings.xml_space) + @p.workbook.xml_space = :default + assert_equal(:default, @p.workbook.shared_strings.xml_space) + end + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string) diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index 1a9b9669..591160f4 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -9,6 +9,23 @@ class TestWorkbook < Test::Unit::TestCase def teardown end + def test_worksheet_users_xml_space + sheet = @wb.add_worksheet(:name => 'foo') + ws_xml = Nokogiri::XML(sheet.to_xml_string) + assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='preserve'")) + + @wb.xml_space = :default + ws_xml = Nokogiri::XML(sheet.to_xml_string) + assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='default'")) + end + + def test_xml_space + assert_equal(:preserve, @wb.xml_space) + @wb.xml_space = :default + assert_equal(:default, @wb.xml_space) + assert_raise(ArgumentError) { @wb.xml_space = :none } + end + def test_no_autowidth assert_equal(@wb.use_autowidth, true) assert_raise(ArgumentError) {@wb.use_autowidth = 0.1} diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index c2bb0faa..ed8c7cab 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -352,15 +352,6 @@ 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