lib/axlsx/workbook/shared_strings_table.rb in axlsx-1.1.0 vs lib/axlsx/workbook/shared_strings_table.rb in axlsx-1.1.1

- old
+ new

@@ -27,15 +27,16 @@ attr_reader :unique_cells # 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 # @param [String] str # @return [String] @@ -50,19 +51,17 @@ # add the cell to our unique cells array and set the ssti attribute on the index of this cell in the shared strings table # if a sharable cell already exists in our unique_cells array, set the ssti attribute of the cell and move on. # @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 end end