From a419b44526678ee1cffdcd6faddc0a6fa0473899 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 27 Mar 2012 16:38:27 +0900 Subject: FAST ENOUGH? user system total real axlsx_noautowidth 1.560000 0.030000 1.590000 ( 1.717595) axlsx 4.360000 0.140000 4.500000 ( 5.748329) axlsx_shared 6.880000 0.160000 7.040000 ( 9.325648) axlsx_stream 4.320000 0.120000 4.440000 ( 5.642124) csv 0.240000 0.010000 0.250000 ( 0.301004) --- lib/axlsx/workbook/shared_strings_table.rb | 9 ++++++--- lib/axlsx/workbook/worksheet/cell.rb | 23 +++++++++-------------- test/workbook/tc_shared_strings_table.rb | 2 +- test/workbook/worksheet/tc_cell.rb | 9 +++++---- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 2fffdd6b..5ebfd87f 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -36,7 +36,7 @@ module Axlsx end def to_xml_string - str = "%s" % [XML_NS, count, unique_count, @unique_cells.map {|cell| cell.run_xml_string}.join] + str = "%s" % [XML_NS, count, unique_count, @unique_cells.map {|cell| cell[:data]}.join] end # Generate the xml document for the Shared Strings Table @@ -62,10 +62,13 @@ module Axlsx # @return [Array] unique cells def resolve(cells) cells.each do |cell| - index = @unique_cells.index { |item| item.shareable(cell) } + cell_hash = cell.shareable_hash + index = @unique_cells.index do |item| + item[:hash] == cell_hash + end if index == nil cell.send :ssti=, @unique_cells.size - @unique_cells << cell + @unique_cells << {:hash => cell_hash, :data => ''<< cell.run_xml_string << ''} else cell.send :ssti=, index end diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 59ce0838..a3ec046a 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -224,16 +224,10 @@ module Axlsx # equality comparison to test value, type and inline style attributes # this is how we work out if the cell needs to be added or already exists in the shared strings table - def shareable(v) - - #using reject becase 1.8.7 select returns an array... - v_hash = v.instance_values.reject { |key, val| !INLINE_STYLES.include?(key) } + def shareable_hash self_hash = self.instance_values.reject { |key, val| !INLINE_STYLES.include?(key) } - # required as color is an object, and the comparison will fail even though both use the same color. - v_hash['color'] = v_hash['color'].instance_values if v_hash['color'] self_hash['color'] = self_hash['color'].instance_values if self_hash['color'] - - v_hash == self_hash + self_hash end # @return [Integer] The index of the cell in the containing row. @@ -284,7 +278,8 @@ module Axlsx def run_xml_string str = "" if is_text_run? - keys = self.instance_values.reject{|key, value| value == nil }.keys & INLINE_STYLES + data = self.instance_values.reject{|key, value| value == nil } + keys = data.keys & INLINE_STYLES keys.delete ['value', 'type'] str << "" keys.each do |key| @@ -292,9 +287,9 @@ module Axlsx when 'font_name' str << "" when 'color' - str << self.instance_values[key].to_xml_string + str << data[key].to_xml_string else - "<" << key << "val='" << self.instance_values[key] << "'/>" + "<" << key.to_s << " val='" << data[key].to_s << "'/>" end end str << "" << "" << value.to_s << "" @@ -354,11 +349,11 @@ module Axlsx end when :date # TODO: See if this is subject to the same restriction as Time below - '' << DateTimeConverter::date_to_serial(@value) << '' + '' << DateTimeConverter::date_to_serial(@value).to_s << '' when :time - '' << DateTimeConverter::time_to_serial(@value) << '' + '' << DateTimeConverter::time_to_serial(@value).to_s << '' when :boolean - '' << ssti << '' + '' << @value.to_s << '' else '' << @value.to_s << '' end diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb index 259f5970..e3c9bf7b 100644 --- a/test/workbook/tc_shared_strings_table.rb +++ b/test/workbook/tc_shared_strings_table.rb @@ -26,7 +26,7 @@ class TestSharedStringsTable < Test::Unit::TestCase def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) - doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml) + doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string) errors = [] schema.validate(doc).each do |error| puts error.message diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index a5b0442f..991181e7 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -206,13 +206,14 @@ class TestCell < Test::Unit::TestCase end def test_equality - c2 = @row.add_cell 1, :type=>:float, :style=>1 - assert(c2.shareable(@c)) + c2 = @row.add_cell 1, :type=>:float, :style=>1 + + assert_equal(c2.shareable_hash,@c.shareable_hash) c3 = @row.add_cell 2, :type=>:float, :style=>1 c4 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF" - assert_equal(c4.shareable(c2) ,false) + assert_equal(c4.shareable_hash == c2.shareable_hash,false) c5 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF" - assert(c5.shareable(c4)) + assert_equal(c5.shareable_hash, c4.shareable_hash) end -- cgit v1.2.3