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, sheet



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/to_xls.rb', line 27

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

  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 || sheet
end

#to_xls_data(options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/to_xls.rb', line 61

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