axlsx_styler 0.0.3 → 0.0.4

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
2
  SHA1:
3
- metadata.gz: 756a9d8d271760c9ae1255f24ca3f4fe8a2a1629
4
- data.tar.gz: 5ebf73fd12289d2092630753093eb5ef2b841bea
3
+ metadata.gz: 21d5d36b1a846a693e3dace67325bb9e8d2317ba
4
+ data.tar.gz: 06e8c8cd836d25e99e6a9d1463536b6e7711a974
5
5
  SHA512:
6
- metadata.gz: 5b323e60aca00a19f961e19a5e1888812eef5eddba3cb45b912237f3c9eb41d9f3078ebccf4a47cf55c00a150b42a009b559f667c5aab0520ba34da611f97f39
7
- data.tar.gz: a0e8b748ef3acfef9dcc7837fad747625e1ffa446002ee625e9cb167a63587ee624040fd2726f0f07d2d2551a81fccda7c3fbc1bbfc184b94973681bd34779c3
6
+ metadata.gz: e84cc140bac5013b7881c81b5c5dd14cabcf8dfe9843729dce817c8ab2f536064d0ec6fd761af1b264682659dbecac42baa32e3cc03a32abbefc117f77f64e54
7
+ data.tar.gz: 8385e37dacf3f373951132a884ab7e6775e7770584d76eae8fa23827e6efff550dd1d19a5983fe2d3b7267dec94eedf895821c04726b6de45dd7a252b347e9fd
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
25
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
26
  spec.require_paths = ["lib"]
27
+ spec.required_ruby_version = '>= 1.9.3'
27
28
 
28
29
  spec.add_dependency "axlsx", "~> 2.0"
29
30
  spec.add_dependency "activesupport", "~> 3.1"
@@ -31,4 +32,5 @@ Gem::Specification.new do |spec|
31
32
  spec.add_development_dependency "bundler", "~> 1.6"
32
33
  spec.add_development_dependency "rake", "~> 0.9"
33
34
  spec.add_development_dependency "minitest", "~> 5.0"
35
+ spec.add_development_dependency 'awesome_print', '~> 1.6'
34
36
  end
@@ -1,26 +1,26 @@
1
- require_relative '../lib/axlsx_styler'
1
+ require 'axlsx_styler'
2
2
 
3
3
  axlsx = Axlsx::Package.new
4
4
  workbook = axlsx.workbook
5
5
 
6
6
  workbook.add_worksheet do |sheet|
7
7
  sheet.add_row
8
- sheet.add_row ["", "Product", "Category", "Price"]
9
- sheet.add_row ["", "Butter", "Dairy", 4.99]
10
- sheet.add_row ["", "Bread", "Baked Goods", 3.45]
11
- sheet.add_row ["", "Broccoli", "Produce", 2.99]
8
+ sheet.add_row ['', 'Product', 'Category', 'Price']
9
+ sheet.add_row ['', 'Butter', 'Dairy', 4.99]
10
+ sheet.add_row ['', 'Bread', 'Baked Goods', 3.45]
11
+ sheet.add_row ['', 'Broccoli', 'Produce', 2.99]
12
12
  sheet.column_widths 5, 20, 20, 20
13
13
 
14
14
  # 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]
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]
22
22
  end
23
23
 
24
24
  workbook.apply_styles
25
25
 
26
- axlsx.serialize "../grocery.xlsx"
26
+ axlsx.serialize File.expand_path('../../tmp/grocery.xlsx', __FILE__)
@@ -2,7 +2,8 @@ require 'axlsx'
2
2
 
3
3
  require 'axlsx_styler/version'
4
4
  require 'axlsx_styler/array'
5
- require 'axlsx_styler/axlsx_extensions'
5
+ require 'axlsx_styler/axlsx_workbook'
6
+ require 'axlsx_styler/axlsx_cell'
6
7
 
7
8
  Array.send :include, AxlsxStyler::Array
8
9
  Axlsx::Workbook.send :include, AxlsxStyler::Axlsx::Workbook
@@ -2,35 +2,21 @@ require 'active_support/core_ext/hash/deep_merge'
2
2
 
3
3
  module AxlsxStyler
4
4
  module Axlsx
5
- module Workbook
6
- attr_accessor :styled_cells
7
-
8
- def add_styled_cell(cell)
9
- self.styled_cells ||= []
10
- self.styled_cells << cell
11
- end
12
-
13
- def apply_styles
14
- return unless styled_cells
15
- styled_cells.each do |cell|
16
- cell.style = styles.add_style(cell.raw_style)
17
- end
18
- end
19
- end
20
-
21
5
  module Cell
22
6
  attr_accessor :raw_style
23
7
 
24
- def workbook
25
- row.worksheet.workbook
26
- end
27
-
28
8
  def add_style(style)
29
9
  self.raw_style ||= {}
30
10
  add_to_raw_style(style)
31
11
  workbook.add_styled_cell self
32
12
  end
33
13
 
14
+ private
15
+
16
+ def workbook
17
+ row.worksheet.workbook
18
+ end
19
+
34
20
  def add_to_raw_style(style)
35
21
  # using deep_merge from active_support:
36
22
  # with regular Hash#merge adding borders fails miserably
