rubyexcel 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 936bd8860beb086b6066d2221d910730e6b1c7d6
4
- data.tar.gz: be5e2a1ed1f5c2fea3229621ee92bc650aab6db2
3
+ metadata.gz: f035e4c22e84db021bc6552a241b2d21ccffb85f
4
+ data.tar.gz: 65b7f9a3afc852a6de257dae02a1a441b8118177
5
5
  SHA512:
6
- metadata.gz: 09f390f15338d999b1611890cf83afc7edc5c1e17b684b8406f26d19ef6d8ebb1d23509098eeb584fb8ea07fba38ae05ae37dd827454d039c3003c4ba40f7bdd
7
- data.tar.gz: 92209b9dd520c03d3785a7090ae1a887985606fd9146772e3cad71edee663ed965000fa1de60d5df78dc0ab2c05cd61ef3a3eb46df7890c7ee9978c8791e7ca8
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 Excel (XLSX) or TSV format (which Excel can interpret).
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.
@@ -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 = 'A'; ( index - 1 ).times { a.next! }; 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
- # Translates an address to a row id
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
@@ -343,6 +343,7 @@ require_relative 'address.rb'
343
343
 
344
344
  def read( addr )
345
345
  row_idx, col_idx = address_to_indices( addr )
346
+ return nil if row_idx > rows
346
347
  @data[ row_idx-1 ][ col_idx-1 ]
347
348
  end
348
349
  alias [] read
@@ -148,11 +148,20 @@ module RubyExcel
148
148
  #
149
149
  # Read a value by address
150
150
  #
151
- # @param [String, Fixnum] id the index or reference of the required value
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( id )
155
- data[ translate_address( id ) ]
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] id the index or reference to write to
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( id, val )
186
-
187
- data[ translate_address( id ) ] = val
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.1
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-12 00:00:00.000000000 Z
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,