summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Gardiner <[email protected]>2013-04-10 14:29:11 +0100
committerAdam Gardiner <[email protected]>2013-04-10 14:29:11 +0100
commitbe5cbe443d7cea3dd7cfc8aec4d984c34a0fb9bf (patch)
tree407e4f71011bf326b257a1a2db04ef601444a3bb
parent23cc21d13f9e761d110d466428d89389d03aa61a (diff)
downloadcaxlsx-be5cbe443d7cea3dd7cfc8aec4d984c34a0fb9bf.tar.gz
caxlsx-be5cbe443d7cea3dd7cfc8aec4d984c34a0fb9bf.zip
Add support for preserving leading and trailing spaces in cell values
-rw-r--r--lib/axlsx/workbook/worksheet/cell_serializer.rb10
-rw-r--r--test/workbook/worksheet/tc_cell.rb13
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 xml:space="preserve">' << t << '</t>'
+ else
+ t = '<t>' << 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 << "</rPr>" << "<t>" << cell.value.to_s << "</t></r>"
+ str << "</rPr>" << t << "</r>"
else
- str << "<t>" << cell.value.to_s << "</t>"
+ 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