rxl 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d71e3c242930b4fbd6c3e05572e51bf19231ea10483b6ca271dd42138e1959a
4
- data.tar.gz: 14c3dfbf017573840b2c31595043f3b587e6550487c91a7de5395ff02282c0fa
3
+ metadata.gz: 62aee75c921b3b6e206dadf145c6421d3bf193c24eadb6f42c9905b4c1f27c4b
4
+ data.tar.gz: a95171c0ebbc1242621af4f2aff4b1157e66c6d0fe601eb44bc8ae83f4e6e34c
5
5
  SHA512:
6
- metadata.gz: a64c63e260638a97ebfcc00270e8d4ca265a693fa6754be61d3307a26e27b863d9d04ef3bdd46ca908ac289102c4f964ac5ff4561dfb43f46f9578494467412d
7
- data.tar.gz: 18d20fc11bd47c9d8f631eca7023f398ab2992817696dbd568a796cf23307b14c8be6929db0bc7c17b00e3f8c8641d5648791179fea9d7a142e7189eb7fa3aad
6
+ metadata.gz: d3ef588d022f83c7f2cb9042521bd2e20699012fcb9155115a07900a7b9367be30fee4a7cf7815da41f18eff3486b4a6492b7ec6cc15f68dbc25c5ba0a87f647
7
+ data.tar.gz: f50bc7833adc83c12bd227c404841a45d9aa0eb553c673da144124dde780610cc713f2baf33aca4cf415d771a750e5a07db2f15136a8f21523a4a14d8457c076
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  /.idea
11
11
  **/sandbox.*
12
12
  /.analysis
13
+ rxl-*.gem
data/Gemfile CHANGED
@@ -6,4 +6,4 @@ gemspec
6
6
  gem 'awesome_print'
7
7
  gem 'mitrush', '>= 0.2.0'
8
8
  gem 'rubocop'
