axlsx_styler 0.0.5 → 0.1.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
  SHA1:
3
- metadata.gz: 83dfa2cca4c51645feaa8b0dbf6693af425e42d9
4
- data.tar.gz: 770fb362a94bc3ce157dbd4df1d61ff657910882
3
+ metadata.gz: c39cb63343b8e6febf1d49849f79bedec5a7411f
4
+ data.tar.gz: 25548e35f4641c7ac07dba8c006d90e530ae0eb9
5
5
  SHA512:
6
- metadata.gz: feba1d502575df2c5742ec0f3aeaf99c12cd9e571e5aab9fb3b58d917ffeea0c866c01b9edfb381b1b3f3b0b41b143fb6d024536c6791348b49d58d747a94ddd
7
- data.tar.gz: 11fbe7c050e4091a084062567478ed84c58cee2a1f6e4a3f055002ad0e8974cdcbc2763b323ad81e1c34442b80c7b290cdbacaef9a42814c19f155ec251437bf
6
+ metadata.gz: 32d496b6251695a0fe9e05ce72fa5a930039f5df6bcb307bf9d0116c87e06f82b73195e6a631dd7cbe89e6cf1230e2aa3a125470d833704872b80f323d4d4ce4
7
+ data.tar.gz: 37898e7b43cbaab2fccaf06c2586c2205b7382bff89144eebfc0599837a3f4cb55251b1968e3532472b127877837de48a7444545765766433527beb72ab1b965
data/README.md CHANGED
@@ -14,21 +14,21 @@ This gem extend `Array` class in a way that allows you to apply
14
14
  styles to ranges of cells, e.g.
15
15
 
16
16
  ```ruby
17
- sheet["A1:D10"].add_style b: true
17
+ sheet.add_style 'A1:D10', b: true
18
18
  ```
19
19
 
20
20
  The styles can be overlayed, so that later on you can add another style
21
21
  to cells that already have styles, e.g.
22
22
 
23
23
  ```ruby
24
- sheet["A1:D1"].add_style bg_color: 'FF0000'
24
+ sheet.add_style 'A1:D1', bg_color: 'FF0000'
25
25
  ```
26
26
 
27
27
  You can also add borders to ranges of cells:
28
28
 
29
29
  ```ruby
30
- sheet["B2:D5"].add_border
31
- sheet["B2:B5"].add_border [:right]
30
+ sheet.add_border 'B2:D5'
31
+ sheet.add_border 'B2:D5', [:right]
32
32
  ```
33
33
 
34
34
  When you are done with styling you just need to run
@@ -38,7 +38,7 @@ workbook.apply_styles
38
38
  ```
39
39
 
40
40
  Here's an example that compares styling a simple table with and without
41
- AxlsxStyler. Suppose make our spreadsheet to look as follows:
41
+ AxlsxStyler. Suppose we wand to create the following spreadsheet:
42
42
 
43
43
  ![alt text](./spreadsheet.png "Sample Spreadsheet")
44
44
 
@@ -51,34 +51,31 @@ require 'axlsx_styler'
51
51
 
52
52
  axlsx = Axlsx::Package.new
53
53
  workbook = axlsx.workbook
54
-
55
54
  workbook.add_worksheet do |sheet|
56
55
  sheet.add_row
57
- sheet.add_row ["", "Product", "Category", "Price"]
58
- sheet.add_row ["", "Butter", "Dairy", 4.99]
59
- sheet.add_row ["", "Bread", "Baked Goods", 3.45]
60
- sheet.add_row ["", "Broccoli", "Produce", 2.99]
56
+ sheet.add_row ['', 'Product', 'Category', 'Price']
57
+ sheet.add_row ['', 'Butter', 'Dairy', 4.99]
58
+ sheet.add_row ['', 'Bread', 'Baked Goods', 3.45]
59
+ sheet.add_row ['', 'Broccoli', 'Produce', 2.99]
61
60
  sheet.column_widths 5, 20, 20, 20
62
61
 
63
62
  # using AxlsxStyler DSL
64
- sheet["B2:D2"].add_style b: true
65
- sheet["B2:B5"].add_style b: true
66
- sheet["B2:D2"].add_style bg_color: "95AFBA"
67
- sheet["B3:D5"].add_style bg_color: "E2F89C"
68
- sheet["D3:D5"].add_style alignment: { horizontal: :left }
69
- sheet["B2:D5"].add_border
70
- sheet["B3:D3"].add_border [:top]
63
+ sheet.add_style 'B2:D2', b: true
64
+ sheet.add_style 'B2:B5', b: true
65
+ sheet.add_style 'B2:D2', bg_color: '95AFBA'
66
+ sheet.add_style 'B3:D5', bg_color: 'E2F89C'
67
+ sheet.add_style 'D3:D5', alignment: { horizontal: :left }
68
+ sheet.add_border 'B2:D5'
69
+ sheet.add_border 'B3:D3', [:top]
71
70
  end
72
-
73
71
  workbook.apply_styles
74
-
75
- axlsx.serialize "grocery.xlsx"
72
+ axlsx.serialize 'grocery.xlsx'
76
73
  ```
