axlsx_styler 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -1
- data/lib/axlsx_styler/axlsx_worksheet.rb +12 -3
- data/lib/axlsx_styler/version.rb +1 -1
- data/test/integration_test.rb +45 -31
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d3232dd2b096374134b17d0580aa8d4b0fdcbd9
|
4
|
+
data.tar.gz: 9add5f73a45dd461c5b2b2f2edfab0d5a57b828c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04792906e4d28e5c31d7f4e30ca9f1dd9cf598bb10bf05828451a2517b44d3e5afcf72070a0c6beedd13230cddb7f10f19d6a613db94ff05c322ed60997dc6f7
|
7
|
+
data.tar.gz: 90ad8832f1378f4d78770eb0e7756c7ba0f4db6da1bc159213b15cef47002e7167e5b26053073668fa23ddbcde152bd533e901c8a3310c14aed961e3a1f27efb
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@ be used outside of any specific ruby framework as shown in example below.
|
|
16
16
|
This gem provides a DSL that allows you to apply styles to ranges of cells, e.g.
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
sheet.add_style 'A1:D10', b: true
|
19
|
+
sheet.add_style 'A1:D10', b: true, sz: 14
|
20
20
|
```
|
21
21
|
|
22
22
|
The styles can be overlayed, so that later on you can add another style
|
@@ -26,6 +26,14 @@ to cells that already have styles, e.g.
|
|
26
26
|
sheet.add_style 'A1:D1', bg_color: 'FF0000'
|
27
27
|
```
|
28
28
|
|
29
|
+
Applying multiple styles as a sequence of Ruby hashes is also possible:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
bold = { b: true }
|
33
|
+
centered = { alignment: { horizontal: :center } }
|
34
|
+
sheet.add_style 'A2:D2', bold, centered
|
35
|
+
```
|
36
|
+
|
29
37
|
You can also add borders to ranges of cells:
|
30
38
|
|
31
39
|
```ruby
|
@@ -3,12 +3,21 @@ require_relative './border_creator'
|
|
3
3
|
module AxlsxStyler
|
4
4
|
module Axlsx
|
5
5
|
module Worksheet
|
6
|
-
# Example:
|
6
|
+
# Example to add a single style:
|
7
7
|
# add_style 'A1:B5', b: true, sz: 14
|
8
|
-
|
8
|
+
#
|
9
|
+
# Example to add multiple styles:
|
10
|
+
# bold = { b: true }
|
11
|
+
# large_text = { sz: 30 }
|
12
|
+
# add_style 'B2:F8', bold, large_text
|
13
|
+
def add_style(cell_ref, *styles)
|
9
14
|
item = self[cell_ref]
|
10
15
|
cells = item.is_a?(Array) ? item : [item]
|
11
|
-
cells.each
|
16
|
+
cells.each do |cell|
|
17
|
+
styles.each do |style|
|
18
|
+
cell.add_style(style)
|
19
|
+
end
|
20
|
+
end
|
12
21
|
end
|
13
22
|
|
14
23
|
# Examples:
|
data/lib/axlsx_styler/version.rb
CHANGED
data/test/integration_test.rb
CHANGED
@@ -1,10 +1,20 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class IntegrationTest < MiniTest::Test
|
4
|
+
def setup
|
5
|
+
@axlsx = Axlsx::Package.new
|
6
|
+
@workbook = @axlsx.workbook
|
7
|
+
end
|
8
|
+
|
9
|
+
# Save files in case you'd like to see what the result is
|
10
|
+
def teardown
|
11
|
+
return unless @filename
|
12
|
+
@axlsx.serialize File.expand_path("../../tmp/#{@filename}.xlsx", __FILE__)
|
13
|
+
end
|
14
|
+
|
4
15
|
def test_table_with_borders
|
5
|
-
|
6
|
-
workbook
|
7
|
-
workbook.add_worksheet do |sheet|
|
16
|
+
@filename = 'borders_test'
|
17
|
+
@workbook.add_worksheet do |sheet|
|
8
18
|
sheet.add_row
|
9
19
|
sheet.add_row ['', 'Product', 'Category', 'Price']
|
10
20
|
sheet.add_row ['', 'Butter', 'Dairy', 4.99]
|
@@ -22,21 +32,16 @@ class IntegrationTest < MiniTest::Test
|
|
22
32
|
sheet.add_border 'B2:D6'
|
23
33
|
sheet.add_border 'B3:D3', [:top]
|
24
34
|
end
|
25
|
-
workbook.apply_styles
|
26
|
-
|
27
|
-
|
28
|
-
__FILE__
|
29
|
-
)
|
30
|
-
assert_equal 12, workbook.style_index.count
|
31
|
-
assert_equal 12 + 2, workbook.style_index.keys.max
|
35
|
+
@workbook.apply_styles
|
36
|
+
assert_equal 12, @workbook.style_index.count
|
37
|
+
assert_equal 12 + 2, @workbook.style_index.keys.max
|
32
38
|
end
|
33
39
|
|
34
40
|
def test_table_with_num_fmt
|
35
|
-
|
36
|
-
workbook = axlsx.workbook
|
41
|
+
@filename = 'num_fmt_test'
|
37
42
|
t = Time.now
|
38
43
|
day = 24 * 60 * 60
|
39
|
-
workbook.add_worksheet do |sheet|
|
44
|
+
@workbook.add_worksheet do |sheet|
|
40
45
|
sheet.add_row %w(Date Count Percent)
|
41
46
|
sheet.add_row [t - 2 * day, 2, 2.to_f.round(2) / 11]
|
42
47
|
sheet.add_row [t - 1 * day, 3, 3.to_f.round(2) / 11]
|
@@ -45,20 +50,15 @@ class IntegrationTest < MiniTest::Test
|
|
45
50
|
sheet.add_style 'A1:B1', b: true
|
46
51
|
sheet.add_style 'A2:A4', format_code: 'YYYY-MM-DD hh:mm:ss'
|
47
52
|
end
|
48
|
-
workbook.apply_styles
|
49
|
-
assert_equal 2, workbook.style_index.count
|
50
|
-
assert_equal 2 + 2, workbook.style_index.keys.max
|
51
|
-
assert_equal 5, workbook.styled_cells.count
|
52
|
-
axlsx.serialize File.expand_path(
|
53
|
-
'../../tmp/num_fmt_test.xlsx',
|
54
|
-
__FILE__
|
55
|
-
)
|
53
|
+
@workbook.apply_styles
|
54
|
+
assert_equal 2, @workbook.style_index.count
|
55
|
+
assert_equal 2 + 2, @workbook.style_index.keys.max
|
56
|
+
assert_equal 5, @workbook.styled_cells.count
|
56
57
|
end
|
57
58
|
|
58
59
|
def test_duplicate_styles
|
59
|
-
|
60
|
-
workbook
|
61
|
-
workbook.add_worksheet do |sheet|
|
60
|
+
@filename = 'duplicate_styles'
|
61
|
+
@workbook.add_worksheet do |sheet|
|
62
62
|
sheet.add_row %w(Index City)
|
63
63
|
sheet.add_row [1, 'Ottawa']
|
64
64
|
sheet.add_row [2, 'Boston']
|
@@ -68,12 +68,26 @@ class IntegrationTest < MiniTest::Test
|
|
68
68
|
sheet.add_style 'A1:A3', b: true
|
69
69
|
sheet.add_style 'A1:A3', b: true
|
70
70
|
end
|
71
|
-
workbook.apply_styles
|
72
|
-
assert_equal 4, workbook.styled_cells.count
|
73
|
-
assert_equal 3, workbook.style_index.count
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
71
|
+
@workbook.apply_styles
|
72
|
+
assert_equal 4, @workbook.styled_cells.count
|
73
|
+
assert_equal 3, @workbook.style_index.count
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_multiple_named_styles
|
77
|
+
@filename = 'multiple_named_styles'
|
78
|
+
bold_blue = { b: true, fg_color: '0000FF' }
|
79
|
+
large = { sz: 16 }
|
80
|
+
red = { fg_color: 'FF0000' }
|
81
|
+
@workbook.add_worksheet do |sheet|
|
82
|
+
sheet.add_row %w(Index City)
|
83
|
+
sheet.add_row [1, 'Ottawa']
|
84
|
+
sheet.add_row [2, 'Boston']
|
85
|
+
|
86
|
+
sheet.add_style 'A1:B1', bold_blue, large
|
87
|
+
sheet.add_style 'A1:A3', red
|
88
|
+
end
|
89
|
+
@workbook.apply_styles
|
90
|
+
assert_equal 4, @workbook.styled_cells.count
|
91
|
+
assert_equal 3, @workbook.style_index.count
|
78
92
|
end
|
79
93
|
end
|
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.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Sakovich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: axlsx
|