ntq_excelsior 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bf1d29e56d85c440a139e5f9e51aab9acd22b68e2da799352435298382e9c11
4
- data.tar.gz: 4de9735295bee676cb0fa6fe738cf18253348021b00060b1dacee3ae6d9d2197
3
+ metadata.gz: 84b27eb6270d0a9527ca77e3c3b9d93328ef2151b14703c796bc07565a16767a
4
+ data.tar.gz: ef00357a9c9a2e1c1bc54d6b3ecbe2cc935ce8ba93452acba4d6ecd6a9ed1d94
5
5
  SHA512:
6
- metadata.gz: 179249f8e55907b347cc715c96c8b0b4ece8c0fa98526a2233fe5587333cf05a89d303b32e15c7ec921c0ccc55e620e330d762d279e99b68d6fa7d9bad8eee73
7
- data.tar.gz: 54fe084c4e0cc8525703547a7075ed64791300cbbeb32664fdf75fba463a2a0e124c6e5c7b9e302c8e0ff8d3c08c660433f189cc2a01c9230a73b6ad2342ce62
6
+ metadata.gz: 830dd162264d62ac828b52d6dd428f251936b2659fb92e5b34ed3e6809bc84e5b2165c804b637685c1e2a82afcbda86685c9afc663149ce000c103ad0954157f
7
+ data.tar.gz: 0baa0a8bc9807bc405d55598d498d731239a1a39823c044784a9ea7c352258ce3b9216d3ded2a329c521a908b0a1ac9f0c7d72bbaa56c09f1ae788e00aca75f5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,3 @@
1
- ## [Unreleased]
2
-
3
1
  ## [0.1.0] - 2023-02-02
4
2
 
5
3
  - Initial release
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: 'Mobilités',
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
- @schema ||= value
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(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: nil, merge_cells: [], height: nil }
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: nil, merge_cells: [], height: nil }
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 = wb_styles.add_style(row[:styles]) if row[:styles].is_a? Hash
154
- row_style = row[:styles]&.map { |style| wb_styles.add_style(style) if style } if row[:styles].is_a? Array
155
- sheet.add_row row[:values], style: row_style, height: row[:height]
156
- next unless row[:merge_cells]
157
-
158
- row[:merge_cells]&.each do |range|
159
- sheet.merge_cells range
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 now rows
182
+ # do not apply styles if there are no rows
164
183
  if content[:rows].present?
165
- content[:styles]&.each_with_index do |(range, styles), index|
184
+ content[:styles]&.each_with_index do |(range, sty), index|
166
185
  begin
167
- sheet.add_style range, styles.except(:border) if range && styles
168
- sheet.add_border range, styles[:border] if range && styles && styles[:border]
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
- p = Axlsx::Package.new
189
- wb = p.workbook
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
- p
213
+ package
195
214
  end
196
215
 
197
216
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NtqExcelsior
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  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.1.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-02 00:00:00.000000000 Z
11
+ date: 2023-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: caxlsx