axlsx_styler 0.1.5 → 0.1.6

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
  SHA1:
3
- metadata.gz: cd31d073d9ff50b0f751553c2623c372ec9a4d1d
4
- data.tar.gz: d8c93fe14f0215c0aa0619ebfcb37cc6479f9781
3
+ metadata.gz: 777a2c460b127a0ce0cb3413631b77e06e22aabe
4
+ data.tar.gz: 1229fcf346e19b981b244208ee0d73d561bce0d1
5
5
  SHA512:
6
- metadata.gz: e99d3aae264ab742166af6b76fb34d9d2b19d61e2a57cf56d9d35d3102f7a45365da068cc18f16f945082907ec73beece46e9a79808acfb3b290498b07b6efde
7
- data.tar.gz: 392fcc8712ce0fc970894f19671324ff327bc8a712a2753be62230ff80f08df12e6501073c7f0521c57b596b144e06b7cfff45ea6e1737f35a99de20c24d84ff
6
+ metadata.gz: f8f5b791535070ee91a18c48ba730a6a530f552c097fd2d3ca8e733be27c19be379b1148a66fd8845340b5c41458e25872a6354828de43cefe85b4854ab8abe4
7
+ data.tar.gz: 3313ffef3a641b259822a22071a58f9c3a5a8f10ad9300039e0895b734c1a6dfbcb39a693d70840e2c7f9c962d85cf28734019c6323958567f875cc2afd25be8
data/.gitignore CHANGED
@@ -22,3 +22,4 @@ tmp
22
22
  mkmf.log
23
23
  *.xlsx
24
24
  *.xlsx#
