Class: Report::Xlsx

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/report/xlsx.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#safe_delete, #tmp_path

Constructor Details

#initialize(report) ⇒ Xlsx

Returns a new instance of Xlsx.



9
10
11
# File 'lib/report/xlsx.rb', line 9

def initialize(report)
  @report = report
end

Instance Attribute Details

#reportObject (readonly)

Returns the value of attribute report.



7
8
9
# File 'lib/report/xlsx.rb', line 7

def report
  @report
end

Instance Method Details

#cleanupObject



46
47
48
# File 'lib/report/xlsx.rb', line 46

def cleanup
  safe_delete @path if @path
end

#pathObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/report/xlsx.rb', line 13

def path
  return @path if defined?(@path)
  require 'xlsx_writer'
  tmp_path = tmp_path(:extname => '.xlsx')
  workbook = XlsxWriter::Document.new
  if f = report.class.xlsx_format
    f.call workbook
  end
  report.class.tables.each do |table|
    sheet = workbook.add_sheet table.name
    cursor = 1 # excel row numbers start at 1
    if table._head
      table._head.each(report) do |row|
        sheet.add_row row.to_a
        cursor += 1
      end
      sheet.add_row []
      cursor += 1
    end
    if table._body
      sheet.add_row table._body.columns.map(&:name)
      table._body.each(report) do |row|
        sheet.add_row row.to_hash
      end
      sheet.freeze_top_left = calculate_top_left(cursor)
      sheet.add_autofilter calculate_autofilter(table, cursor)
    end
  end
  FileUtils.mv workbook.path, tmp_path
  workbook.cleanup
  @path = tmp_path
end