Sha256: 1f2a8482973e191778d08093eb8e7acc7496a440b895e0ac9b0d33293a491817

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

module AxlsxStyler
  module Array
    def add_style(style)
      validate_cells
      each do |cell|
        cell.add_style(style)
      end
    end

    # - edges is either :all or an an array with elements
    #   in [:top, :bottom, :bottom, :left]
    # - couldn't do variable border thickness around the same cell;
    #   hardcode style to :thin for now
    def add_border(edges = :all)
      validate_cells
      selected_edges(edges).each{ |edge| add_border_at(edge) }
    end

    private

    def selected_edges(edges)
      all_edges = [:top, :right, :bottom, :left]
      if edges == :all
        all_edges
      elsif edges.is_a?(Array) && edges - all_edges == []
        edges.uniq
      else
        []
      end
    end

    def validate_cells
      valid = map{ |e| e.is_a? Axlsx::Cell }.uniq == [ true ]
      raise "Not a range of cells" unless valid
    end

    def border_cells
      @border_cells ||= get_border_cells
    end

    def get_border_cells
      first = self.first.r
      last  = self.last.r

      first_row = first.scan(/\d+/).first
      last_row  = last.scan(/\d+/).first
      first_col = first.scan(/\D+/).first
      last_col  = last.scan(/\D+/).first

      # example range "B2:D5"
      {
        top:     "#{first}:#{last_col}#{first_row}", # "B2:D2"
        right:   "#{last_col}#{first_row}:#{last}",  # "D2:D5"
        bottom:  "#{first_col}#{last_row}:#{last}",  # "B5:D5"
        left:    "#{first}:#{first_col}#{last_row}"  # "B2:B5"
      }
    end

    def worksheet
      @worksheet ||= self.first.row.worksheet
    end

    def add_border_at(position)
      style = {
        border: {
          style: :thin, color: "000000", edges: [position.to_sym]
        }
      }
      worksheet[border_cells[position.to_sym]].add_style(style)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
axlsx_styler-0.0.4 lib/axlsx_styler/array.rb
axlsx_styler-0.0.3 lib/axlsx_styler/array.rb