diff --git a/README.rdoc b/README.rdoc index 4591a70..0067915 100644 --- a/README.rdoc +++ b/README.rdoc @@ -14,6 +14,7 @@ It is (very slightly) oriented towards exporting ActiveRecord sets, but it can b @users.to_xls(:columns => [:name, :role]) # include only these columns, on this order @users.to_xls(:columns => [:name, {:company => [:name, :address]}]) # able to pick associations/called methods @users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => ['Name', 'Company', 'Address']) # provide better names for the associated columns + @users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => [['Personal', 'Company'], ['Name', 'Name', 'Address']], :multiple_header_rows => true) # use multiple rows of headers == Format diff --git a/lib/to_xls/writer.rb b/lib/to_xls/writer.rb index d655df5..f9038de 100644 --- a/lib/to_xls/writer.rb +++ b/lib/to_xls/writer.rb @@ -35,10 +35,15 @@ def write_sheet(sheet) if columns.any? row_index = 0 - if headers_should_be_included? - apply_format_to_row(sheet.row(0), @header_format) - fill_row(sheet.row(0), headers) - row_index = 1 + + if multiple_header_rows_should_be_included? + headers.each do |header_row| + fill_headers(sheet, header_row, row_index) + row_index += 1 + end + elsif headers_should_be_included? + fill_headers(sheet, headers, row_index) + row_index += 1 end @array.each do |model| @@ -79,6 +84,13 @@ def headers_should_be_included? @options[:headers] != false end + def multiple_header_rows_should_be_included? + return false unless headers_should_be_included? && @options[:multiple_header_rows] && @options[:headers] + unless headers.all? {|h| h.is_a? Array} + raise ArgumentError, ":headers (#{@headers.inspect}) must be an array of arrays" + end + true + end private def apply_format_to_row(row, format) @@ -89,6 +101,11 @@ def create_format(name) Spreadsheet::Format.new @options[name] if @options.has_key? name end + def fill_headers(sheet, headers, row_index) + apply_format_to_row(sheet.row(row_index), @header_format) + fill_row(sheet.row(row_index), headers) + end + def fill_row(row, column, model=nil) case column when String, Symbol diff --git a/spec/writer_spec.rb b/spec/writer_spec.rb index 1414652..18793b7 100644 --- a/spec/writer_spec.rb +++ b/spec/writer_spec.rb @@ -77,6 +77,31 @@ ) end + it "uses multiple header rows if the option is set and headers is an array of arrays" do + xls = make_book( mock_users, + :columns => [:name, :email, :age], + :multiple_header_rows => true, + :headers => [['Info', '', 'Other Info'], ['Nombre', 'Correo', 'Edad']] + ) + check_sheet( xls.worksheets.first, + [ ['Info', '', 'Other Info'], + ['Nombre', 'Correo', 'Edad'], + ['Peter', 'peter@gmail.com', 20], + ['John', 'john@gmail.com', 25], + ['Day9', 'day9@day9tv.com', 27] + ] + ) + end + + it "throws an error if multiple header rows option is set but headers is not an array of arrays" do + lambda { + make_book( mock_users, + :columns => [:name, :email, :age], + :multiple_header_rows => true, + :headers => [['Info', '', 'Other Info'], 'Nombre', 'Correo', 'Edad'] + ) }.should raise_error + end + it "includes no headers if the headers option is false" do xls = make_book( mock_users, :columns => [:name, :email, :age], diff --git a/to_xls.gemspec b/to_xls.gemspec index ac3b0b2..a9e0fb1 100644 --- a/to_xls.gemspec +++ b/to_xls.gemspec @@ -20,8 +20,7 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_runtime_dependency "spreadsheet" - s.add_development_dependency "spreadsheet" - s.add_development_dependency "rspec", "~> 2.3.0" + s.add_development_dependency "rspec", "~> 2.4.0" s.add_development_dependency "bundler", "> 1.1.0" s.add_development_dependency "rake", "~> 0.9.2" end