class SpreadsheetBuilder::Builder
Attributes
book[R]
name[RW]
sheets[R]
Public Class Methods
new()
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 6 def initialize @cells = Hash.new { |h,k| h[k] = { value: nil, format: {} } } @sheets = [] @merges = Hash.new { |h,k| h[k] = [] } @row_heights = Hash.new { |h,k| h[k] = {} } @col_widths = Hash.new { |h,k| h[k] = {} } end
Public Instance Methods
_cells()
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 30 def _cells @cells end
_print()
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 14 def _print index = current_sheet cells = sheet_cells(index) 0.upto(sheet_rows(cells).last) do |row| cols = [] 0.upto(sheet_cols(cells).last) do |col| cols << @cells[[index, row, col]][:value] end puts cols.join("\t") end end
add_blank_row(row)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 74 def add_blank_row(row) sheet_cols(sheet_cells(current_sheet)).each do |col| set_cell_value(row, col, '') end end
add_each_row(sheet, cells, index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 123 def add_each_row(sheet, cells, index) 0.upto(sheet_rows(cells).last) do |row| cols = [] 0.upto(sheet_cols(cells).last) do |col| cols << @cells[[index, row, col]][:value] end sheet.row(row).concat(cols) end end
add_format_to_box(r1, c1, r2, c2, options)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 100 def add_format_to_box(r1, c1, r2, c2, options) (r1..r2).each do |row| (c1..c2).each do |col| add_format_to_cell(row, col, options) end end end
add_format_to_cell(row, col, options)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 84 def add_format_to_cell(row, col, options) @cells[[current_sheet, row, col]][:format].merge!(options) end
add_format_to_col(col, options)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 94 def add_format_to_col(col, options) sheet_rows(sheet_cells(current_sheet)).each do |row| add_format_to_cell(row, col, options) end end
add_format_to_row(row, options)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 88 def add_format_to_row(row, options) sheet_cols(sheet_cells(current_sheet)).each do |col| add_format_to_cell(row, col, options) end end
add_merge(r1, c1, r2, c2)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 50 def add_merge(r1, c1, r2, c2) @merges[current_sheet] << [r1, c1 ,r2, c2] end
add_sheet(name)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 62 def add_sheet(name) @sheets << name.to_s end
build_sheet(sheet, index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 181 def build_sheet(sheet, index) cells = sheet_cells(index) unless cells.empty? add_each_row(sheet, cells, index) handle_height_or_width_format(sheet, cells, index) format_each_row(sheet, cells, index) merge_cells(sheet, index) set_row_heights(sheet, index) set_col_widths(sheet, index) end end
current_sheet()
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 26 def current_sheet @current_sheet ||= 0 end
format_each_row(sheet, cells, index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 133 def format_each_row(sheet, cells, index) sheet_rows(cells).each do |row| sheet_cols(cells).each do |col| sheet.row(row).set_format( col, Spreadsheet::Format.new(@cells[[index, row, col]][:format]) ) end end end
handle_height_or_width_format(sheet, cells, index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 162 def handle_height_or_width_format(sheet, cells, index) sheet_cells(index).each do |(_, row, col), cell| if cell[:format].keys.include?(:height) current = @row_heights[index][row] if !current || current < Integer(cell[:format][:height]) @row_heights[index][row] = Integer(cell[:format][:height]) end cell[:format].delete(:height) end if cell[:format].keys.include?(:width) current = @col_widths[index][col] if !current || current < Integer(cell[:format][:width]) @col_widths[index][col] = Integer(cell[:format][:width]) end cell[:format].delete(:width) end end end
merge_cells(sheet, index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 144 def merge_cells(sheet, index) @merges[index].each do |points| sheet.merge_cells(*points) end end
set_cell_value(row, col, val)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 80 def set_cell_value(row, col, val) @cells[[current_sheet, row, col]][:value] = val end
set_col_width(col, width)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 58 def set_col_width(col, width) @col_widths[current_sheet][col] = Integer(width) end
set_col_widths(sheet, index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 156 def set_col_widths(sheet, index) @col_widths[index].each do |col, width| sheet.column(col).width = width end end
set_row_height(row, height)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 54 def set_row_height(row, height) @row_heights[current_sheet][row] = Integer(height) end
set_row_heights(sheet, index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 150 def set_row_heights(sheet, index) @row_heights[index].each do |row, height| sheet.row(row).height = height end end
set_sheet(index)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 70 def set_sheet(index) @current_sheet = index end
set_sheet_by_name(name)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 66 def set_sheet_by_name(name) @current_sheet = @sheets.index(name) end
sheet_cells(index)
click to toggle source
private
# File lib/spreadsheet_builder/builder.rb, line 109 def sheet_cells(index) @cells.select { |(sheet,_,_),_| sheet == index } end
sheet_cols(cells)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 118 def sheet_cols(cells) cols = cells.keys.map { |(_,_,col)| col }.sort cols.first..cols.last end
sheet_rows(cells)
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 113 def sheet_rows(cells) rows = cells.keys.map { |(_,row,_)| row }.sort rows.first..rows.last end
to_spreadsheet()
click to toggle source
# File lib/spreadsheet_builder/builder.rb, line 34 def to_spreadsheet @sheets << 'Sheet 1' if @sheets.empty? @book = Spreadsheet::Workbook.new CUSTOM_PALETTE.each do |name, color| @book.set_custom_color(name, color.r, color.g, color.b) end @book_sheets = @sheets.map { |n| book.create_worksheet(name: n) } @book_sheets.each_with_index do |sheet, index| build_sheet(sheet, index) end @book end