Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

put only plain string cells in shared string table

  • Loading branch information...
commit 3431214a4feaa076ebc246552e5e61331d35c398 1 parent 78e2f07
@ochko authored
View
5 lib/axlsx/stylesheet/color.rb
@@ -71,10 +71,5 @@ def to_xml_string(str = '')
end
str << "/>"
end
-
- def signature
- "#{@rgb}:#{auto}:#{@tint}"
- end
-
end
end
View
8 lib/axlsx/workbook/shared_strings_table.rb
@@ -29,12 +29,12 @@ def unique_count
# 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 @@ def to_xml_string
# @return [Array] unique cells
def resolve(cells)
cells.each do |cell|
- cell_hash = cell.shareable_hash
+ cell_hash = cell.value
@jurriaan
jurriaan added a note

Why not use cell.value.hash?

@ochko Owner
ochko added a note

That will be done implicitly when object is put in a hash.

@jurriaan
jurriaan added a note

That's true, but benchmarking shows inserting hashes is faster: https://gist.github.com/2318872
Which is weird :)

@ochko Owner
ochko added a note

Worth to know. It seems putting hash of object straight in hash key helps because maybe it cuts one step in internals of Hash.

Note: wrote without any reference into actual source code of Hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
if index = @unique_cells[cell_hash]
cell.send :ssti=, index
else
View
19 lib/axlsx/workbook/worksheet/cell.rb
@@ -74,13 +74,20 @@ def value=(v)
@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 @@ def scheme=(v)
# @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 @@ def initialize(row, value="", options={})
# @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 @@ def to_xml_string(r_index, c_index, str = '')
# 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
View
34 test/workbook/worksheet/tc_cell.rb
@@ -212,24 +212,34 @@ def test_merge_with_cell
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)
@jurriaan

Why not use cell.value.hash?

@ochko

That will be done implicitly when object is put in a hash.

@jurriaan

That's true, but benchmarking shows inserting hashes is faster: https://gist.github.com/2318872
Which is weird :)

@ochko

Worth to know. It seems putting hash of object straight in hash key helps because maybe it cuts one step in internals of Hash.

Note: wrote without any reference into actual source code of Hash.

Please sign in to comment.
Something went wrong with that request. Please try again.