class Bmg::Writer::Xlsx

Constants

DEFAULT_OPTIONS

Attributes

csv_options[R]
output_preferences[R]
workbook[R]
worksheet[R]

Public Class Methods

new(csv_options, output_preferences = nil) click to toggle source
# File lib/bmg/writer/xlsx.rb, line 9
def initialize(csv_options, output_preferences = nil)
  @csv_options = DEFAULT_OPTIONS.merge(csv_options)
  @output_preferences = OutputPreferences.dress(output_preferences)
end

Public Instance Methods

call(relation, path) click to toggle source
# File lib/bmg/writer/xlsx.rb, line 15
def call(relation, path)
  require 'write_xlsx'
  dup._call(relation, path)
end

Protected Instance Methods

_call(relation, path) click to toggle source
# File lib/bmg/writer/xlsx.rb, line 23
def _call(relation, path)
  @workbook = WriteXLSX.new(path)
  @worksheet = workbook.add_worksheet

  headers = infer_headers(relation.type)
  relation.each_with_index do |tuple,i|
    headers = infer_headers(tuple) if headers.nil?
    headers.each_with_index do |h,i|
      worksheet.write_string(0, i, h)
    end if i == 0
    headers.each_with_index do |h,j|
      meth, *args = write_pair(tuple[h])
      worksheet.send(meth, 1+i, j, *args)
    end
  end

  workbook.close
  path
end
date_format() click to toggle source
# File lib/bmg/writer/xlsx.rb, line 54
def date_format
  @date_format ||= workbook.add_format(:num_format => 'yyyy-mm-dd')
end
write_pair(value) click to toggle source
# File lib/bmg/writer/xlsx.rb, line 43
def write_pair(value)
  case value
  when Numeric
    [:write_number, value]
  when Date
    [:write_date_time, value, date_format]
  else
    [:write_string, value.to_s]
  end
end