class BorderCreator

Attributes

cells[R]
color[R]
edges[R]
width[R]
worksheet[R]

Public Class Methods

new(worksheet, cells, args) click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 4
def initialize(worksheet, cells, args)
  @worksheet = worksheet
  @cells     = cells
  if args.is_a?(Hash)
    @edges = args[:edges] || :all
    @width = args[:style] || :thin
    @color = args[:color] || '000000'
  else
    @edges = args || :all
    @width = :thin
    @color = '000000'
  end
end

Public Instance Methods

draw() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 18
def draw
  selected_edges(edges).each { |edge| add_border(edge, width, color) }
end

Private Instance Methods

add_border(position, width, color) click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 35
def add_border(position, width, color)
  style = {
    border: {
      style: width, color: color, edges: [position.to_sym]
    }
  }
  worksheet.add_style border_cells[position.to_sym], style
end
border_cells() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 44
def border_cells
  # example range "B2:D5"
  {
    top:     "#{first_cell}:#{last_col}#{first_row}", # "B2:D2"
    right:   "#{last_col}#{first_row}:#{last_cell}",  # "D2:D5"
    bottom:  "#{first_col}#{last_row}:#{last_cell}",  # "B5:D5"
    left:    "#{first_cell}:#{first_col}#{last_row}"  # "B2:B5"
  }
end
first_cell() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 54
def first_cell
  @first_cell ||= cells.first.r
end
first_col() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 66
def first_col
  @first_col ||= first_cell.scan(/\D+/).first
end
first_row() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 62
def first_row
  @first_row ||= first_cell.scan(/\d+/).first
end
last_cell() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 58
def last_cell
  @last_cell ||= cells.last.r
end
last_col() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 74
def last_col
  @last_col ||= last_cell.scan(/\D+/).first
end
last_row() click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 70
def last_row
  @last_row ||= last_cell.scan(/\d+/).first
end
selected_edges(edges) click to toggle source
# File lib/axlsx_styler/border_creator.rb, line 24
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