ntq_excelsior 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -2
- data/README.md +26 -2
- data/lib/ntq_excelsior/exporter.rb +64 -27
- data/lib/ntq_excelsior/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1f1c651087df6ca7f9dea48884d8976b210294cb95a2577a3048f57ac8488ee
|
4
|
+
data.tar.gz: ce39afeae3e37049578a191dea797ce8c7cb06be31c3e60fb726cb823a93140f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62c3a791e7fce170b95c2fcd07fa4c9410b57c7aa10570925169cdfa2f38ee603c102a59fafeb49db077ddba9b9644bd6380662105936f12f033c04794d55f33
|
7
|
+
data.tar.gz: c22d31f130c0d0c3ae5f9a52f346255cb0926ad5c56f93880ba12dc8e622260c12a8e35de0eca1bb2d4a58c50bb4fd3d1d2037c53d377fbf3e618b6ed804db24
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -17,15 +17,23 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
17
17
|
### Export
|
18
18
|
|
19
19
|
```ruby
|
20
|
+
# Exporter class
|
20
21
|
class UserExporter < NtqExcelsior::Exporter
|
21
22
|
|
23
|
+
styles ({
|
24
|
+
blue: {
|
25
|
+
fg_color: "2F5496",
|
26
|
+
}
|
27
|
+
})
|
28
|
+
|
22
29
|
schema ({
|
23
|
-
name: '
|
30
|
+
name: 'Utilisateurs',
|
24
31
|
extra_headers: [
|
25
32
|
[
|
26
33
|
{
|
27
34
|
title: "Utilisateurs",
|
28
|
-
width: 4
|
35
|
+
width: 4,
|
36
|
+
styles: [:bold]
|
29
37
|
}
|
30
38
|
],
|
31
39
|
],
|
@@ -33,11 +41,16 @@ class UserExporter < NtqExcelsior::Exporter
|
|
33
41
|
{
|
34
42
|
title: 'Name',
|
35
43
|
resolve: -> (record) { [record.first_name, record.last_name].join(' ') },
|
44
|
+
styles: [:bold, :blue]
|
36
45
|
},
|
37
46
|
{
|
38
47
|
title: 'Email',
|
39
48
|
resolve: 'email'
|
40
49
|
},
|
50
|
+
{
|
51
|
+
title: 'Birthdate',
|
52
|
+
resolve: 'birthdate'
|
53
|
+
}
|
41
54
|
{
|
42
55
|
title: 'Address (nested)',
|
43
56
|
resolve: ['address', 'address_one']
|
@@ -45,12 +58,23 @@ class UserExporter < NtqExcelsior::Exporter
|
|
45
58
|
{
|
46
59
|
title: 'City (nested)',
|
47
60
|
resolve: ['address', 'city']
|
61
|
+
},
|
62
|
+
{
|
63
|
+
title: 'Age',
|
64
|
+
resolve: 'age',
|
65
|
+
type: :number
|
48
66
|
}
|
49
67
|
]
|
50
68
|
})
|
51
69
|
|
52
70
|
end
|
53
71
|
|
72
|
+
exporter = UserExporter.new(@users)
|
73
|
+
exporter.export
|
74
|
+
File.open("export.xlsx", "w") do |tpm|
|
75
|
+
tpm.binmode
|
76
|
+
tpm.write(file.to_stream.read)
|
77
|
+
end
|
54
78
|
```
|
55
79
|
|
56
80
|
## Development
|
@@ -5,6 +5,12 @@ module NtqExcelsior
|
|
5
5
|
attr_accessor :data
|
6
6
|
|
7
7
|
DEFAULT_STYLES = {
|
8
|
+
date_format: {
|
9
|
+
format_code: 'dd-mm-yyyy'
|
10
|
+
},
|
11
|
+
time_format: {
|
12
|
+
format_code: 'dd-mm-yyyy hh:mm:ss'
|
13
|
+
},
|
8
14
|
bold: {
|
9
15
|
b: true
|
10
16
|
},
|
@@ -23,7 +29,7 @@ module NtqExcelsior
|
|
23
29
|
@schema ||= value
|
24
30
|
end
|
25
31
|
def styles(value = nil)
|
26
|
-
@
|
32
|
+
@styles ||= value
|
27
33
|
end
|
28
34
|
end
|
29
35
|
|
@@ -73,16 +79,27 @@ module NtqExcelsior
|
|
73
79
|
count
|
74
80
|
end
|
75
81
|
|
76
|
-
def get_styles(
|
82
|
+
def get_styles(row_styles, cell_styles = [])
|
83
|
+
row_styles ||= []
|
84
|
+
return {} if row_styles.length == 0 && cell_styles.length == 0
|
85
|
+
|
86
|
+
styles_hash = {}
|
87
|
+
stylesheet = styles || {}
|
88
|
+
(row_styles + cell_styles).each do |style_key|
|
89
|
+
styles_hash = styles_hash.merge(stylesheet[style_key] || DEFAULT_STYLES[style_key] || {})
|
90
|
+
end
|
91
|
+
styles_hash
|
77
92
|
end
|
78
93
|
|
79
94
|
def resolve_header_row(headers, index)
|
80
|
-
row = { values: [], styles:
|
81
|
-
|
95
|
+
row = { values: [], styles: [], merge_cells: [], height: nil }
|
96
|
+
return row unless headers
|
97
|
+
|
82
98
|
col_index = 1
|
83
99
|
headers.each do |header|
|
84
100
|
width = header[:width] || 1
|
85
101
|
row[:values] << header[:title] || ''
|
102
|
+
row[:styles] << get_styles(header[:styles])
|
86
103
|
if width > 1
|
87
104
|
colspan = width - 1
|
88
105
|
row[:values].push(*Array.new(colspan, nil))
|
@@ -105,20 +122,37 @@ module NtqExcelsior
|
|
105
122
|
end
|
106
123
|
|
107
124
|
def format_value(resolver, record)
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
125
|
+
styles = []
|
126
|
+
type = nil
|
127
|
+
if resolver.is_a?(Proc)
|
128
|
+
value = resolver.call(record)
|
129
|
+
else
|
130
|
+
accessors = resolver
|
131
|
+
accessors = accessors.split(".") if accessors.is_a?(String)
|
132
|
+
value = dig_value(record, accessors)
|
133
|
+
end
|
134
|
+
if value.is_a?(Date)
|
135
|
+
value = value.strftime("%Y-%m-%d")
|
136
|
+
styles << :date_format
|
137
|
+
type = :date
|
138
|
+
end
|
139
|
+
if value.is_a?(Time) | value.is_a?(DateTime)
|
140
|
+
value = value.strftime("%Y-%m-%d %H:%M:%S")
|
141
|
+
styles << :time_format
|
142
|
+
type = :time
|
143
|
+
end
|
144
|
+
{ value: value, styles: styles, type: type }
|
113
145
|
end
|
114
146
|
|
115
147
|
def resolve_record_row(schema, record, index)
|
116
|
-
row = { values: [], styles:
|
148
|
+
row = { values: [], styles: [], merge_cells: [], height: nil, types: [] }
|
117
149
|
col_index = 1
|
118
150
|
schema.each do |column|
|
119
151
|
width = column[:width] || 1
|
120
|
-
|
121
|
-
|
152
|
+
formatted_value = format_value(column[:resolve], record)
|
153
|
+
row[:values] << formatted_value[:value]
|
154
|
+
row[:types] << (column[:type] || formatted_value[:type])
|
155
|
+
row[:styles] << get_styles(column[:styles], formatted_value[:styles])
|
122
156
|
if width > 1
|
123
157
|
colspan = width - 1
|
124
158
|
row[:values].push(*Array.new(colspan, nil))
|
@@ -128,7 +162,6 @@ module NtqExcelsior
|
|
128
162
|
|
129
163
|
col_index += 1
|
130
164
|
end
|
131
|
-
|
132
165
|
row
|
133
166
|
end
|
134
167
|
|
@@ -150,22 +183,26 @@ module NtqExcelsior
|
|
150
183
|
|
151
184
|
def add_sheet_content(content, wb_styles, sheet)
|
152
185
|
content[:rows].each do |row|
|
153
|
-
row_style =
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
186
|
+
row_style = []
|
187
|
+
if row[:styles].is_a?(Array) && row[:styles].any?
|
188
|
+
row[:styles].each do |style|
|
189
|
+
row_style << wb_styles.add_style(style || {})
|
190
|
+
end
|
191
|
+
end
|
192
|
+
sheet.add_row row[:values], style: row_style, height: row[:height], types: row[:types]
|
193
|
+
if row[:merge_cells]
|
194
|
+
row[:merge_cells]&.each do |range|
|
195
|
+
sheet.merge_cells range
|
196
|
+
end
|
160
197
|
end
|
161
198
|
end
|
162
199
|
|
163
|
-
# do not apply styles if there are
|
200
|
+
# do not apply styles if there are no rows
|
164
201
|
if content[:rows].present?
|
165
|
-
content[:styles]&.each_with_index do |(range,
|
202
|
+
content[:styles]&.each_with_index do |(range, sty), index|
|
166
203
|
begin
|
167
|
-
sheet.add_style range,
|
168
|
-
sheet.add_border range,
|
204
|
+
sheet.add_style range, sty.except(:border) if range && sty
|
205
|
+
sheet.add_border range, sty[:border] if range && sty && sty[:border]
|
169
206
|
rescue NoMethodError
|
170
207
|
# do not apply styles if error
|
171
208
|
end
|
@@ -185,13 +222,13 @@ module NtqExcelsior
|
|
185
222
|
end
|
186
223
|
|
187
224
|
def export
|
188
|
-
|
189
|
-
wb =
|
225
|
+
package = Axlsx::Package.new
|
226
|
+
wb = package.workbook
|
190
227
|
wb_styles = wb.styles
|
191
228
|
|
192
229
|
generate_workbook(wb, wb_styles)
|
193
230
|
|
194
|
-
|
231
|
+
package
|
195
232
|
end
|
196
233
|
|
197
234
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ntq_excelsior
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: caxlsx
|