Class: Spyro::ActionViewExtension::CollectionForHelper::Output::XLSX

Inherits:
Base
  • Object
show all
Defined in:
lib/spyro/collections/outputs/xlsx.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, #t

Constructor Details

This class inherits a constructor from Spyro::ActionViewExtension::CollectionForHelper::Output::Base

Instance Method Details

#renderObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spyro/collections/outputs/xlsx.rb', line 20

def render
  return "" if @unicollection.rows.empty?

  filename = @unicollection.meta[:filename] || @unicollection.meta[:model_class] || "file"
  xlsx_package = Axlsx::Package.new({})
  wb = xlsx_package.workbook


  wb.add_worksheet(name: filename) do |sheet|
    render_header sheet
    render_body sheet
  end

  xlsx_package.to_stream.string
end

#render_body(sheet) ⇒ Object



14
15
16
17
18
# File 'lib/spyro/collections/outputs/xlsx.rb', line 14

def render_body sheet
  @unicollection.rows.each do |row|
    sheet.add_row row[:data].map(&:value)
  end
end

#render_header(sheet) ⇒ Object



9
10
11
12
# File 'lib/spyro/collections/outputs/xlsx.rb', line 9

def render_header sheet
  return unless @unicollection.meta[:data]
  sheet.add_row @unicollection.meta[:data]
end