axlsx_worksheet_builder 1.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.
- checksums.yaml +7 -0
- data/.tool-versions +1 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +21 -0
- data/README.md +73 -0
- data/Rakefile +12 -0
- data/lib/axlsx_worksheet_builder/builder.rb +126 -0
- data/lib/axlsx_worksheet_builder/version.rb +5 -0
- data/lib/axlsx_worksheet_builder.rb +34 -0
- data/sig/axlsx_worksheet_builder.rbs +4 -0
- metadata +63 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 86a36ede4b7181a6f615de7c8f104a3695513d59243bb028e03204cd14fb48f3
|
|
4
|
+
data.tar.gz: e150d4ed14a1637fcadebc8d8fb985374a101ea5267fb954b604872202c8b284
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a5a07760bee6385df39d3b2961848686fcafe298fff949d3d965592d814cc2ad96de4154b8e541afd4cade233bb9e4730544b5f0ba9f6ebe6ee689a4b60480f9
|
|
7
|
+
data.tar.gz: c313576179a829f9a5e42774098d8ce984610368094e585b3aae9a71805cc0a1cf28203eee01f55cce15064ce87d8d5b65ca86f1f0f7ca7fcc12d7c3ea4cde9f
|
data/.tool-versions
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby 4.0.1
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dreeven-oss
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# AxlsxWorksheetBuilder
|
|
2
|
+
|
|
3
|
+
Inspired from code originally written by [Samuel Trottier](https://github.com/SamuelTrottier).
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bundle add axlsx_worksheet_builder
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
gem install axlsx_worksheet_builder
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Add a simple table to an existing axlsx worksheet.
|
|
24
|
+
|
|
25
|
+
### Iterate through an array of hash
|
|
26
|
+
```ruby
|
|
27
|
+
authors = [
|
|
28
|
+
{name: "Alice", dob: "1959-01-02", books: [{title: "Book 1"}, {title: "Book 2"}]},
|
|
29
|
+
{name: "Bob", dob: "1962-03-04", books: [{title: "Book A"}]}
|
|
30
|
+
]
|
|
31
|
+
AxlsxWorksheetBuilder::build(sheet, authors) do |worksheet|
|
|
32
|
+
worksheet.add_column("Author name", property: :name)
|
|
33
|
+
worksheet.add_column("Year of birth") { |author| Date.parse(author[:dob])&.year }
|
|
34
|
+
worksheet.add_column("Number of books") { |author| author[:books]&.size || 0 }
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Iterate through an array of objects
|
|
39
|
+
```ruby
|
|
40
|
+
authors = [
|
|
41
|
+
Author.new(name: "Alice", dob: "1959-01-02"),
|
|
42
|
+
Author.new(name: "Bob", dob: "1959-01-02")
|
|
43
|
+
]
|
|
44
|
+
AxlsxWorksheetBuilder::build(sheet, authors) do |worksheet|
|
|
45
|
+
worksheet.add_column("Author name", property: :name)
|
|
46
|
+
worksheet.add_column("Year of birth") { |author| Date.parse(author.dob)&.year }
|
|
47
|
+
end
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Iterate through an Array property
|
|
51
|
+
```ruby
|
|
52
|
+
authors = [{name: "Martin", books: [{title: "Book 1"}, {title: "Book 2"}]}]
|
|
53
|
+
AxlsxWorksheetBuilder::build(sheet, authors) do |worksheet|
|
|
54
|
+
worksheet.iterate_through_property(:books)
|
|
55
|
+
worksheet.add_column("Author name", property: :name)
|
|
56
|
+
worksheet.add_column("Number of books") { |author| author.books.count }
|
|
57
|
+
worksheet.add_column("Book title") { |author, book| books.title }
|
|
58
|
+
end
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Development
|
|
62
|
+
|
|
63
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
64
|
+
|
|
65
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
66
|
+
|
|
67
|
+
## Contributing
|
|
68
|
+
|
|
69
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dreeven-oss/axlsx_worksheet_builder.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AxlsxWorksheetBuilder
|
|
4
|
+
##
|
|
5
|
+
# Builder instance
|
|
6
|
+
# @param
|
|
7
|
+
# worksheet [Axlsx::Worksheet] The worksheet to be built
|
|
8
|
+
class Builder
|
|
9
|
+
attr_accessor :column_headers, :value_getters, :column_types, :worksheet
|
|
10
|
+
|
|
11
|
+
def initialize(worksheet)
|
|
12
|
+
@column_headers = []
|
|
13
|
+
@value_getters = []
|
|
14
|
+
@iterated_properties = []
|
|
15
|
+
@column_types = []
|
|
16
|
+
@worksheet = worksheet
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
##
|
|
20
|
+
# Adds a new column to the worksheet
|
|
21
|
+
# @param
|
|
22
|
+
# column_header [String] The header of the column
|
|
23
|
+
# &bloc [Proc] A proc that will be called to get the value of the column for each row
|
|
24
|
+
# This proc will receive as argument each relevant object for the row, see iterate_through_properties
|
|
25
|
+
# property [Symbol] The property of the object to be added to the column for each row
|
|
26
|
+
# type [Symbol] The type for the column (:string, :float, :integer, :date, :time or :boolean)
|
|
27
|
+
# @example
|
|
28
|
+
# add_column("Author name", property: :name)
|
|
29
|
+
# add_column("Author name") { |author| author.name }
|
|
30
|
+
# add_column("Author name") do |author|
|
|
31
|
+
# if author.name.present?
|
|
32
|
+
# author.name
|
|
33
|
+
# else
|
|
34
|
+
# "Anonymous"
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
# # In case of a nested iteration(iterate_through_properties),
|
|
38
|
+
# # the values of the properties being iterated through get passed to the proc
|
|
39
|
+
# add_column("Book title") |author, book| { books.title }
|
|
40
|
+
def add_column(column_header, property: nil, type: nil, &block)
|
|
41
|
+
column_headers << column_header
|
|
42
|
+
column_types << type
|
|
43
|
+
value_getters << ({
|
|
44
|
+
getter_proc: block,
|
|
45
|
+
property: property
|
|
46
|
+
})
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def build_sheet(data)
|
|
50
|
+
worksheet.add_row column_headers
|
|
51
|
+
data.each do |entry|
|
|
52
|
+
add_entry_rows(entry, @iterated_properties, [entry])
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
##
|
|
57
|
+
# Iterates through the property of each object in data
|
|
58
|
+
# @param
|
|
59
|
+
# property [Symbol] The property of the object to be iterated through
|
|
60
|
+
# @example
|
|
61
|
+
# # Given a data representing a list of authors with their books:
|
|
62
|
+
# # [{name: "Martin", books: [{title: "Book 1"}, {title: "Book 2"}]}]
|
|
63
|
+
# # the following code would create a row for each book of each author
|
|
64
|
+
# iterate_through_properties(:books)
|
|
65
|
+
def iterate_through_property(property)
|
|
66
|
+
@iterated_properties = [property]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
##
|
|
70
|
+
# Iterates through the properties of each object in data
|
|
71
|
+
# The proc passed to add_column will receive as argument each objects being iterated through
|
|
72
|
+
# @param
|
|
73
|
+
# properties [Array<Symbol>] The properties of the object to be iterated through
|
|
74
|
+
# @example
|
|
75
|
+
# # Given a data representing a list of authors with their books and the chapter of each book:
|
|
76
|
+
# # [{name: "Martin", books: [{title: "Book 1", chapter: ["Chapter 1", "Chapter 2"]}]}]
|
|
77
|
+
# # the following code would create a row for each chapter of each book of each author
|
|
78
|
+
# iterate_through_properties(:books, :chapter)
|
|
79
|
+
# # In the previous case, the add_column proc would receive the following arguments:
|
|
80
|
+
# # (author, book, chapter)
|
|
81
|
+
def iterate_through_properties(*properties)
|
|
82
|
+
@iterated_properties = properties
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def add_entry_rows(entry, local_iterated_properties, precedent_entries)
|
|
88
|
+
properties = local_iterated_properties.clone
|
|
89
|
+
property = properties.shift
|
|
90
|
+
if property
|
|
91
|
+
process_entries(entry, property, precedent_entries, properties)
|
|
92
|
+
else
|
|
93
|
+
add_worksheet_row(entry, precedent_entries)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def process_entries(entry, property, precedent_entries, properties)
|
|
98
|
+
entries = entry_poperty(entry, property)
|
|
99
|
+
entries.each do |entry_property|
|
|
100
|
+
prec_entries = precedent_entries.clone
|
|
101
|
+
|
|
102
|
+
prec_entries.push(entry_property)
|
|
103
|
+
add_entry_rows(entry_property, properties, prec_entries)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def add_worksheet_row(entry, precedent_entries)
|
|
108
|
+
row = value_getters.map do |value_getter|
|
|
109
|
+
property = value_getter[:property]
|
|
110
|
+
if !property.nil?
|
|
111
|
+
entry_poperty(entry, property)
|
|
112
|
+
else
|
|
113
|
+
arguments = precedent_entries[0..(value_getter[:getter_proc].parameters.count - 1)]
|
|
114
|
+
value_getter[:getter_proc].call(*arguments)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
worksheet.add_row row, types: column_types
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def entry_poperty(entry, property)
|
|
121
|
+
entry.send(property)
|
|
122
|
+
rescue StandardError
|
|
123
|
+
entry[property]
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "axlsx"
|
|
4
|
+
require_relative "axlsx_worksheet_builder/version"
|
|
5
|
+
require_relative "axlsx_worksheet_builder/builder"
|
|
6
|
+
|
|
7
|
+
##
|
|
8
|
+
# Axlsx Worksheet Builder
|
|
9
|
+
module AxlsxWorksheetBuilder
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
class InvalidWorksheet < ArgumentError; end
|
|
12
|
+
|
|
13
|
+
##
|
|
14
|
+
# Builds the worksheet row, given the columns being added in the block
|
|
15
|
+
# @param
|
|
16
|
+
# worksheet [Axlsx::Worksheet] The worksheet to be built
|
|
17
|
+
# data [Array] The data to be added to the worksheet
|
|
18
|
+
# @example
|
|
19
|
+
# # The following code will build the worksheet with a row for each book of each author
|
|
20
|
+
# authors = [{name: "Martin", books: [{title: "Book 1"}, {title: "Book 2"}]}]
|
|
21
|
+
# AxlsxWorksheetBuilder::build(sheet, authors) do |worksheet|
|
|
22
|
+
# worksheet.iterate_through_property(:books)
|
|
23
|
+
# worksheet.add_column("Author name", property: :name)
|
|
24
|
+
# worksheet.add_column("Number of books") { |author| author.books.count }
|
|
25
|
+
# worksheet.add_column("Book title") { |author, book| books.title }
|
|
26
|
+
# end
|
|
27
|
+
def self.build(worksheet, data)
|
|
28
|
+
raise InvalidWorksheet, "Worksheet must be an Axlsx::Worksheet" unless worksheet.is_a?(Axlsx::Worksheet)
|
|
29
|
+
|
|
30
|
+
builder = Builder.new(worksheet)
|
|
31
|
+
yield builder
|
|
32
|
+
builder.build_sheet(data)
|
|
33
|
+
end
|
|
34
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: axlsx_worksheet_builder
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- drvn-eb
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: caxlsx
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: Simple axlsx worksheet generator for iterated data
|
|
27
|
+
executables: []
|
|
28
|
+
extensions: []
|
|
29
|
+
extra_rdoc_files: []
|
|
30
|
+
files:
|
|
31
|
+
- ".tool-versions"
|
|
32
|
+
- CHANGELOG.md
|
|
33
|
+
- LICENSE
|
|
34
|
+
- README.md
|
|
35
|
+
- Rakefile
|
|
36
|
+
- lib/axlsx_worksheet_builder.rb
|
|
37
|
+
- lib/axlsx_worksheet_builder/builder.rb
|
|
38
|
+
- lib/axlsx_worksheet_builder/version.rb
|
|
39
|
+
- sig/axlsx_worksheet_builder.rbs
|
|
40
|
+
homepage: https://github.com/dreeven-oss/axlsx_worksheet_builder
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
metadata:
|
|
44
|
+
homepage_uri: https://github.com/dreeven-oss/axlsx_worksheet_builder
|
|
45
|
+
source_code_uri: https://github.com/dreeven-oss/axlsx_worksheet_builder
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 3.2.0
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements: []
|
|
60
|
+
rubygems_version: 4.0.3
|
|
61
|
+
specification_version: 4
|
|
62
|
+
summary: Axlsx Worksheet Builder
|
|
63
|
+
test_files: []
|