Permalink
Browse files

pre-2.0 commit

  • Loading branch information...
1 parent 2a3e2a4 commit cb66b434774a34d608135819bc704b1fb5fd3cf4 @kikito kikito committed Jun 26, 2011
Showing with 132 additions and 53 deletions.
  1. +38 −29 README.rdoc
  2. +1 −1 Rakefile
  3. +5 −8 lib/to_xls/array_writer.rb
  4. +19 −0 spec/array_spec.rb
  5. +69 −15 spec/array_writer_spec.rb
View
@@ -7,32 +7,12 @@ This gem transform an Array or Hash into a excel file using the spreadsheet gem.
@users = User.all
@users.to_xls
- @users.to_xls(:headers => false)
- @users.to_xls(:columns => [:name, :role])
- @users.to_xls(:columns => [:name, {:company => [:name, :address]}])
- @users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => [:name, :company, :address])
+ @users.to_xls(:name => "Users") # specifies the Sheet name (by default Sheet1)
+ @users.to_xls(:headers => false) # don't include headers
+ @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
-When invoked on a Hash each value is output to a separate sheet named after the corresponding key. Options for each sheet can be specified using key/values in the options hash.
-
- { :users => @users, :posts => @posts }.to_xls
- { :users => @users, :posts => @posts }.to_xls(:users => { :columns => [:name, :role] }, :posts => { :columns => [:title, :content] })
-
-If you want to use the file, for example in rails, you have two options. The first one is saving it first, and using send_file:
-
- @users.to_xls.write '/path/to/file/users.xls'
- send_file '/path/to/file/users.xls'
-
-The second option is using the method to_xls_data and send_data
-
- send_data @users.to_xls_data, :filename => 'users.xls'
-
-The method to_xls_data accepts the same parameters as to_xls.
-
-== Requirements
-
-On rails, you might want to modify config/initializers/mime_types.rb and register a custom mime type there:
-
- Mime::Type.register "application/vnd.ms-excel", :xls
== Example of use in Rails
@@ -46,22 +26,51 @@ In the controller where you want to export to excel, add the format.xls line.
respond_to do |format|
format.html
format.xml { render :xml => @users }
- format.xls { send_data @users.to_xls_data, :filename => 'users.xls' }
+ format.xls { send_data @users.to_xls, :filename => 'users.xls' }
end
end
end
+== Requirements
+
+On rails, you might want to modify config/initializers/mime_types.rb and register a custom mime type there:
+
+ Mime::Type.register "application/vnd.ms-excel", :xls
== Dependencies
- spreadsheet gem (automatically included if you require to_xls)
+* spreadsheet gem (automatically included if you require to_xls)
-== Installing on rails
+== Installing on rails 2.x
- Include next gems in your environment.rb config file:
+Include next gems in your environment.rb config file:
config.gem 'to_xls'
+Then execute
+
+ rake gems:install
+
+
+== Installing on rails 3.x
+
+In your Gemfile
+
+ gem 'to_xls'
+
+Then execute
+
+ bundle install
+
+== ToXml::ArrayWriter
+
+You can export to a file or spreadsheet book using the internal ToXml::ArrayWriter class:
+
+ ToXls::ArrayWriter.new(users, :name => 'Users').write_io(file) # writes to a given file
+ ToXls::ArrayWriter.new(users, :name => 'Users').write_book(book) # writes to a spreadsheet book (adds a new sheet)
+
+The options of ArrayWriter.new(array, options) are the same as in to_xls.
+
== Contributing to to_xls
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
View
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
gem.summary = %Q{to_xls for Arrays and Hashes}
gem.description = %Q{Adds a to_xls method to arrays, which can be used to generate excel files conveniently. Can rely on ActiveRecord sugar for obtaining attribute names.}
gem.email = "egarcia@splendeo.es"
- gem.authors = ["Enrique García Cota", "Francisco de Juan"]
+ gem.authors = ["Enrique Garcia Cota", "Francisco de Juan"]
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
gem.add_runtime_dependency 'spreadsheet', '> 0'
View
@@ -5,7 +5,7 @@
module ToXls
class ArrayWriter
- def initialize(array, options)
+ def initialize(array, options = {})
@array = array
@options = options
end
@@ -49,11 +49,8 @@ def write_sheet(sheet)
def columns
return @columns if @columns
@columns = @options[:columns]
- raise ArgumentError, ":columns (#{columns}) must be an array or nil" unless (@columns.nil? || @columns.is_a?(Array))
- if !@columns && can_get_columns_from_first_element?
- @columns = get_columns_from_first_element
- end
- @columns = @columns || []
+ raise ArgumentError.new(":columns (#{columns}) must be an array or nil") unless (@columns.nil? || @columns.is_a?(Array))
+ @columns ||= can_get_columns_from_first_element? ? get_columns_from_first_element : []
end
def can_get_columns_from_first_element?
@@ -64,13 +61,13 @@ def can_get_columns_from_first_element?
end
def get_columns_from_first_element
- @array.first.attributes.keys.sort_by {|sym| sym.to_s}.collect
+ @array.first.attributes.keys.sort_by {|sym| sym.to_s}.collect.to_a
end
def headers
return @headers if @headers
@headers = @options[:headers] || columns
- raise ArgumentError, ":headers (#{@headers}) must be an array" unless @headers.is_a? Array
+ raise ArgumentError, ":headers (#{@headers.inspect}) must be an array" unless @headers.is_a? Array
@headers
end
View
@@ -0,0 +1,19 @@
+# encoding: utf-8
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe Array do
+
+ describe :to_xls do
+ it "should throw no error without data" do
+ lambda { [].to_xls }.should_not raise_error
+ end
+
+ it "should return the correct string representing a spreadsheet" do
+ s = StringIO.new
+ xls = make_book(mock_users)
+ xls.write(s)
+ s.string.bytes.to_a.should == mock_users.to_xls.bytes.to_a
+ end
+ end
+
+end
View
@@ -1,29 +1,29 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-describe Array do
+describe ToXls::ArrayWriter do
- it "should throw no error without data" do
+ it "throws no error without data" do
lambda { [].to_xls }.should_not raise_error
end
describe ":name option" do
- it "should default to 'Sheet 1' for sheets with no name" do
+ it "defaults to 'Sheet 1' for sheets with no name" do
make_book([]).worksheets.first.name.should == 'Sheet 1'
end
- it "should use the :name option" do
+ it "uses the :name option" do
make_book([], :name => 'Empty').worksheets.first.name.should == 'Empty'
end
end
describe ":columns option" do
- it "should throw no error without columns" do
+ it "throws no error without columns" do
lambda { make_book([1,2,3]) }.should_not raise_error
end
- it "should throw an error if columns isn't an array" do
+ it "throws an error if columns isn't an array" do
lambda { make_book([1,2,3], :columns => :foo) }.should raise_error
end
- it "should use the attribute keys as columns if it exists" do
+ it "uses the attribute keys as columns if it exists" do
xls = make_book(mock_users)
check_sheet( xls.worksheets.first,
[ [:age, :email, :name],
@@ -33,7 +33,7 @@
]
)
end
- it "should allow re-sorting of the columns by using the :columns option" do
+ it "allows re-sorting of the columns by using the :columns option" do
xls = make_book(mock_users, :columns => [:name, :email, :age])
check_sheet( xls.worksheets.first,
[ [:name, :email, :age],
@@ -44,12 +44,12 @@
)
end
- it "should work properly when you provide it with both data and column names" do
+ it "works properly when you provide it with both data and column names" do
xls = make_book([1,2,3], :columns => [:to_s])
check_sheet( xls.worksheets.first, [ [:to_s], ['1'], ['2'], ['3'] ] )
end
- it "should pick data from associations" do
+ it "picks data from associations" do
xls = make_book(mock_users, :columns => [:name, {:company => [:name]}])
check_sheet( xls.worksheets.first,
[ [:name, :name],
@@ -63,7 +63,7 @@
describe ":headers option" do
- it "should use the headers option if it exists" do
+ it "uses the headers option if it exists" do
xls = make_book( mock_users,
:columns => [:name, :email, :age],
:headers => ['Nombre', 'Correo', 'Edad']
@@ -77,7 +77,7 @@
)
end
- it "should include no headers if the headers option is false" do
+ it "includes no headers if the headers option is false" do
xls = make_book( mock_users,
:columns => [:name, :email, :age],
:headers => false
@@ -90,12 +90,12 @@
)
end
- it "should pick data from associations" do
- xls = make_book( mock_users,
+ it "picks data from associations" do
+ book = make_book( mock_users,
:columns => [:name, {:company => [:name]}],
:headers => [:name, :company_name]
)
- check_sheet( xls.worksheets.first,
+ check_sheet( book.worksheets.first,
[ [:name, :company_name],
['Peter', 'Acme'],
['John', 'Acme'],
@@ -106,5 +106,59 @@
end
+ describe "#write_book" do
+ it "writes a new sheet in a book" do
+ book = Spreadsheet::Workbook.new
+ ToXls::ArrayWriter.new(mock_users).write_book(book)
+ check_sheet( book.worksheets.first,
+ [ [:age, :email, :name],
+ [ 20, 'peter@gmail.com', 'Peter'],
+ [ 25, 'john@gmail.com', 'John'],
+ [ 27, 'day9@day9tv.com', 'Day9']
+ ]
+ )
+ end
+ end
+
+ describe "#write_sheet" do
+ it "writes a new sheet in a book" do
+ book = Spreadsheet::Workbook.new
+ sheet = book.create_worksheet
+ ToXls::ArrayWriter.new(mock_users).write_sheet(sheet)
+ check_sheet( sheet,
+ [ [:age, :email, :name],
+ [ 20, 'peter@gmail.com', 'Peter'],
+ [ 25, 'john@gmail.com', 'John'],
+ [ 27, 'day9@day9tv.com', 'Day9']
+ ]
+ )
+ end
+ end
+
+ describe "#write_io" do
+ it "writes a new book in a stream" do
+ io1 = StringIO.new
+ ToXls::ArrayWriter.new(mock_users).write_io(io1)
+ io2 = StringIO.new
+ xls = make_book(mock_users, {})
+ xls.write(io2)
+
+ io1.string.bytes.to_a.should == io2.string.bytes.to_a
+ end
+ end
+
+ describe "#write_string" do
+ it "writes a new sheet in a string" do
+ str = ToXls::ArrayWriter.new(mock_users).write_string()
+ io = StringIO.new
+ xls = make_book(mock_users, {})
+ xls.write(io)
+
+ str.bytes.to_a.should == io.string.bytes.to_a
+ end
+ end
+
+
+
end

0 comments on commit cb66b43

Please sign in to comment.