Class: WorkbookRails::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/workbook_rails/template_handler.rb

Class Method Summary collapse

Class Method Details

.call(template, source = nil) ⇒ Object



18
19
20
21
22
# File 'lib/workbook_rails/template_handler.rb', line 18

def self.call(template, source = nil)
  "workbook = Workbook::Book.new;\n" +
    (source || template.source) +
    ";\nWorkbookRails::TemplateHandler.workbook_to_string(workbook, lookup_context.formats.last);"
end

.workbook_to_string(workbook, format) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/workbook_rails/template_handler.rb', line 6

def self.workbook_to_string(workbook, format)
  case format
  when :csv then workbook.sheet.table.to_csv
  when :xlsx then workbook.stream_xlsx
  when :xls then
    io = StringIO.new
    xls = workbook.is_a?(Spreadsheet::Workbook) ? workbook : workbook.to_xls
    xls.write(io)
    io.string
  end
end