summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-04-09 03:08:13 -0700
committerRandy Morgan <[email protected]>2012-04-09 03:08:13 -0700
commit8c14a6846d53a23e442275346bca85f714a1df48 (patch)
treea218a30a58fbf058bf2440ae497193dc8786e39c /lib
parent9b48a368014be3ba94d69d6db505569c89ec8e9f (diff)
parent289f6718b466cd37bba1afbbb8269ec57b1f8b2f (diff)
downloadcaxlsx-8c14a6846d53a23e442275346bca85f714a1df48.tar.gz
caxlsx-8c14a6846d53a23e442275346bca85f714a1df48.zip
Merge pull request #76 from ochko/master
Shared string table works!
Diffstat (limited to 'lib')
-rw-r--r--lib/axlsx/stylesheet/color.rb1
-rw-r--r--lib/axlsx/workbook/shared_strings_table.rb25
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb20
3 files changed, 21 insertions, 25 deletions
diff --git a/lib/axlsx/stylesheet/color.rb b/lib/axlsx/stylesheet/color.rb
index 78171607..065eeb46 100644
--- a/lib/axlsx/stylesheet/color.rb
+++ b/lib/axlsx/stylesheet/color.rb
@@ -71,6 +71,5 @@ module Axlsx
end
str << "/>"
end
-
end
end
diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb
index 45402f5b..61402aec 100644
--- a/lib/axlsx/workbook/shared_strings_table.rb
+++ b/lib/axlsx/workbook/shared_strings_table.rb
@@ -29,11 +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?('=') }
- @count = cells.size
- @unique_cells = []
+ @index = 0
+ @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
@@ -52,16 +53,14 @@ module Axlsx
# @return [Array] unique cells
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 << '<si>' << cell.run_xml_string << '</si>'
- @unique_cells << cell_hash
- else
+ cell_hash = cell.value
+ if index = @unique_cells[cell_hash]
cell.send :ssti=, index
+ else
+ cell.send :ssti=, @index
+ @shared_xml_string << '<si>' << cell.run_xml_string << '</si>'
+ @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..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
+ [email protected]? && # Not nil
+ [email protected]? && # Not empty
+ [email protected]_with?('=') # Not a formula
+ end
# The inline font_name property for the cell
# @return [String]
@@ -154,7 +161,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
@@ -220,15 +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
- 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
- end
-
# @return [Integer] The index of the cell in the containing row.
def index
@row.cells.index(self)