Class: Daru::IO::Exporters::Excel

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/exporters/excel.rb

Overview

Excel Exporter Class, that extends to_excel_string and write_excel methods to Daru::DataFrame instance variables

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initialize(dataframe, header: true, data: true, index: true) ⇒ Excel

Note:

For giving formatting options as hashes to the :data, :index or header keyword argument(s), please have a look at the Spreadsheet::Font and Spreadsheet::Format pages.

Initializes an Excel Exporter instance.

Examples:

Initializing an Excel Exporter instance

df = Daru::DataFrame.new([[1,2],[3,4]], order: [:a, :b])

#=> #<Daru::DataFrame(2x2)>
#      a   b
#  0   1   3
#  1   2   4

simple_instance = Daru::IO::Exporters::Excel.new(df)
formatted_instance = Daru::IO::Exporters::Excel.new(
  df,
  header: { color: :red, weight: :bold },
  index: false,
  data: { color: :blue }
)


51
52
53
54
55
56
57
58
# File 'lib/daru/io/exporters/excel.rb', line 51

def initialize(dataframe, header: true, data: true, index: true)
  optional_gem 'spreadsheet', '~> 1.1.1'

  super(dataframe)
  @data   = data
  @index  = index
  @header = header
end

Instance Method Details

#to_sString

Exports an Excel Exporter instance to a file-writable String.

Examples:

Getting a file-writable string from Excel Exporter instance

simple_instance.to_s #! same as df.to_avro_string(schema)

#=> "\xD0\xCF\u0011\u0871\u001A\xE1\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000..."

formatted_instance.to_s

#=> "\xD0\xCF\u0011\u0871\u001A\xE1\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000..."


72
73
74
# File 'lib/daru/io/exporters/excel.rb', line 72

def to_s
  super
end

#write(path) ⇒ Object

Exports an Excel Exporter instance to an xls file.

Examples:

Writing an Excel Exporter instance to an xls file

instance.write('filename.xls')


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/daru/io/exporters/excel.rb', line 82

def write(path)
  @book  = Spreadsheet::Workbook.new
  @sheet = @book.create_worksheet

  process_offsets
  write_headers

  @dataframe.each_row_with_index.with_index do |(row, idx), r|
    write_index(idx, r+@row_offset)
    write_data(row,  r+@row_offset)
  end

  @book.write(path)
end