25
+ .DS_Store
data/README.md CHANGED
@@ -86,6 +86,7 @@ Producing the same spreadsheet with vanilla `axlsx` turns out [a bit trickier](.
86
86
 
87
87
  Version | Change
88
88
  --------|-------
89
+ 0.1.6 | Allow mixing vanilla `axlsx` styles and those from `add_style` and `add_border` (see [this example](./examples/mixing_styles.rb))
89
90
  0.1.5 | Hide `Workbook#apply_styles` method to make it easier to use.
90
91
  0.1.3 | Make border styles customazible.
91
92
  0.1.2 | Allow applying multiple style hashes.
@@ -27,7 +27,9 @@ Gem::Specification.new do |spec|
27
27
  spec.required_ruby_version = '>= 1.9.3'
28
28
 
29
29
  spec.add_dependency 'axlsx', '~> 2.0'
30
- spec.add_dependency 'activesupport', '>= 3.1'
30
+ # TODO: extract active_support/core_ext/hash/deep_merge
31
+ # and remove the dependency
32
+ spec.add_dependency 'activesupport', '~> 3.1'
31
33
 
32
34
  spec.add_development_dependency 'bundler', '~> 1.6'
33
35
  spec.add_development_dependency 'rake', '~> 0.9'
@@ -0,0 +1,22 @@
1
+ # As of v0.1.6 it is possible to add styles using vanilla axlsx approach
2
+ # as well as using add_style and add_border. This is something users have
3
+ # been asking for.
4
+ #
5
+ # The example below is superfluous and the same results are more easily achieved
6
+ # without workbook.styles.add_style. You've been warned!
7
+ require 'axlsx_styler'
8
+
9
+ axlsx = Axlsx::Package.new
10
+ workbook = axlsx.workbook
11
+ red = workbook.styles.add_style fg_color: 'FF0000'
12
+ workbook.add_worksheet do |sheet|
13
+ sheet.add_row
14
+ sheet.add_row ['', 'B2', 'C2', 'D2'], style: [nil, red, nil, nil]
15
+ sheet.add_row ['', 'B3', 'C3', 'D3'], style: [nil, red, nil, nil]
16
+ sheet.add_row ['', 'B4', 'C4', 'D4'], style: [nil, red, nil, nil]
17
+
18
+ sheet.add_style 'B2:D2', b: true
19
+ sheet.add_border 'B2:D4'
20
+ end
21
+ workbook.apply_styles
22
+ axlsx.serialize File.expand_path('../../tmp/mixing_styles.xlsx', __FILE__)
@@ -11,7 +11,6 @@ Axlsx::Cell.send :include, AxlsxStyler::Axlsx::Cell
11
11
 
12
12
  module Axlsx
13
13
  class Package
14
-
15
14
  # Patches the original Axlsx::Package#serialize method so that styles are
16
15
  # applied when the workbook is saved
17
16
  original_serialize = instance_method(:serialize)
@@ -28,4 +27,39 @@ module Axlsx
28
27
  original_to_stream.bind(self).(*args)
29
28
  end
30
29
  end
30
+
31
+ class Styles
32
+ # An index for cell styles
33
+ # {
34
+ # 1 => < style_hash >,
35
+ # 2 => < style_hash >,
36
+ # ...
37
+ # K => < style_hash >
38
+ # }
39
+ # where keys are Cell#raw_style and values are styles
40
+ # codes as per Axlsx::Style
41
+ attr_accessor :style_index
42
+
43
+ # Patches the original Axlsx::Styles#add_style method so that plain axlsx
44
+ # styles are added to the axlsx_styler style_index cache
45
+ original_add_style = instance_method(:add_style)
46
+ define_method :add_style do |style|
47
+ self.style_index ||= {}
48
+
49
+ raw_style = {type: :xf, name: 'Arial', sz: 11, family: 1}.merge(style)
50
+ if raw_style[:format_code]
51
+ raw_style.delete(:num_fmt)
52
+ end
53
+
54
+ index = style_index.key(raw_style)
55
+ if !index
56
+ index = original_add_style.bind(self).(style)
57
+ self.style_index[index] = raw_style
58
+ end
59
+ return index
60
+ end
61
+
62
+ private
63
+
64
+ end
31
65
  end
@@ -21,12 +21,14 @@ module AxlsxStyler
21
21
  # using deep_merge from active_support:
22
22
  # with regular Hash#merge adding borders fails miserably
23
23
  new_style = raw_style.deep_merge style
24
+
24
25
  if with_border?(raw_style) && with_border?(style)
25
26
  border_at = (raw_style[:border][:edges] || all_edges) + (style[:border][:edges] || all_edges)
26
27
  new_style[:border][:edges] = border_at.uniq.sort
27
28
  elsif with_border?(style)
28
29
  new_style[:border] = style[:border]
29
30
  end
31
+
30
32
  self.raw_style = new_style
31
33
  end
32
34
 
@@ -1,4 +1,5 @@
1
1
  require 'set'
2
+ require 'active_support/core_ext/hash/deep_merge'
2
3
 
3
4
  module AxlsxStyler
4
5
  module Axlsx
@@ -6,21 +7,10 @@ module AxlsxStyler
6
7
  # An array that holds all cells with styles
7
8
  attr_accessor :styled_cells
8
9
 
9
- # Checks if styles are idexed to make it work for pre 0.1.5 version
10
+ # Checks if styles are indexed to make it work for pre 0.1.5 version
10
11
  # users that still explicitly call @workbook.apply_styles
11
12
  attr_accessor :styles_applied
12
13
 
13
- # An index for cell styles
14
- # {
15
- # 1 => < style_hash >,
16
- # 2 => < style_hash >,
17
- # ...
18
- # K => < style_hash >
19
- # }
20
- # where keys are Cell#raw_style and values are styles
21
- # codes as per Axlsx::Style
22
- attr_accessor :style_index
23
-
24
14
  def add_styled_cell(cell)
25
15
  self.styled_cells ||= Set.new
26
16
  self.styled_cells << cell
@@ -28,29 +18,20 @@ module AxlsxStyler
28
18
 
29
19
  def apply_styles
30
20
  return unless styled_cells
21
+
31
22
  styled_cells.each do |cell|
32
- set_style_index(cell)
23
+ if styles.style_index && styles.style_index[cell.style]
24
+ current_style = styles.style_index[cell.style]
25
+ new_style = current_style.deep_merge(cell.raw_style)
26
+ else
27
+ new_style = cell.raw_style
28
+ end
29
+
30
+ cell.style = styles.add_style(new_style)
33
31
  end
34
32
  self.styles_applied = true
35
33
  end
36
34
 
37
- private
38
-
39
- # Check if style code
40
- def set_style_index(cell)
41
- self.style_index ||= {}
42
-
43
- index_item = style_index.select { |_, v| v == cell.raw_style }.first
44
- if index_item
45
- cell.style = index_item.first
46
- else
47
- old_style = cell.raw_style.dup
48
- new_style = styles.add_style(cell.raw_style)
49
- cell.style = new_style
50
- # cell.raw_style.delete(:num_fmt)
51
- style_index[new_style] = old_style
52
- end
53
- end
54
35
  end
55
36
  end
56
37
  end
@@ -1,3 +1,3 @@
1
1
  module AxlsxStyler
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
@@ -9,6 +9,6 @@ class CellTest < MiniTest::Test
9
9
  cell = row.cells.first
10
10
 
11
11
  cell.add_style b: true
12
- assert_equal({ b: true }, cell.raw_style)
12
+ assert_equal({b: true}, cell.raw_style)
13
13
  end
14
14
  end
@@ -28,7 +28,7 @@ class IntegrationTest < MiniTest::Test
28
28
  sheet.add_style 'A1:B1', b: true
29
29
  end
30
30
  serialize(filename)
31
- assert_equal 1, @workbook.style_index.count
31
+ assert_equal 1, @workbook.styles.style_index.count
32
32
  end
33
33
 
34
34
  # New functionality as of 0.1.5 (to_stream)
@@ -40,7 +40,7 @@ class IntegrationTest < MiniTest::Test
40
40
  sheet.add_style 'A1:B1', b: true
41
41
  end
42
42
  to_stream(filename)
43
- assert_equal 1, @workbook.style_index.count
43
+ assert_equal 1, @workbook.styles.style_index.count
44
44
  end
45
45
 
46
46
  # Backwards compatibility with pre 0.1.5 (serialize)
@@ -52,7 +52,7 @@ class IntegrationTest < MiniTest::Test
52
52
  sheet.add_style 'A1:B1', b: true
53
53
  end
54
54
  @workbook.apply_styles # important for backwards compatibility
55
- assert_equal 1, @workbook.style_index.count
55
+ assert_equal 1, @workbook.styles.style_index.count
56
56
  serialize(filename)
57
57
  end
58
58
 
@@ -65,7 +65,7 @@ class IntegrationTest < MiniTest::Test
65
65
  sheet.add_style 'A1:B1', b: true
66
66
  end
67
67
  @workbook.apply_styles # important for backwards compatibility
68
- assert_equal 1, @workbook.style_index.count
68
+ assert_equal 1, @workbook.styles.style_index.count
69
69
  to_stream(filename)
70
70
  end
71
71
 
@@ -91,8 +91,8 @@ class IntegrationTest < MiniTest::Test
91
91
  sheet.add_border 'B3:D3', edges: [:bottom], style: :medium, color: '32f332'
92
92
  end
93
93
  serialize(filename)
94
- assert_equal 12, @workbook.style_index.count
95
- assert_equal 12 + 2, @workbook.style_index.keys.max
94
+ assert_equal 12, @workbook.styles.style_index.count
95
+ assert_equal 12 + 2, @workbook.styles.style_index.keys.max
96
96
  end
97
97
 
98
98
  def test_duplicate_borders
@@ -107,11 +107,11 @@ class IntegrationTest < MiniTest::Test
107
107
  sheet.add_border 'B2:D4'
108
108
  end
109
109
  serialize(filename)
110
- assert_equal 8, @workbook.style_index.count
110
+ assert_equal 8, @workbook.styles.style_index.count
111
111
  assert_equal 8, @workbook.styled_cells.count
112
112
  end
113
113
 
114
- def test_multiple_style_borders_on_same_sells
114
+ def test_multiple_style_borders_on_same_cells
115
115
  filename = 'multiple_style_borders'
116
116
  @workbook.add_worksheet do |sheet|
117
117
  sheet.add_row
@@ -122,7 +122,7 @@ class IntegrationTest < MiniTest::Test
122
122
  sheet.add_border 'B2:D2', edges: [:bottom], style: :thick, color: 'ff0000'
123
123
  end
124
124
  serialize(filename)
125
- assert_equal 6, @workbook.style_index.count
125
+ assert_equal 6, @workbook.styles.style_index.count
126
126
  assert_equal 6, @workbook.styled_cells.count
127
127
 
128
128
  b2_cell_style = {
@@ -130,20 +130,26 @@ class IntegrationTest < MiniTest::Test
130
130
  style: :thick,
131
131
  color: 'ff0000',
132
132
  edges: [:bottom, :left, :top]
133
- }
133
+ },
134
+ type: :xf,
135
+ name: 'Arial',
136
+ sz: 11,
137
+ family: 1
134
138
  }
