rubyexcel 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rubyexcel/element.rb +89 -41
- data/lib/rubyexcel/section.rb +7 -7
- data/lib/rubyexcel/sheet.rb +9 -8
- metadata +2 -2
data/lib/rubyexcel/element.rb
CHANGED
@@ -32,7 +32,6 @@ module RubyExcel
|
|
32
32
|
#
|
33
33
|
|
34
34
|
def initialize( sheet, addr )
|
35
|
-
fail ArgumentError, "Invalid range: #{ addr }" unless addr =~ /\A[A-Z]{1,3}\d+:[A-Z]{1,3}\d+\z|\A[A-Z]{1,3}\d+\z|\A[A-Z]{1,3}:[A-Z]{1,3}\z|\A\d+:\d+\z/
|
36
35
|
@sheet = sheet
|
37
36
|
@data = sheet.data
|
38
37
|
@address = addr
|
@@ -63,7 +62,7 @@ module RubyExcel
|
|
63
62
|
|
64
63
|
def each_cell
|
65
64
|
return to_enum( :each_cell ) unless block_given?
|
66
|
-
expand( address ).flatten.each { |addr| yield
|
65
|
+
expand( address ).flatten.each { |addr| yield Cell.new( sheet, addr ) }
|
67
66
|
end
|
68
67
|
|
69
68
|
#
|
@@ -77,11 +76,11 @@ module RubyExcel
|
|
77
76
|
#
|
78
77
|
# Return the first cell in the Range
|
79
78
|
#
|
80
|
-
# @return [RubyExcel::
|
79
|
+
# @return [RubyExcel::Cell]
|
81
80
|
#
|
82
81
|
|
83
82
|
def first_cell
|
84
|
-
|
83
|
+
Cell.new( sheet, expand( address ).flatten.first )
|
85
84
|
end
|
86
85
|
|
87
86
|
#
|
@@ -89,17 +88,17 @@ module RubyExcel
|
|
89
88
|
#
|
90
89
|
|
91
90
|
def inspect
|
92
|
-
"#{ self.class }:0x#{ '%x' % ( object_id << 1 ) }: #{ address }"
|
91
|
+
"#{ self.class }:0x#{ '%x' % ( object_id << 1 ) }: '#{ address }'"
|
93
92
|
end
|
94
93
|
|
95
94
|
#
|
96
95
|
# Return the last cell in the Range
|
97
96
|
#
|
98
|
-
# @return [RubyExcel::
|
97
|
+
# @return [RubyExcel::Cell]
|
99
98
|
#
|
100
99
|
|
101
100
|
def last_cell
|
102
|
-
|
101
|
+
Cell.new( sheet, expand( address ).flatten.last )
|
103
102
|
end
|
104
103
|
|
105
104
|
#
|
@@ -111,58 +110,105 @@ module RubyExcel
|
|
111
110
|
expand( address ).flatten.each { |addr| data[ addr ] = yield data[ addr ] }
|
112
111
|
end
|
113
112
|
|
113
|
+
end
|
114
|
+
|
115
|
+
#
|
116
|
+
# A single Cell
|
117
|
+
#
|
118
|
+
|
119
|
+
class Cell < Element
|
120
|
+
|
121
|
+
def initialize( sheet, addr )
|
122
|
+
fail ArgumentError, "Invalid Cell address: #{ addr }" unless addr =~ /\A[A-Z]{1,3}\d+\z/
|
123
|
+
super
|
124
|
+
end
|
125
|
+
|
114
126
|
#
|
115
|
-
# Return the value at this
|
127
|
+
# Return the value at this Cell's address
|
116
128
|
#
|
117
|
-
# @return [Object
|
129
|
+
# @return [Object ] the Object within the data, referenced by the address
|
118
130
|
#
|
119
131
|
|
120
132
|
def value
|
121
|
-
|
133
|
+
data[ address ]
|
122
134
|
end
|
123
135
|
|
124
136
|
#
|
125
|
-
# Set the value at this
|
137
|
+
# Set the value at this Cell's address
|
138
|
+
#
|
139
|
+
# @param [Object] val the Object to write into the data
|
140
|
+
#
|
141
|
+
|
142
|
+
def value=( val )
|
143
|
+
data[ address ] = val
|
144
|
+
end
|
145
|
+
|
146
|
+
#
|
147
|
+
# The data at address as a String
|
148
|
+
#
|
149
|
+
|
150
|
+
def to_s
|
151
|
+
val.to_s
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
#
|
157
|
+
# A Range of Cells
|
158
|
+
#
|
159
|
+
|
160
|
+
class Range < Element
|
161
|
+
|
162
|
+
def initialize( sheet, addr )
|
163
|
+
fail ArgumentError, "Invalid Range address: #{ addr }" unless addr =~ /\A[A-Z]{1,3}\d+:[A-Z]{1,3}\d+\z|\A[A-Z]{1,3}:[A-Z]{1,3}\z|\A\d+:\d+\z/
|
164
|
+
super
|
165
|
+
end
|
166
|
+
|
167
|
+
#
|
168
|
+
# Return the value at this Range's address
|
169
|
+
#
|
170
|
+
# @return [Array<Object>] the Array of Objects within the data, referenced by the address
|
171
|
+
#
|
172
|
+
|
173
|
+
def value
|
174
|
+
expand( address ).map { |ar| ar.map { |addr| data[ addr ] } }
|
175
|
+
end
|
176
|
+
|
177
|
+
#
|
178
|
+
# Set the value at this Range's address
|
126
179
|
#
|
127
180
|
# @param [Object, Array<Object>] val the Object or Array of Objects to write into the data
|
128
181
|
#
|
129
182
|
|
130
183
|
def value=( val )
|
131
184
|
|
132
|
-
|
133
|
-
|
185
|
+
addresses = expand( address )
|
186
|
+
|
187
|
+
# 2D Array of Values
|
188
|
+
if multi_array?( val ) && addresses.length > 1
|
134
189
|
|
135
|
-
|
190
|
+
# Check the dimensions
|
191
|
+
val_rows, val_cols, range_rows, range_cols = val.length, val.max_by(&:length).length, addresses.length, addresses.max_by(&:length).length
|
192
|
+
val_rows == range_rows && val_cols == range_cols or fail ArgumentError, "Dimension mismatch! Value - rows: #{val_rows}, columns: #{ val_cols }. Range - rows: #{ range_rows }, columns: #{ range_cols }"
|
136
193
|
|
137
|
-
#
|
138
|
-
|
194
|
+
# Write the values in order
|
195
|
+
addresses.each_with_index { |row,idx| row.each_with_index { |el,i| data[el] = val[idx][i] } }
|
139
196
|
|
140
|
-
|
141
|
-
|
142
|
-
val_rows == range_rows && val_cols == range_cols or fail ArgumentError, "Dimension mismatch! Value - rows: #{val_rows}, columns: #{ val_cols }. Range - rows: #{ range_rows }, columns: #{ range_cols }"
|
143
|
-
|
144
|
-
#Write the values in order
|
145
|
-
addresses.each_with_index { |row,idx| row.each_with_index { |el,i| data[el] = val[idx][i] } }
|
146
|
-
|
147
|
-
# Array of Values
|
148
|
-
elsif val.is_a?( Array )
|
149
|
-
|
150
|
-
addresses.flatten.each_with_index { |addr, i| data[addr] = val[i] }
|
151
|
-
|
152
|
-
# Single Value
|
153
|
-
else
|
197
|
+
# Array of Values
|
198
|
+
elsif val.is_a?( Array )
|
154
199
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
#Cell
|
200
|
+
# Write the values in order
|
201
|
+
addresses.flatten.each_with_index { |addr, i| data[addr] = val[i] }
|
202
|
+
|
203
|
+
# Single Value
|
161
204
|
else
|
162
|
-
data[ address ] = val
|
163
|
-
end
|
164
205
|
|
165
|
-
|
206
|
+
# Write the same value to every cell in the Range
|
207
|
+
addresses.each { |ar| ar.each { |addr| data[ addr ] = val } }
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
val
|
166
212
|
end
|
167
213
|
|
168
214
|
#
|
@@ -170,9 +216,11 @@ module RubyExcel
|
|
170
216
|
#
|
171
217
|
|
172
218
|
def to_s
|
173
|
-
value.
|
219
|
+
value.map { |ar| ar.join "\t" }.join($/)
|
174
220
|
end
|
175
221
|
|
222
|
+
|
176
223
|
end
|
224
|
+
|
225
|
+
end
|
177
226
|
|
178
|
-
end
|
data/lib/rubyexcel/section.rb
CHANGED
@@ -43,7 +43,7 @@ module RubyExcel
|
|
43
43
|
#
|
44
44
|
|
45
45
|
def cell( ref )
|
46
|
-
|
46
|
+
Cell.new( sheet, translate_address( ref ) )
|
47
47
|
end
|
48
48
|
|
49
49
|
#
|
@@ -79,7 +79,7 @@ module RubyExcel
|
|
79
79
|
|
80
80
|
def each_cell
|
81
81
|
return to_enum( :each_cell ) unless block_given?
|
82
|
-
each_address { |addr| yield
|
82
|
+
each_address { |addr| yield Cell.new( sheet, addr ) }
|
83
83
|
end
|
84
84
|
|
85
85
|
#
|
@@ -88,7 +88,7 @@ module RubyExcel
|
|
88
88
|
|
89
89
|
def each_cell_without_headers
|
90
90
|
return to_enum( :each_cell_without_headers ) unless block_given?
|
91
|
-
each_address( false ) { |addr| yield
|
91
|
+
each_address( false ) { |addr| yield Cell.new( sheet, addr ) }
|
92
92
|
end
|
93
93
|
alias each_cell_wh each_cell_without_headers
|
94
94
|
|
@@ -123,11 +123,11 @@ module RubyExcel
|
|
123
123
|
#
|
124
124
|
# Return the last cell
|
125
125
|
#
|
126
|
-
# @return [RubyExcel::
|
126
|
+
# @return [RubyExcel::Cell]
|
127
127
|
#
|
128
128
|
|
129
129
|
def last_cell
|
130
|
-
|
130
|
+
Cell.new( sheet, each_address.to_a.last )
|
131
131
|
end
|
132
132
|
|
133
133
|
#
|
@@ -215,10 +215,10 @@ module RubyExcel
|
|
215
215
|
end
|
216
216
|
|
217
217
|
#
|
218
|
-
# Access a
|
218
|
+
# Access a Cell by its header
|
219
219
|
#
|
220
220
|
# @param [String] header the header to search for
|
221
|
-
# @return [RubyExcel::
|
221
|
+
# @return [RubyExcel::Cell] the cell
|
222
222
|
#
|
223
223
|
|
224
224
|
def cell_by_header( header )
|
data/lib/rubyexcel/sheet.rb
CHANGED
@@ -133,16 +133,16 @@ module RubyExcel
|
|
133
133
|
end
|
134
134
|
|
135
135
|
#
|
136
|
-
# Access an
|
136
|
+
# Access an Cell by indices.
|
137
137
|
#
|
138
138
|
# @param [Fixnum] row the row index
|
139
139
|
# @param [Fixnum] col the column index
|
140
|
-
# @return [RubyExcel::
|
140
|
+
# @return [RubyExcel::Cell]
|
141
141
|
# @note Indexing is 1-based like Excel VBA
|
142
142
|
#
|
143
143
|
|
144
144
|
def cell( row, col )
|
145
|
-
|
145
|
+
Cell.new( self, indices_to_address( row, col ) )
|
146
146
|
end
|
147
147
|
alias cells cell
|
148
148
|
|
@@ -397,11 +397,11 @@ module RubyExcel
|
|
397
397
|
end
|
398
398
|
|
399
399
|
#
|
400
|
-
# Access
|
400
|
+
# Access a Range by address.
|
401
401
|
#
|
402
|
-
# @param [String,
|
403
|
-
# @param [String,
|
404
|
-
# @return [RubyExcel::
|
402
|
+
# @param [String, Cell, Range] first_cell the first Cell or Address in the Range
|
403
|
+
# @param [String, Cell, Range] last_cell the last Cell or Address in the Range
|
404
|
+
# @return [RubyExcel::Range]
|
405
405
|
# @note These are all valid arguments:
|
406
406
|
# ('A1')
|
407
407
|
# ('A1:B2')
|
@@ -413,7 +413,8 @@ module RubyExcel
|
|
413
413
|
#
|
414
414
|
|
415
415
|
def range( first_cell, last_cell=nil )
|
416
|
-
|
416
|
+
addr = to_range_address( first_cell, last_cell )
|
417
|
+
addr.include?(':') ? Range.new( self, addr ) : Cell.new( self, addr )
|
417
418
|
end
|
418
419
|
|
419
420
|
#
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyexcel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tabular data structure in Ruby, with header-based helper methods and
|
15
15
|
some of Excel's API style. Designed for Windows + Excel.
|