export_to 0.1.1 → 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 +5 -5
- data/Gemfile +4 -0
- data/Gemfile.lock +64 -21
- data/README.md +1 -1
- data/export_to.gemspec +3 -3
- data/lib/export_to/base.rb +18 -46
- data/lib/export_to/exporter/csv.rb +15 -0
- data/lib/export_to/exporter/xls.rb +21 -0
- data/lib/export_to/exporter/xlsx.rb +62 -0
- data/lib/export_to/version.rb +1 -1
- data/lib/export_to.rb +3 -0
- metadata +22 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: dcdb1f8a1a6dec33c36024fc0f577b1a492d7fe7e1bd7a8cf3d639be76814bde
|
|
4
|
+
data.tar.gz: 8b844513054aba11c5384dfc30a46ba9ae9abded9ed62001c9fb4af9d0e9a0d1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 58e3294ec055895590fe1766d0b0ab54b0c5176942481400ec9d142b25dc2680171888e0a8f9d8338d264bc75870f08ab8d564b198b0ee95d2bac26edacbc66c
|
|
7
|
+
data.tar.gz: e7dd88214c37584d37c164dc1be3ec418adaa7e51bf5fce99b8f60ad648646378584f95d79d9bb7347df8fd8370f8d51b92fc006a9f6a25829ce6319005ebd8d
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
export_to (0.1.
|
|
5
|
-
activesupport
|
|
4
|
+
export_to (0.1.2)
|
|
5
|
+
activesupport (>= 6, < 7)
|
|
6
6
|
fast_excel
|
|
7
7
|
iconv
|
|
8
8
|
spreadsheet
|
|
@@ -10,37 +10,80 @@ PATH
|
|
|
10
10
|
GEM
|
|
11
11
|
remote: https://rubygems.org/
|
|
12
12
|
specs:
|
|
13
|
-
|
|
13
|
+
actionpack (6.1.7.2)
|
|
14
|
+
actionview (= 6.1.7.2)
|
|
15
|
+
activesupport (= 6.1.7.2)
|
|
16
|
+
rack (~> 2.0, >= 2.0.9)
|
|
17
|
+
rack-test (>= 0.6.3)
|
|
18
|
+
rails-dom-testing (~> 2.0)
|
|
19
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
20
|
+
actionview (6.1.7.2)
|
|
21
|
+
activesupport (= 6.1.7.2)
|
|
22
|
+
builder (~> 3.1)
|
|
23
|
+
erubi (~> 1.4)
|
|
24
|
+
rails-dom-testing (~> 2.0)
|
|
25
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
26
|
+
activesupport (6.1.7.2)
|
|
14
27
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
15
|
-
i18n (>=
|
|
16
|
-
minitest (
|
|
17
|
-
tzinfo (~>
|
|
18
|
-
|
|
19
|
-
|
|
28
|
+
i18n (>= 1.6, < 2)
|
|
29
|
+
minitest (>= 5.1)
|
|
30
|
+
tzinfo (~> 2.0)
|
|
31
|
+
zeitwerk (~> 2.3)
|
|
32
|
+
builder (3.2.4)
|
|
33
|
+
concurrent-ruby (1.2.0)
|
|
34
|
+
crass (1.0.6)
|
|
35
|
+
erubi (1.12.0)
|
|
36
|
+
fast_excel (0.4.1)
|
|
20
37
|
ffi (> 1.9, < 2)
|
|
21
|
-
ffi (1.
|
|
22
|
-
i18n (1.
|
|
38
|
+
ffi (1.15.5)
|
|
39
|
+
i18n (1.12.0)
|
|
23
40
|
concurrent-ruby (~> 1.0)
|
|
24
|
-
iconv (1.0.
|
|
41
|
+
iconv (1.0.8)
|
|
42
|
+
loofah (2.19.1)
|
|
43
|
+
crass (~> 1.0.2)
|
|
44
|
+
nokogiri (>= 1.5.9)
|
|
45
|
+
method_source (1.0.0)
|
|
46
|
+
mini_portile2 (2.8.1)
|
|
25
47
|
minitest (5.11.3)
|
|
26
|
-
|
|
27
|
-
|
|
48
|
+
nokogiri (1.14.1)
|
|
49
|
+
mini_portile2 (~> 2.8.0)
|
|
50
|
+
racc (~> 1.4)
|
|
51
|
+
racc (1.6.2)
|
|
52
|
+
rack (2.2.6.2)
|
|
53
|
+
rack-test (2.0.2)
|
|
54
|
+
rack (>= 1.3)
|
|
55
|
+
rails-dom-testing (2.0.3)
|
|
56
|
+
activesupport (>= 4.2.0)
|
|
57
|
+
nokogiri (>= 1.6)
|
|
58
|
+
rails-html-sanitizer (1.5.0)
|
|
59
|
+
loofah (~> 2.19, >= 2.19.1)
|
|
60
|
+
railties (6.1.7.2)
|
|
61
|
+
actionpack (= 6.1.7.2)
|
|
62
|
+
activesupport (= 6.1.7.2)
|
|
63
|
+
method_source
|
|
64
|
+
rake (>= 12.2)
|
|
65
|
+
thor (~> 1.0)
|
|
66
|
+
rake (13.0.6)
|
|
67
|
+
ruby-ole (1.2.12.2)
|
|
28
68
|
shoulda-context (1.2.2)
|
|
29
|
-
spreadsheet (1.
|
|
30
|
-
ruby-ole
|
|
31
|
-
|
|
32
|
-
tzinfo (
|
|
33
|
-
|
|
69
|
+
spreadsheet (1.3.0)
|
|
70
|
+
ruby-ole
|
|
71
|
+
thor (1.2.1)
|
|
72
|
+
tzinfo (2.0.6)
|
|
73
|
+
concurrent-ruby (~> 1.0)
|
|
74
|
+
zeitwerk (2.6.6)
|
|
34
75
|
|
|
35
76
|
PLATFORMS
|
|
36
77
|
ruby
|
|
37
78
|
|
|
38
79
|
DEPENDENCIES
|
|
39
|
-
|
|
80
|
+
activesupport (>= 6, < 7)
|
|
81
|
+
bundler (>= 2.0.0)
|
|
40
82
|
export_to!
|
|
41
83
|
minitest (~> 5.0)
|
|
42
|
-
|
|
84
|
+
railties (>= 6, < 7)
|
|
85
|
+
rake (>= 13)
|
|
43
86
|
shoulda-context
|
|
44
87
|
|
|
45
88
|
BUNDLED WITH
|
|
46
|
-
|
|
89
|
+
2.3.14
|
data/README.md
CHANGED
|
@@ -32,7 +32,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
32
32
|
|
|
33
33
|
## Contributing
|
|
34
34
|
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/superlanding/export_to.
|
|
36
36
|
|
|
37
37
|
## License
|
|
38
38
|
|
data/export_to.gemspec
CHANGED
|
@@ -23,13 +23,13 @@ Gem::Specification.new do |spec|
|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
24
24
|
spec.require_paths = ["lib"]
|
|
25
25
|
|
|
26
|
-
spec.add_development_dependency "bundler", "
|
|
27
|
-
spec.add_development_dependency "rake", "
|
|
26
|
+
spec.add_development_dependency "bundler", ">= 2.0.0"
|
|
27
|
+
spec.add_development_dependency "rake", ">= 13"
|
|
28
28
|
spec.add_development_dependency "minitest", "~> 5.0"
|
|
29
29
|
spec.add_development_dependency "shoulda-context"
|
|
30
30
|
|
|
31
31
|
spec.add_dependency "fast_excel"
|
|
32
32
|
spec.add_dependency "spreadsheet"
|
|
33
33
|
spec.add_dependency "iconv"
|
|
34
|
-
spec.add_dependency "activesupport"
|
|
34
|
+
spec.add_dependency "activesupport", ">= 6", "< 7"
|
|
35
35
|
end
|
data/lib/export_to/base.rb
CHANGED
|
@@ -1,72 +1,36 @@
|
|
|
1
1
|
module ExportTo
|
|
2
2
|
class Base < Struct.new(:records)
|
|
3
|
+
class_attribute :options
|
|
3
4
|
class_attribute :head_titles
|
|
4
5
|
class_attribute :body_keys
|
|
6
|
+
class_attribute :column_formats
|
|
5
7
|
class_attribute :body_column_proc
|
|
8
|
+
|
|
6
9
|
class_attribute :presenter_klass
|
|
7
10
|
class_attribute :join_relation
|
|
8
11
|
class_attribute :each_proc, :each_method
|
|
9
|
-
class_attribute :xlsx_file_path, :xlsx_file_name
|
|
10
12
|
|
|
11
13
|
# 預設 presenter
|
|
12
14
|
self.presenter_klass = Presenter
|
|
13
15
|
self.each_method = :each
|
|
14
|
-
self.xlsx_file_path = '/tmp'
|
|
15
|
-
self.xlsx_file_name = '/file'
|
|
16
16
|
|
|
17
17
|
attr_accessor :object
|
|
18
18
|
|
|
19
19
|
def to_csv
|
|
20
|
-
|
|
21
|
-
rows! do |columns, model, x|
|
|
22
|
-
csv << columns
|
|
23
|
-
end
|
|
24
|
-
end
|
|
20
|
+
ExportTo::Exporter::Csv.new(self).export
|
|
25
21
|
end
|
|
26
22
|
|
|
27
23
|
# 新版 Excel
|
|
28
|
-
def to_xlsx
|
|
29
|
-
|
|
30
|
-
file_name ||= self.class.xlsx_file_name
|
|
31
|
-
path = to_xlsx_file(file_path, file_name)
|
|
32
|
-
# TODO: 讀取檔案回傳
|
|
33
|
-
File.open(path, 'rb') { |f| f.read }
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# 新版 Excel (outpuut: path)
|
|
37
|
-
def to_xlsx_file(file_path="tmp", file_name="export")
|
|
38
|
-
path = "#{file_path}/#{file_name}_#{Time.now.to_i}_#{SecureRandom.hex}.xlsx"
|
|
39
|
-
workbook = FastExcel.open(path, constant_memory: true)
|
|
40
|
-
worksheet = workbook.add_worksheet("Default")
|
|
41
|
-
|
|
42
|
-
bold = workbook.bold_cell_format
|
|
43
|
-
worksheet.set_row(0, FastExcel::DEF_COL_WIDTH, bold)
|
|
44
|
-
|
|
45
|
-
rows! do |columns, model, x|
|
|
46
|
-
worksheet.write_row(x, columns)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
workbook.close
|
|
50
|
-
path
|
|
24
|
+
def to_xlsx
|
|
25
|
+
ExportTo::Exporter::Xlsx.new(self).export
|
|
51
26
|
end
|
|
52
27
|
|
|
53
28
|
# 舊版 Excel
|
|
54
29
|
def to_xls
|
|
55
|
-
|
|
56
|
-
sheet = book.create_worksheet
|
|
57
|
-
|
|
58
|
-
rows! do |columns, model, x|
|
|
59
|
-
sheet.row(x).concat(columns)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
spreadsheet = StringIO.new
|
|
63
|
-
book.write(spreadsheet)
|
|
64
|
-
spreadsheet.string
|
|
30
|
+
ExportTo::Exporter::Xlsx.new(self).export
|
|
65
31
|
end
|
|
66
32
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def rows
|
|
33
|
+
def each
|
|
70
34
|
data = []
|
|
71
35
|
rows! do |columns, model, x|
|
|
72
36
|
data.push(columns)
|
|
@@ -74,7 +38,7 @@ module ExportTo
|
|
|
74
38
|
data
|
|
75
39
|
end
|
|
76
40
|
|
|
77
|
-
def
|
|
41
|
+
def each!
|
|
78
42
|
i = 0
|
|
79
43
|
|
|
80
44
|
yield(self.class.head_titles, nil, i)
|
|
@@ -106,6 +70,8 @@ module ExportTo
|
|
|
106
70
|
end
|
|
107
71
|
end
|
|
108
72
|
|
|
73
|
+
private
|
|
74
|
+
|
|
109
75
|
def each_models(&block)
|
|
110
76
|
case self.class.each_method
|
|
111
77
|
when :each
|
|
@@ -129,13 +95,19 @@ module ExportTo
|
|
|
129
95
|
|
|
130
96
|
protected
|
|
131
97
|
|
|
132
|
-
def
|
|
98
|
+
def excel(options)
|
|
99
|
+
self.options = options
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def set(title, key, format: {}, &block)
|
|
133
103
|
self.head_titles ||= []
|
|
134
104
|
self.body_keys ||= []
|
|
105
|
+
self.column_formats ||= []
|
|
135
106
|
self.body_column_proc ||= []
|
|
136
107
|
|
|
137
108
|
self.head_titles.push(title)
|
|
138
109
|
self.body_keys.push(key)
|
|
110
|
+
self.column_formats.push(format)
|
|
139
111
|
self.body_column_proc.push(block)
|
|
140
112
|
end
|
|
141
113
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module ExportTo
|
|
2
|
+
module Exporter
|
|
3
|
+
class Xls < Struct.new(:rows)
|
|
4
|
+
|
|
5
|
+
def to_xls
|
|
6
|
+
book = Spreadsheet::Workbook.new
|
|
7
|
+
sheet = book.create_worksheet
|
|
8
|
+
|
|
9
|
+
rows.each! do |columns, model, x|
|
|
10
|
+
sheet.row(x).concat(columns)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
spreadsheet = StringIO.new
|
|
14
|
+
book.write(spreadsheet)
|
|
15
|
+
|
|
16
|
+
spreadsheet.string
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module ExportTo
|
|
2
|
+
module Exporter
|
|
3
|
+
class Xlsx < Struct.new(:rows)
|
|
4
|
+
|
|
5
|
+
def export
|
|
6
|
+
rows.each! do |columns, model, x|
|
|
7
|
+
worksheet.set_row(x, height, nil)
|
|
8
|
+
worksheet.write_row(x, columns)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
workbook.read_string
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
protected
|
|
15
|
+
|
|
16
|
+
def workbook
|
|
17
|
+
@workbook ||= FastExcel.open(constant_memory: true)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def worksheet
|
|
21
|
+
@worksheet ||= begin
|
|
22
|
+
ws = workbook.add_worksheet(options.fetch(:worksheet) { "Default" })
|
|
23
|
+
# 設定表頭樣式
|
|
24
|
+
ws.set_row(0, height, head_format)
|
|
25
|
+
|
|
26
|
+
# 表身樣式
|
|
27
|
+
column_formats.each_with_index do |format, i|
|
|
28
|
+
next if format.blank?
|
|
29
|
+
|
|
30
|
+
width = format.delete(:width) { FastExcel::DEF_COL_WIDTH }
|
|
31
|
+
|
|
32
|
+
puts "#{i} -> #{width} -> #{format}"
|
|
33
|
+
|
|
34
|
+
ws.set_column(i, i, width, workbook.add_format(format))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
ws
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def head_format
|
|
42
|
+
workbook.add_format(bold: true, bg_color: "#e5dbad", align: { h: :center })
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def options
|
|
46
|
+
@options ||= (rows.class.options || {})
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def worksheet_name
|
|
50
|
+
options.fetch(:worksheet) { "Default" }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def height
|
|
54
|
+
options.fetch(:height) { 20 }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def column_formats
|
|
58
|
+
@column_formats ||= rows.class.column_formats.dup || []
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/export_to/version.rb
CHANGED
data/lib/export_to.rb
CHANGED
metadata
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: export_to
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- eddie
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2023-02-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- - "
|
|
17
|
+
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: 2.0.0
|
|
20
20
|
type: :development
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
|
-
- - "
|
|
24
|
+
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version:
|
|
26
|
+
version: 2.0.0
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: rake
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- - "
|
|
31
|
+
- - ">="
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
33
|
+
version: '13'
|
|
34
34
|
type: :development
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
|
-
- - "
|
|
38
|
+
- - ">="
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
40
|
+
version: '13'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: minitest
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -114,14 +114,20 @@ dependencies:
|
|
|
114
114
|
requirements:
|
|
115
115
|
- - ">="
|
|
116
116
|
- !ruby/object:Gem::Version
|
|
117
|
-
version: '
|
|
117
|
+
version: '6'
|
|
118
|
+
- - "<"
|
|
119
|
+
- !ruby/object:Gem::Version
|
|
120
|
+
version: '7'
|
|
118
121
|
type: :runtime
|
|
119
122
|
prerelease: false
|
|
120
123
|
version_requirements: !ruby/object:Gem::Requirement
|
|
121
124
|
requirements:
|
|
122
125
|
- - ">="
|
|
123
126
|
- !ruby/object:Gem::Version
|
|
124
|
-
version: '
|
|
127
|
+
version: '6'
|
|
128
|
+
- - "<"
|
|
129
|
+
- !ruby/object:Gem::Version
|
|
130
|
+
version: '7'
|
|
125
131
|
description: Export activerecord easiest way (csv, xls, xlsx)
|
|
126
132
|
email:
|
|
127
133
|
- eddie.li.624@gmail.com
|
|
@@ -141,6 +147,9 @@ files:
|
|
|
141
147
|
- export_to.gemspec
|
|
142
148
|
- lib/export_to.rb
|
|
143
149
|
- lib/export_to/base.rb
|
|
150
|
+
- lib/export_to/exporter/csv.rb
|
|
151
|
+
- lib/export_to/exporter/xls.rb
|
|
152
|
+
- lib/export_to/exporter/xlsx.rb
|
|
144
153
|
- lib/export_to/presenter.rb
|
|
145
154
|
- lib/export_to/version.rb
|
|
146
155
|
homepage: https://github.com/superlanding/export_to
|
|
@@ -162,8 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
162
171
|
- !ruby/object:Gem::Version
|
|
163
172
|
version: '0'
|
|
164
173
|
requirements: []
|
|
165
|
-
|
|
166
|
-
rubygems_version: 2.4.8
|
|
174
|
+
rubygems_version: 3.1.6
|
|
167
175
|
signing_key:
|
|
168
176
|
specification_version: 4
|
|
169
177
|
summary: Export activerecord easiest way (csv, xls, xlsx)
|