77
74
 
78
75
  ### With plain `Axlsx` gem
79
76
 
80
77
  This example can be DRYied up, but it gives you a rough idea of what you
81
- need to go through.
78
+ need to go through. You need to compile all the styles before you apply them.
82
79
 
83
80
  ```ruby
84
81
  require 'axlsx'
@@ -2,7 +2,6 @@ require 'axlsx_styler'
2
2
 
3
3
  axlsx = Axlsx::Package.new
4
4
  workbook = axlsx.workbook
5
-
6
5
  workbook.add_worksheet do |sheet|
7
6
  sheet.add_row
8
7
  sheet.add_row ['', 'Product', 'Category', 'Price']
@@ -12,15 +11,13 @@ workbook.add_worksheet do |sheet|
12
11
  sheet.column_widths 5, 20, 20, 20
13
12
 
14
13
  # using AxlsxStyler DSL
15
- sheet['B2:D2'].add_style b: true
16
- sheet['B2:B5'].add_style b: true
17
- sheet['B2:D2'].add_style bg_color: '95AFBA'
18
- sheet['B3:D5'].add_style bg_color: 'E2F89C'
19
- sheet['D3:D5'].add_style alignment: { horizontal: :left }
20
- sheet['B2:D5'].add_border
21
- sheet['B3:D3'].add_border [:top]
14
+ sheet.add_style 'B2:D2', b: true
15
+ sheet.add_style 'B2:B5', b: true
16
+ sheet.add_style 'B2:D2', bg_color: '95AFBA'
17
+ sheet.add_style 'B3:D5', bg_color: 'E2F89C'
18
+ sheet.add_style 'D3:D5', alignment: { horizontal: :left }
19
+ sheet.add_border 'B2:D5'
20
+ sheet.add_border 'B3:D3', [:top]
22
21
  end
23
-
24
22
  workbook.apply_styles
25
-
26
23
  axlsx.serialize File.expand_path('../../tmp/grocery.xlsx', __FILE__)
@@ -1,10 +1,10 @@
1
1
  require 'axlsx'
2
2
 
3
3
  require 'axlsx_styler/version'
4
- require 'axlsx_styler/array'
5
4
  require 'axlsx_styler/axlsx_workbook'
5
+ require 'axlsx_styler/axlsx_worksheet'
6
6
  require 'axlsx_styler/axlsx_cell'
7
7
 
8
- Array.send :include, AxlsxStyler::Array
9
8
  Axlsx::Workbook.send :include, AxlsxStyler::Axlsx::Workbook
9
+ Axlsx::Worksheet.send :include, AxlsxStyler::Axlsx::Worksheet
10
10
  Axlsx::Cell.send :include, AxlsxStyler::Axlsx::Cell
@@ -0,0 +1,25 @@
1
+ require_relative './border_creator'
2
+
3
+ module AxlsxStyler
4
+ module Axlsx
5
+ module Worksheet
6
+ # Example:
7
+ # add_style 'A1:B5', b: true, sz: 14
8
+ def add_style(cell_ref, style)
9
+ item = self[cell_ref]
10
+ cells = item.is_a?(Array) ? item : [item]
11
+ cells.each { |cell| cell.add_style(style) }
12
+ end
13
+
14
+ # Examples:
15
+ # add_border 'B2:F8', [:left, :top]
16
+ # add_border 'C2:G10', [:top]
17
+ # add_border 'C2:G10'
18
+ # @TODO: allow to pass in custom border style
19
+ def add_border(cell_ref, edges = :all)
20
+ cells = self[cell_ref]
21
+ BorderCreator.new(self, cells, edges).draw
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,70 @@
1
+ class BorderCreator
2
+ attr_reader :worksheet, :cells, :edges
3
+
4
+ def initialize(worksheet, cells, edges)
5
+ @worksheet = worksheet
6
+ @cells = cells
7
+ @edges = edges
8
+ end
9
+
10
+ def draw
11
+ selected_edges(edges).each { |edge| add_border(edge) }
12
+ end
13
+
14
+ private
15
+
16
+ def selected_edges(edges)
17
+ all_edges = [:top, :right, :bottom, :left]
18
+ if edges == :all
19
+ all_edges
20
+ elsif edges.is_a?(Array) && edges - all_edges == []
21
+ edges.uniq
22
+ else
23
+ []
24
+ end
25
+ end
26
+
27
+ def add_border(position)
28
+ style = {
29
+ border: {
30
+ style: :thin, color: '000000', edges: [position.to_sym]
31
+ }
32
+ }
33
+ worksheet.add_style border_cells[position.to_sym], style
34
+ # add_style border_cells(cell_ref)[position.to_sym], style
35
+ end
36
+
37
+ def border_cells
38
+ # example range "B2:D5"
39
+ {
40
+ top: "#{first_cell}:#{last_col}#{first_row}", # "B2:D2"
41
+ right: "#{last_col}#{first_row}:#{last_cell}", # "D2:D5"
42
+ bottom: "#{first_col}#{last_row}:#{last_cell}", # "B5:D5"
43
+ left: "#{first_cell}:#{first_col}#{last_row}" # "B2:B5"
44
+ }
45
+ end
46
+
47
+ def first_cell
48
+ @first_cell ||= cells.first.r
49
+ end
50
+
51
+ def last_cell
52
+ @last_cell ||= cells.last.r
53
+ end
54
+
55
+ def first_row
56
+ @first_row ||= first_cell.scan(/\d+/).first
57
+ end
58
+
59
+ def first_col
60
+ @first_col ||= first_cell.scan(/\D+/).first
61
+ end
62
+
63
+ def last_row
64
+ @last_row ||= last_cell.scan(/\d+/).first
65
+ end
66
+
67
+ def last_col
68
+ @last_col ||= last_cell.scan(/\D+/).first
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module AxlsxStyler
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class IntegrationTest < MiniTest::Test
4
- # taken from /examples
5
4
  def test_table_with_borders
6
5
  axlsx = Axlsx::Package.new
7
6
  workbook = axlsx.workbook
@@ -15,13 +14,13 @@ class IntegrationTest < MiniTest::Test
15
14
  sheet.column_widths 5, 20, 20, 20
16
15
 
17
16
  # using AxlsxStyler DSL
18
- sheet['B2:D2'].add_style b: true
19
- sheet['B2:B6'].add_style b: true
20
- sheet['B2:D2'].add_style bg_color: '95AFBA'
21
- sheet['B3:D6'].add_style bg_color: 'E2F89C'
22
- sheet['D3:D6'].add_style alignment: { horizontal: :left }
23
- sheet['B2:D6'].add_border
24
- sheet['B3:D3'].add_border [:top]
17
+ sheet.add_style 'B2:D2', b: true
18
+ sheet.add_style 'B2:B6', b: true
19
+ sheet.add_style 'B2:D2', bg_color: '95AFBA'
20
+ sheet.add_style 'B3:D6', bg_color: 'E2F89C'
21
+ sheet.add_style 'D3:D6', alignment: { horizontal: :left }
22
+ sheet.add_border 'B2:D6'
23
+ sheet.add_border 'B3:D3', [:top]
25
24
  end
26
25
  workbook.apply_styles
27
26
  axlsx.serialize File.expand_path(
@@ -39,15 +38,17 @@ class IntegrationTest < MiniTest::Test
39
38
  day = 24 * 60 * 60
40
39
  workbook.add_worksheet do |sheet|
41
40
  sheet.add_row %w(Date Count Percent)
42
- sheet.add_row [t - 2 * day, 2, 2.to_f / 11]
43
- sheet.add_row [t - 1 * day, 3, 3.to_f / 11]
44
- sheet.add_row [t, 6, 6.to_f / 11]
41
+ sheet.add_row [t - 2 * day, 2, 2.to_f.round(2) / 11]
42
+ sheet.add_row [t - 1 * day, 3, 3.to_f.round(2) / 11]
43
+ sheet.add_row [t, 6, 6.to_f.round(2) / 11]
45
44
 
46
- sheet['A1:B1'].add_style b: true
47
- sheet['A2:A4'].add_style format_code: 'YYYY-MM-DD hh:mm:ss'
45
+ sheet.add_style 'A1:B1', b: true
46
+ sheet.add_style 'A2:A4', format_code: 'YYYY-MM-DD hh:mm:ss'
48
47
  end
49
48
  workbook.apply_styles
50
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
51
52
  axlsx.serialize File.expand_path(
52
53
  '../../tmp/num_fmt_test.xlsx',
53
54
  __FILE__
@@ -62,10 +63,10 @@ class IntegrationTest < MiniTest::Test
62
63
  sheet.add_row [1, 'Ottawa']
63
64
  sheet.add_row [2, 'Boston']
64
65
 
65
- sheet['A1:B1'].add_border [:bottom]
66
- sheet['A1:B1'].add_border [:bottom]
67
- sheet['A1:A3'].add_style b: true
68
- sheet['A1:A3'].add_style b: true
66
+ sheet.add_border 'A1:B1', [:bottom]
67
+ sheet.add_border 'A1:B1', [:bottom]
68
+ sheet.add_style 'A1:A3', b: true
69
+ sheet.add_style 'A1:A3', b: true
69
70
  end
70
71
  workbook.apply_styles
71
72
  assert_equal 4, workbook.styled_cells.count
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: axlsx_styler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sakovich
@@ -114,12 +114,12 @@ files:
114
114
  - axlsx_styler.gemspec
115
115
  - examples/colors_and_borders.rb
116
116
  - lib/axlsx_styler.rb
117
- - lib/axlsx_styler/array.rb
118
117
  - lib/axlsx_styler/axlsx_cell.rb
119
118
  - lib/axlsx_styler/axlsx_workbook.rb
119
+ - lib/axlsx_styler/axlsx_worksheet.rb
120
+ - lib/axlsx_styler/border_creator.rb
120
121
  - lib/axlsx_styler/version.rb
121
122
  - spreadsheet.png
122
- - test/array_test.rb
123
123
  - test/cell_test.rb
124
124
  - test/integration_test.rb
125
125
  - test/test_helper.rb
@@ -149,7 +149,6 @@ signing_key:
149
149
  specification_version: 4
150
150
  summary: This gem allows to separate data from styles when using Axlsx gem.
151
151
  test_files:
152
- - test/array_test.rb
153
152
  - test/cell_test.rb
154
153
  - test/integration_test.rb
155
154
  - test/test_helper.rb
@@ -1,77 +0,0 @@
1
- module AxlsxStyler
2
- # @TODO
3
- # Move the functionality from Array to Worksheet. New DSL
4
- # sheet.add_style 'A1:B5', b: true
5
- # instead of
6
- # sheet['A1:B5'].add_style, b: true
7
- module Array
8
- def add_style(style)
9
- validate_cells
10
- each do |cell|
11
- cell.add_style(style)
12
- end
13
- end
14
-
15
- # - edges is either :all or an an array with elements
16
- # in [:top, :bottom, :bottom, :left]
17
- # - couldn't do variable border thickness around the same cell;
18
- # hardcode style to :thin for now
19
- def add_border(edges = :all)
20
- validate_cells
21
- selected_edges(edges).each { |edge| add_border_at(edge) }
22
- end
23
-
24
- private
25
-
26
- def selected_edges(edges)
27
- all_edges = [:top, :right, :bottom, :left]
28
- if edges == :all
29
- all_edges
30
- elsif edges.is_a?(Array) && edges - all_edges == []
31
- edges.uniq
32
- else
33
- []
34
- end
35
- end
36
-
37
- def validate_cells
38
- valid = map{ |e| e.is_a? Axlsx::Cell }.uniq == [ true ]
39
- raise "Not a range of cells" unless valid
40
- end
41
-
42
- def border_cells
43
- @border_cells ||= get_border_cells
44
- end
45
-
46
- def get_border_cells
47
- first = self.first.r
48
- last = self.last.r
49
-
50
- first_row = first.scan(/\d+/).first
51
- last_row = last.scan(/\d+/).first
52
- first_col = first.scan(/\D+/).first
53
- last_col = last.scan(/\D+/).first
54
-
55
- # example range "B2:D5"
56
- {
57
- top: "#{first}:#{last_col}#{first_row}", # "B2:D2"
58
- right: "#{last_col}#{first_row}:#{last}", # "D2:D5"
59
- bottom: "#{first_col}#{last_row}:#{last}", # "B5:D5"
60
- left: "#{first}:#{first_col}#{last_row}" # "B2:B5"
61
- }
62
- end
63
-
64
- def worksheet
65
- @worksheet ||= self.first.row.worksheet
66
- end
67
-
68
- def add_border_at(position)
69
- style = {
70
- border: {
71
- style: :thin, color: "000000", edges: [position.to_sym]
72
- }
73
- }
74
- worksheet[border_cells[position.to_sym]].add_style(style)
75
- end
76
- end
77
- end
@@ -1,8 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ArrayTest < MiniTest::Test
4
- def test_presesence_of_add_style
5
- array = Array.new
6
- assert array.respond_to?(:add_style)
7
- end
8
- end