active_record_to_xls 1.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.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +116 -0
- data/Rakefile +7 -0
- data/VERSION +1 -0
- data/active_record_to_xls.gemspec +20 -0
- data/init.rb +1 -0
- data/lib/active_record_to_xls.rb +59 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c52c2f9748f585a25ce7925fe0ae4f138c4ce1c672e508da8a9325d503ce8a1f
|
4
|
+
data.tar.gz: 8e95741bd3ad84276ce8583a1f7801c0003a2504db9511fa70f499ce9048dd2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c2be1c0e1487891b38220e98b953c7178219bdfdbb27c4c4ca1b98ca79cc1686bbf28ff695a8b3c0d1e17be7a9064fb24e9763babda62af7cb4af65c175f33f
|
7
|
+
data.tar.gz: 574e48a1a514b27a5984bc6339df7786ae26ff096329cbcc9e8a896bb96e9b46c32645b890d62dfbd8b4b73abcbf8c035210a658b909d40ff720916c45bbb7a4
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Mike Liang
|
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.md
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# ActiveRecordToXls
|
2
|
+
|
3
|
+
This gem will generate XLS data for a collection of ActiveRecord models.
|
4
|
+
|
5
|
+
It was forked from the [to_xls-rails gem](https://github.com/liangwenke/to_xls-rails).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
**Gemfile**:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'active_record_to_xls'
|
13
|
+
```
|
14
|
+
|
15
|
+
**config/initializers/mime_types.rb**:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
Mime::Type.register_alias "text/excel", :xls
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
**controller**:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
format.xls {
|
27
|
+
filename = “my_export.xls”
|
28
|
+
send_data ActiveRecordToXls.call(Widget.all)
|
29
|
+
}
|
30
|
+
```
|
31
|
+
|
32
|
+
**view:**
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
link_to 'Export Excel', my_action_path(format: :xls)
|
36
|
+
```
|
37
|
+
|
38
|
+
### Options
|
39
|
+
|
40
|
+
#### client_encoding
|
41
|
+
|
42
|
+
- **Type:** String
|
43
|
+
- **Default**: UTF-8
|
44
|
+
|
45
|
+
#### only
|
46
|
+
|
47
|
+
- **Type:** Array
|
48
|
+
|
49
|
+
Will only include the columns specified in this array:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
send_data ActiveRecordToXls.call( Widget.all, only: [:title, :body] )
|
53
|
+
```
|
54
|
+
|
55
|
+
#### except
|
56
|
+
|
57
|
+
- **Type:** Array
|
58
|
+
|
59
|
+
Will omit the columns specified in this array, eg:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
send_data ActiveRecordToXls.call( Widget.all, omit: [:id] )
|
63
|
+
```
|
64
|
+
|
65
|
+
#### prepend
|
66
|
+
|
67
|
+
- **Type:** Array of row arrays
|
68
|
+
|
69
|
+
Will prepend above header:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
send_data ActiveRecordToXls.call( Widget.all, prepend: [["Col 0, Row 0", "Col 1, Row 0"], ["Col 0, Row 1"]] )
|
73
|
+
```
|
74
|
+
|
75
|
+
#### header
|
76
|
+
|
77
|
+
- **Type:** true or false
|
78
|
+
- **Default:** false
|
79
|
+
|
80
|
+
Will omit the header row:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
send_data ActiveRecordToXls.call( Widget.all, header: false )
|
84
|
+
```
|
85
|
+
|
86
|
+
#### header_columns
|
87
|
+
|
88
|
+
- **Type**: Array
|
89
|
+
|
90
|
+
Specify header column names:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
send_data ActiveRecordToXls.call( Widget.all, header: false , header_columns: ['Title', 'Description])
|
94
|
+
```
|
95
|
+
|
96
|
+
#### column_width
|
97
|
+
|
98
|
+
- **Type:** Array of integers
|
99
|
+
|
100
|
+
Set column widths:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
send_data ActiveRecordToXls.call( Widget.all, column_width: [17,15,15,40,25,37] )
|
104
|
+
```
|
105
|
+
|
106
|
+
#### append
|
107
|
+
|
108
|
+
- **Type:** Array of row arrays
|
109
|
+
|
110
|
+
Will append this row at end, eg:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
send_data ActiveRecordToXls.call( Widget.all, append: [["Col 0, Row 0", "Col 1, Row 0"], ["Col 0, Row 1"]] )
|
114
|
+
```
|
115
|
+
|
116
|
+
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
version = File.read(File.expand_path("../VERSION", __FILE__)).strip
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'active_record_to_xls'
|
7
|
+
spec.version = version
|
8
|
+
spec.author = "Gordon B. Isnor"
|
9
|
+
spec.email = "info@isnorcreative.com"
|
10
|
+
spec.homepage = "http://github.com/gordonbisnor/active_record_to_xls"
|
11
|
+
spec.summary = "Export Rails ActiveRecord data to Excel XLS data"
|
12
|
+
spec.description = "This simple plugin gives you the ability to get XLS for a collection of ActiveRecord models."
|
13
|
+
spec.files = Dir["{lib,test}/**/*", "[a-zA-Z]*", "init.rb"] - ["Gemfile.lock"]
|
14
|
+
spec.require_path = "lib"
|
15
|
+
spec.license = 'MIT'
|
16
|
+
spec.add_runtime_dependency 'spreadsheet', '~> 0.8.5'
|
17
|
+
spec.add_development_dependency 'rspec', '~> 3.7', '>= 3.7.0'
|
18
|
+
spec.platform = Gem::Platform::RUBY
|
19
|
+
spec.required_rubygems_version = ">= 1.3.4"
|
20
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_record_to_xls'
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spreadsheet'
|
2
|
+
|
3
|
+
class ActiveRecordToXls
|
4
|
+
|
5
|
+
def self.call(target, options = {}, &block)
|
6
|
+
|
7
|
+
return '' if target.nil? && options[:prepend].nil?
|
8
|
+
|
9
|
+
columns = []
|
10
|
+
|
11
|
+
show_header = options[:header] == true
|
12
|
+
|
13
|
+
xls_report = StringIO.new
|
14
|
+
|
15
|
+
Spreadsheet.client_encoding = options[:client_encoding] || "UTF-8"
|
16
|
+
|
17
|
+
book = Spreadsheet::Workbook.new
|
18
|
+
sheet = book.create_worksheet
|
19
|
+
|
20
|
+
columns = if options[:only]
|
21
|
+
Array(options[:only]).map(&:to_sym)
|
22
|
+
elsif !target.nil?
|
23
|
+
if target.first.is_a? Hash
|
24
|
+
target.first.keys.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
|
25
|
+
else
|
26
|
+
target.first.attributes.keys.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
return '' if columns.empty? && options[:prepend].nil?
|
31
|
+
|
32
|
+
sheet_index = 0
|
33
|
+
|
34
|
+
options[:prepend].map { |arr| sheet.row(sheet_index).concat(arr); sheet_index += 1 } unless options[:prepend].nil?
|
35
|
+
|
36
|
+
if show_header
|
37
|
+
sheet.row(sheet_index).concat(options[:header_columns].nil? ? columns.map(&:to_s).map(&:humanize) : options[:header_columns])
|
38
|
+
sheet_index += 1
|
39
|
+
end
|
40
|
+
|
41
|
+
options[:column_width].each_index { |index| sheet.column(index).width = options[:column_width][index] } if options[:column_width]
|
42
|
+
|
43
|
+
target.each_with_index do |obj, i|
|
44
|
+
if block
|
45
|
+
sheet.row(sheet_index).replace(columns.map { |col| block.call(col, obj.is_a?(Hash) ? obj[col] : obj.send(col), i) } )
|
46
|
+
else
|
47
|
+
sheet.row(sheet_index).replace(columns.map { |col| obj.is_a?(Hash) ? obj[col] : obj.send(col) } )
|
48
|
+
end
|
49
|
+
sheet_index += 1
|
50
|
+
end
|
51
|
+
|
52
|
+
options[:append].map { |arr| sheet.row(sheet_index).concat(arr); sheet_index += 1 } unless options[:append].nil?
|
53
|
+
|
54
|
+
book.write(xls_report)
|
55
|
+
|
56
|
+
xls_report.string
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_to_xls
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gordon B. Isnor
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: spreadsheet
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.5
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.7'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 3.7.0
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.7'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.7.0
|
47
|
+
description: This simple plugin gives you the ability to get XLS for a collection
|
48
|
+
of ActiveRecord models.
|
49
|
+
email: info@isnorcreative.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- Gemfile
|
55
|
+
- MIT-LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- VERSION
|
59
|
+
- active_record_to_xls.gemspec
|
60
|
+
- init.rb
|
61
|
+
- lib/active_record_to_xls.rb
|
62
|
+
homepage: http://github.com/gordonbisnor/active_record_to_xls
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 1.3.4
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.7.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Export Rails ActiveRecord data to Excel XLS data
|
86
|
+
test_files: []
|