9
- gem 'rubyXL'
9
+ gem 'rubyXL', '3.3.30'
data/README.md CHANGED
@@ -132,8 +132,6 @@ Returns the files with sheet contents populated with hash of cells (or array of
132
132
 
133
133
  ### Write to file
134
134
 
135
- Any exception is swallowed and returned.
136
-
137
135
  To write a file pass the filename and hash:
138
136
 
139
137
  ```ruby
@@ -152,39 +150,6 @@ The format of the excel hash_workbook has sheet names as keys and hashes of cell
152
150
  }
153
151
  ```
154
152
 
155
- ### Write to file as tables
156
-
157
- Any exception is swallowed and returned.
158
-
159
- To write a file as pass the filename and hash:
160
-
161
- ```ruby
162
- Rxl.write_file_as_tables('path/to/save.xlsx', hash_tables, order)
163
- ```
164
-
165
- The worsheets' top row will be populated with values specified in the `order` array. Those array items will also be used to extract the current row from the current hash.
166
-
167
- * use `nil` in the `order` array to leave blank columns (including blank header)
168
- * string or symbol keys can be used, so long as the key in order is the same as in the hashes
169
-
170
- The format of the excel hash_workbook has sheet names as keys and hashes of rows as values:
171
-
172
- ```ruby
173
- order = %i[header_a header_b]
174
- hash_tables = {
175
- 'Sheet1' => [
176
- {
177
- header_a: 'some_value',
178
- header_b: 'other_value'
179
- },
180
- {
181
- header_a: 'some_value',
182
- header_b: 'other_value'
183
- }
184
- ]
185
- }
186
- ```
187
-
188
153
  #### Cell specification
189
154
 
190
155
  All cells are written with the format set to general except those with a number format specified
@@ -233,6 +198,76 @@ Other keys can be specified:
233
198
 
234
199
  TODO: add full description for hash_cell_to_rubyxl_cell and rubyxl_cell_to_hash_cell (and check they're as consistent as possible)
235
200
 
201
+ ### Write to file as tables
202
+
203
+ To write a file as pass the filename and hash:
204
+
205
+ ```ruby
206
+ Rxl.write_file_as_tables('path/to/save.xlsx', hash_tables, order)
207
+ ```
208
+
209
+ The worksheets' top row will be populated with values specified in the `order` array. Those array items will also be used to extract the current row from the current hash.
210
+
211
+ * use `nil` in the `order` array to leave blank columns (including blank header)
212
+ * string or symbol keys can be used, so long as the key in order is the same as in the hashes
213
+
214
+ The format of the excel hash_workbook has sheet names as keys and hashes of rows as values:
215
+
216
+ ```ruby
217
+ order = %i[header_a header_b]
218
+ hash_tables = {
219
+ 'Sheet1' => [
220
+ {
221
+ header_a: 'some_value',
222
+ header_b: 'other_value'
223
+ },
224
+ {
225
+ header_a: 'some_value',
226
+ header_b: 'other_value'
227
+ }
228
+ ]
229
+ }
230
+ ```
231
+
232
+ #### Formatting for tables
233
+
234
+ Add formatting to tables by adding a `:formats` key to the top level hash.
235
+
236
+ Inside the formatting hash add child hashes with keys for the relevant table.
237
+
238
+ Within the table add hashes for each column with the key as the column letter and the value as a cell hash (excluding `:value`).
239
+
240
+ Formatting for rows is not currently implemented.
241
+
242
+ Additionally inside the table hash add a key `:headers` with a cell hash (excluding `:value`) to set formatting for the header row.
243
+
244
+ ```ruby
245
+ order = %i[header_a header_b]
246
+ hash_tables = {
247
+ formats: {
248
+ 'Sheet1' => {
249
+ headers: {
250
+ bold: true,
251
+ align: 'center'
252
+ },
253
+ 'B' => {
254
+ fill: 'feb302'
255
+ }
256
+ }
257
+ },
258
+ 'Sheet1' => [
259
+ {
260
+ 'col_1' => 'some_value',
261
+ 'col_2' => 'other_value'
262
+ },
263
+ {
264
+ 'col_1' => 'some_value',
265
+ 'col_2' => 'other_value'
266
+ }
267
+ ]
268
+ }
269
+ ```
270
+
236
271
  ## Development
237
272
 
238
273
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -8,12 +8,27 @@ module Cell
8
8
 
9
9
  def self.rubyxl_cell_to_hash_cell(rubyxl_cell = nil)
10
10
  rubyxl_cell_value = rubyxl_cell.nil? ? RubyXL::Cell.new.value : rubyxl_cell.value
11
- {
11
+ values = {
12
12
  value: rubyxl_cell_value,
13
13
  format: hash_cell_format(rubyxl_cell_value),
14
14
  formula: rubyxl_cell_formula(rubyxl_cell),
15
15
  h_align: rubyxl_cell_horizontal_alignment(rubyxl_cell),
16
- v_align: rubyxl_cell_vertical_alignment(rubyxl_cell)
16
+ v_align: rubyxl_cell_vertical_alignment(rubyxl_cell),
17
+ bold: rubyxl_cell.is_bolded,
18
+ fill: rubyxl_cell.fill_color,
19
+ font_name: rubyxl_cell.font_name,
20
+ font_size: rubyxl_cell.font_size.to_i,
21
+ border: rubyxl_cell_to_border_hash(rubyxl_cell)
22
+ }
23
+ return values
24
+ end
25
+
26
+ def self.rubyxl_cell_to_border_hash(rubyxl_cell)
27
+ {
28
+ top: rubyxl_cell.get_border(:top),
29
+ bottom: rubyxl_cell.get_border(:bottom),
30
+ left: rubyxl_cell.get_border(:left),
31
+ right: rubyxl_cell.get_border(:right)
17
32
  }
18
33
  end
19
34
 
@@ -57,7 +72,7 @@ module Cell
57
72
  rubyxl_worksheet[row_index][column_index].change_font_name(combined_hash_cell[:font_style]) if combined_hash_cell[:font_style]
58
73
  rubyxl_worksheet[row_index][column_index].change_font_size(combined_hash_cell[:font_size]) if combined_hash_cell[:font_size]
59
74
  rubyxl_worksheet[row_index][column_index].change_fill(combined_hash_cell[:fill]) if combined_hash_cell[:fill]
60
- rubyxl_worksheet[row_index][column_index].change_horizontal_alignment(combined_hash_cell[:align]) if combined_hash_cell[:align]
75
+ rubyxl_worksheet[row_index][column_index].change_horizontal_alignment(combined_hash_cell[:h_align]) if combined_hash_cell[:h_align]
61
76
  rubyxl_worksheet[row_index][column_index].change_font_bold(combined_hash_cell[:bold]) if combined_hash_cell[:bold]
62
77
 
63
78
  if combined_hash_cell[:border_all]
@@ -91,9 +106,10 @@ module Cell
91
106
  unless hash_cell.keys.reject { |key| key.is_a?(Symbol) }.empty?
92
107
  raise("cell key at path #{trace + [hash_cell_key]} must be a Symbol")
93
108
  end
94
- unless hash_cell.keys.delete_if { |key| valid_cell_keys.include?(key) }.empty?
109
+ invalid_keys = hash_cell.keys.delete_if { |key| valid_cell_keys.include?(key) }
110
+ unless invalid_keys.empty?
95
111
  valid_cell_keys_string = ":#{valid_cell_keys.join(', :')}"
96
- raise(%(invalid cell hash key at path #{trace + [hash_cell_key]}, valid keys are: [#{valid_cell_keys_string}]))
112
+ raise(%(invalid cell hash key(s) #{invalid_keys} at path #{trace + [hash_cell_key]}, valid keys are: [#{valid_cell_keys_string}]))
97
113
  end
98
114
  # TODO: add validation for hash_cell specification
99
115
  end
@@ -112,6 +128,13 @@ module Cell
112
128
  value
113
129
  number
114
130
  formula
131
+ bold
132
+ h_align
133
+ v_align
134
+ border
135
+ fill
136
+ font_name
137
+ font_size
115
138
  ]
116
139
  end
117
140
 
data/lib/rxl.rb CHANGED
@@ -5,18 +5,15 @@ require_relative 'workbook'
5
5
  module Rxl
6
6
 
7
7
  def self.write_file(filepath, hash_workbook)
8
- begin
9
- rubyxl_workbook = Workbook.hash_workbook_to_rubyxl_workbook(hash_workbook)
10
- rubyxl_workbook.write(filepath)
11
- nil
12
- rescue => e
13
- return e
14
- end
8
+ rubyxl_workbook = Workbook.hash_workbook_to_rubyxl_workbook(hash_workbook)
9
+ rubyxl_workbook.write(filepath)
10
+ nil
15
11
  end
16
12
 
17
13
  def self.write_file_as_tables(filepath, hash_tables, order, write_headers: true)
18
14
  hash_workbook = Workbook.hashes_to_hash_workbook(hash_tables, order, write_headers: write_headers)
19
15
  write_file(filepath, hash_workbook)
16
+ nil
20
17
  end
21
18
 
22
19
  def self.read_file(filepath)
@@ -1,3 +1,3 @@
1
1
  module Rxl
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -28,7 +28,11 @@ module Workbook
28
28
 
29
29
  def self.hashes_to_hash_workbook(hash_tables, order, write_headers: true)
30
30
  hash_workbook = {}
31
- hash_tables.each { |k, v| hash_workbook[k] = Worksheet.hashes_to_hash_worksheet(v, order, write_headers: write_headers) }
31
+ all_formats = hash_tables.delete(:formats) || {}
32
+ hash_tables.each do |k, v|
33
+ formats = all_formats[k]
34
+ hash_workbook[k] = Worksheet.hashes_to_hash_worksheet(v, order, formats, write_headers: write_headers)
35
+ end
32
36
  hash_workbook
33
37
  end
34
38
 
@@ -38,22 +38,37 @@ module Worksheet
38
38
  ### GET RUBYXL WORKSHEET FROM HASHES ###
39
39
  ################################################
40
40
 
41
- def self.hashes_to_hash_worksheet(hashes, order, write_headers: true)
41
+ def self.hashes_to_hash_worksheet(hashes, order, formats, write_headers: true)
42
42
  rows = hashes.map do |hash|
43
43
  order.map { |item| hash[item] }
44
44
  end
45
45
  rows.unshift(order.map { |item| "#{item}" }) if write_headers
46
- rows_to_hash_worksheet(rows)
46
+ hash_worksheet = rows_to_hash_worksheet(rows)
47
+ format_hash_worksheet(hash_worksheet, formats, write_headers) if formats
48
+ hash_worksheet
47
49
  end
48
50
 
49
51
  def self.rows_to_hash_worksheet(rows)
50
- rxl_worksheet = {}
52
+ hash_worksheet = {}
51
53
  rows.count.times do |i|
52
54
  rows[i].each_with_index do |cell_value, index|
53
- rxl_worksheet["#{column_name(index)}#{i + 1}"] = { value: cell_value }
55
+ hash_worksheet["#{column_name(index)}#{i + 1}"] = { value: cell_value }
56
+ end
57
+ end
58
+ hash_worksheet
59
+ end
60
+
61
+ def self.format_hash_worksheet(hash_worksheet, formats, write_headers)
62
+ if write_headers && formats[:headers]
63
+ hash_worksheet.keys.grep(/^\D+1$/).each { |key| hash_worksheet[key].update(formats[:headers]) }
64
+ end
65
+ formats.keys.each do |col|
66
+ next if col == :header
67
+ hash_worksheet.keys.grep(/^#{col}/).each do |key|
68
+ next if write_headers && key[/^\D+1$/]
69
+ hash_worksheet[key].update(formats[col])
54
70
  end
55
71
  end
56
- rxl_worksheet
57
72
  end
58
73
 
59
74
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rxl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian McWilliams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-10 00:00:00.000000000 Z
11
+ date: 2019-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler