lazy-wombat 0.0.2
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.
- data/.gitignore +18 -0
- data/.travis.yml +4 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +96 -0
- data/Rakefile +1 -0
- data/features/generate_spreadsheet.feature +14 -0
- data/features/step_definitions/generate_spreadsheet_steps.rb +44 -0
- data/features/support/env.rb +4 -0
- data/lazy-wombat.gemspec +24 -0
- data/lib/lazy-wombat.rb +143 -0
- data/lib/lazy-wombat/version.rb +3 -0
- metadata +132 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Alexander Kaupanin
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# LazyWombat 
|
2
|
+
|
3
|
+
A simple yet powerful DSL to generate Excel spreadsheets built on top of [axlsx](https://github.com/randym/axlsx) gem.
|
4
|
+
|
5
|
+

|
6
|
+
|
7
|
+
## Why Lazy Wombat?
|
8
|
+
Axlsx is awesome and quite complex in usage.
|
9
|
+
Often you need something simple and easy-to-use to generate an excel spreadsheet as easy, as you markup tables with HTML.
|
10
|
+
Now you can.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'lazy-wombat'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install lazy-wombat
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### Basic sample: no styles, just content
|
29
|
+
|
30
|
+
Code
|
31
|
+
```ruby
|
32
|
+
LazyWombat::Xlsx.new do
|
33
|
+
spreadsheet do
|
34
|
+
row do
|
35
|
+
cell 'Cyberdyne Systems'
|
36
|
+
cell 'Model 101'
|
37
|
+
cell 'The Terminator'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end.save 'my_laziness.xlsx'
|
41
|
+
```
|
42
|
+
Or shortened:
|
43
|
+
```ruby
|
44
|
+
LazyWombat::Xlsx.new do
|
45
|
+
cell 'Cyberdyne Systems'
|
46
|
+
cell 'Model 101'
|
47
|
+
cell 'The Terminator'
|
48
|
+
end.save 'my_laziness.xlsx'
|
49
|
+
```
|
50
|
+
will create `my_laziness.xlsx` spreadsheet looks like this:
|
51
|
+
|
52
|
+

|
53
|
+
|
54
|
+
Since spreadsheet elements inheritance is alike `spreadsheet -> row -> cell`, you can arbitrary omit every unnecessary
|
55
|
+
elder element of your spreadsheets.
|
56
|
+
|
57
|
+
### Where is my HTML??
|
58
|
+
Oh yeah, I promised you `as you markup tables with HTML`: there're logic aliases:
|
59
|
+
`spreadsheet` is `table`, `row` is `tr`, `cell` is, of course, `td`
|
60
|
+
|
61
|
+
Thus here's our simplest example:
|
62
|
+
```ruby
|
63
|
+
LazyWombat::Xlsx.new do
|
64
|
+
table do
|
65
|
+
tr do
|
66
|
+
td 'Cyberdyne Systems'
|
67
|
+
td 'Model 101'
|
68
|
+
td 'The Terminator'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end.save 'my_laziness.xlsx'
|
72
|
+
```
|
73
|
+
|
74
|
+
### Additional options: spreadsheet names, rows and cells styles
|
75
|
+
By default spreadsheets are named as `Sheet 1`, `Sheet 2`, etc. It can be overwritten using `name` option.
|
76
|
+
```ruby
|
77
|
+
LazyWombat::Xlsx.new do
|
78
|
+
spreadsheet name: 'My Laziness' do
|
79
|
+
row style: :bold do
|
80
|
+
cell 'Cyberdyne Systems'
|
81
|
+
cell 'Model 101', style: :italic
|
82
|
+
cell 'The Terminator'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end.save 'my_laziness.xlsx'
|
86
|
+
```
|
87
|
+

|
88
|
+
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
1. Fork it
|
93
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
94
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
95
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
96
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Feature: As a lazy wombat I should be able to generate an xlsx spreadsheet
|
2
|
+
|
3
|
+
Scenario: I generate spreadsheet and save it to file
|
4
|
+
Given I generate following spreadsheet:
|
5
|
+
| Arnold | Schwarzenegger | Cyberdyne Systems Model 101 | The Terminator | 1 |
|
6
|
+
| Wombat | Animal | Universal | Optimum Nutrition | 001 |
|
7
|
+
| Hamster | Mole | Apartments | Valuation | Mockups |
|
8
|
+
And save it as "my_spreadsheet.xlsx"
|
9
|
+
Then "my_spreadsheet.xlsx" file should exist
|
10
|
+
And "my_spreadsheet.xlsx" should have 1 spreadsheet
|
11
|
+
And "my_spreadsheet.xlsx" should look like this:
|
12
|
+
| Arnold | Schwarzenegger | Cyberdyne Systems Model 101 | The Terminator | <1.0> |
|
13
|
+
| Wombat | Animal | Universal | Optimum Nutrition | <1.0> |
|
14
|
+
| Hamster | Mole | Apartments | Valuation | Mockups |
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'roo'
|
2
|
+
|
3
|
+
Given /^I generate following spreadsheet:$/ do |table|
|
4
|
+
@excel = LazyWombat::Xlsx.new do
|
5
|
+
row do
|
6
|
+
table.hashes.first.keys.each { |key| cell key }
|
7
|
+
end
|
8
|
+
table.hashes.each do |hash|
|
9
|
+
row do
|
10
|
+
hash.values.each { |value| cell value }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Given /^save it as "(.*?)"$/ do |file_name|
|
17
|
+
@excel.save file_name
|
18
|
+
end
|
19
|
+
|
20
|
+
Then /^"(.*?)" file should exist$/ do |file_name|
|
21
|
+
File.exists? file_name
|
22
|
+
end
|
23
|
+
|
24
|
+
Then /^"(.*?)" should have (\d+) spreadsheet$/ do |file_name, count|
|
25
|
+
Excelx.new(file_name).sheets.should have(count).records
|
26
|
+
end
|
27
|
+
|
28
|
+
# gem 'roo' casts all read numbers to float
|
29
|
+
Then /^"(.*?)" should look like this:$/ do |file_name, table|
|
30
|
+
workbook = Excelx.new file_name
|
31
|
+
prototype = table.raw
|
32
|
+
columns_count = prototype.first.count
|
33
|
+
1.upto(workbook.last_row) do |row_number|
|
34
|
+
row = columns_count.times.map { |column| workbook.cell row_number, column + 1 }
|
35
|
+
row.should == row_with_types(prototype[ row_number - 1 ])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def row_with_types row
|
40
|
+
return if row.nil?
|
41
|
+
row.each_with_index do |value, index|
|
42
|
+
row[index] = (value =~ /\<(.*)\>/) ? eval(value[1..-2]) : value
|
43
|
+
end
|
44
|
+
end
|
data/lazy-wombat.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lazy-wombat/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'lazy-wombat'
|
8
|
+
s.version = LazyWombat::VERSION
|
9
|
+
s.authors = ['Alexander Kaupanin']
|
10
|
+
s.email = %w(kaupanin@gmail.com)
|
11
|
+
s.description = %q{A simple yet powerful DSL to create Excel spreadsheets built on top of axlsx gem}
|
12
|
+
s.summary = %q{A simple yet powerful DSL to create Excel spreadsheets built on top of axlsx gem}
|
13
|
+
s.homepage = 'http://github.com/simsalabim/lazy-wombat'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split($/)
|
16
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
18
|
+
s.require_paths = %w(lib)
|
19
|
+
|
20
|
+
s.add_development_dependency 'cucumber', '~> 1.1'
|
21
|
+
s.add_development_dependency 'rspec', '~> 2.9'
|
22
|
+
s.add_development_dependency 'roo'
|
23
|
+
s.add_runtime_dependency 'axlsx'
|
24
|
+
end
|
data/lib/lazy-wombat.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'lazy-wombat/version'
|
3
|
+
require 'axlsx'
|
4
|
+
|
5
|
+
module LazyWombat
|
6
|
+
|
7
|
+
class Xlsx
|
8
|
+
|
9
|
+
def initialize options = {}, &block
|
10
|
+
@package_options = options
|
11
|
+
@package = Axlsx::Package.new
|
12
|
+
instance_exec &block
|
13
|
+
end
|
14
|
+
|
15
|
+
def spreadsheet options = {}, &block
|
16
|
+
if block_given?
|
17
|
+
@package.workbook.add_worksheet do |spreadsheet|
|
18
|
+
@spreadsheet = spreadsheet
|
19
|
+
@spreadsheet.name = options[:name] unless options[:name].respond_to?(:empty?) ? options[:name].empty? : !options[:name]
|
20
|
+
build_spreadsheet_styles
|
21
|
+
instance_exec &block
|
22
|
+
end
|
23
|
+
else
|
24
|
+
build_spreadsheet
|
25
|
+
end
|
26
|
+
end
|
27
|
+
alias_method :table, :spreadsheet
|
28
|
+
|
29
|
+
def build_spreadsheet
|
30
|
+
@spreadsheet ||= @package.workbook.add_worksheet.tap { @row = @cell = nil }
|
31
|
+
end
|
32
|
+
|
33
|
+
def row options = {}, &block
|
34
|
+
if block_given?
|
35
|
+
@row_options = options
|
36
|
+
spreadsheet.add_row do |row|
|
37
|
+
@row = row
|
38
|
+
instance_exec &block
|
39
|
+
end
|
40
|
+
else
|
41
|
+
build_row
|
42
|
+
end
|
43
|
+
end
|
44
|
+
alias_method :tr, :row
|
45
|
+
|
46
|
+
def build_row
|
47
|
+
@row ||= spreadsheet.add_row.tap{ @cell = nil }
|
48
|
+
end
|
49
|
+
|
50
|
+
def cell value, options = {}
|
51
|
+
unless @row_options.respond_to?(:empty?) ? @row_options.empty? : !@row_options
|
52
|
+
options = @row_options.merge(options){ |key, row_option, cell_option| Array.new << row_option << cell_option }
|
53
|
+
end
|
54
|
+
@cell = row.add_cell value.to_s, style: style_from_options(options)
|
55
|
+
unless options[:colspan].respond_to?(:empty?) ? options[:colspan].empty? : !options[:colspan]
|
56
|
+
spreadsheet.merge_cells "#{pos[:x]}#{pos[:y]}:#{shift_x(pos[:x], 5)}#{pos[:y]}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
alias_method :td, :cell
|
60
|
+
|
61
|
+
def save file_name
|
62
|
+
@package.serialize file_name
|
63
|
+
File.open file_name
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_temp_file
|
67
|
+
stream = @package.to_stream
|
68
|
+
Tempfile.new(%w(temporary-workbook .xlsx), encoding: 'utf-8').tap do |file|
|
69
|
+
file.write stream.read
|
70
|
+
file.close
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_xml
|
75
|
+
spreadsheet.to_xml_string
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def pos
|
81
|
+
{ x: x_axis[@cell.pos[0]], y: y_axis[@cell.pos[1]] }
|
82
|
+
end
|
83
|
+
|
84
|
+
def shift_x x, diff
|
85
|
+
x_axis[x_axis.index(x) + diff]
|
86
|
+
end
|
87
|
+
|
88
|
+
def style_from_options options
|
89
|
+
if options[:style].is_a? Array
|
90
|
+
add_complex_style options[:style]
|
91
|
+
end
|
92
|
+
spreadsheet_style_by_name style_name(options[:style])
|
93
|
+
end
|
94
|
+
|
95
|
+
def style_name style_keys
|
96
|
+
Array(style_keys).join('_').to_sym
|
97
|
+
end
|
98
|
+
|
99
|
+
def add_complex_style style_keys
|
100
|
+
complex_option_args = {}
|
101
|
+
style_keys.map{ |style_key| style_builder_args[style_key] }.compact.map{ |args| complex_option_args.merge! args }
|
102
|
+
@style_builder_args[style_name(style_keys)] = complex_option_args
|
103
|
+
rebuild_spreadsheet_styles
|
104
|
+
end
|
105
|
+
|
106
|
+
def style_builder_args
|
107
|
+
@style_builder_args ||= {
|
108
|
+
center: { alignment: { horizontal: :center } },
|
109
|
+
bold: { b: true },
|
110
|
+
italic: { i: true }
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
def build_spreadsheet_styles
|
115
|
+
@spreadsheet_styles = {}
|
116
|
+
spreadsheet.workbook.styles do |style_builder|
|
117
|
+
style_builder_args.each do |style_name, style_args|
|
118
|
+
@spreadsheet_styles[style_name] = style_builder.add_style style_args
|
119
|
+
end
|
120
|
+
end
|
121
|
+
@spreadsheet_styles
|
122
|
+
end
|
123
|
+
alias_method :rebuild_spreadsheet_styles, :build_spreadsheet_styles
|
124
|
+
|
125
|
+
def spreadsheet_styles
|
126
|
+
@spreadsheet_styles ||= build_spreadsheet_styles
|
127
|
+
end
|
128
|
+
|
129
|
+
def spreadsheet_style_by_name name
|
130
|
+
spreadsheet_styles[name]
|
131
|
+
end
|
132
|
+
|
133
|
+
# should be dynamic but for now to avoid calculations is static
|
134
|
+
def x_axis
|
135
|
+
('A'..'Z').to_a
|
136
|
+
end
|
137
|
+
|
138
|
+
# should be dynamic but for now to avoid calculations is static
|
139
|
+
def y_axis
|
140
|
+
(1..1000).to_a
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy-wombat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander Kaupanin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: cucumber
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.1'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.1'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.9'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.9'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: roo
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: axlsx
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: A simple yet powerful DSL to create Excel spreadsheets built on top of
|
79
|
+
axlsx gem
|
80
|
+
email:
|
81
|
+
- kaupanin@gmail.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- .travis.yml
|
88
|
+
- Gemfile
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- features/generate_spreadsheet.feature
|
93
|
+
- features/step_definitions/generate_spreadsheet_steps.rb
|
94
|
+
- features/support/env.rb
|
95
|
+
- lazy-wombat.gemspec
|
96
|
+
- lib/lazy-wombat.rb
|
97
|
+
- lib/lazy-wombat/version.rb
|
98
|
+
homepage: http://github.com/simsalabim/lazy-wombat
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
hash: -126332881
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
hash: -126332881
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.8.24
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: A simple yet powerful DSL to create Excel spreadsheets built on top of axlsx
|
128
|
+
gem
|
129
|
+
test_files:
|
130
|
+
- features/generate_spreadsheet.feature
|
131
|
+
- features/step_definitions/generate_spreadsheet_steps.rb
|
132
|
+
- features/support/env.rb
|