From 0d54b473cfa8a3325e1bc3fcd5525ebb7f4b68cb Mon Sep 17 00:00:00 2001 From: Arkadiy Butermanov Date: Wed, 23 Mar 2016 15:52:49 +0300 Subject: Implement :text cell type --- lib/axlsx/workbook/worksheet/cell.rb | 2 +- lib/axlsx/workbook/worksheet/cell_serializer.rb | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index cd832f7b..a96b3cb7 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -66,7 +66,7 @@ module Axlsx :vertAlign, :sz, :color, :scheme].freeze CELL_TYPES = [:date, :time, :float, :integer, :richtext, - :string, :boolean, :iso_8601].freeze + :string, :boolean, :iso_8601, :text].freeze # The index of the cellXfs item to be applied to this cell. # @return [Integer] diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index 9a9f9465..4da391b6 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -14,7 +14,7 @@ module Axlsx method = cell.type self.send(method, cell, str) str << '' - end + end # builds an xml text run based on this cells attributes. # @param [String] str The string instance this run will be concated to. @@ -22,7 +22,7 @@ module Axlsx def run_xml_string(cell, str = '') if cell.is_text_run? valid = RichTextRun::INLINE_STYLES - [:value, :type] - data = Hash[cell.instance_values.map{ |k, v| [k.to_sym, v] }] + data = Hash[cell.instance_values.map{ |k, v| [k.to_sym, v] }] data = data.select { |key, value| valid.include?(key) && !value.nil? } RichText.new(cell.value.to_s, data).to_xml_string(str) elsif cell.contains_rich_text? @@ -124,7 +124,7 @@ module Axlsx inline_string_serialization cell, str end end - + # Serializes cells that are of the type richtext # @param [Cell] cell The cell that is being serialized # @param [String] str The string the serialized content will be appended to. @@ -137,6 +137,14 @@ module Axlsx end end + # Serializes cells that are of the type text + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] + def text(cell, str='') + inline_string_serialization cell, str + end + private def numeric(cell, str = '') -- cgit v1.2.3 From a0dc908f9df76f8c31a3238b1829eea2fd10c666 Mon Sep 17 00:00:00 2001 From: Arkadiy Butermanov Date: Tue, 5 Apr 2016 14:06:10 +0300 Subject: Escape html in :text type --- lib/axlsx/workbook/worksheet/cell.rb | 4 ++-- lib/axlsx/workbook/worksheet/cell_serializer.rb | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index a96b3cb7..777b7812 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -123,7 +123,7 @@ module Axlsx # Indicates if the cell is good for shared string table def plain_string? - type == :string && # String typed + (type == :string || type == :text) && # String typed !is_text_run? && # No inline styles !@value.nil? && # Not nil !@value.empty? && # Not empty @@ -368,7 +368,7 @@ module Axlsx # TODO find a better way to do this as it accounts for 30% of # processing time in benchmarking... def clean_value - if type == :string && !Axlsx::trust_input + if (type == :string || type == :text) && !Axlsx::trust_input Axlsx::sanitize(::CGI.escapeHTML(@value.to_s)) else @value.to_s diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index 4da391b6..76a3c386 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -141,8 +141,12 @@ module Axlsx # @param [Cell] cell The cell that is being serialized # @param [String] str The string the serialized content will be appended to. # @return [String] - def text(cell, str='') - inline_string_serialization cell, str + def text(cell, str) + if cell.ssti.nil? + inline_string_serialization cell, str + else + value_serialization 's', cell.ssti, str + end end private -- cgit v1.2.3 From c06ac853c57cf1aca2dd258915dd12003d9f9859 Mon Sep 17 00:00:00 2001 From: Arkadiy Butermanov Date: Thu, 3 Nov 2016 18:27:00 +0300 Subject: Add tests --- test/workbook/worksheet/tc_cell.rb | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index 5ae2a51f..428e55a5 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -238,7 +238,7 @@ class TestCell < Test::Unit::TestCase @c.merge @row.cells.last assert_equal(@c.row.worksheet.send(:merged_cells).last, "A1:C1") end - + def test_reverse_merge_with_cell @c.row.add_cell 2 @c.row.add_cell 3 @@ -284,7 +284,7 @@ class TestCell < Test::Unit::TestCase c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) assert_equal(c_xml.xpath("/c[@s=1]").size, 1) end - + def test_to_xml_string_with_run # Actually quite a number of similar run styles # but the processing should be the same @@ -319,6 +319,21 @@ class TestCell < Test::Unit::TestCase assert(doc.xpath("//f[@ref='A1']")) end + def test_to_xml_string_text_formula + p = Axlsx::Package.new + ws = p.workbook.add_worksheet do |sheet| + sheet.add_row ["=1+1", "-1+1"], type: :text + end + doc = Nokogiri::XML(ws.to_xml_string) + doc.remove_namespaces! + + assert(doc.xpath("//f[text()='1+1']").empty?) + assert(doc.xpath("//t[text()='=1+1']").any?) + + assert(doc.xpath("//f[text()='1+1']").empty?) + assert(doc.xpath("//t[text()='-1+1']").any?) + end + def test_font_size_with_custom_style_and_no_sz @c.style = @c.row.worksheet.workbook.styles.add_style :bg_color => 'FF00FF' sz = @c.send(:font_size) @@ -335,17 +350,17 @@ class TestCell < Test::Unit::TestCase sz = @c.send(:font_size) assert_equal(sz, 52) end - + def test_cell_with_sz @c.sz = 25 assert_equal(25, @c.send(:font_size)) end - + def test_to_xml # TODO This could use some much more stringent testing related to the xml content generated! @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)", "2013-01-13T13:31:25.123"] @ws.rows.last.cells[5].u = true - + schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@ws.to_xml_string) errors = [] -- cgit v1.2.3