Module: Axlsx::Ar::SingletonMethods

Defined in:
lib/axlsx/ar.rb

Overview

Singleton methods for the mixin

Instance Method Summary (collapse)

Instance Method Details

- (Object) to_xlsx(number = :all, options = {})

Maps the AR class to an Axlsx package options are passed into AR find

Parameters:

  • Symbol, (Symbol, :all, etc.)

    :all, etc.

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

    a customizable set of options

Options Hash (options):

  • header_style (Integer)

    to apply to the first row of field names

  • an (Array, Symbol)

    array of Axlsx types for each cell in data rows or a single type that will be applied to all types.

  • style (Integer, Array)

    The style to pass to Worksheet#add_row

See Also:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/axlsx/ar.rb', line 32

def to_xlsx(number = :all, options = {})
  row_style = options.delete(:style)
  header_style = options.delete(:header_style) || row_style
  types = options.delete(:types)

  data = [*find(number, options)]
  data.compact!
  data.flatten!
  columns = data.first.attributes.keys
  p = Package.new
  row_style = p.workbook.styles.add_style(row_style) unless row_style.nil?
  header_style = p.workbook.styles.add_style(header_style) unless header_style.nil?
  
  p.workbook.add_worksheet(:name=>table_name.humanize) do |sheet|
    sheet.add_row columns, :style=>header_style
    data.each do |r|
      sheet.add_row r.attributes.values, :style=>row_style, :types=>types
    end
  end
  p
end