to_xls 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +62 -0
  3. data/init.rb +1 -0
  4. data/lib/to_xls.rb +57 -0
  5. data/to_xls.gemspec +39 -0
  6. metadata +86 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ary Djmal
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.
@@ -0,0 +1,62 @@
1
+ = to_xls gem
2
+
3
+ This gem transform an Array into a excel file using the spreadsheet gem.
4
+
5
+ == Usage
6
+
7
+ @users = User.all
8
+
9
+ #
10
+ # defaults are export headers and all fields
11
+ #
12
+
13
+ @users.to_xls
14
+ @users.to_xls(:headers => false)
15
+ @users.to_xls(:columns => [:name, :role])
16
+ @users.to_xls(:columns => [:name, {:company => [:name, :address]}])
17
+ @users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => [:name, :company, :address])
18
+
19
+
20
+ == Requirements
21
+
22
+ In config/initializers/mime_types.rb register the custom mime type.
23
+
24
+ Mime::Type.register "application/vnd.ms-excel", :xls
25
+
26
+ == How to use
27
+
28
+ In the controller where you want to export to excel, add the format.xls line.
29
+
30
+ class UserController < ApplicationController
31
+
32
+ def index
33
+ @users = User.all
34
+
35
+ respond_to do |format|
36
+ format.html
37
+ format.xml { render :xml => @users }
38
+ format.xls { send_data @users.to_xls }
39
+ end
40
+ end
41
+
42
+ def show...
43
+ def new...
44
+ def edit...
45
+ def create...
46
+ def update...
47
+ def destroy...
48
+
49
+ end
50
+
51
+
52
+ == Dependencies
53
+
54
+ spreadsheet gem
55
+
56
+
57
+ == Install
58
+
59
+ Include this gem in your environment.rb config file:
60
+
61
+ config.gem 'to_xls'
62
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'to_xls'
@@ -0,0 +1,57 @@
1
+ class Array
2
+ # Options for to_xls: columns, name, header
3
+ def to_xls(options = {})
4
+
5
+ book = Spreadsheet::Workbook.new
6
+ sheet = book.create_worksheet
7
+
8
+ sheet.name = options[:name] || 'Sheet 1'
9
+
10
+ if self.any?
11
+ columns = options[:columns] || self.first.attributes.keys.sort
12
+
13
+ if columns.any?
14
+ line = 0
15
+
16
+ unless options[:headers] == false
17
+ if options[:headers].is_a?(Array)
18
+ sheet.row(0).concat options[:headers]
19
+ else
20
+ aux_headers_to_xls(self.first, columns, sheet.row(0))
21
+ end
22
+ line = 1
23
+ end
24
+
25
+ self.each do |item|
26
+ row = sheet.row(line)
27
+ columns.each {|column| aux_to_xls(item, column, row)}
28
+ line += 1
29
+ end
30
+ end
31
+ end
32
+
33
+ return book
34
+ end
35
+
36
+ private
37
+ def aux_to_xls(item, column, row)
38
+ if column.is_a?(String) or column.is_a?(Symbol)
39
+ row.push(item.send(column))
40
+ elsif column.is_a?(Hash)
41
+ column.each{|key, values| aux_to_xls(item.send(key), values, row)}
42
+ elsif column.is_a?(Array)
43
+ column.each{|value| aux_to_xls(item, value, row)}
44
+ end
45
+ end
46
+
47
+ def aux_headers_to_xls(item, column, row)
48
+ if column.is_a?(String) or column.is_a?(Symbol)
49
+ row.push("#{item.class.name.underscore}_#{column}")
50
+ elsif column.is_a?(Hash)
51
+ column.each{|key, values| aux_headers_to_xls(item.send(key), values, row)}
52
+ elsif column.is_a?(Array)
53
+ column.each{|value| aux_headers_to_xls(item, value, row)}
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,39 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{to_xls}
3
+ s.version = "0.0.1"
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"]
7
+ s.date = %q{2010-05-18}
8
+ s.description = %q{Transform an Array into a excel file using the spreadsheet gem.}
9
+ s.email = %q{github@splendeo.es}
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/splendeo/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
+ if s.respond_to? :specification_version then
28
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
29
+ s.specification_version = 3
30
+
31
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
32
+ s.add_development_dependency(%q<spreadsheet>, [">= 0"])
33
+ else
34
+ s.add_dependency(%q<spreadsheet>, [">= 0"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<spreadsheet>, [">= 0"])
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_xls
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Enrique Garcia Cota
14
+ - Francisco de Juan
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-05-18 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: spreadsheet
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: Transform an Array into a excel file using the spreadsheet gem.
37
+ email: github@splendeo.es
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - MIT-LICENSE
44
+ - README.rdoc
45
+ files:
46
+ - MIT-LICENSE
47
+ - README.rdoc
48
+ - to_xls.gemspec
49
+ - init.rb
50
+ - lib/to_xls.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/splendeo/to_xls
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: To xls
85
+ test_files: []
86
+