135
- assert_equal b2_cell_style, @workbook.style_index
136
- .find { |_, v| v[:border][:edges] == [:bottom, :left, :top] }[1]
139
+ assert_equal b2_cell_style, @workbook.styles.style_index.values.find{|x| x == b2_cell_style}
137
140
 
138
141
  d3_cell_style = {
139
142
  border: {
140
143
  style: :thin,
141
144
  color: '000000',
142
145
  edges: [:bottom, :right]
143
- }
146
+ },
147
+ type: :xf,
148
+ name: 'Arial',
149
+ sz: 11,
150
+ family: 1
144
151
  }
145
- assert_equal d3_cell_style, @workbook.style_index
146
- .find { |_, v| v[:border][:edges] == [:bottom, :right] }[1]
152
+ assert_equal d3_cell_style, @workbook.styles.style_index.values.find{|x| x == d3_cell_style}
147
153
  end
148
154
 
149
155
  def test_table_with_num_fmt
@@ -160,8 +166,8 @@ class IntegrationTest < MiniTest::Test
160
166
  sheet.add_style 'A2:A4', format_code: 'YYYY-MM-DD hh:mm:ss'
161
167
  end
162
168
  serialize(filename)
163
- assert_equal 2, @workbook.style_index.count
164
- assert_equal 2 + 2, @workbook.style_index.keys.max
169
+ assert_equal 2, @workbook.styles.style_index.count
170
+ assert_equal 2 + 2, @workbook.styles.style_index.keys.max
165
171
  assert_equal 5, @workbook.styled_cells.count
