Class: Array

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

Instance Method Summary collapse

Instance Method Details

#to_xls(options = {}) ⇒ Object

Options for to_xls: columns, name, header



7
8
9
10
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
36
37
38
# File 'lib/to_xls.rb', line 7

def to_xls(options = {})
  
  book = Spreadsheet::Workbook.new
  sheet = book.create_worksheet
  
  sheet.name = options[:name] || 'Sheet 1'    

  if self.any?
    columns = options[:columns] || self.first.attributes.keys.sort

    if columns.any?
      line = 0
      
      unless options[:headers] == false
        if options[:headers].is_a?(Array)
          sheet.row(0).concat options[:headers].collect(&:to_s)
        else
          aux_headers_to_xls(self.first, columns, sheet.row(0))
        end
        line = 1
      end
      
      self.each do |item|
        row = sheet.row(line)
        columns.each {|column| aux_to_xls(item, column, row)}
        line += 1
      end
    end
  end

  return book
end

#to_xls_data(options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/to_xls.rb', line 40

def to_xls_data(options = {})
  data = StringIO.new('')
  self.to_xls(options).write(data)
  return data.string
end