diff options
| author | Randy Morgan <[email protected]> | 2013-04-28 12:55:46 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2013-04-28 12:55:46 +0900 |
| commit | e49aee3e2f700569a519c5346746d239a05383cf (patch) | |
| tree | 7f449191165a3fbdf05b34c4239dda6ec179d033 | |
| parent | a120fdb2e787986ced4952b6ef3f4b70056960d9 (diff) | |
| download | caxlsx-e49aee3e2f700569a519c5346746d239a05383cf.tar.gz caxlsx-e49aee3e2f700569a519c5346746d239a05383cf.zip | |
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.
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | examples/2010_comments.rb | 17 | ||||
| -rw-r--r-- | lib/axlsx/version.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/workbook/shared_strings_table.rb | 14 | ||||
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 20 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 11 | ||||
| -rw-r--r-- | lib/schema/sml.xsd | 3 | ||||
| -rw-r--r-- | test/workbook/tc_shared_strings_table.rb | 6 | ||||
| -rw-r--r-- | test/workbook/tc_workbook.rb | 17 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 9 |
10 files changed, 88 insertions, 19 deletions
@@ -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 - '<?xml version="1.0" encoding="UTF-8"?><sst xmlns="' << XML_NS << '" count="' << @count.to_s << '" uniqueCount="' << unique_count.to_s << '">' << @shared_xml_string << '</sst>' + def to_xml_string(str='') + str << '<?xml version="1.0" encoding="UTF-8"?><sst xmlns="' << XML_NS << '"' + str << ' count="' << @count.to_s << '" uniqueCount="' << unique_count.to_s << '"' + str << 'xml:space="' << xml_space.to_s << '>' << @shared_xml_string << '</sst>' end private diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index e329ae74..1397552a 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -285,7 +285,25 @@ require 'axlsx/workbook/worksheet/selection.rb' # generates a shared string object against all cells in all worksheets. # @return [SharedStringTable] def shared_strings - SharedStringsTable.new(worksheets.collect { |ws| ws.cells }) + SharedStringsTable.new(worksheets.collect { |ws| ws.cells }, xml_space) + end + + # The xml:space attribute for the worksheet. + # This determines how whitespace is handled withing the document. + # The most relevant part being whitespace in the cell text. + # allowed values are :preserve and :default. Axlsx uses :preserve unless + # you explicily set this to :default. + # @return Symbol + def xml_space + @xml_space ||= :preserve + end + + # Sets the xml:space attribute for the worksheet + # @see Worksheet#xml_space + # @param [Symbol] space must be one of :preserve or :default + def xml_space=(space) + Axlsx::RestrictionValidator.validate(:xml_space, [:preserve, :default], space) + @xml_space = space; end # returns a range of cells in a worksheet diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 36031be7..32e616e2 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -37,9 +37,9 @@ 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 # @return [String] def name @@ -634,6 +634,11 @@ module Axlsx end private + + def xml_space + workbook.xml_space + end + def outline(collection, range, level = 1, collapsed = true) range.each do |index| unless (item = collection[index]).nil? @@ -704,9 +709,7 @@ module Axlsx # Helper method for parsingout the root node for worksheet # @return [String] def worksheet_node - (@preserve_spaces ? - "<worksheet xmlns=\"%s\" xmlns:r=\"%s\" xml:space=\"preserve\">" : - "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">") % [XML_NS, XML_NS_R] + "<worksheet xmlns=\"%s\" xmlns:r=\"%s\" xml:space=\"#{xml_space}\">" % [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 @@ <xsd:import
namespace="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
schemaLocation="dml-spreadsheetDrawing.xsd"/>
+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/> + <xsd:complexType name="CT_AutoFilter">
<xsd:sequence>
<xsd:element name="filterColumn" minOccurs="0" maxOccurs="unbounded" type="CT_FilterColumn"/>
@@ -2213,6 +2215,7 @@ <xsd:element name="tableParts" type="CT_TableParts" minOccurs="0" maxOccurs="1"/>
<xsd:element name="extLst" type="CT_ExtensionList" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
+ <xsd:attribute ref="xml:space"/> </xsd:complexType>
<xsd:complexType name="CT_SheetData">
<xsd:sequence>
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 |
