From c97f35a31fb20d1dd3f6383c87a6a7bfc43a625a Mon Sep 17 00:00:00 2001 From: ochko Date: Wed, 28 Mar 2012 12:44:40 +0900 Subject: ignore example.csv --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1c698903..29af6b25 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ coverage .yardoc *.gem *.xlsx +example.csv *.*~ .DS_Store tmp \ No newline at end of file -- cgit v1.2.3 From db2633cd108ee40e8fa740feae90a33a33087501 Mon Sep 17 00:00:00 2001 From: ochko Date: Wed, 28 Mar 2012 12:45:25 +0900 Subject: use consisten name for benchmark xlsx files --- test/benchmark.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/benchmark.rb b/test/benchmark.rb index bb9a7b7d..a2179a24 100644 --- a/test/benchmark.rb +++ b/test/benchmark.rb @@ -24,7 +24,7 @@ Benchmark.bm(100) do |x| sheet << row end end - p.serialize("example.xlsx") + p.serialize("example_noautowidth.xlsx") } x.report('axlsx') { @@ -38,7 +38,7 @@ Benchmark.bm(100) do |x| sheet << row end end - p.serialize("example.xlsx") + p.serialize("example_autowidth.xlsx") } x.report('axlsx_shared') { @@ -53,7 +53,7 @@ Benchmark.bm(100) do |x| end end p.use_shared_strings = true - p.serialize("example.xlsx") + p.serialize("example_shared.xlsx") } x.report('axlsx_stream') { -- cgit v1.2.3 From 78e2f076f0b0a1bbe651a3ce53075ae2cf778284 Mon Sep 17 00:00:00 2001 From: ochko Date: Wed, 4 Apr 2012 00:34:24 +0900 Subject: shared string should be faster than non-shared string serialization --- lib/axlsx/stylesheet/color.rb | 4 ++++ lib/axlsx/workbook/shared_strings_table.rb | 17 ++++++++--------- lib/axlsx/workbook/worksheet/cell.rb | 11 +++++------ test/profile.rb | 3 ++- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb index 78171607..497e2d4d 100644 --- a/lib/axlsx/stylesheet/color.rb +++ b/lib/axlsx/stylesheet/color.rb @@ -72,5 +72,9 @@ module Axlsx str << "/>" end + def signature + "#{@rgb}:#{auto}:#{@tint}" + end + end end diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 45402f5b..90aefced 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -30,8 +30,9 @@ module Axlsx # @param [Array] cells This is an array of all of the cells in the workbook def initialize(cells) cells = cells.flatten.reject { |c| c.type != :string || c.value.nil? || c.value.start_with?('=') } + @index = 0 @count = cells.size - @unique_cells = [] + @unique_cells = {} @shared_xml_string = "" resolve(cells) end @@ -53,15 +54,13 @@ module Axlsx def resolve(cells) cells.each do |cell| cell_hash = cell.shareable_hash - index = @unique_cells.index do |item| - item == cell_hash - end - if index == nil - cell.send :ssti=, @unique_cells.size - @shared_xml_string << '' << cell.run_xml_string << '' - @unique_cells << cell_hash - else + if index = @unique_cells[cell_hash] cell.send :ssti=, index + else + cell.send :ssti=, @index + @shared_xml_string << '' << cell.run_xml_string << '' + @unique_cells[cell_hash] = @index + @index += 1 end end end diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index f4c6ff09..262cab28 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -154,7 +154,7 @@ module Axlsx # @param [String] The 8 character representation for an rgb color #FFFFFFFF" def color=(v) @color = v.is_a?(Color) ? v : Color.new(:rgb=>v) - @has_run_style = true + @is_text_run = true end # The inline sz property for the cell @@ -203,6 +203,7 @@ module Axlsx # @option options [String] color an 8 letter rgb specification # @option options [Symbol] scheme must be one of :none, major, :minor def initialize(row, value="", options={}) + @signature = 0 self.row=row @font_name = @charset = @family = @b = @i = @strike = @outline = @shadow = nil @condense = @u = @vertAlign = @sz = @color = @scheme = @extend = @ssti = nil @@ -223,10 +224,7 @@ 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_hash - self_hash = {} - INLINE_STYLES.each { |style| self_hash[style] = self.instance_variable_get("@" + style) } - self_hash['color'] = self_hash['color'].instance_values if self_hash['color'] - self_hash + "#{@signature} & #{@value} & #{@color && @color.signature}" end # @return [Integer] The index of the cell in the containing row. @@ -339,8 +337,9 @@ module Axlsx # Utility method for setting inline style attributes def set_run_style( validator, attr, value) - return unless INLINE_STYLES.include?(attr.to_s) + return unless idx = INLINE_STYLES.index(attr.to_s) Axlsx.send(validator, value) unless validator == nil + @signature += 2**idx self.instance_variable_set :"@#{attr.to_s}", value @is_text_run = true end diff --git a/test/profile.rb b/test/profile.rb index 97d2e2bc..fa81577b 100644 --- a/test/profile.rb +++ b/test/profile.rb @@ -15,11 +15,12 @@ require 'perftools' row = [] input = (32..126).to_a.pack('U*').chars.to_a 20.times { row << input.shuffle.join} -times = 1000 +times = 3000 PerfTools::CpuProfiler.start("/tmp/axlsx_noautowidth") do p = Axlsx::Package.new p.use_autowidth = false + p.use_shared_strings = true wb = p.workbook #A Simple Workbook -- cgit v1.2.3 From 3431214a4feaa076ebc246552e5e61331d35c398 Mon Sep 17 00:00:00 2001 From: ochko Date: Fri, 6 Apr 2012 16:33:15 +0900 Subject: put only plain string cells in shared string table --- lib/axlsx/stylesheet/color.rb | 5 ----- lib/axlsx/workbook/shared_strings_table.rb | 8 +++---- lib/axlsx/workbook/worksheet/cell.rb | 19 ++++++++--------- test/workbook/worksheet/tc_cell.rb | 34 +++++++++++++++++++----------- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb index 497e2d4d..065eeb46 100644 --- a/lib/axlsx/stylesheet/color.rb +++ b/lib/axlsx/stylesheet/color.rb @@ -71,10 +71,5 @@ module Axlsx end str << "/>" end - - def signature - "#{@rgb}:#{auto}:#{@tint}" - end - end end diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 90aefced..61402aec 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -29,12 +29,12 @@ module Axlsx # 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) - cells = cells.flatten.reject { |c| c.type != :string || c.value.nil? || c.value.start_with?('=') } @index = 0 - @count = cells.size @unique_cells = {} @shared_xml_string = "" - resolve(cells) + shareable_cells = cells.flatten.select{ |cell| cell.plain_string? } + @count = shareable_cells.size + resolve(shareable_cells) end # Serializes the object @@ -53,7 +53,7 @@ module Axlsx # @return [Array] unique cells def resolve(cells) cells.each do |cell| - cell_hash = cell.shareable_hash + cell_hash = cell.value if index = @unique_cells[cell_hash] cell.send :ssti=, index else diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 262cab28..6979a6d1 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -74,13 +74,20 @@ module Axlsx @value = cast_value(v) end - # Indicates that the cell has one or more of the custom cell styles applied. # @return [Boolean] def is_text_run? @is_text_run ||= false end + # Indicates if the cell is good for shared string table + def plain_string? + @type == :string && # String typed + !@is_text_run && # No inline styles + !@value.nil? && # Not nil + !@value.empty? && # Not empty + !@value.start_with?('=') # Not a formula + end # The inline font_name property for the cell # @return [String] @@ -203,7 +210,6 @@ module Axlsx # @option options [String] color an 8 letter rgb specification # @option options [Symbol] scheme must be one of :none, major, :minor def initialize(row, value="", options={}) - @signature = 0 self.row=row @font_name = @charset = @family = @b = @i = @strike = @outline = @shadow = nil @condense = @u = @vertAlign = @sz = @color = @scheme = @extend = @ssti = nil @@ -221,12 +227,6 @@ module Axlsx # @return [Integer] attr_reader :ssti - # 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_hash - "#{@signature} & #{@value} & #{@color && @color.signature}" - end - # @return [Integer] The index of the cell in the containing row. def index @row.cells.index(self) @@ -337,9 +337,8 @@ module Axlsx # Utility method for setting inline style attributes def set_run_style( validator, attr, value) - return unless idx = INLINE_STYLES.index(attr.to_s) + return unless INLINE_STYLES.include?(attr.to_s) Axlsx.send(validator, value) unless validator == nil - @signature += 2**idx self.instance_variable_set :"@#{attr.to_s}", value @is_text_run = true end diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index 1cfd6169..12b4fb4c 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -212,24 +212,34 @@ class TestCell < Test::Unit::TestCase assert_equal(@c.row.worksheet.merged_cells.last, "A1:C1") end - def test_equality - 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_hash == c2.shareable_hash,false) - c5 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF" - assert_equal(c5.shareable_hash, c4.shareable_hash) - - end - def test_ssti assert_raise(ArgumentError, "ssti must be an unsigned integer!") { @c.send(:ssti=, -1) } @c.send :ssti=, 1 assert_equal(@c.ssti, 1) end + def test_plain_string + @c.type = :integer + assert_equal(@c.plain_string?, false) + + @c.type = :string + @c.value = 'plain string' + assert_equal(@c.plain_string?, true) + + @c.value = nil + assert_equal(@c.plain_string?, false) + + @c.value = '' + assert_equal(@c.plain_string?, false) + + @c.value = '=sum' + assert_equal(@c.plain_string?, false) + + @c.value = 'plain string' + @c.font_name = 'Arial' + assert_equal(@c.plain_string?, false) + end + def test_to_xml_string c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) assert_equal(c_xml.xpath("/c[@s=1]").size, 1) -- cgit v1.2.3