Class: Labimotion::XlsxExporter::SheetBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/xlsx_exporter.rb

Overview

SheetBuilder Helper class to build worksheet content with convenient methods

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#sheetObject (readonly)

Returns the value of attribute sheet.



74
75
76
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 74

def sheet
  @sheet
end

#stylesObject (readonly)

Returns the value of attribute styles.



74
75
76
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 74

def styles
  @styles
end

#workbookObject (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

Parameters:

  • range (String) (defaults to: nil)

    range to apply filter (e.g., ‘A1:D1’)



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_rowInteger

Add an empty row (for spacing)

Returns:

  • (Integer)

    row index



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

Parameters:

  • data (Array)

    array of header values

  • options (Hash) (defaults to: {})

    style options (color, bg_color, bold, etc.)

Returns:

  • (Integer)

    row index



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 87

def add_header(data, options = {})
  style_options = {
    bold: true,
    fg_color: '000000', # Black text
    bg_color: 'DDDDDD',
    border: { style: :thin, color: '000000' }
  }.merge(options)

  style = @styles.create_style(style_options)
  add_row(data, style: style)
end

Add a hyperlink to a specific cell

Parameters:

  • row_index (Integer)

    0-based row index

  • col_index (Integer)

    0-based column index

  • url (String)

    the URL for the hyperlink

  • display_text (String) (defaults to: nil)

    optional display text (defaults to cell value)



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

Parameters:

  • data (Array)

    array of cell values

  • options (Hash) (defaults to: {})

    options including :style, :height, :types

Returns:

  • (Integer)

    row index



103
104
105
106
107
108
109
110
111
112
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 103

def add_row(data, options = {})
  row_options = {}
  row_options[:style] = options[:style] if options[:style]
  row_options[:height] = options[:height] if options[:height]
  row_options[:types] = options[:types] if options[:types]

  @sheet.add_row(data, row_options)
  @current_row += 1
  @current_row - 1
end

#add_rows(rows, options = {}) ⇒ Integer

Add multiple rows at once

Parameters:

  • rows (Array<Array>)

    array of row data

  • options (Hash) (defaults to: {})

    options for all rows

Returns:

  • (Integer)

    number of rows added



118
119
120
121
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 118

def add_rows(rows, options = {})
  rows.each { |row| add_row(row, options) }
  rows.length
end

#add_section(title, data, options = {}) ⇒ Object

Add a titled section (title + data rows)

Parameters:

  • title (String)

    section title

  • data (Array<Array>)

    data rows

  • options (Hash) (defaults to: {})

    options for the section



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, options = {})
  add_blank_row if @current_row.positive?

  # Add title
  title_style = @styles.create_style(
    bold: true,
    font_size: 14,
    bg_color: options[:title_bg_color] || 'CCCCCC'
  )
  add_row([title], style: title_style)

  # Add data
  add_rows(data, options)

  add_blank_row
end

#auto_fit_columns(columns = nil) ⇒ Object

Auto-fit column widths based on content

Parameters:

  • columns (Array<Integer>) (defaults to: nil)

    column indices to auto-fit (nil for all)



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)

Parameters:

  • row (Integer) (defaults to: 1)

    row number to freeze at

  • column (Integer) (defaults to: 0)

    column number to freeze at



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

Parameters:

  • start_cell (String)

    starting cell (e.g., ‘A1’)

  • end_cell (String)

    ending cell (e.g., ‘C1’)



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

Parameters:

  • widths (Array<Numeric>)

    array of column widths



131
132
133
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 131

def set_column_widths(*widths)
  @sheet.column_widths(*widths)
end