diff --git a/lib/daru/io/exporters/excel.rb b/lib/daru/io/exporters/excel.rb index 29d01ff..ad93bf0 100755 --- a/lib/daru/io/exporters/excel.rb +++ b/lib/daru/io/exporters/excel.rb @@ -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]) @@ -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]) + # + # #=> # + # # 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 diff --git a/test.xls b/test.xls new file mode 100644 index 0000000..484656a Binary files /dev/null and b/test.xls differ