166
172
  end
167
173
 
@@ -179,7 +185,7 @@ class IntegrationTest < MiniTest::Test
179
185
  end
180
186
  serialize(filename)
181
187
  assert_equal 4, @workbook.styled_cells.count
182
- assert_equal 3, @workbook.style_index.count
188
+ assert_equal 3, @workbook.styles.style_index.count
183
189
  end
184
190
 
185
191
  def test_multiple_named_styles
@@ -197,12 +203,12 @@ class IntegrationTest < MiniTest::Test
197
203
  end
198
204
  serialize(filename)
199
205
  assert_equal 4, @workbook.styled_cells.count
200
- assert_equal 3, @workbook.style_index.count
206
+ assert_equal 3, @workbook.styles.style_index.count
201
207
  end
202
208
 
203
209
  # Overriding borders (part 1)
204
210
  def test_mixed_borders_1
205
- @filename = 'mixed_borders_1'
211
+ filename = 'mixed_borders_1'
206
212
  @workbook.add_worksheet do |sheet|
207
213
  sheet.add_row
208
214
  sheet.add_row ['', '1', '2', '3']
@@ -213,12 +219,13 @@ class IntegrationTest < MiniTest::Test
213
219
  end
214
220
  @workbook.apply_styles
215
221
  assert_equal 9, @workbook.styled_cells.count
