Class: Breathing::Excel

Inherits:
Object
  • Object
show all
Defined in:
lib/breathing/excel.rb

Instance Method Summary collapse

Constructor Details

#initializeExcel

Returns a new instance of Excel.



7
8
9
# File 'lib/breathing/excel.rb', line 7

def initialize
  @workbook = RubyXL::Workbook.new
end

Instance Method Details

#create(id: 1, file_name: 'breathing.xlsx') ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/breathing/excel.rb', line 11

def create(id: 1, file_name: 'breathing.xlsx')
  sheet       = @workbook[0]
  table_names = Breathing::ChangeLog.where('id >= ?', id).group(:table_name).pluck(:table_name)
  table_names.sort.each do |table_name|
    if sheet.sheet_name == 'Sheet1'
      sheet.sheet_name = table_name
    else
      sheet = @workbook.add_worksheet(table_name)
    end

    rows = Breathing::ChangeLog.where(table_name: table_name).where('id >= ?', id).order(:id).to_a

    next if rows.size.zero?

    add_header_row(sheet, rows.first)
    add_body_rows(sheet, rows)
    add_style(sheet)
  end

  if table_names.size.positive?
    add_change_logs_sheet(id)
    @workbook.worksheets.rotate!(-1)
  end
  @workbook.write(file_name)
end