Skip to content

Commit

Permalink
Passes user-given options to Spreadsheet::Format
Browse files Browse the repository at this point in the history
  • Loading branch information
athityakumar committed Jul 11, 2017
1 parent 08f2fd8 commit 365ee8e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions lib/daru/io/exporters/excel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ class Excel < Base

# Exports +Daru::DataFrame+ to an Excel Spreadsheet.
#
# @note To know more about the +Spreadsheet+ parameter hashes that can be given as
# +data_options+ and +header_options+ parameters, please have a look at the methods
# described in
# {http://www.rubydoc.info/gems/ruby-spreadsheet/Spreadsheet/Font Spreadsheet::Font}
# and
# {http://www.rubydoc.info/gems/ruby-spreadsheet/Spreadsheet/Format Spreadsheet::Format}
# pages.
#
# @param dataframe [Daru::DataFrame] A dataframe to export
# @param path [String] Path of the file where the +Daru::DataFrame+
# should be written.
# @param options [Hash] A set of options containing user-preferences
# @param data_options [Hash] A set of +Spreadsheet+ options containing user-preferences
# about the +Daru::DataFrame+ data being written.
# @param header_options [Hash] A set of +Spreadseet+ options containing user-preferences
# about the +Daru::DataFrame+ headers being written.
#
# @example Writing to an Excel file without options
# df = Daru::DataFrame.new([[1,2],[3,4]], order: [:a, :b])
Expand All @@ -23,32 +34,42 @@ class Excel < Base
#
# Daru::IO::Exporters::Excel.new(df, "dataframe_df.xls").call
#
# @todo The +opts+ parameter isn't used while creating the Excel Spreadsheet
# yet. Implementing this feature will greatly allow the user to generate a
# Spreadsheet of their choice.
def initialize(dataframe, path, **options)
# @example Writing to an Excel file with options
# df = Daru::DataFrame.new([[1,2],[3,4]], order: [:a, :b])
#
# #=> #<Daru::DataFrame(2x2)>
# # a b
# # 0 1 3
# # 1 2 4
#
# normal = { color: :blue }
# heading = { color: :red, weight: :bold }
#
# Daru::IO::Exporters::Excel.new(df,
# "dataframe_df.xls",
# data_options: normal,
# header_options: heading
# ).call
def initialize(dataframe, path, header_options: {}, data_options: {})
optional_gem 'spreadsheet', '~> 1.1.1'

super(dataframe)
@path = path
@options = options
@path = path
@data_options = data_options
@header_options = header_options
end

# @note
#
# The +format+ variable used in this method, has to be given
# as options by the user via the +options+ hash input.
#
# Signed off by @athityakumar on 03/06/2017 at 7:00PM
def call
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet

format = Spreadsheet::Format.new color: :blue, weight: :bold
sheet.row(0).concat(@dataframe.vectors.to_a.map(&:to_s))
sheet.row(0).default_format = Spreadsheet::Format.new(@header_options)

sheet.row(0).concat(@dataframe.vectors.to_a.map(&:to_s)) # Unfreeze strings
sheet.row(0).default_format = format
@dataframe.each_row_with_index { |row, i| sheet.row(i+1).concat(row.to_a) }
@dataframe.each_row_with_index do |row, i|
sheet.row(i+1).concat(row.to_a)
sheet.row(i+1).default_format = Spreadsheet::Format.new(@data_options)
end

book.write(@path)
end
Expand Down
Binary file added test.xls
Binary file not shown.

0 comments on commit 365ee8e

Please sign in to comment.