class NeatSpreadsheet::Helpers::Sheet

Public Class Methods

new(workbook, options={}) { |self| ... } click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 3
def initialize(workbook, options={}, &block)
  options.reverse_merge! title: nil, columns_width: [10, 10, 10, 10, 10]

  init_sheet workbook, options[:title]
  init_columns options[:columns_width]
  init_styles

  yield self
end

Public Instance Methods

cell(value, options={}) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 13
def cell(value, options={})
  if options[:colspan]
    options[:horizontal_align] = :merge

    (@column_position..(@column_position+options[:colspan]-1)).each do |i|
      current_row.set_format(i, current_format(options))
    end
  else
    current_row.set_format(@column_position, current_format(options))
  end

  set_current_column value

  @column_position += 1
end
row(options={}) { |self| ... } click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 29
def row(options={}, &block)
  options.reverse_merge! height: nil, start_at_column: 0, style: :normal

  @column_position = 0

  set_current_style options[:style]
  set_current_height options[:height]

  (0..options[:start_at_column]-1).each { |i| cell(' ') } if options[:start_at_column] > 0

  yield self

  @row_position += 1
end
skip_cell() click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 44
def skip_cell
  skip_cells 1
end
skip_cells(nb) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 48
def skip_cells(nb)
  (@column_position..(@column_position+nb-1)).each { |i| cell(' ') }
end
skip_row() click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 52
def skip_row
  @row_position += 1
end
spacer(options={}) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 56
def spacer(options={})
  options.reverse_merge! height: 20

  @column_position = 0

  current_row.height = options[:height]
  (0..@total_columns-1).each { |i| current_row.set_format(i, Spreadsheet::Format.new(horizontal_align: :merge)) }
  set_current_column ' '

  @row_position += 1
end

Private Instance Methods

current_format(options={}) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 70
def current_format(options={})
  format = current_style(options)

  format.delete(:colspan)

  return Spreadsheet::Format.new(format)
end
current_row() click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 78
def current_row
  @sheet.row(@row_position)
end
current_style(options={}) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 82
def current_style(options={})
  @styles[@current_style].merge(options)
end
init_columns(sizes) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 86
def init_columns(sizes)
  sizes.each_with_index { |width, i| @sheet.column(i).width = width }

  @total_columns = sizes.length
end
init_sheet(workbook, title) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 92
def init_sheet(workbook, title)
  @sheet = workbook.create_worksheet
  @sheet.name = title if title
  @row_position = 0
end
init_styles() click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 98
def init_styles
  @styles = {}

  @styles[:h1] = { weight: :bold,   size: 16, horizontal_align: :left, vertical_align: :middle }
  @styles[:h2] = { weight: :normal, size: 12, horizontal_align: :left, vertical_align: :middle }
  @styles[:h3] = { weight: :normal, size: 9,  horizontal_align: :left, vertical_align: :middle }
  @styles[:h4] = { weight: :bold,   size: 8,  horizontal_align: :left, vertical_align: :middle }
  @styles[:th] = { weight: :bold,   size: 8,  horizontal_align: :center, vertical_align: :middle, pattern_fg_color: :xls_color_19, pattern: 1 }

  @styles[:normal] = { size: 8, horizontal_align: :left , vertical_align: :middle }
  @styles[:small] = { size: 7, horizontal_align: :left , vertical_align: :middle }
  @styles[:tiny] = { size: 6, horizontal_align: :left , vertical_align: :middle }
  @styles[:unknown] = { weight: :bold, size: 5, color: :red }
end
set_column(position, value) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 113
def set_column(position, value)
  @sheet[@row_position, position] = value
end
set_current_column(value) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 117
def set_current_column(value)
  set_column @column_position, value
end
set_current_height(height) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 121
def set_current_height(height)
  height = current_style[:size] + 8 if not height

  current_row.height = height
end
set_current_style(name) click to toggle source
# File lib/neat-spreadsheet/helpers/sheet.rb, line 127
def set_current_style(name)
  @current_style = (@styles.has_key?(name) ? name : :unknown)
end