caracal 1.1.2 → 1.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
- SHA1:
3
- metadata.gz: 1f3944130ad07554a021682f7365b9b6db126c8d
4
- data.tar.gz: 358849a03c5e6806f77590a4eb3ce86de6c6a68e
2
+ SHA256:
3
+ metadata.gz: 5e775f349922d6591347d38de57b4211581c75d1b1b1fe08f67d887d68833a6b
4
+ data.tar.gz: 635e787f52491c8f06ff3dcb32cd0d573a4abc7b44aa693dc8421a74108942e0
5
5
  SHA512:
6
- metadata.gz: c84b97f6fbbf1038db085d86205e2f50f74eb4c09f3aff1391ed65b8c111c1def9f05fba78027a36a7a626a6ecc2b95543e9737949d3190bd6cfdc1e7940f9d1
7
- data.tar.gz: 2643cfcd452911dce56bb8cf96884021cd0d11ea4e041e35cc1af8a5ba45147b444237413d69c501d2d8e1acf8d23e06a579a69806e93eb4a3e24b4b052c3e60
6
+ metadata.gz: 20b0fd3d486e99180c7a871610ba7d619d75cefc2116cb54e3920ed8bf2c036a6d93789df6570dde608cc68ece8693e272da093ad5ac26dad8962804c35f2171
7
+ data.tar.gz: d69653b8075ec87ab191cf07d4bc1090fb0822b3ae05a1f24d9edfd8ac3476efa2291c46a21c8194dca4d99505fee569cf1ddaf85a4f03dce7ae1de3857cc486
@@ -1,8 +1,15 @@
1
+ #### v1.2.0
2
+
3
+ * Enhancements
4
+ * Added colspan and rowspan functionality to table styling feature. (@ViktorKopychko)
5
+
6
+
1
7
  #### v1.1.2
2
8
 
3
9
  * Enhancements
4
10
  * Added font size options for page numbers. (@cfacello)
5
11
 
12
+
6
13
  #### v1.1.1
7
14
 
8
15
  * Enhancements
data/README.md CHANGED
@@ -639,6 +639,17 @@ docx.table [['Header 1','Header 2'],['Cell 1', 'Cell 2']], border_size: 4 do
639
639
  end
