summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb9
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb9
2 files changed, 17 insertions, 1 deletions
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
- "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">" % [XML_NS, XML_NS_R]
+ (@preserve_spaces ?
+ "<worksheet xmlns=\"%s\" xmlns:r=\"%s\" xml:space=\"preserve\">" :
+ "<worksheet xmlns=\"%s\" xmlns:r=\"%s\">") % [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