Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 21 additions & 4 deletions lib/to_xls/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions spec/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
3 changes: 1 addition & 2 deletions to_xls.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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