640
640
  ```
641
641
 
642
+ It is possible to merge cells vertically and horizontally using the `rowspan` and `colspan` options in `cell_style` method e.g.
643
+
644
+ ```ruby
645
+ docx.table [['11', '1213', '14'], ['21', '22', '23', '24']] do
646
+ cell_style rows[0][0], rowspan: 2
647
+ cell_style rows[0][1], colspan: 2
648
+ cell_style rows[0][2], rowspan: 2
649
+ end
650
+ ```
651
+
652
+ *Note: content of cells 21 and 24 will disappear*
642
653
 
643
654
  ### Table Cells
644
655
 
@@ -25,6 +25,8 @@ module Caracal
25
25
  attr_reader :cell_width
26
26
  attr_reader :cell_margins
27
27
  attr_reader :cell_vertical_align
28
+ attr_reader :cell_rowspan
29
+ attr_reader :cell_colspan
28
30
 
29
31
  # initialization
30
32
  def initialize(options={}, &block)
@@ -118,7 +120,7 @@ module Caracal
118
120
  #=============== SETTERS ==============================
119
121
 
120
122
  # integers
121
- [:width].each do |m|
123
+ [:width, :colspan, :rowspan].each do |m|
122
124
  define_method "#{ m }" do |value|
123
125
  instance_variable_set("@cell_#{ m }", value.to_i)
124
126
  end
@@ -145,7 +147,6 @@ module Caracal
145
147
  end
146
148
  end
147
149
 
148
-
149
150
  #=============== VALIDATION ===========================
150
151
 
151
152
  def valid?
@@ -159,7 +160,7 @@ module Caracal
159
160
  private
160
161
 
161
162
  def option_keys
162
- [:background, :margins, :width, :vertical_align]
163
+ [:background, :margins, :width, :vertical_align, :rowspan, :colspan]
163
164
  end
164
165
 
165
166
  end
@@ -95,7 +95,7 @@ module Caracal
95
95
  # end
96
96
  #
97
97
  def cell_style(models, options={})
98
- [models].flatten.each do |m|
98
+ [models].flatten.compact.each do |m|
99
99
  m.apply_styles(options)
100
100
  end
101
101
  end
@@ -319,23 +319,42 @@ module Caracal
319
319
  end
320
320
  xml['w'].tblGrid do
321
321
  model.rows.first.each do |tc|
322
- xml['w'].gridCol({ 'w:w' => tc.cell_width })
322
+ (tc.cell_colspan || 1).times do
323
+ xml['w'].gridCol({ 'w:w' => tc.cell_width })
324
+ end
323
325
  end
324
326
  xml['w'].tblGridChange({ 'w:id' => '0' }) do
325
327
  xml['w'].tblGrid do
326
328
  model.rows.first.each do |tc|
327
- xml['w'].gridCol({ 'w:w' => tc.cell_width })
329
+ (tc.cell_colspan || 1).times do
330
+ xml['w'].gridCol({ 'w:w' => tc.cell_width })
331
+ end
328
332
  end
329
333
  end
330
334
  end
331
335
  end
336
+
337
+ rowspan_hash = {}
332
338
  model.rows.each do |row|
333
339
  xml['w'].tr do
334
- row.each do |tc|
340
+ row.each_with_index do |tc, tc_index|
335
341
  xml['w'].tc do
336
342
  xml['w'].tcPr do
337
343
  xml['w'].shd({ 'w:fill' => tc.cell_background })
338
344
  xml['w'].vAlign({ 'w:val' => tc.cell_vertical_align })
345
+
346
+ # applying rowspan
347
+ if tc.cell_rowspan && tc.cell_rowspan > 0
348
+ rowspan_hash[tc_index] = tc.cell_rowspan - 1
349
+ xml['w'].vMerge({ 'w:val' => 'restart' })
350
+ elsif rowspan_hash[tc_index] && rowspan_hash[tc_index] > 0
351
+ xml['w'].vMerge({ 'w:val' => 'continue' })
352
+ rowspan_hash[tc_index] -= 1
353
+ end
354
+
355
+ # applying colspan
356
+ xml['w'].gridSpan({ 'w:val' => tc.cell_colspan }) if tc.cell_colspan
357
+
339
358
  xml['w'].tcMar do
340
359
  %w(top left bottom right).each do |d|
341
360
  xml['w'].method_missing "#{ d }", { 'w:w' => tc.send("cell_margin_#{ d }").to_f, 'w:type' => 'dxa' }
@@ -349,6 +368,22 @@ module Caracal
349
368
  end
350
369
  end
351
370
  end
371
+
372
+ # we need to adjust rowspan indexes as they depend on previous row colspans
373
+ adjusted_rowspan_hash = {}
374
+ rowspan_hash.each do |rowspan_cell_index, rowspan_value|
375
+ adjusted_rowspan_cell_index = rowspan_cell_index
376
+ row.each_with_index do |tc, tc_index|
377
+ if tc.cell_colspan && tc.cell_colspan >= 1
378
+ if tc_index < rowspan_cell_index
379
+ adjusted_rowspan_cell_index += tc.cell_colspan - 1
380
+ end
381
+ end
382
+ end
383
+ adjusted_rowspan_hash[adjusted_rowspan_cell_index] = rowspan_value
384
+ end
385
+
386
+ rowspan_hash = adjusted_rowspan_hash
352
387
  end
353
388
  end
354
389
 
@@ -1,3 +1,3 @@
1
1
  module Caracal
2
- VERSION = '1.1.2'
2
+ VERSION = '1.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caracal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trade Infomatics
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-01-03 00:00:00.000000000 Z
12
+ date: 2018-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -231,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
231
  version: '0'
232
232
  requirements: []
233
233
  rubyforge_project:
234
- rubygems_version: 2.6.14
234
+ rubygems_version: 2.7.6
235
235
  signing_key:
236
236
  specification_version: 4
237
237
  summary: Fast, professional Microsoft Word (docx) writer for Ruby.