Class: Labimotion::XlsxExporter::SheetBuilder
- Inherits:
-
Object
- Object
- Labimotion::XlsxExporter::SheetBuilder
- Defined in:
- lib/labimotion/libs/xlsx_exporter.rb
Overview
SheetBuilder Helper class to build worksheet content with convenient methods
Instance Attribute Summary collapse
-
#sheet ⇒ Object
readonly
Returns the value of attribute sheet.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
-
#workbook ⇒ Object
readonly
Returns the value of attribute workbook.
Instance Method Summary collapse
-
#add_auto_filter(range = nil) ⇒ Object
Apply auto-filter to a range.
-
#add_blank_row ⇒ Integer
Add an empty row (for spacing).
-
#add_header(data, options = {}) ⇒ Integer
Add a header row with bold styling.
-
#add_hyperlink(row_index, col_index, url, display_text = nil) ⇒ Object
Add a hyperlink to a specific cell.
-
#add_row(data, options = {}) ⇒ Integer
Add a data row.
-
#add_rows(rows, options = {}) ⇒ Integer
Add multiple rows at once.
-
#add_section(title, data, options = {}) ⇒ Object
Add a titled section (title + data rows).
-
#auto_fit_columns(columns = nil) ⇒ Object
Auto-fit column widths based on content.
-
#freeze_panes(row = 1, column = 0) ⇒ Object
Freeze panes (typically for freezing header rows).
-
#initialize(sheet, workbook) ⇒ SheetBuilder
constructor
A new instance of SheetBuilder.
-
#merge_cells(start_cell, end_cell) ⇒ Object
Merge cells in a range.
-
#set_column_widths(*widths) ⇒ Object
Set column widths.
Constructor Details
#initialize(sheet, workbook) ⇒ SheetBuilder
Returns a new instance of SheetBuilder.
76 77 78 79 80 81 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 76 def initialize(sheet, workbook) @sheet = sheet @workbook = workbook @styles = StyleManager.new(workbook) @current_row = 0 end |
Instance Attribute Details
#sheet ⇒ Object (readonly)
Returns the value of attribute sheet.
74 75 76 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 74 def sheet @sheet end |
#styles ⇒ Object (readonly)
Returns the value of attribute styles.
74 75 76 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 74 def styles @styles end |
#workbook ⇒ Object (readonly)
Returns the value of attribute workbook.
74 75 76 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 74 def workbook @workbook end |
Instance Method Details
#add_auto_filter(range = nil) ⇒ Object
Apply auto-filter to a range
188 189 190 191 192 193 194 195 196 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 188 def add_auto_filter(range = nil) if range @sheet.auto_filter = range else # Auto-detect range from first row last_col = (@sheet.rows.first&.cells&.length || 1) - 1 @sheet.auto_filter = "A1:#{Axlsx.col_ref(last_col)}1" end end |
#add_blank_row ⇒ Integer
Add an empty row (for spacing)
125 126 127 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 125 def add_blank_row add_row([]) end |
#add_header(data, options = {}) ⇒ Integer
Add a header row with bold styling
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 87 def add_header(data, = {}) = { bold: true, fg_color: '000000', # Black text bg_color: 'DDDDDD', border: { style: :thin, color: '000000' } }.merge() style = @styles.create_style() add_row(data, style: style) end |
#add_hyperlink(row_index, col_index, url, display_text = nil) ⇒ Object
Add a hyperlink to a specific cell
203 204 205 206 207 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 203 def add_hyperlink(row_index, col_index, url, display_text = nil) cell = @sheet.rows[row_index].cells[col_index] cell.value = display_text if display_text @sheet.add_hyperlink location: url, ref: cell end |
#add_row(data, options = {}) ⇒ Integer
Add a data row
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 103 def add_row(data, = {}) = {} [:style] = [:style] if [:style] [:height] = [:height] if [:height] [:types] = [:types] if [:types] @sheet.add_row(data, ) @current_row += 1 @current_row - 1 end |
#add_rows(rows, options = {}) ⇒ Integer
Add multiple rows at once
118 119 120 121 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 118 def add_rows(rows, = {}) rows.each { |row| add_row(row, ) } rows.length end |
#add_section(title, data, options = {}) ⇒ Object
Add a titled section (title + data rows)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 156 def add_section(title, data, = {}) add_blank_row if @current_row.positive? # Add title title_style = @styles.create_style( bold: true, font_size: 14, bg_color: [:title_bg_color] || 'CCCCCC' ) add_row([title], style: title_style) # Add data add_rows(data, ) add_blank_row end |
#auto_fit_columns(columns = nil) ⇒ Object
Auto-fit column widths based on content
137 138 139 140 141 142 143 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 137 def auto_fit_columns(columns = nil) cols = columns || (0...(@sheet.rows.first&.cells&.length || 0)).to_a cols.each do |col_index| max_width = @sheet.rows.map { |row| row.cells[col_index]&.value.to_s.length || 0 }.max @sheet.column_info[col_index].width = [max_width + 2, 100].min if max_width end end |
#freeze_panes(row = 1, column = 0) ⇒ Object
Freeze panes (typically for freezing header rows)
176 177 178 179 180 181 182 183 184 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 176 def freeze_panes(row = 1, column = 0) @sheet.sheet_view.pane do |pane| pane.top_left_cell = Axlsx.cell_r(column, row) pane.state = :frozen pane.y_split = row pane.x_split = column pane.active_pane = :bottom_right end end |
#merge_cells(start_cell, end_cell) ⇒ Object
Merge cells in a range
148 149 150 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 148 def merge_cells(start_cell, end_cell) @sheet.merge_cells("#{start_cell}:#{end_cell}") end |
#set_column_widths(*widths) ⇒ Object
Set column widths
131 132 133 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 131 def set_column_widths(*widths) @sheet.column_widths(*widths) end |