Class: Tabledata::Presenters::Excel

Inherits:
Tabledata::Presenter show all
Defined in:
lib/tabledata/presenters/excel.rb

Constant Summary collapse

Bold =
Spreadsheet::Format.new weight: :bold

Instance Attribute Summary

Attributes inherited from Tabledata::Presenter

#options, #table, #tables

Instance Method Summary collapse

Methods inherited from Tabledata::Presenter

add_presenter!, #initialize, #multiple_tables?, present, presenter_exists?, replace_presenter!, #single_table?

Constructor Details

This class inherits a constructor from Tabledata::Presenter

Instance Method Details

#documentObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tabledata/presenters/excel.rb', line 13

def document
  document = Spreadsheet::Workbook.new

  tables.each do |id, table|
    sheet = document.create_worksheet(name: worksheet_name(id))
    sheet.row(0).default_format = Bold if @options[:bold_headers]

    table.data.each_with_index do |row, row_nr|
      row.each_with_index do |col, col_nr|
        sheet[row_nr, col_nr] = col
      end
    end
  end

  document
end

#string(options = nil) ⇒ Object


34
35
36
# File 'lib/tabledata/presenters/excel.rb', line 34

def string(options=nil)
  document.to_string
end

#worksheet_name(id) ⇒ Object


30
31
32
# File 'lib/tabledata/presenters/excel.rb', line 30

def worksheet_name(id)
  single_table? || id.nil? ? @options[:worksheet_name] : @options.fetch(:worksheet_names, {}).fetch(id, id.to_s)
end

#write(path, options = nil) ⇒ Object


38
39
40
41
42
# File 'lib/tabledata/presenters/excel.rb', line 38

def write(path, options=nil)
  document.write(path)

  path
end