rubyexcel 0.3.1 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +33 -1
- data/lib/rubyexcel/address.rb +33 -16
- data/lib/rubyexcel/data.rb +1 -0
- data/lib/rubyexcel/section.rb +28 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f035e4c22e84db021bc6552a241b2d21ccffb85f
|
4
|
+
data.tar.gz: 65b7f9a3afc852a6de257dae02a1a441b8118177
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b39e0df6ab2591a899252e8c1782dd6142241eabe7d199d2705c42bc3e02682fbcb7a72576666c09cffeda1c58ee6da1a005739219eed19a33d3586ed9f0c21
|
7
|
+
data.tar.gz: 3cbd1db9522e89b233b4b6856fd78a0a5b7d150a52c8f8396e6797c323a30293f799cded9869b40a48c8cee0dd32e6cdc580d18b4a34ea3fc4288971754994e7
|
data/README.md
CHANGED
@@ -39,7 +39,7 @@ Typical usage:
|
|
39
39
|
About
|
40
40
|
-----
|
41
41
|
|
42
|
-
This gem is designed as a way to conveniently edit table data before outputting it to
|
42
|
+
This gem is designed as a way to conveniently edit table data before outputting it to a variety of formats which Excel can interpret, including WIN32OLE Excel Workbooks.
|
43
43
|
It attempts to take as much as possible from Excel's API while providing some of the best bits of Ruby ( e.g. Enumerators, Blocks, Regexp ).
|
44
44
|
An important feature is allowing reference to Columns via their Headers for convenience and enhanced code readability.
|
45
45
|
As this works directly on the data, processing is faster than using Excel itself.
|
@@ -130,8 +130,16 @@ s.range( '1:2' ) #=> Range (Rows)
|
|
130
130
|
s.range( 'A1', 'B3' ) #=> Range
|
131
131
|
s.range( s.cell( 1, 1 ), s.cell( 3, 2 ) ) #=> Range
|
132
132
|
s.row( 1 ) #=> Row
|
133
|
+
s.row(1)[1] #=> Value
|
134
|
+
s.row(1)[1, 2] #=> Array
|
135
|
+
s.row(1)['A', 2] #=> Array
|
136
|
+
s.row(1)[1..2] #=> Array
|
137
|
+
s.row(1)['A'..'B'] #=> Array
|
133
138
|
s.column( 'A' ) #=> Column
|
134
139
|
s.column( 1 ) #=> Column
|
140
|
+
s.column(1)[1] #=> Value
|
141
|
+
s.column(1)[1, 2] #=> Array
|
142
|
+
s.column(1)[1..2] #=> Array
|
135
143
|
```
|
136
144
|
|
137
145
|
Using headers to reference the data
|
@@ -403,6 +411,30 @@ Row / Column (Section)
|
|
403
411
|
row = s.row(2)
|
404
412
|
col = s.column('B')
|
405
413
|
|
414
|
+
#Read a value
|
415
|
+
row[1] #=> "Type1"
|
416
|
+
row['A'] #=> "Type1"
|
417
|
+
col[1] #=> "Ref1"
|
418
|
+
|
419
|
+
#Read multiple values
|
420
|
+
row[1..2] #=> ["Type1", "QT1"]
|
421
|
+
row[1, 2] #=> ["Type1", "QT1"]
|
422
|
+
row['A', 2] #=> ["Type1", "QT1"]
|
423
|
+
col[1..2] #=> ["Ref1", "QT1"]
|
424
|
+
col[1, 2] #=> ["Ref1", "QT1"]
|
425
|
+
|
426
|
+
#Write a value
|
427
|
+
row[1] = "Type1"
|
428
|
+
row['A'] = "Type1"
|
429
|
+
col[1] = "Ref1"
|
430
|
+
|
431
|
+
#Write multiple values
|
432
|
+
row[1..2] = "Type1", "QT1"
|
433
|
+
row[1, 2] = "Type1", "QT1"
|
434
|
+
row['A', 2] = "Type1", "QT1"
|
435
|
+
col[1..2] = "Ref1", "QT1"
|
436
|
+
col[1, 2] = "Ref1", "QT1"
|
437
|
+
|
406
438
|
=begin
|
407
439
|
Append a value
|
408
440
|
Note: Only extends the data boundaries when at the first row or column.
|
data/lib/rubyexcel/address.rb
CHANGED
@@ -49,10 +49,10 @@ module RubyExcel
|
|
49
49
|
# @return [String] the column letter
|
50
50
|
#
|
51
51
|
|
52
|
-
def col_letter( index )
|
52
|
+
def col_letter( index, start='A' )
|
53
53
|
return index if index.is_a? String
|
54
54
|
index > 0 or fail ArgumentError, 'Indexing is 1-based'
|
55
|
-
a =
|
55
|
+
a = start.dup; ( index - 1 ).times { a.next! }; a
|
56
56
|
end
|
57
57
|
|
58
58
|
#
|
@@ -141,6 +141,34 @@ module RubyExcel
|
|
141
141
|
( col_letter( address_to_col_index( address ) + col ) ) + ( row_id( address ) + row ).to_s
|
142
142
|
end
|
143
143
|
|
144
|
+
#
|
145
|
+
# Translates an address to a row id
|
146
|
+
#
|
147
|
+
# @param [String] address the address to translate
|
148
|
+
# @return [Fixnum] the row id
|
149
|
+
#
|
150
|
+
|
151
|
+
def row_id( address )
|
152
|
+
address[/\d+/].to_i
|
153
|
+
end
|
154
|
+
|
155
|
+
#
|
156
|
+
# Step an index forward for an Array-style slice
|
157
|
+
#
|
158
|
+
# @param [Fixnum, String] start the index to start at
|
159
|
+
# @param [Fixnum] slice the amount to advance to (1 means keep the same index)
|
160
|
+
#
|
161
|
+
|
162
|
+
def step_index( start, slice )
|
163
|
+
if start.is_a?( Fixnum )
|
164
|
+
start + slice - 1
|
165
|
+
else
|
166
|
+
x = start.dup
|
167
|
+
( slice - 1 ).times { x.next! }
|
168
|
+
x
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
144
172
|
#
|
145
173
|
# Translates two objects to a range address
|
146
174
|
#
|
@@ -154,18 +182,7 @@ module RubyExcel
|
|
154
182
|
addr << ':' + ( obj2.respond_to?( :address ) ? obj2.address : obj2.to_s ) if obj2
|
155
183
|
addr
|
156
184
|
end
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
#
|
161
|
-
# @param [String] address the address to translate
|
162
|
-
# @return [Fixnum] the row id
|
163
|
-
#
|
164
|
-
|
165
|
-
def row_id( address )
|
166
|
-
address[/\d+/].to_i
|
167
|
-
end
|
168
|
-
|
169
|
-
end
|
170
|
-
|
185
|
+
|
186
|
+
end
|
187
|
+
|
171
188
|
end
|
data/lib/rubyexcel/data.rb
CHANGED
data/lib/rubyexcel/section.rb
CHANGED
@@ -148,11 +148,20 @@ module RubyExcel
|
|
148
148
|
#
|
149
149
|
# Read a value by address
|
150
150
|
#
|
151
|
-
# @param [String, Fixnum]
|
151
|
+
# @param [String, Fixnum, ::Range] start an index or Range of indices.
|
152
|
+
# @param [Fixnum] slice if the first argument is an index, how many cells to read.
|
152
153
|
#
|
153
154
|
|
154
|
-
def read(
|
155
|
-
|
155
|
+
def read( start, slice=nil )
|
156
|
+
if slice
|
157
|
+
( start..( step_index( start, slice ) ) ).map { |n| data[ translate_address( n ) ] }
|
158
|
+
else
|
159
|
+
if start.is_a?( ::Range ) # Standard Ruby Range
|
160
|
+
start.map { |n| data[ translate_address( n ) ] }
|
161
|
+
else # Single value
|
162
|
+
data[ translate_address( start ) ]
|
163
|
+
end
|
164
|
+
end
|
156
165
|
end
|
157
166
|
alias [] read
|
158
167
|
|
@@ -178,17 +187,24 @@ module RubyExcel
|
|
178
187
|
#
|
179
188
|
# Write a value by address
|
180
189
|
#
|
181
|
-
# @param [String, Fixnum]
|
182
|
-
# @param [Object] val the object to place at the address
|
190
|
+
# @param [Array<String, Fixnum, ::Range, Object>] args the address to write the data to, and the data to write.
|
183
191
|
#
|
184
192
|
|
185
|
-
def write(
|
186
|
-
|
187
|
-
|
193
|
+
def write( *args )
|
194
|
+
val = args.pop
|
195
|
+
if args.length == 1
|
196
|
+
if args[0].is_a?( ::Range ) # Standard Ruby Range
|
197
|
+
sheet.range( to_range_address( translate_address( args[0].first ), translate_address( args[0].last ) ) ).value = val
|
198
|
+
else # Single value
|
199
|
+
data[ translate_address( args[0] ) ] = val
|
200
|
+
end
|
201
|
+
else # Slice
|
202
|
+
sheet.range( to_range_address( translate_address( args[0] ), translate_address( step_index( args[0], args[1] ) ) ) ).value = val
|
203
|
+
end
|
188
204
|
end
|
189
205
|
alias []= write
|
190
206
|
|
191
|
-
end
|
207
|
+
end # Section
|
192
208
|
|
193
209
|
#
|
194
210
|
# A Row in the Sheet
|
@@ -296,7 +312,7 @@ module RubyExcel
|
|
296
312
|
col_letter( addr ) + idx.to_s
|
297
313
|
end
|
298
314
|
|
299
|
-
end
|
315
|
+
end # Row
|
300
316
|
|
301
317
|
#
|
302
318
|
# A Column in the Sheet
|
@@ -356,6 +372,6 @@ module RubyExcel
|
|
356
372
|
idx + addr
|
357
373
|
end
|
358
374
|
|
359
|
-
end
|
375
|
+
end # Column
|
360
376
|
|
361
|
-
end
|
377
|
+
end # RubyExcel
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyexcel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Pearson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A tabular data structure in Ruby, with header-based helper methods for
|
14
14
|
analysis and editing, and some of Excel's API style. Can output as 2D Array, HTML,
|