bc-to_xls 0.0.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 +70 -0
- data/VERSION +1 -0
- data/lib/to_xls.rb +38 -0
- data/rails/init.rb +1 -0
- data/test/test_helper.rb +29 -0
- data/test/to_xls_test.rb +56 -0
- data/test/user_model.rb +21 -0
- metadata +76 -0
data/MIT-LICENSE
ADDED
@@ -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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= to_xls plugin
|
2
|
+
|
3
|
+
This Rails plugin is the easiest way to export to Excel. It gives you the ability to call to_xls to a collection of activerecords (Array). The builder options are the same as to_json / to_xml, except for the :include.
|
4
|
+
|
5
|
+
NOTE: IMO I would use the to_csv plugin; it's more compatible. (http://github.com/arydjmal/to_csv)
|
6
|
+
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
|
10
|
+
@users = User.all
|
11
|
+
|
12
|
+
#
|
13
|
+
# defaults are export headers and all fields
|
14
|
+
#
|
15
|
+
|
16
|
+
@users.to_xls
|
17
|
+
@users.to_xls(:only => [:last_name, :role])
|
18
|
+
@users.to_xls(:headers => false)
|
19
|
+
@users.to_xls(:except => [:last_name, :role])
|
20
|
+
@users.to_xls(:except => :role, :methods => :admin?)
|
21
|
+
|
22
|
+
|
23
|
+
== Real life example
|
24
|
+
|
25
|
+
In config/initializers/mime_types.rb register the custom mime type.
|
26
|
+
|
27
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
28
|
+
|
29
|
+
|
30
|
+
In the controller where you want to export to excel, add the format.xls line.
|
31
|
+
|
32
|
+
class UserController < ApplicationController
|
33
|
+
|
34
|
+
def index
|
35
|
+
@users = User.all
|
36
|
+
|
37
|
+
respond_to do |format|
|
38
|
+
format.html
|
39
|
+
format.xml { render :xml => @users }
|
40
|
+
format.xls { send_data @users.to_xls }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def show...
|
45
|
+
def new...
|
46
|
+
def edit...
|
47
|
+
def create...
|
48
|
+
def update...
|
49
|
+
def destroy...
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
== Dependencies
|
55
|
+
|
56
|
+
None.
|
57
|
+
|
58
|
+
|
59
|
+
== Install
|
60
|
+
|
61
|
+
./script/plugin install git://github.com/arydjmal/to_xls.git
|
62
|
+
|
63
|
+
|
64
|
+
== Note
|
65
|
+
|
66
|
+
Does not work on a single activerecord, ie, User.first.to_xls.
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
Copyright (c) 2009 Ary Djmal, released under the MIT license
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/to_xls.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def to_xls(options = {})
|
4
|
+
output = '<?xml version="1.0" encoding="UTF-8"?><Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"><Worksheet ss:Name="Sheet1"><Table>'
|
5
|
+
|
6
|
+
if self.any?
|
7
|
+
|
8
|
+
attributes = self.first.attributes.keys.map { |c| c.to_sym }
|
9
|
+
|
10
|
+
if options[:only]
|
11
|
+
# the "& attributes" get rid of invalid columns
|
12
|
+
columns = options[:only].to_a & attributes
|
13
|
+
else
|
14
|
+
columns = attributes - options[:except].to_a
|
15
|
+
end
|
16
|
+
|
17
|
+
columns += options[:methods].to_a
|
18
|
+
|
19
|
+
if columns.any?
|
20
|
+
unless options[:headers] == false
|
21
|
+
output << "<Row>"
|
22
|
+
columns.each { |column| output << "<Cell><Data ss:Type=\"String\">#{self.class.human_attribute_name(column)}</Data></Cell>" }
|
23
|
+
output << "</Row>"
|
24
|
+
end
|
25
|
+
|
26
|
+
self.each do |item|
|
27
|
+
output << "<Row>"
|
28
|
+
columns.each { |column| output << "<Cell><Data ss:Type=\"#{item.send(column).kind_of?(Integer) ? 'Number' : 'String'}\">#{item.send(column)}</Data></Cell>" }
|
29
|
+
output << "</Row>"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
output << '</Table></Worksheet></Workbook>'
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'to_xls'
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
plugin_test_dir = File.dirname(__FILE__)
|
3
|
+
RAILS_ROOT = plugin_test_dir
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'test/unit'
|
7
|
+
require 'multi_rails_init'
|
8
|
+
require 'test_help'
|
9
|
+
|
10
|
+
require plugin_test_dir + '/../init.rb'
|
11
|
+
|
12
|
+
TestCaseClass = ActiveSupport::TestCase rescue Test::Unit::TestCase
|
13
|
+
|
14
|
+
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
|
15
|
+
|
16
|
+
ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml"))
|
17
|
+
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
|
18
|
+
ActiveRecord::Migration.verbose = false
|
19
|
+
load(File.join(plugin_test_dir, "db", "schema.rb"))
|
20
|
+
|
21
|
+
Dir["#{plugin_test_dir}/fixtures/*.rb"].each {|file| require file }
|
22
|
+
|
23
|
+
class TestCaseClass #:nodoc:
|
24
|
+
self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
25
|
+
self.use_transactional_fixtures = true
|
26
|
+
self.use_instantiated_fixtures = false
|
27
|
+
|
28
|
+
fixtures :categories, :notes, :departments
|
29
|
+
end
|
data/test/to_xls_test.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require File.dirname(__FILE__) + '/../lib/to_xls'
|
4
|
+
require File.dirname(__FILE__) + '/user_model'
|
5
|
+
|
6
|
+
class ToXlsTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@users = []
|
10
|
+
@users << User.new(:id => 1, :name => 'Ary', :age => 24)
|
11
|
+
@users << User.new(:id => 2, :name => 'Nati', :age => 21)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_with_empty_array
|
15
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table></Table></Worksheet></Workbook>", [].to_xls )
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_with_no_options
|
19
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls )
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_with_no_headers
|
23
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:headers => false) )
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_with_only
|
27
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => :name) )
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_with_empty_only
|
31
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table></Table></Worksheet></Workbook>", @users.to_xls(:only => "") )
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_with_only_and_wrong_column_names
|
35
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:only => [:name, :yoyo]) )
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_with_except
|
39
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">age</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">24</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">21</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name]) )
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_with_except_and_only_should_listen_to_only
|
43
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">name</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:except => [:id, :name], :only => :name) )
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_with_except
|
47
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">id</Data></Cell><Cell><Data ss:Type=\"String\">name</Data></Cell><Cell><Data ss:Type=\"String\">age</Data></Cell><Cell><Data ss:Type=\"String\">is_old?</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">1</Data></Cell><Cell><Data ss:Type=\"String\">Ary</Data></Cell><Cell><Data ss:Type=\"Number\">24</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row><Row><Cell><Data ss:Type=\"Number\">2</Data></Cell><Cell><Data ss:Type=\"String\">Nati</Data></Cell><Cell><Data ss:Type=\"Number\">21</Data></Cell><Cell><Data ss:Type=\"String\">false</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls(:methods => [:is_old?]) )
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_with_i18n
|
51
|
+
I18n.locale = "pt-PT"
|
52
|
+
debugger
|
53
|
+
assert_equal( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><Worksheet ss:Name=\"Sheet1\"><Table><Row><Cell><Data ss:Type=\"String\">nome</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Ary</Data></Cell></Row><Row><Cell><Data ss:Type=\"String\">Nati</Data></Cell></Row></Table></Worksheet></Workbook>", @users.to_xls)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/test/user_model.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class User
|
2
|
+
COLUMNS = ["id", "name", "age"]
|
3
|
+
|
4
|
+
COLUMNS.each {|column| attr_reader column }
|
5
|
+
|
6
|
+
def initialize(params={})
|
7
|
+
COLUMNS.each {|column| eval("@#{column} = params[:#{column}]")}
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def keys
|
15
|
+
COLUMNS
|
16
|
+
end
|
17
|
+
|
18
|
+
def is_old?
|
19
|
+
age > 40
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bc-to_xls
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Jo\xC3\xA3o Jesus"
|
8
|
+
- Vasco Andrade e Silva
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-02-12 00:00:00 +00:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "1.1"
|
25
|
+
version:
|
26
|
+
description: to_xls from http://github.com/arydjmal/to_xls with i18n support
|
27
|
+
email: info@byclosure.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- VERSION
|
38
|
+
- lib/to_xls.rb
|
39
|
+
- rails/init.rb
|
40
|
+
- test/to_xls_test.rb
|
41
|
+
- test/user_model.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/Byclosure/to_xls
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --main
|
49
|
+
- README.rdoc
|
50
|
+
- --inline-source
|
51
|
+
- --line-numbers
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: to_xls from http://github.com/arydjmal/to_xls with i18n support
|
73
|
+
test_files:
|
74
|
+
- test/test_helper.rb
|
75
|
+
- test/to_xls_test.rb
|
76
|
+
- test/user_model.rb
|