axlsx_styler 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2536d38bb1cb2ea59a3c8c3b4271056e6ed61e4f
4
+ data.tar.gz: 49dd2f0a2069ccc497167b2ceef045420236c2c2
5
+ SHA512:
6
+ metadata.gz: 9f1ae20c49e60b41217a3c43e398685fa7de9d66b401e309fe5f69a6c938fd2c93df70028ae73949b3a685ea03ce46c80662c84387a227cebc2bf8df4c6f92a0
7
+ data.tar.gz: 9f8ce7362c84f93d1c9c747d73cfbe9d5067b2934c39c58d7003bc3fbd3219e21f9ed2e05a0c50cc32fe1f8fcbcf78d0ca750feb972a4322c2591d75b7e43cb2
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ *.xlsx
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in axlsx_styler.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Anton Sakovich
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # AxlsxStyler
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'axlsx_styler'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install axlsx_styler
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/axlsx_styler/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'axlsx_styler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "axlsx_styler"
8
+ spec.version = AxlsxStyler::VERSION
9
+ spec.authors = ["Anton Sakovich"]
10
+ spec.email = ["sakovias@gmail.com"]
11
+ spec.summary = %q{This gem allows to separate data from styles when using Axlsx gem.}
12
+ spec.description = %q{
13
+ Axlsx gem is an excellent tool to build Excel worksheets. The sheets are
14
+ created row-by-row and styles are immediately added to each cell when a
15
+ row is created. This gem allows to follow an alternative route: fill out
16
+ a spreadsheet with data and apply styles later. Styles can be added
17
+ to individual cells as well as to ranges of cells. As a bonus, this gem
18
+ also simplifies drawing borders around groups of cells.
19
+ }
20
+ spec.homepage = "https://github.com/sakovias/axlsx_styler"
21
+ spec.license = "MIT"
22
+
23
+ spec.files = `git ls-files -z`.split("\x0")
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "axlsx", "~> 2.0"
29
+ spec.add_dependency "activesupport", "~> 3.1"
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.6"
32
+ spec.add_development_dependency "rake", "~> 0.9"
33
+ spec.add_development_dependency "minitest", "~> 5.0"
34
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../lib/axlsx_styler'
2
+
3
+ axlsx = Axlsx::Package.new
4
+ workbook = axlsx.workbook
5
+
6
+ workbook.add_worksheet do |sheet|
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]
12
+
13
+ sheet["B2:D2"].add_style(b: true)
14
+ sheet["B2:D5"].add_style(bg_color: "E2D3EB")
15
+ sheet["B2:D5"].add_border
16
+ sheet["B3:D3"].add_border([:top])
17
+ end
18
+
19
+ workbook.apply_styles
20
+
21
+ axlsx.serialize "grocery.xlsx"
@@ -0,0 +1,9 @@
1
+ require 'axlsx'
2
+
3
+ require 'axlsx_styler/version'
4
+ require 'axlsx_styler/array'
5
+ require 'axlsx_styler/axlsx_extensions'
6
+
7
+ Array.include AxlsxStyler::Array
8
+ Axlsx::Workbook.include AxlsxStyler::Axlsx::Workbook
9
+ Axlsx::Cell.include AxlsxStyler::Axlsx::Cell
@@ -0,0 +1,66 @@
1
+ module AxlsxStyler
2
+ module Array
3
+ def add_style(style)
4
+ validate_cells
5
+ each do |cell|
6
+ cell.add_style(style)
7
+ end
8
+ end
9
+
10
+ # - edges is either :all or an an array with elements
11
+ # in [:top, :bottom, :bottom, :left]
12
+ # - couldn't do variable border thickness around the same cell;
13
+ # hardcode style to :thin for now
14
+ def add_border(edges = :all)
15
+ validate_cells
16
+ selected_edges(edges).each{ |edge| add_border_at(edge) }
17
+ end
18
+
19
+ private
20
+
21
+ def selected_edges(edges)
22
+ all_edges = [:top, :right, :bottom, :left]
23
+ if edges == :all
24
+ all_edges
25
+ elsif edges.is_a?(Array) && edges - all_edges == []
26
+ edges.uniq
27
+ else
28
+ []
29
+ end
30
+ end
31
+
32
+ def validate_cells
33
+ valid = map{ |e| e.is_a? Axlsx::Cell }.uniq == [ true ]
34
+ raise "Not a range of cells" unless valid
35
+ end
36
+
37
+ def border_cells
38
+ @border_cells ||= get_border_cells
39
+ end
40
+
41
+ def get_border_cells
42
+ first = self.first.r
43
+ last = self.last.r
44
+ # example range "B2:D5"
45
+ {
46
+ top: "#{first}:#{last[0]}#{first[1]}", # "B2:D2"
47
+ right: "#{last[0]}#{first[1]}:#{last}", # "D2:D5"
48
+ bottom: "#{first[0]}#{last[1]}:#{last}", # "B5:D5"
49
+ left: "#{first}:#{first[0]}#{last[1]}" # "B2:B5"
50
+ }
51
+ end
52
+
53
+ def worksheet
54
+ @worksheet ||= self.first.row.worksheet
55
+ end
56
+
57
+ def add_border_at(position)
58
+ style = {
59
+ border: {
60
+ style: :thin, color: "000000", edges: [position.to_sym]
61
+ }
62
+ }
63
+ worksheet[border_cells[position.to_sym]].add_style(style)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,52 @@
1
+ require 'active_support/core_ext/hash/deep_merge'
2
+
3
+ module AxlsxStyler
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
+ module Cell
22
+ attr_accessor :raw_style
23
+
24
+ def workbook
25
+ row.worksheet.workbook
26
+ end
27
+
28
+ def add_style(style)
29
+ self.raw_style ||= {}
30
+ add_to_raw_style(style)
31
+ workbook.add_styled_cell self
32
+ end
33
+
34
+ def add_to_raw_style(style)
35
+ # using deep_merge from active_support:
36
+ # with regular Hash#merge adding borders fails miserably
37
+ new_style = raw_style.deep_merge style
38
+ if with_border?(raw_style) && with_border?(style)
39
+ border_at = raw_style[:border][:edges] + style[:border][:edges]
40
+ new_style[:border][:edges] = border_at
41
+ elsif with_border?(style)
42
+ new_style[:border] = style[:border]
43
+ end
44
+ self.raw_style = new_style
45
+ end
46
+
47
+ def with_border?(style)
48
+ !style[:border].nil?
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module AxlsxStyler
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class CellTest < MiniTest::Test
4
+ def test_can_add_style
5
+ p = Axlsx::Package.new
6
+ wb = p.workbook
7
+ sheet = wb.add_worksheet
8
+ row = sheet.add_row ["x", "y"]
9
+ cell = row.cells.first
10
+
11
+ cell.add_style b: true
12
+ assert_equal({ b: true }, cell.raw_style)
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ require 'axlsx_styler'
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
@@ -0,0 +1,9 @@
1
+ class WorkbookTest < MiniTest::Test
2
+ def test_adding_styled_cells
3
+ p = Axlsx::Package.new
4
+ wb = p.workbook
5
+ wb.add_styled_cell 'Cell 1'
6
+ wb.add_styled_cell 'Cell 2'
7
+ assert_equal ['Cell 1', 'Cell 2'], wb.styled_cells
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: axlsx_styler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Anton Sakovich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: axlsx
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: "\n Axlsx gem is an excellent tool to build Excel worksheets. The
84
+ sheets are\n created row-by-row and styles are immediately added to each cell
85
+ when a\n row is created. This gem allows to follow an alternative route: fill
86
+ out\n a spreadsheet with data and apply styles later. Styles can be added\n to
87
+ individual cells as well as to ranges of cells. As a bonus, this gem\n also simplifies
88
+ drawing borders around groups of cells.\n "
89
+ email:
90
+ - sakovias@gmail.com
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - ".gitignore"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - axlsx_styler.gemspec
101
+ - examples/colors_and_borders.rb
102
+ - examples/grocery.xlsx
103
+ - lib/axlsx_styler.rb
104
+ - lib/axlsx_styler/array.rb
105
+ - lib/axlsx_styler/axlsx_extensions.rb
106
+ - lib/axlsx_styler/version.rb
107
+ - test/array_test.rb
108
+ - test/cell_test.rb
109
+ - test/test_helper.rb
110
+ - test/workbook_test.rb
111
+ homepage: https://github.com/sakovias/axlsx_styler
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: This gem allows to separate data from styles when using Axlsx gem.
135
+ test_files:
136
+ - test/array_test.rb
137
+ - test/cell_test.rb
138
+ - test/test_helper.rb
139
+ - test/workbook_test.rb