rxl 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -7
- data/lib/rxl.rb +7 -1
- data/lib/rxl/version.rb +1 -1
- data/lib/workbook.rb +6 -0
- data/lib/worksheet.rb +29 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d71e3c242930b4fbd6c3e05572e51bf19231ea10483b6ca271dd42138e1959a
|
4
|
+
data.tar.gz: 14c3dfbf017573840b2c31595043f3b587e6550487c91a7de5395ff02282c0fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a64c63e260638a97ebfcc00270e8d4ca265a693fa6754be61d3307a26e27b863d9d04ef3bdd46ca908ac289102c4f964ac5ff4561dfb43f46f9578494467412d
|
7
|
+
data.tar.gz: 18d20fc11bd47c9d8f631eca7023f398ab2992817696dbd568a796cf23307b14c8be6929db0bc7c17b00e3f8c8641d5648791179fea9d7a142e7189eb7fa3aad
|
data/README.md
CHANGED
@@ -42,7 +42,7 @@ The format of the excel read hash has the following skeleton:
|
|
42
42
|
|
43
43
|
```ruby
|
44
44
|
{
|
45
|
-
|
45
|
+
'Sheet1' => {
|
46
46
|
row_count: 1,
|
47
47
|
column_count: 1,
|
48
48
|
rows: {},
|
@@ -88,7 +88,7 @@ The format of the excel table read hash has the following skeleton:
|
|
88
88
|
|
89
89
|
```ruby
|
90
90
|
{
|
91
|
-
|
91
|
+
'Sheet1' => [
|
92
92
|
{
|
93
93
|
header_a: value,
|
94
94
|
header_b: value
|
@@ -120,18 +120,20 @@ Returns the files with sheet contents populated with hash of cells (or array of
|
|
120
120
|
```ruby
|
121
121
|
{
|
122
122
|
first_file: {
|
123
|
-
|
124
|
-
|
123
|
+
'Sheet1' => 'sheet_contents',
|
124
|
+
'Sheet2' => 'sheet_contents'
|
125
125
|
},
|
126
126
|
second_file: {
|
127
|
-
|
128
|
-
|
127
|
+
'Sheet1' => 'sheet_contents',
|
128
|
+
'Sheet2' => 'sheet_contents'
|
129
129
|
},
|
130
130
|
}
|
131
131
|
```
|
132
132
|
|
133
133
|
### Write to file
|
134
134
|
|
135
|
+
Any exception is swallowed and returned.
|
136
|
+
|
135
137
|
To write a file pass the filename and hash:
|
136
138
|
|
137
139
|
```ruby
|
@@ -142,7 +144,7 @@ The format of the excel hash_workbook has sheet names as keys and hashes of cell
|
|
142
144
|
|
143
145
|
```ruby
|
144
146
|
{
|
145
|
-
|
147
|
+
'Sheet1' => {
|
146
148
|
'A1' => {
|
147
149
|
value: 'abc'
|
148
150
|
}
|
@@ -150,6 +152,39 @@ The format of the excel hash_workbook has sheet names as keys and hashes of cell
|
|
150
152
|
}
|
151
153
|
```
|
152
154
|
|
155
|
+
### Write to file as tables
|
156
|
+
|
157
|
+
Any exception is swallowed and returned.
|
158
|
+
|
159
|
+
To write a file as pass the filename and hash:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
Rxl.write_file_as_tables('path/to/save.xlsx', hash_tables, order)
|
163
|
+
```
|
164
|
+
|
165
|
+
The worsheets' top row will be populated with values specified in the `order` array. Those array items will also be used to extract the current row from the current hash.
|
166
|
+
|
167
|
+
* use `nil` in the `order` array to leave blank columns (including blank header)
|
168
|
+
* string or symbol keys can be used, so long as the key in order is the same as in the hashes
|
169
|
+
|
170
|
+
The format of the excel hash_workbook has sheet names as keys and hashes of rows as values:
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
order = %i[header_a header_b]
|
174
|
+
hash_tables = {
|
175
|
+
'Sheet1' => [
|
176
|
+
{
|
177
|
+
header_a: 'some_value',
|
178
|
+
header_b: 'other_value'
|
179
|
+
},
|
180
|
+
{
|
181
|
+
header_a: 'some_value',
|
182
|
+
header_b: 'other_value'
|
183
|
+
}
|
184
|
+
]
|
185
|
+
}
|
186
|
+
```
|
187
|
+
|
153
188
|
#### Cell specification
|
154
189
|
|
155
190
|
All cells are written with the format set to general except those with a number format specified
|
data/lib/rxl.rb
CHANGED
@@ -7,10 +7,16 @@ module Rxl
|
|
7
7
|
def self.write_file(filepath, hash_workbook)
|
8
8
|
begin
|
9
9
|
rubyxl_workbook = Workbook.hash_workbook_to_rubyxl_workbook(hash_workbook)
|
10
|
+
rubyxl_workbook.write(filepath)
|
11
|
+
nil
|
10
12
|
rescue => e
|
11
13
|
return e
|
12
14
|
end
|
13
|
-
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.write_file_as_tables(filepath, hash_tables, order, write_headers: true)
|
18
|
+
hash_workbook = Workbook.hashes_to_hash_workbook(hash_tables, order, write_headers: write_headers)
|
19
|
+
write_file(filepath, hash_workbook)
|
14
20
|
end
|
15
21
|
|
16
22
|
def self.read_file(filepath)
|
data/lib/rxl/version.rb
CHANGED
data/lib/workbook.rb
CHANGED
@@ -26,6 +26,12 @@ module Workbook
|
|
26
26
|
rubyxl_workbook
|
27
27
|
end
|
28
28
|
|
29
|
+
def self.hashes_to_hash_workbook(hash_tables, order, write_headers: true)
|
30
|
+
hash_workbook = {}
|
31
|
+
hash_tables.each { |k, v| hash_workbook[k] = Worksheet.hashes_to_hash_worksheet(v, order, write_headers: write_headers) }
|
32
|
+
hash_workbook
|
33
|
+
end
|
34
|
+
|
29
35
|
def self.hash_workbook_to_hash_tables(hash_workbook)
|
30
36
|
hash_workbook.keys.each_with_object({}) do |key, hash_tables|
|
31
37
|
hash_tables[key] = Worksheet.hash_worksheet_to_hash_table(hash_workbook[key])
|
data/lib/worksheet.rb
CHANGED
@@ -34,6 +34,29 @@ module Worksheet
|
|
34
34
|
end
|
35
35
|
|
36
36
|
|
37
|
+
################################################
|
38
|
+
### GET RUBYXL WORKSHEET FROM HASHES ###
|
39
|
+
################################################
|
40
|
+
|
41
|
+
def self.hashes_to_hash_worksheet(hashes, order, write_headers: true)
|
42
|
+
rows = hashes.map do |hash|
|
43
|
+
order.map { |item| hash[item] }
|
44
|
+
end
|
45
|
+
rows.unshift(order.map { |item| "#{item}" }) if write_headers
|
46
|
+
rows_to_hash_worksheet(rows)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.rows_to_hash_worksheet(rows)
|
50
|
+
rxl_worksheet = {}
|
51
|
+
rows.count.times do |i|
|
52
|
+
rows[i].each_with_index do |cell_value, index|
|
53
|
+
rxl_worksheet["#{column_name(index)}#{i + 1}"] = { value: cell_value }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
rxl_worksheet
|
57
|
+
end
|
58
|
+
|
59
|
+
|
37
60
|
##############################
|
38
61
|
### SHARED METHODS ###
|
39
62
|
##############################
|
@@ -89,4 +112,10 @@ module Worksheet
|
|
89
112
|
end
|
90
113
|
end
|
91
114
|
|
115
|
+
def self.column_name(int)
|
116
|
+
name = 'A'
|
117
|
+
int.times { name.succ! }
|
118
|
+
name
|
119
|
+
end
|
120
|
+
|
92
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rxl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian McWilliams
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|