@@ -0,0 +1,53 @@
1
+ module AxlsxStyler
2
+ module Axlsx
3
+ module Workbook
4
+ # An array that holds all cells with styles
5
+ attr_accessor :styled_cells
6
+
7
+ # An index for cell styles
8
+ # {
9
+ # < style_hash > => 1,
10
+ # < style_hash > => 2,
11
+ # ...
12
+ # < style_hash > => K
13
+ # }
14
+ # where keys are Cell#raw_style and values are styles
15
+ # codes as per Axlsx::Style
16
+ attr_accessor :style_index
17
+
18
+ def add_styled_cell(cell)
19
+ self.styled_cells ||= Set.new
20
+ self.styled_cells << cell
21
+ end
22
+
23
+ def apply_styles
24
+ return unless styled_cells
25
+ styled_cells.each do |cell|
26
+ set_style_index(cell)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ # Check if style code
33
+ def set_style_index(cell)
34
+ # @TODO fix this hack
35
+ self.style_index ||= {}
36
+
37
+ style = style_index[cell.raw_style]
38
+ if style
39
+ cell.style = style
40
+ else
41
+ new_style = styles.add_style(cell.raw_style)
42
+ cell.style = new_style
43
+
44
+ # :num_fmt is distinct even though the styles are
45
+ # the same; not sure if it's intended functionality
46
+ cell.raw_style.delete(:num_fmt)
47
+
48
+ style_index[cell.raw_style] = new_style
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module AxlsxStyler
2
- VERSION = "0.0.3"
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ class IntegrationTest < MiniTest::Test
4
+ # taken from /examples
5
+ def test_table_with_borders
6
+ axlsx = Axlsx::Package.new
7
+ workbook = axlsx.workbook
8
+ workbook.add_worksheet do |sheet|
9
+ sheet.add_row
10
+ sheet.add_row ['', 'Product', 'Category', 'Price']
11
+ sheet.add_row ['', 'Butter', 'Dairy', 4.99]
12
+ sheet.add_row ['', 'Bread', 'Baked Goods', 3.45]
13
+ sheet.add_row ['', 'Broccoli', 'Produce', 2.99]
14
+ sheet.add_row ['', 'Pizza', 'Frozen Foods', 4.99]
15
+ sheet.column_widths 5, 20, 20, 20
16
+
17
+ # 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]
25
+ end
26
+ workbook.apply_styles
27
+ axlsx.serialize File.expand_path(
28
+ '../../tmp/borders_test.xlsx',
29
+ __FILE__
30
+ )
31
+ assert_equal 12, workbook.style_index.count
32
+ end
33
+
34
+ def test_table_with_num_fmt
35
+ axlsx = Axlsx::Package.new
36
+ workbook = axlsx.workbook
37
+ t = Time.now
38
+ day = 24 * 60 * 60
39
+ workbook.add_worksheet do |sheet|
40
+ sheet.add_row %w(Date Count Percent)
41
+ sheet.add_row [t - 2 * day, 2, 2.to_f / 11]
42
+ sheet.add_row [t - 1 * day, 3, 3.to_f / 11]
43
+ sheet.add_row [t, 6, 6.to_f / 11]
44
+
45
+ sheet['A1:B1'].add_style b: true
46
+ sheet['A2:A4'].add_style format_code: 'YYYY-MM-DD hh:mm:ss'
47
+ end
48
+ workbook.apply_styles
49
+ assert_equal 2, workbook.style_index.count
50
+ axlsx.serialize File.expand_path(
51
+ '../../tmp/num_fmt_test.xlsx',
52
+ __FILE__
53
+ )
54
+ end
55
+ end
@@ -1,3 +1,4 @@
1
1
  require 'axlsx_styler'
2
2
  require 'minitest/autorun'
3
3
  require 'minitest/pride'
4
+ require 'awesome_print'
@@ -4,6 +4,6 @@ class WorkbookTest < MiniTest::Test
4
4
  wb = p.workbook
5
5
  wb.add_styled_cell 'Cell 1'
6
6
  wb.add_styled_cell 'Cell 2'
7
- assert_equal ['Cell 1', 'Cell 2'], wb.styled_cells
7
+ assert_equal ['Cell 1', 'Cell 2'].to_set, wb.styled_cells
8
8
  end
9
9
  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.0.3
4
+ version: 0.0.4
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-02-23 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: axlsx
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.6'
83
97
  description: "\n Axlsx gem is an excellent tool to build Excel worksheets. The
84
98
  sheets are\n created row-by-row and styles are immediately added to each cell
85
99
  when a\n row is created. This gem allows to follow an alternative route: fill
@@ -101,11 +115,13 @@ files:
101
115
  - examples/colors_and_borders.rb
102
116
  - lib/axlsx_styler.rb
103
117
  - lib/axlsx_styler/array.rb
104
- - lib/axlsx_styler/axlsx_extensions.rb
118
+ - lib/axlsx_styler/axlsx_cell.rb
119
+ - lib/axlsx_styler/axlsx_workbook.rb
105
120
  - lib/axlsx_styler/version.rb
106
121
  - spreadsheet.png
107
122
  - test/array_test.rb
108
123
  - test/cell_test.rb
124
+ - test/integration_test.rb
109
125
  - test/test_helper.rb
110
126
  - test/workbook_test.rb
111
127
  homepage: https://github.com/sakovias/axlsx_styler
@@ -120,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
136
  requirements:
121
137
  - - ">="
122
138
  - !ruby/object:Gem::Version
123
- version: '0'
139
+ version: 1.9.3
124
140
  required_rubygems_version: !ruby/object:Gem::Requirement
125
141
  requirements:
126
142
  - - ">="
@@ -135,5 +151,6 @@ summary: This gem allows to separate data from styles when using Axlsx gem.
135
151
  test_files:
136
152
  - test/array_test.rb
137
153
  - test/cell_test.rb
154
+ - test/integration_test.rb
138
155
  - test/test_helper.rb
139
156
  - test/workbook_test.rb