Sha256: dc03a2ce41d8cdd81e42ca888fcf333439f575571cd54b0c05f51d9375b57e89

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

module AxlsxStyler
  module Axlsx
    module Workbook
      # An array that holds all cells with styles
      attr_accessor :styled_cells

      # An index for cell styles
      #   {
      #     1 => < style_hash >,
      #     2 => < style_hash >,
      #     ...
      #     K => < style_hash >
      #   }
      # where keys are Cell#raw_style and values are styles
      # codes as per Axlsx::Style
      attr_accessor :style_index

      def add_styled_cell(cell)
        self.styled_cells ||= Set.new
        self.styled_cells << cell
      end

      def apply_styles
        return unless styled_cells
        styled_cells.each do |cell|
          set_style_index(cell)
        end
      end

      private

      # Check if style code
      def set_style_index(cell)
        # @TODO fix this hack
        self.style_index ||= {}

        index_item = style_index.select { |_, v| v == cell.raw_style }.first
        if index_item
          cell.style = index_item.first
        else
          old_style = cell.raw_style.dup
          new_style = styles.add_style(cell.raw_style)
          cell.style = new_style
          # cell.raw_style.delete(:num_fmt)
          style_index[new_style] = old_style
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
axlsx_styler-0.1.3 lib/axlsx_styler/axlsx_workbook.rb
axlsx_styler-0.1.2 lib/axlsx_styler/axlsx_workbook.rb
axlsx_styler-0.1.1 lib/axlsx_styler/axlsx_workbook.rb
axlsx_styler-0.1.0 lib/axlsx_styler/axlsx_workbook.rb
axlsx_styler-0.0.5 lib/axlsx_styler/axlsx_workbook.rb