Class: CaxlsxBuilder::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/caxlsx_builder/sheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Sheet

Returns a new instance of Sheet.



7
8
9
10
11
12
13
14
15
# File 'lib/caxlsx_builder/sheet.rb', line 7

def initialize(name)
  @name          = name
  @headers       = []
  @cell_styles   = []
  @footers       = []
  @cell_builders = []
  @row_cells     = []
  @styles        = {}
end

Instance Attribute Details

#cell_stylesObject (readonly)

Returns the value of attribute cell_styles.



5
6
7
# File 'lib/caxlsx_builder/sheet.rb', line 5

def cell_styles
  @cell_styles
end

#footersObject (readonly)

Returns the value of attribute footers.



5
6
7
# File 'lib/caxlsx_builder/sheet.rb', line 5

def footers
  @footers
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/caxlsx_builder/sheet.rb', line 5

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/caxlsx_builder/sheet.rb', line 5

def name
  @name
end

#stylesObject (readonly)

Returns the value of attribute styles.



5
6
7
# File 'lib/caxlsx_builder/sheet.rb', line 5

def styles
  @styles
end

Instance Method Details

#add_style(name, style) ⇒ Object



24
25
26
# File 'lib/caxlsx_builder/sheet.rb', line 24

def add_style(name, style)
  @styles[name] = style
end

#cells(index) ⇒ Object



44
45
46
# File 'lib/caxlsx_builder/sheet.rb', line 44

def cells(index)
  @row_cells.flat_map { |row| row[index] }
end

#column(header, style: :default, type: :string, footer: nil, &cell_builder) ⇒ Object



17
18
19
20
21
22
# File 'lib/caxlsx_builder/sheet.rb', line 17

def column(header, style: :default, type: :string, footer: nil, &cell_builder)
  @headers << header
  @cell_styles << Cell.new(style:, type:)
  @footers << footer
  @cell_builders << cell_builder
end

#make_row(item, rescue_errors: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/caxlsx_builder/sheet.rb', line 28

def make_row(item, rescue_errors: false)
  # Build the new row
  row = @cell_builders.map do |cell|
    begin
      cell.call(item)
    rescue StandardError => e
      next nil if rescue_errors

      raise e
    end
  end

  # Add the new row to the cache then return it
  (@row_cells << row).last
end