ntq_excelsior 0.1.0 → 0.2.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 +22 -2
- data/lib/ntq_excelsior/exporter.rb +39 -20
- 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: 84b27eb6270d0a9527ca77e3c3b9d93328ef2151b14703c796bc07565a16767a
|
4
|
+
data.tar.gz: ef00357a9c9a2e1c1bc54d6b3ecbe2cc935ce8ba93452acba4d6ecd6a9ed1d94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 830dd162264d62ac828b52d6dd428f251936b2659fb92e5b34ed3e6809bc84e5b2165c804b637685c1e2a82afcbda86685c9afc663149ce000c103ad0954157f
|
7
|
+
data.tar.gz: 0baa0a8bc9807bc405d55598d498d731239a1a39823c044784a9ea7c352258ce3b9216d3ded2a329c521a908b0a1ac9f0c7d72bbaa56c09f1ae788e00aca75f5
|
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,17 @@ 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
|
+
type: :date
|
54
|
+
}
|
41
55
|
{
|
42
56
|
title: 'Address (nested)',
|
43
57
|
resolve: ['address', 'address_one']
|
@@ -51,6 +65,12 @@ class UserExporter < NtqExcelsior::Exporter
|
|
51
65
|
|
52
66
|
end
|
53
67
|
|
68
|
+
exporter = UserExporter.new(@users)
|
69
|
+
exporter.export
|
70
|
+
File.open("export.xlsx", "w") do |tpm|
|
71
|
+
tpm.binmode
|
72
|
+
tpm.write(file.to_stream.read)
|
73
|
+
end
|
54
74
|
```
|
55
75
|
|
56
76
|
## Development
|
@@ -23,7 +23,7 @@ module NtqExcelsior
|
|
23
23
|
@schema ||= value
|
24
24
|
end
|
25
25
|
def styles(value = nil)
|
26
|
-
@
|
26
|
+
@styles ||= value
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -73,16 +73,26 @@ module NtqExcelsior
|
|
73
73
|
count
|
74
74
|
end
|
75
75
|
|
76
|
-
def get_styles(
|
76
|
+
def get_styles(row_styles)
|
77
|
+
return {} unless row_styles && row_styles.length > 0
|
78
|
+
|
79
|
+
styles_hash = {}
|
80
|
+
stylesheet = styles || {}
|
81
|
+
row_styles.each do |style_key|
|
82
|
+
styles_hash = styles_hash.merge(stylesheet[style_key] || DEFAULT_STYLES[style_key] || {})
|
83
|
+
end
|
84
|
+
styles_hash
|
77
85
|
end
|
78
86
|
|
79
87
|
def resolve_header_row(headers, index)
|
80
|
-
row = { values: [], styles:
|
81
|
-
|
88
|
+
row = { values: [], styles: [], merge_cells: [], height: nil }
|
89
|
+
return row unless headers
|
90
|
+
|
82
91
|
col_index = 1
|
83
92
|
headers.each do |header|
|
84
93
|
width = header[:width] || 1
|
85
94
|
row[:values] << header[:title] || ''
|
95
|
+
row[:styles] << get_styles(header[:styles])
|
86
96
|
if width > 1
|
87
97
|
colspan = width - 1
|
88
98
|
row[:values].push(*Array.new(colspan, nil))
|
@@ -109,15 +119,20 @@ module NtqExcelsior
|
|
109
119
|
|
110
120
|
accessors = resolver
|
111
121
|
accessors = accessors.split(".") if accessors.is_a?(String)
|
112
|
-
dig_value(record, accessors)
|
122
|
+
value = dig_value(record, accessors)
|
123
|
+
value = value.strftime("%Y-%m-%d") if value.is_a?(Date)
|
124
|
+
value = value.strftime("%Y-%m-%d %H:%M:%S") if value.is_a?(Time) | value.is_a?(DateTime)
|
125
|
+
value
|
113
126
|
end
|
114
127
|
|
115
128
|
def resolve_record_row(schema, record, index)
|
116
|
-
row = { values: [], styles:
|
129
|
+
row = { values: [], styles: [], merge_cells: [], height: nil, types: [] }
|
117
130
|
col_index = 1
|
118
131
|
schema.each do |column|
|
119
132
|
width = column[:width] || 1
|
120
133
|
row[:values] << format_value(column[:resolve], record)
|
134
|
+
row[:types] << column[:type] || :string
|
135
|
+
row[:styles] << get_styles(column[:styles])
|
121
136
|
|
122
137
|
if width > 1
|
123
138
|
colspan = width - 1
|
@@ -150,22 +165,26 @@ module NtqExcelsior
|
|
150
165
|
|
151
166
|
def add_sheet_content(content, wb_styles, sheet)
|
152
167
|
content[:rows].each do |row|
|
153
|
-
row_style =
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
168
|
+
row_style = []
|
169
|
+
if row[:styles].is_a?(Array) && row[:styles].any?
|
170
|
+
row[:styles].each do |style|
|
171
|
+
row_style << wb_styles.add_style(style || {})
|
172
|
+
end
|
173
|
+
end
|
174
|
+
sheet.add_row row[:values], style: row_style, height: row[:height], types: row[:types]
|
175
|
+
if row[:merge_cells]
|
176
|
+
row[:merge_cells]&.each do |range|
|
177
|
+
sheet.merge_cells range
|
178
|
+
end
|
160
179
|
end
|
161
180
|
end
|
162
181
|
|
163
|
-
# do not apply styles if there are
|
182
|
+
# do not apply styles if there are no rows
|
164
183
|
if content[:rows].present?
|
165
|
-
content[:styles]&.each_with_index do |(range,
|
184
|
+
content[:styles]&.each_with_index do |(range, sty), index|
|
166
185
|
begin
|
167
|
-
sheet.add_style range,
|
168
|
-
sheet.add_border range,
|
186
|
+
sheet.add_style range, sty.except(:border) if range && sty
|
187
|
+
sheet.add_border range, sty[:border] if range && sty && sty[:border]
|
169
188
|
rescue NoMethodError
|
170
189
|
# do not apply styles if error
|
171
190
|
end
|
@@ -185,13 +204,13 @@ module NtqExcelsior
|
|
185
204
|
end
|
186
205
|
|
187
206
|
def export
|
188
|
-
|
189
|
-
wb =
|
207
|
+
package = Axlsx::Package.new
|
208
|
+
wb = package.workbook
|
190
209
|
wb_styles = wb.styles
|
191
210
|
|
192
211
|
generate_workbook(wb, wb_styles)
|
193
212
|
|
194
|
-
|
213
|
+
package
|
195
214
|
end
|
196
215
|
|
197
216
|
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.2.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-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: caxlsx
|