Sha256: 39a66f6d9a23bb08cea5e4005689b5a0c9fe6fa0b22c030dc0ca4196a0bc8ae0

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'rubygems'
require 'spreadsheet'
require 'stringio'

class Array
  # Options for to_xls: columns, name, header
  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]
          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
  
  def to_xls_data(options = {})
    data = StringIO.new('')
    self.to_xls(options).write(data)
    return data.string
  end
  
  private  
  def aux_to_xls(item, column, row)
    if column.is_a?(String) or column.is_a?(Symbol)
      row.push(item.send(column))
    elsif column.is_a?(Hash)
      column.each{|key, values| aux_to_xls(item.send(key), values, row)}
    elsif column.is_a?(Array)
      column.each{|value| aux_to_xls(item, value, row)}
    end
  end
  
  def aux_headers_to_xls(item, column, row)
    if column.is_a?(String) or column.is_a?(Symbol)
      row.push("#{item.class.name.underscore}_#{column}")
    elsif column.is_a?(Hash)
      column.each{|key, values| aux_headers_to_xls(item.send(key), values, row)}
    elsif column.is_a?(Array)
      column.each{|value| aux_headers_to_xls(item, value, row)}
    end
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
to_xls-0.1.0 lib/to_xls.rb