al-to_xls 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +64 -0
- data/init.rb +1 -0
- data/lib/to_xls.rb +92 -0
- data/to_xls.gemspec +29 -0
- metadata +87 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ary Djmal, Enrique García, Francisco Juan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= to_xls gem
|
2
|
+
|
3
|
+
This gem transform an Array or Hash into a excel file using the spreadsheet gem.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
@users = User.all
|
8
|
+
|
9
|
+
@users.to_xls
|
10
|
+
@users.to_xls(:headers => false)
|
11
|
+
@users.to_xls(:columns => [:name, :role])
|
12
|
+
@users.to_xls(:columns => [:name, {:company => [:name, :address]}])
|
13
|
+
@users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => [:name, :company, :address])
|
14
|
+
|
15
|
+
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.
|
16
|
+
|
17
|
+
{ :users => @users, :posts => @posts }.to_xls
|
18
|
+
{ :users => @users, :posts => @posts }.to_xls(:users => { :columns => [:name, :role] }, :posts => { :headers => [:title, :content] })
|
19
|
+
|
20
|
+
In order to send a file from the controller, you can save it on your server first:
|
21
|
+
|
22
|
+
@users.to_xls.write '/path/to/file/users.xls'
|
23
|
+
send_file '/path/to/file/users.xls'
|
24
|
+
|
25
|
+
Alternatively you can use the method to_xls_data and send_data
|
26
|
+
|
27
|
+
send_data @users.to_xls_data, :filename => 'users.xls'
|
28
|
+
|
29
|
+
The method to_xls_data accepts the same parameters as to_xls.
|
30
|
+
|
31
|
+
== Requirements
|
32
|
+
|
33
|
+
In config/initializers/mime_types.rb register the custom mime type.
|
34
|
+
|
35
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
36
|
+
|
37
|
+
== How to use
|
38
|
+
|
39
|
+
In the controller where you want to export to excel, add the format.xls line.
|
40
|
+
|
41
|
+
class UserController < ApplicationController
|
42
|
+
|
43
|
+
def index
|
44
|
+
@users = User.all
|
45
|
+
|
46
|
+
respond_to do |format|
|
47
|
+
format.html
|
48
|
+
format.xml { render :xml => @users }
|
49
|
+
format.xls { send_data @users.to_xls_data, :filename => 'users.xls' }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
== Dependencies
|
56
|
+
|
57
|
+
spreadsheet gem
|
58
|
+
|
59
|
+
== Install
|
60
|
+
|
61
|
+
Include next gems in your environment.rb config file:
|
62
|
+
|
63
|
+
config.gem 'to_xls'
|
64
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'to_xls'
|
data/lib/to_xls.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spreadsheet'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
class Hash
|
6
|
+
def to_xls(options = {})
|
7
|
+
book = Spreadsheet::Workbook.new
|
8
|
+
|
9
|
+
each do |key, value|
|
10
|
+
sheet = book.create_worksheet
|
11
|
+
sheet.name = key.to_s
|
12
|
+
value.to_xls({ :sheet => sheet }.merge(options[key] || {}))
|
13
|
+
end
|
14
|
+
|
15
|
+
return book
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_xls_data(options = {})
|
19
|
+
data = StringIO.new('')
|
20
|
+
self.to_xls(options).write(data)
|
21
|
+
return data.string
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Array
|
26
|
+
# Options for to_xls: columns, name, header, sheet
|
27
|
+
def to_xls(options = {})
|
28
|
+
sheet = options[:sheet]
|
29
|
+
unless sheet
|
30
|
+
book = Spreadsheet::Workbook.new
|
31
|
+
sheet = book.create_worksheet
|
32
|
+
sheet.name = options[:name] || 'Sheet 1'
|
33
|
+
end
|
34
|
+
|
35
|
+
if self.any?
|
36
|
+
columns = options[:columns] || self.first.attributes.keys.sort
|
37
|
+
|
38
|
+
if columns.any?
|
39
|
+
line = 0
|
40
|
+
|
41
|
+
unless options[:headers] == false
|
42
|
+
if options[:headers].is_a?(Array)
|
43
|
+
sheet.row(0).concat options[:headers].collect(&:to_s)
|
44
|
+
else
|
45
|
+
aux_headers_to_xls(self.first, columns, sheet.row(0))
|
46
|
+
end
|
47
|
+
line = 1
|
48
|
+
end
|
49
|
+
|
50
|
+
self.each do |item|
|
51
|
+
row = sheet.row(line)
|
52
|
+
columns.each {|column| aux_to_xls(item, column, row)}
|
53
|
+
line += 1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
return book || sheet
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_xls_data(options = {})
|
62
|
+
data = StringIO.new('')
|
63
|
+
self.to_xls(options).write(data)
|
64
|
+
return data.string
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def aux_to_xls(item, column, row)
|
69
|
+
if item.nil?
|
70
|
+
row.push(nil)
|
71
|
+
elsif column.is_a?(String) or column.is_a?(Symbol)
|
72
|
+
row.push(item.send(column))
|
73
|
+
elsif column.is_a?(Hash)
|
74
|
+
column.each{|key, values| aux_to_xls(item.send(key), values, row)}
|
75
|
+
elsif column.is_a?(Array)
|
76
|
+
column.each{|value| aux_to_xls(item, value, row)}
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def aux_headers_to_xls(item, column, row)
|
81
|
+
if item.nil?
|
82
|
+
row.push(nil)
|
83
|
+
elsif column.is_a?(String) or column.is_a?(Symbol)
|
84
|
+
row.push("#{item.class.name.underscore}_#{column}")
|
85
|
+
elsif column.is_a?(Hash)
|
86
|
+
column.each{|key, values| aux_headers_to_xls(item.send(key), values, row)}
|
87
|
+
elsif column.is_a?(Array)
|
88
|
+
column.each{|value| aux_headers_to_xls(item, value, row)}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
data/to_xls.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{al-to_xls}
|
3
|
+
s.version = "0.2.0"
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Enrique Garcia Cota", "Francisco de Juan", "Alan Larkin"]
|
7
|
+
s.date = %q{2010-10-31}
|
8
|
+
s.description = %q{Transform an Array or Hash into a excel file using the spreadsheet gem.}
|
9
|
+
s.email = %q{alan.larkin+to_xls@gmail.com}
|
10
|
+
s.extra_rdoc_files = [
|
11
|
+
"MIT-LICENSE",
|
12
|
+
"README.rdoc"
|
13
|
+
]
|
14
|
+
s.files = [
|
15
|
+
"MIT-LICENSE",
|
16
|
+
"README.rdoc",
|
17
|
+
"to_xls.gemspec",
|
18
|
+
"init.rb",
|
19
|
+
"lib/to_xls.rb"
|
20
|
+
]
|
21
|
+
s.homepage = %q{http://github.com/al/to_xls}
|
22
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
s.rubygems_version = %q{1.3.5}
|
25
|
+
s.summary = %q{To xls}
|
26
|
+
|
27
|
+
s.add_dependency("spreadsheet", ">= 0")
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: al-to_xls
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Enrique Garcia Cota
|
14
|
+
- Francisco de Juan
|
15
|
+
- Alan Larkin
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2010-10-31 00:00:00 +01:00
|
21
|
+
default_executable:
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
name: spreadsheet
|
25
|
+
prerelease: false
|
26
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 3
|
32
|
+
segments:
|
33
|
+
- 0
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Transform an Array or Hash into a excel file using the spreadsheet gem.
|
38
|
+
email: alan.larkin+to_xls@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- MIT-LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- MIT-LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- to_xls.gemspec
|
50
|
+
- init.rb
|
51
|
+
- lib/to_xls.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/al/to_xls
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --charset=UTF-8
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.7
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: To xls
|
86
|
+
test_files: []
|
87
|
+
|