Class: SpreadsheetBuilder::Builder
- Inherits:
-
Object
- Object
- SpreadsheetBuilder::Builder
- Defined in:
- lib/spreadsheet_builder/builder.rb
Instance Attribute Summary collapse
-
#book ⇒ Object
readonly
Returns the value of attribute book.
-
#name ⇒ Object
Returns the value of attribute name.
-
#sheets ⇒ Object
readonly
Returns the value of attribute sheets.
Instance Method Summary collapse
- #_cells ⇒ Object
- #_print ⇒ Object
- #add_blank_row(row) ⇒ Object
- #add_each_row(sheet, cells, index) ⇒ Object
- #add_format_to_box(r1, c1, r2, c2, options) ⇒ Object
- #add_format_to_cell(row, col, options) ⇒ Object
- #add_format_to_col(col, options) ⇒ Object
- #add_format_to_row(row, options) ⇒ Object
- #add_merge(r1, c1, r2, c2) ⇒ Object
- #add_sheet(name) ⇒ Object
- #build_sheet(sheet, index) ⇒ Object
- #current_sheet ⇒ Object
- #format_each_row(sheet, cells, index) ⇒ Object
- #handle_height_or_width_format(sheet, cells, index) ⇒ Object
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
- #merge_cells(sheet, index) ⇒ Object
- #set_cell_value(row, col, val) ⇒ Object
- #set_col_width(col, width) ⇒ Object
- #set_col_widths(sheet, index) ⇒ Object
- #set_row_height(row, height) ⇒ Object
- #set_row_heights(sheet, index) ⇒ Object
- #set_sheet(index) ⇒ Object
- #set_sheet_by_name(name) ⇒ Object
-
#sheet_cells(index) ⇒ Object
private.
- #sheet_cols(cells) ⇒ Object
- #sheet_rows(cells) ⇒ Object
- #to_spreadsheet ⇒ Object
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
6 7 8 9 10 11 12 |
# 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 |
Instance Attribute Details
#book ⇒ Object (readonly)
Returns the value of attribute book.
5 6 7 |
# File 'lib/spreadsheet_builder/builder.rb', line 5 def book @book end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/spreadsheet_builder/builder.rb', line 4 def name @name end |
#sheets ⇒ Object (readonly)
Returns the value of attribute sheets.
5 6 7 |
# File 'lib/spreadsheet_builder/builder.rb', line 5 def sheets @sheets end |
Instance Method Details
#_cells ⇒ Object
30 31 32 |
# File 'lib/spreadsheet_builder/builder.rb', line 30 def _cells @cells end |
#_print ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# 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) ⇒ Object
74 75 76 77 78 |
# 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) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# 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) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/spreadsheet_builder/builder.rb', line 100 def add_format_to_box(r1, c1, r2, c2, ) (r1..r2).each do |row| (c1..c2).each do |col| add_format_to_cell(row, col, ) end end end |
#add_format_to_cell(row, col, options) ⇒ Object
84 85 86 |
# File 'lib/spreadsheet_builder/builder.rb', line 84 def add_format_to_cell(row, col, ) @cells[[current_sheet, row, col]][:format].merge!() end |
#add_format_to_col(col, options) ⇒ Object
94 95 96 97 98 |
# File 'lib/spreadsheet_builder/builder.rb', line 94 def add_format_to_col(col, ) sheet_rows(sheet_cells(current_sheet)).each do |row| add_format_to_cell(row, col, ) end end |
#add_format_to_row(row, options) ⇒ Object
88 89 90 91 92 |
# File 'lib/spreadsheet_builder/builder.rb', line 88 def add_format_to_row(row, ) sheet_cols(sheet_cells(current_sheet)).each do |col| add_format_to_cell(row, col, ) end end |
#add_merge(r1, c1, r2, c2) ⇒ Object
50 51 52 |
# 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) ⇒ Object
62 63 64 |
# File 'lib/spreadsheet_builder/builder.rb', line 62 def add_sheet(name) @sheets << name.to_s end |
#build_sheet(sheet, index) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 |
# 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 ⇒ Object
26 27 28 |
# File 'lib/spreadsheet_builder/builder.rb', line 26 def current_sheet @current_sheet ||= 0 end |
#format_each_row(sheet, cells, index) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# 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) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# 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) ⇒ Object
144 145 146 147 148 |
# 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) ⇒ Object
80 81 82 |
# 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) ⇒ Object
58 59 60 |
# 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) ⇒ Object
156 157 158 159 160 |
# 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) ⇒ Object
54 55 56 |
# 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) ⇒ Object
150 151 152 153 154 |
# 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) ⇒ Object
70 71 72 |
# File 'lib/spreadsheet_builder/builder.rb', line 70 def set_sheet(index) @current_sheet = index end |
#set_sheet_by_name(name) ⇒ Object
66 67 68 |
# File 'lib/spreadsheet_builder/builder.rb', line 66 def set_sheet_by_name(name) @current_sheet = @sheets.index(name) end |
#sheet_cells(index) ⇒ Object
private
109 110 111 |
# File 'lib/spreadsheet_builder/builder.rb', line 109 def sheet_cells(index) @cells.select { |(sheet,_,_),_| sheet == index } end |
#sheet_cols(cells) ⇒ Object
118 119 120 121 |
# 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) ⇒ Object
113 114 115 116 |
# 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 ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 |