Sha256: 8385685acd56a37b31479ac2415c05f466ca2dc327b956a79496a6a55da3d73a

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'spreadsheet'

class ActiveRecordToXls

  def self.call(target, options = {}, &block)

    return '' if target.nil? && options[:prepend].nil?

    columns = []
    
    show_header = options[:header] == true

    xls_report = StringIO.new

    Spreadsheet.client_encoding = options[:client_encoding] || "UTF-8"

    book = Spreadsheet::Workbook.new
    sheet = book.create_worksheet

    columns = if options[:only]
      Array(options[:only]).map(&:to_sym)
    elsif !target.nil?
      if target.first.is_a? Hash
        target.first.keys.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
      elsif target.first && target.first.respond_to?('attributes')
        target.first.attributes.keys.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
      end
    end

    return '' if (columns.nil? || columns.empty?) && options[:prepend].nil?

    sheet_index = 0

    options[:prepend].map { |arr| sheet.row(sheet_index).concat(arr); sheet_index += 1 } unless options[:prepend].nil?

    if show_header
      sheet.row(sheet_index).concat(options[:header_columns].nil? ? columns.map(&:to_s).map(&:humanize) : options[:header_columns])
      sheet_index += 1
    end
    
    options[:column_width].each_index { |index| sheet.column(index).width = options[:column_width][index] } if options[:column_width]

    target.each_with_index do |obj, i|
      if block
        sheet.row(sheet_index).replace(columns.map { |col| block.call(col, obj.is_a?(Hash) ? obj[col] : obj.send(col), i) } )
      else
        sheet.row(sheet_index).replace(columns.map { |col| obj.is_a?(Hash) ? obj[col] : obj.send(col) } )
      end
      sheet_index += 1
    end
    
    options[:append].map { |arr| sheet.row(sheet_index).concat(arr); sheet_index += 1 } unless options[:append].nil?

    book.write(xls_report)

    xls_report.string
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_to_xls-1.0.2 lib/active_record_to_xls.rb