summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/workbook/worksheet/border_creator.rb
blob: a4146ee155e4741f6b0e140b2cb4bc31b7187d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# encoding: UTF-8

module Axlsx
  class BorderCreator
    attr_reader :worksheet, :cells, :edges, :width, :color

    def initialize(worksheet, cells, args)
      @worksheet = worksheet
      @cells     = cells
      if args.is_a?(Hash)
        @edges = args[:edges] || Axlsx::Border::EDGES
        @width = args[:style] || :thin
        @color = args[:color] || '000000'
      else
        @edges = args || Axlsx::Border::Edges
        @width = :thin
        @color = '000000'
      end

      if @edges == :all
        @edges = Axlsx::Border::EDGES
      elsif @edges.is_a?(Array)
        @edges = (@edges.map(&:to_sym).uniq & Axlsx::Border::EDGES)
      else
        @edges = []
      end
    end

    def draw
      @edges.each do |edge| 
        worksheet.add_style(
          border_cells[edge], 
          {
            border: {style: @width, color: @color, edges: [edge]}
          }
        )
      end
    end

    private

    def border_cells
      {
        top:     "#{first_cell}:#{last_col}#{first_row}",
        right:   "#{last_col}#{first_row}:#{last_cell}",
        bottom:  "#{first_col}#{last_row}:#{last_cell}",
        left:    "#{first_cell}:#{first_col}#{last_row}",
      }
    end

    def first_cell
      @first_cell ||= cells.first.r
    end

    def last_cell
      @last_cell ||= cells.last.r
    end

    def first_row
      @first_row ||= first_cell.scan(/\d+/).first
    end

    def first_col
      @first_col ||= first_cell.scan(/\D+/).first
    end

    def last_row
      @last_row ||= last_cell.scan(/\d+/).first
    end

    def last_col
      @last_col ||= last_cell.scan(/\D+/).first
    end

  end
end