216
- assert_equal 2, @workbook.style_index.count
222
+ assert_equal 2, @workbook.styles.style_index.count
223
+ serialize(filename)
217
224
  end
218
225
 
219
226
  # Overriding borders (part 2)
220
227
  def test_mixed_borders
221
- @filename = 'mixed_borders_2'
228
+ filename = 'mixed_borders_2'
222
229
  @workbook.add_worksheet do |sheet|
223
230
  sheet.add_row
224
231
  sheet.add_row ['', '1', '2', '3']
@@ -229,6 +236,53 @@ class IntegrationTest < MiniTest::Test
229
236
  end
230
237
  @workbook.apply_styles
231
238
  assert_equal 8, @workbook.styled_cells.count
232
- assert_equal 6, @workbook.style_index.count
239
+ assert_equal 6, @workbook.styles.style_index.count
240
+ serialize(filename)
241
+ end
242
+
243
+ def test_merge_styles_1
244
+ filename = 'merge_styles_1'
245
+ bold = @workbook.styles.add_style b: true
246
+
247
+ @workbook.add_worksheet do |sheet|
248
+ sheet.add_row
249
+ sheet.add_row ['', '1', '2', '3'], style: [nil, bold]
250
+ sheet.add_row ['', '4', '5', '6'], style: bold
251
+ sheet.add_row ['', '7', '8', '9']
252
+ sheet.add_style 'B2:D4', b: true
253
+ sheet.add_border 'B2:D4', { style: :thin, color: '000000' }
254
+ end
255
+ @workbook.apply_styles
256
+ assert_equal 9, @workbook.styles.style_index.count
257
+ serialize(filename)
258
+ end
259
+
260
+ def test_merge_styles_2
261
+ filename = 'merge_styles_2'
262
+ bold = @workbook.styles.add_style b: true
263
+
264
+ @workbook.add_worksheet do |sheet|
265
+ sheet.add_row ['A1', 'B1'], style: [nil, bold]
266
+ sheet.add_row ['A2', 'B2'], style: bold
267
+ sheet.add_row ['A3', 'B3']
268
+ sheet.add_style 'A1:A2', i: true
269
+ end
270
+ @workbook.apply_styles
271
+ assert_equal 3, @workbook.styles.style_index.count
272
+ serialize(filename)
273
+ end
274
+
275
+ def test_merge_styles_3
276
+ filename = 'merge_styles_3'
277
+ bold = @workbook.styles.add_style b: true
278
+
279
+ @workbook.add_worksheet do |sheet|
280
+ sheet.add_row ['A1', 'B1'], style: [nil, bold]
281
+ sheet.add_row ['A2', 'B2']
282
+ sheet.add_style 'B1:B2', bg_color: 'FF0000'
283
+ end
284
+ @workbook.apply_styles
285
+ assert_equal 3, @workbook.styles.style_index.count
286
+ serialize(filename)
233
287
  end
234
288
  end
@@ -2,3 +2,5 @@ require 'axlsx_styler'
2
2
  require 'minitest/autorun'
3
3
  require 'minitest/pride'
4
4
  require 'awesome_print'
5
+
6
+ mkdir_p(File.expand_path("../../tmp", __FILE__))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: axlsx_styler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sakovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-06 00:00:00.000000000 Z
11
+ date: 2016-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: axlsx
@@ -28,14 +28,14 @@ dependencies:
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '3.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
@@ -113,6 +113,7 @@ files:
113
113
  - Rakefile
114
114
  - axlsx_styler.gemspec
115
115
  - examples/colors_and_borders.rb
116
+ - examples/mixing_styles.rb
116
117
  - examples/vanilla_axlsx.md
117
118
  - lib/axlsx_styler.rb
118
119
  - lib/axlsx_styler/axlsx_cell.rb