rubyexcel 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/README.md +26 -4
- data/lib/rubyexcel/data.rb +2 -2
- data/lib/rubyexcel/element.rb +71 -46
- data/lib/rubyexcel/excel_tools.rb +2 -2
- data/lib/rubyexcel/rubyexcel_components.rb +4 -0
- data/lib/rubyexcel/section.rb +13 -0
- data/lib/rubyexcel.rb +16 -2
- metadata +2 -8
- data/lib/test/rubyexcel/tc_address.rb +0 -81
- data/lib/test/rubyexcel/tc_data.rb +0 -153
- data/lib/test/rubyexcel/tc_element.rb +0 -42
- data/lib/test/rubyexcel/tc_section.rb +0 -59
- data/lib/test/tc_rubyexcel.rb +0 -272
- data/lib/test/test_all.rb +0 -5
data/lib/README.md
CHANGED
@@ -10,9 +10,10 @@ A Data-analysis tool for Ruby, with an Excel-style API.
|
|
10
10
|
|
11
11
|
You can find the gem [here](https://rubygems.org/gems/rubyexcel "Rubygems").
|
12
12
|
|
13
|
-
Main
|
13
|
+
Main documentation is [here](http://rubydoc.info/gems/rubyexcel "Rubydoc")
|
14
14
|
|
15
|
-
For any requests, comments, etc. I keep an eye on [
|
15
|
+
For any requests, comments, etc. I keep an eye on [this forum](http://www.ruby-forum.com/forum/ruby "Ruby Mailing List").
|
16
|
+
If you put "RubyExcel" in the subject title I should see it.
|
16
17
|
|
17
18
|
Please feel free to log any bugs you find [here](https://github.com/VirtuosoJoel/RubyExcel/issues "Bug Tracker").
|
18
19
|
|
@@ -92,6 +93,9 @@ wb = s.parent
|
|
92
93
|
```
|
93
94
|
|
94
95
|
Using the Mechanize gem to get data
|
96
|
+
|
97
|
+
This example is for context, there are many potential data sources
|
98
|
+
|
95
99
|
--------
|
96
100
|
|
97
101
|
```ruby
|
@@ -132,6 +136,20 @@ Some Examples
|
|
132
136
|
|
133
137
|
This list may be removed in favour of the gem's documentation on rubydoc.
|
134
138
|
|
139
|
+
Common Operations
|
140
|
+
--------
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
#Append a Column by adding a header
|
144
|
+
s << 'Numbers'
|
145
|
+
x = 1
|
146
|
+
#Iterate through the rest of the rows while appending data
|
147
|
+
s.rows(2) { |row| row << x; x+=1 }
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
```
|
152
|
+
|
135
153
|
Workbook
|
136
154
|
--------
|
137
155
|
|
@@ -273,6 +291,10 @@ s.sort_by! { |r| r['A'] }
|
|
273
291
|
s.sumif( 'Part', 'Cost' ) { |part| part == 'Type1' } #=> 169.15
|
274
292
|
s.sumif( 'Part', 'Cost', &/Type1/ ) #=> 169.15
|
275
293
|
|
294
|
+
#Summarise a column by header into a Hash.
|
295
|
+
s.summarise( 'Part' )
|
296
|
+
#=> {"Type1"=>3, "Type2"=>2, "Type3"=>1, "Type4"=>1}
|
297
|
+
|
276
298
|
#Convert the data into various formats:
|
277
299
|
s.to_a #=> 2D Array
|
278
300
|
s.to_excel #=> WIN32OLE Excel Workbook (Contains only the current sheet)
|
@@ -417,8 +439,8 @@ Importing a Hash
|
|
417
439
|
```ruby
|
418
440
|
#Import a nested Hash (useful if you're summarising data before handing it to RubyExcel)
|
419
441
|
|
420
|
-
#Here's an example Hash
|
421
|
-
h = {
|
442
|
+
#Here's an example Hash (built into the gem as RubyExcel.sample_hash)
|
443
|
+
h = {
|
422
444
|
Part1: {
|
423
445
|
Type1: {
|
424
446
|
SubType1: 1, SubType2: 2, SubType3: 3
|
data/lib/rubyexcel/data.rb
CHANGED
@@ -190,7 +190,7 @@ require_relative 'address.rb'
|
|
190
190
|
#
|
191
191
|
|
192
192
|
def empty?
|
193
|
-
no_headers.empty?
|
193
|
+
no_headers.empty? rescue true
|
194
194
|
end
|
195
195
|
|
196
196
|
#
|
@@ -383,7 +383,7 @@ require_relative 'address.rb'
|
|
383
383
|
row_idx, col_idx = address_to_indices( addr )
|
384
384
|
( row_idx - rows ).times { @data << [] }
|
385
385
|
@data[ row_idx-1 ][ col_idx-1 ] = val
|
386
|
-
calc_dimensions
|
386
|
+
calc_dimensions if row_idx > rows || col_idx > cols
|
387
387
|
val
|
388
388
|
end
|
389
389
|
alias []= write
|
data/lib/rubyexcel/element.rb
CHANGED
@@ -48,6 +48,69 @@ module RubyExcel
|
|
48
48
|
data.delete( self ); self
|
49
49
|
end
|
50
50
|
|
51
|
+
#
|
52
|
+
# Yields each value in the data referenced by the address
|
53
|
+
#
|
54
|
+
|
55
|
+
def each
|
56
|
+
return to_enum( :each ) unless block_given?
|
57
|
+
expand( address ).flatten.each { |addr| yield data[ addr ] }
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Yields each Element referenced by the address
|
62
|
+
#
|
63
|
+
|
64
|
+
def each_cell
|
65
|
+
return to_enum( :each_cell ) unless block_given?
|
66
|
+
expand( address ).flatten.each { |addr| yield Element.new( sheet, addr ) }
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Checks whether the data referenced by the address is empty
|
71
|
+
#
|
72
|
+
|
73
|
+
def empty?
|
74
|
+
all? { |v| v.to_s.empty? }
|
75
|
+
end
|
76
|
+
|
77
|
+
#
|
78
|
+
# Return the first cell in the Range
|
79
|
+
#
|
80
|
+
# @return [RubyExcel::Element]
|
81
|
+
#
|
82
|
+
|
83
|
+
def first_cell
|
84
|
+
Element.new( sheet, expand( address ).flatten.first )
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# View the object for debugging
|
89
|
+
#
|
90
|
+
|
91
|
+
def inspect
|
92
|
+
"#{ self.class }:0x#{ '%x' % ( object_id << 1 ) }: #{ address }"
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Return the last cell in the Range
|
97
|
+
#
|
98
|
+
# @return [RubyExcel::Element]
|
99
|
+
#
|
100
|
+
|
101
|
+
def last_cell
|
102
|
+
Element.new( sheet, expand( address ).flatten.last )
|
103
|
+
end
|
104
|
+
|
105
|
+
#
|
106
|
+
# Replaces each value with the result of the block
|
107
|
+
#
|
108
|
+
|
109
|
+
def map!
|
110
|
+
return to_enum( :map! ) unless block_given?
|
111
|
+
expand( address ).flatten.each { |addr| data[ addr ] = yield data[ addr ] }
|
112
|
+
end
|
113
|
+
|
51
114
|
#
|
52
115
|
# Return the value at this Element's address
|
53
116
|
#
|
@@ -72,14 +135,19 @@ module RubyExcel
|
|
72
135
|
addresses = expand( address )
|
73
136
|
|
74
137
|
# 2D Array of Values
|
75
|
-
if multi_array?( val )
|
138
|
+
if multi_array?( val ) && addresses.length > 1
|
76
139
|
|
77
140
|
#Check the dimensions
|
78
141
|
val_rows, val_cols, range_rows, range_cols = val.length, val.max_by(&:length).length, addresses.length, addresses.max_by(&:length).length
|
79
|
-
val_rows == range_rows && val_cols == range_cols or fail ArgumentError, "Dimension mismatch! Value rows
|
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 }"
|
80
143
|
|
81
144
|
#Write the values in order
|
82
|
-
addresses.each_with_index { |row,idx| row.each_with_index { |el,i| data[
|
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] }
|
83
151
|
|
84
152
|
# Single Value
|
85
153
|
else
|
@@ -104,49 +172,6 @@ module RubyExcel
|
|
104
172
|
def to_s
|
105
173
|
value.is_a?( Array ) ? value.map { |ar| ar.join "\t" }.join($/) : value.to_s
|
106
174
|
end
|
107
|
-
|
108
|
-
#
|
109
|
-
# View the object for debugging
|
110
|
-
#
|
111
|
-
|
112
|
-
def inspect
|
113
|
-
"#{ self.class }:0x#{ '%x' % ( object_id << 1 ) }: #{ address }"
|
114
|
-
end
|
115
|
-
|
116
|
-
#
|
117
|
-
# Yields each value in the data referenced by the address
|
118
|
-
#
|
119
|
-
|
120
|
-
def each
|
121
|
-
return to_enum( :each ) unless block_given?
|
122
|
-
expand( address ).flatten.each { |addr| yield data[ addr ] }
|
123
|
-
end
|
124
|
-
|
125
|
-
#
|
126
|
-
# Yields each Element referenced by the address
|
127
|
-
#
|
128
|
-
|
129
|
-
def each_cell
|
130
|
-
return to_enum( :each_cell ) unless block_given?
|
131
|
-
expand( address ).flatten.each { |addr| yield Element.new( sheet, addr ) }
|
132
|
-
end
|
133
|
-
|
134
|
-
#
|
135
|
-
# Checks whether the data referenced by the address is empty
|
136
|
-
#
|
137
|
-
|
138
|
-
def empty?
|
139
|
-
all? { |v| v.to_s.empty? }
|
140
|
-
end
|
141
|
-
|
142
|
-
#
|
143
|
-
# Replaces each value with the result of the block
|
144
|
-
#
|
145
|
-
|
146
|
-
def map!
|
147
|
-
return to_enum( :map! ) unless block_given?
|
148
|
-
expand( address ).flatten.each { |addr| data[ addr ] = yield data[ addr ] }
|
149
|
-
end
|
150
175
|
|
151
176
|
end
|
152
177
|
|
@@ -40,7 +40,7 @@ module RubyExcel
|
|
40
40
|
def dump_to_sheet( data, sheet=nil )
|
41
41
|
data.is_a?( Array ) or fail ArgumentError, "Invalid data type: #{ data.class }"
|
42
42
|
sheet ||= get_workbook.sheets(1)
|
43
|
-
sheet.range( sheet.cells( 1, 1 ), sheet.cells( data.length, data
|
43
|
+
sheet.range( sheet.cells( 1, 1 ), sheet.cells( data.length, data.max_by(&:length).length ) ).value = data
|
44
44
|
sheet
|
45
45
|
end
|
46
46
|
|
@@ -125,7 +125,7 @@ module RubyExcel
|
|
125
125
|
self.each do |s|
|
126
126
|
sht = ( first_time ? wb.sheets(1) : wb.sheets.add( { 'after' => wb.sheets( wb.sheets.count ) } ) ); first_time = false
|
127
127
|
sht.name = s.name
|
128
|
-
make_sheet_pretty( dump_to_sheet( s.
|
128
|
+
make_sheet_pretty( dump_to_sheet( s.to_a, sht ) )
|
129
129
|
end
|
130
130
|
wb.sheets(1).select
|
131
131
|
wb.application.visible = true unless invisible
|
data/lib/rubyexcel/section.rb
CHANGED
@@ -124,6 +124,16 @@ module RubyExcel
|
|
124
124
|
"#{ self.class }:0x#{ '%x' % (object_id << 1) }: #{ idx }"
|
125
125
|
end
|
126
126
|
|
127
|
+
#
|
128
|
+
# Return the last cell
|
129
|
+
#
|
130
|
+
# @return [RubyExcel::Element]
|
131
|
+
#
|
132
|
+
|
133
|
+
def last_cell
|
134
|
+
Element.new( sheet, each_address.to_a.last )
|
135
|
+
end
|
136
|
+
|
127
137
|
#
|
128
138
|
# Replaces each value with the result of the block
|
129
139
|
#
|
@@ -258,6 +268,7 @@ module RubyExcel
|
|
258
268
|
private
|
259
269
|
|
260
270
|
def each_address
|
271
|
+
return to_enum( :each_address ) unless block_given?
|
261
272
|
( 'A'..col_letter( data.cols ) ).each { |col_id| yield "#{col_id}#{idx}" }
|
262
273
|
end
|
263
274
|
alias each_address_without_headers each_address
|
@@ -294,10 +305,12 @@ module RubyExcel
|
|
294
305
|
private
|
295
306
|
|
296
307
|
def each_address
|
308
|
+
return to_enum( :each_address ) unless block_given?
|
297
309
|
( 1..data.rows ).each { |row_id| yield idx + row_id.to_s }
|
298
310
|
end
|
299
311
|
|
300
312
|
def each_address_without_headers
|
313
|
+
return to_enum( :each_address_without_headers ) unless block_given?
|
301
314
|
( sheet.header_rows+1 ).upto( data.rows ) { |row_id| yield idx + row_id.to_s }
|
302
315
|
end
|
303
316
|
|
data/lib/rubyexcel.rb
CHANGED
@@ -262,6 +262,7 @@ module RubyExcel
|
|
262
262
|
#
|
263
263
|
# @param [Array<Array>, Hash<Hash>, RubyExcel::Sheet] other the data to add
|
264
264
|
# @return [RubyExcel::Sheet] returns a new Sheet
|
265
|
+
# @note When adding another Sheet it won't import the headers unless this Sheet is empty.
|
265
266
|
#
|
266
267
|
|
267
268
|
def +( other )
|
@@ -288,14 +289,16 @@ module RubyExcel
|
|
288
289
|
#
|
289
290
|
# @param [Array<Array>, Hash<Hash>, RubyExcel::Sheet] other the data to append
|
290
291
|
# @return [self]
|
292
|
+
# @note When adding another Sheet it won't import the headers unless this Sheet is empty.
|
293
|
+
# @note Anything other than an an Array, Hash, or Sheet will be appended to the first row
|
291
294
|
#
|
292
295
|
|
293
296
|
def <<( other )
|
294
297
|
case other
|
295
298
|
when Array ; load( data.all + other, header_rows )
|
296
299
|
when Hash ; load( data.all + _convert_hash( other ), header_rows )
|
297
|
-
when Sheet ; load( data.all + other.data.no_headers, header_rows )
|
298
|
-
else ;
|
300
|
+
when Sheet ; empty? ? load( other.to_a, other.header_rows ) : load( data.all + other.data.no_headers, header_rows )
|
301
|
+
else ; row(1) << other
|
299
302
|
end
|
300
303
|
self
|
301
304
|
end
|
@@ -698,6 +701,17 @@ module RubyExcel
|
|
698
701
|
find_col.each_cell.inject(0) { |sum,ce| yield( ce.value ) && ce.row > header_rows ? sum + sum_col[ ce.row ] : sum }
|
699
702
|
end
|
700
703
|
|
704
|
+
#
|
705
|
+
# Summarise the values of a Column into a Hash
|
706
|
+
#
|
707
|
+
# @param [String] header the header of the Column to summarise
|
708
|
+
# @return [Hash]
|
709
|
+
#
|
710
|
+
|
711
|
+
def summarise( header )
|
712
|
+
ch( header ).summarise
|
713
|
+
end
|
714
|
+
|
701
715
|
#
|
702
716
|
# The Sheet as a 2D Array
|
703
717
|
#
|
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.1
|
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-02 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tabular data structure, mixing Ruby with some of Excel's API style.
|
15
15
|
email: VirtuosoJoel@gmail.com
|
@@ -24,12 +24,6 @@ files:
|
|
24
24
|
- lib/rubyexcel/rubyexcel_components.rb
|
25
25
|
- lib/rubyexcel/section.rb
|
26
26
|
- lib/rubyexcel.rb
|
27
|
-
- lib/test/rubyexcel/tc_address.rb
|
28
|
-
- lib/test/rubyexcel/tc_data.rb
|
29
|
-
- lib/test/rubyexcel/tc_element.rb
|
30
|
-
- lib/test/rubyexcel/tc_section.rb
|
31
|
-
- lib/test/tc_rubyexcel.rb
|
32
|
-
- lib/test/test_all.rb
|
33
27
|
- lib/README.md
|
34
28
|
homepage: https://github.com/VirtuosoJoel
|
35
29
|
licenses: []
|
@@ -1,81 +0,0 @@
|
|
1
|
-
require_relative '../../rubyexcel'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
|
5
|
-
class TestAddress < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def setup
|
8
|
-
@s = RubyExcel.sample_sheet
|
9
|
-
end
|
10
|
-
|
11
|
-
def teardown
|
12
|
-
@s = nil
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_address_to_col_index
|
16
|
-
|
17
|
-
assert( @s.address_to_col_index( 'A1' ) == 1 )
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_address_to_indices
|
22
|
-
|
23
|
-
assert_equal( [ 1, 1 ], @s.address_to_indices( 'A1' ) )
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_col_index
|
28
|
-
|
29
|
-
assert_equal( 1, @s.col_index( 'A' ) )
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_col_letter
|
34
|
-
|
35
|
-
assert_equal( 'A', @s.col_letter( 1 ) )
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_column_id
|
40
|
-
|
41
|
-
assert_equal( 'A', @s.column_id( 'A1' ) )
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_expand
|
46
|
-
|
47
|
-
assert_equal( [['A1','B1'],['A2', 'B2']], @s.expand( 'A1:B2' ) )
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_indices_to_address
|
52
|
-
|
53
|
-
assert_equal( 'A1', @s.indices_to_address( 1, 1 ) )
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_multi_array?
|
58
|
-
|
59
|
-
assert( @s.multi_array? RubyExcel.sample_data )
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_offset
|
64
|
-
|
65
|
-
assert_equal( 'B2', @s.offset( 'A1', 1, 1 ) )
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_to_range_address
|
70
|
-
|
71
|
-
assert_equal( 'A1:B2', @s.to_range_address( @s.cell(1,1), @s.cell(2,2) ) )
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_row_id
|
76
|
-
|
77
|
-
assert_equal( 2, @s.row_id( 'A2' ) )
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
@@ -1,153 +0,0 @@
|
|
1
|
-
require_relative '../../rubyexcel'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
class TestData < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def setup
|
7
|
-
@s = RubyExcel.sample_sheet
|
8
|
-
end
|
9
|
-
|
10
|
-
def teardown
|
11
|
-
@s = nil
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_advanced_filter!
|
15
|
-
|
16
|
-
assert_equal( 3, @s.data.advanced_filter!( 'Part', :=~, /Type[13]/, 'Qty', :>, 1 ).rows )
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_colref_by_header
|
21
|
-
|
22
|
-
assert_equal( 'B', @s.data.colref_by_header( 'Ref1' ) )
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_compact
|
27
|
-
|
28
|
-
@s << [[1,2,3], [4,5,6]]
|
29
|
-
assert( @s.data.cols == 5 && @s.data.rows == 10 )
|
30
|
-
|
31
|
-
@s.rows( 9 ) { |r| r.map! { nil } }
|
32
|
-
@s.data.compact!
|
33
|
-
assert_equal( 8, @s.data.rows )
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_delete
|
38
|
-
|
39
|
-
@s.data.delete( @s.row(1) )
|
40
|
-
assert_equal( 7, @s.data.rows )
|
41
|
-
|
42
|
-
@s.data.delete( @s.column(1) )
|
43
|
-
assert_equal( 4, @s.data.cols )
|
44
|
-
|
45
|
-
@s.data.delete( @s.range('A:A') )
|
46
|
-
assert_equal( 3, @s.data.cols )
|
47
|
-
|
48
|
-
assert_raise( NoMethodError ) { @s.data.delete( [[]] ) }
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_each
|
53
|
-
|
54
|
-
assert_equal( 8, @s.data.each.count )
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_filter!
|
59
|
-
|
60
|
-
assert_equal( 3, @s.data.filter!( 'Part', &/Type2/ ).rows )
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_get_columns!
|
65
|
-
|
66
|
-
assert_equal( [ 'Ref2', 'Part' ], @s.data.get_columns!( 'Ref2', 'Part' ).sheet.row(1).to_a )
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_headers
|
71
|
-
|
72
|
-
assert_equal( 1, @s.data.headers.length )
|
73
|
-
|
74
|
-
@s.headers = 0
|
75
|
-
assert_equal( nil, @s.data.headers )
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_index_by_header
|
80
|
-
|
81
|
-
assert_equal( 1, @s.data.index_by_header( 'Part' ) )
|
82
|
-
|
83
|
-
@s.headers = 0
|
84
|
-
assert_raise( NoMethodError ) { @s.data.index_by_header( 'Part' ) }
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_insert
|
89
|
-
|
90
|
-
@s.data.insert_columns( 'A', 2 )
|
91
|
-
assert_equal( 7, @s.maxcol )
|
92
|
-
assert_equal( nil, @s['B2'] )
|
93
|
-
|
94
|
-
@s.data.insert_rows( 2, 2 )
|
95
|
-
assert_equal( 10, @s.maxrow )
|
96
|
-
assert_equal( nil, @s['B4'] )
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_no_headers
|
101
|
-
|
102
|
-
assert_equal( 7, @s.data.no_headers.length )
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_partition
|
107
|
-
|
108
|
-
ar1, ar2 = @s.data.partition( 'Part', &/Type[13]/ )
|
109
|
-
assert_equal( 5, ar1.length )
|
110
|
-
assert_equal( 'Type1', ar1[1][0] )
|
111
|
-
assert_equal( 4, ar2.length )
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_read_write
|
116
|
-
|
117
|
-
assert_equal( '123', @s.data.read( 'C3' ) )
|
118
|
-
|
119
|
-
@s.data.write( 'C3', '321' )
|
120
|
-
assert_equal( '321', @s.data.read( 'C3' ) )
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_reverse
|
125
|
-
|
126
|
-
@s.data.reverse_columns!
|
127
|
-
assert_equal( 'Cost', @s.A1 )
|
128
|
-
|
129
|
-
@s.data.reverse_rows!
|
130
|
-
assert_equal( 'QT1', @s.d8 )
|
131
|
-
|
132
|
-
@s.headers = 0
|
133
|
-
@s.data.reverse_rows!
|
134
|
-
assert_equal( 'Ref1', @s.d8 )
|
135
|
-
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_skip_headers
|
139
|
-
|
140
|
-
@s.load( @s.data.skip_headers { |data| data.map { |row| row.map { nil } } } )
|
141
|
-
assert_equal( 'Part', @s.a1 )
|
142
|
-
assert_equal( nil, @s.a2 )
|
143
|
-
|
144
|
-
end
|
145
|
-
|
146
|
-
def test_uniq!
|
147
|
-
|
148
|
-
@s.data.uniq!( 'Part' )
|
149
|
-
assert_equal( 5, @s.maxrow )
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
require_relative '../../rubyexcel'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
class TestElement < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def setup
|
7
|
-
@s = RubyExcel.sample_sheet
|
8
|
-
end
|
9
|
-
|
10
|
-
def teardown
|
11
|
-
@s = nil
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_initialize
|
15
|
-
|
16
|
-
r = @s.range( 'A1:B2' )
|
17
|
-
assert_equal( @s, r.sheet )
|
18
|
-
assert_equal( 'A1:B2', r.address )
|
19
|
-
assert_equal( 'A', r.column )
|
20
|
-
assert_equal( 1, r.row )
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_value
|
25
|
-
|
26
|
-
r = @s.range( 'A1' )
|
27
|
-
assert_equal( 'Part', r.value )
|
28
|
-
|
29
|
-
r = @s.range( 'A1:B2' )
|
30
|
-
assert_equal( [['Part', 'Ref1'], ['Type1', 'QT1']], r.value )
|
31
|
-
|
32
|
-
assert_raise( ArgumentError ) { r.value = [[1, 2]] }
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_each
|
37
|
-
|
38
|
-
assert_equal( 6, @s.range( 'A1:C2' ).each.count )
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require_relative '../../rubyexcel'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
class TestRowColumn < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def setup
|
7
|
-
@s = RubyExcel.sample_sheet
|
8
|
-
@r = @s.row(2)
|
9
|
-
@c = @s.column(2)
|
10
|
-
end
|
11
|
-
|
12
|
-
def teardown
|
13
|
-
@s = nil
|
14
|
-
@r = nil
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_shovel
|
18
|
-
|
19
|
-
@r << 1
|
20
|
-
assert_equal( 5, @r.length )
|
21
|
-
|
22
|
-
@r = @s.row(1)
|
23
|
-
@r << 1
|
24
|
-
assert_equal( 6, @r.length )
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_cell
|
29
|
-
|
30
|
-
assert_equal( @r.cell(2).address, @c.cell(2).address )
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_find
|
35
|
-
|
36
|
-
assert_equal( 'B2', @r.find( &/QT1/ ) )
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_summarise
|
41
|
-
|
42
|
-
h = { 'Type1' => 3, 'Type2' => 2, 'Type3' => 1, 'Type4' => 1 }
|
43
|
-
assert_equal( h, @s.column(1).summarise )
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_getref
|
48
|
-
|
49
|
-
assert_equal( 'A', @r.getref( 'Part' ) )
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_value_by_header
|
54
|
-
|
55
|
-
assert_equal( 'Type1', @r.val( 'Part' ) )
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
data/lib/test/tc_rubyexcel.rb
DELETED
@@ -1,272 +0,0 @@
|
|
1
|
-
require_relative '../rubyexcel'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
class TestRegexp < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_to_proc
|
7
|
-
assert_equal(0, /a/.to_proc.call('a') )
|
8
|
-
end
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
class TestWorkbook < Test::Unit::TestCase
|
13
|
-
|
14
|
-
def setup
|
15
|
-
@wb = RubyExcel::Workbook.new
|
16
|
-
3.times { @wb.add.load RubyExcel.sample_data }
|
17
|
-
end
|
18
|
-
|
19
|
-
def teardown
|
20
|
-
@wb = nil
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_shovel
|
24
|
-
|
25
|
-
@wb << @wb.dup
|
26
|
-
assert_equal( 6, @wb.sheets.count )
|
27
|
-
|
28
|
-
@wb << @wb.sheets(1)
|
29
|
-
assert_equal( 7, @wb.sheets.count )
|
30
|
-
|
31
|
-
@wb << @wb.sheets(1).data.all
|
32
|
-
assert_equal( 8, @wb.sheets.count )
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_add
|
37
|
-
|
38
|
-
@wb.add 'Sheet4'
|
39
|
-
assert_equal( 4, @wb.sheets.count )
|
40
|
-
|
41
|
-
@wb.add
|
42
|
-
assert_equal( 5, @wb.sheets.count )
|
43
|
-
|
44
|
-
@wb.add @wb.sheets(1)
|
45
|
-
assert_equal( 6, @wb.sheets.count )
|
46
|
-
|
47
|
-
assert_raise( TypeError ) { @wb.add 1 }
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_clear_all
|
52
|
-
|
53
|
-
assert_equal( 0, @wb.clear_all.sheets.count )
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_delete
|
58
|
-
|
59
|
-
assert_equal( 2, @wb.delete(1).sheets.count )
|
60
|
-
|
61
|
-
assert_equal( 1, @wb.delete( 'Sheet2' ).sheets.count )
|
62
|
-
|
63
|
-
assert_equal( 0, @wb.delete( /Sheet/ ).sheets.count )
|
64
|
-
|
65
|
-
assert_equal( 0, @wb.delete( @wb.add ).sheets.count )
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_dup
|
70
|
-
|
71
|
-
dup_wb = @wb.dup
|
72
|
-
|
73
|
-
assert_equal( @wb.sheets(1).to_a, dup_wb.sheets(1).to_a )
|
74
|
-
|
75
|
-
assert_not_equal( @wb.object_id, dup_wb.object_id )
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_empty?
|
80
|
-
|
81
|
-
assert( !@wb.empty? )
|
82
|
-
|
83
|
-
assert( @wb.clear_all.empty? )
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_load
|
88
|
-
|
89
|
-
assert( @wb.load( [[]] ).class == RubyExcel::Sheet )
|
90
|
-
|
91
|
-
assert( @wb.load( RubyExcel.sample_data )['A1'] == 'Part' )
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_sheets
|
96
|
-
|
97
|
-
assert( @wb.sheets.class == Enumerator )
|
98
|
-
|
99
|
-
assert( @wb.sheets(2) == @wb.sheets('Sheet2') )
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
class TestSheet < Test::Unit::TestCase
|
106
|
-
|
107
|
-
def setup
|
108
|
-
@s = RubyExcel.sample_sheet
|
109
|
-
end
|
110
|
-
|
111
|
-
def teardown
|
112
|
-
@s = nil
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_basics
|
116
|
-
|
117
|
-
assert( @s['A1'] == @s.A1 )
|
118
|
-
|
119
|
-
@s << RubyExcel.sample_hash
|
120
|
-
assert( @s.maxrow == 20 && @s.maxcol == 5 )
|
121
|
-
|
122
|
-
@s << RubyExcel.sample_data
|
123
|
-
assert( @s.maxrow == 28 && @s.maxcol == 5 )
|
124
|
-
|
125
|
-
@s << @s
|
126
|
-
assert( @s.maxrow == 55 && @s.maxcol == 5 )
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_advanced_filter
|
131
|
-
|
132
|
-
@s.advanced_filter!( 'Part', :=~, /Type[13]/, 'Qty', :>, 1 )
|
133
|
-
assert( @s.maxrow == 3 )
|
134
|
-
|
135
|
-
setup
|
136
|
-
@s.advanced_filter!( 'Part', :==, 'Type1', 'Ref1', :include?, 'X' )
|
137
|
-
assert( @s.maxrow == 2 )
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_cell
|
142
|
-
|
143
|
-
assert( @s.cell(1,1).value == 'Part' && @s.cell(1,1).address == 'A1' )
|
144
|
-
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_column
|
148
|
-
|
149
|
-
assert( @s.column('A')[1] == 'Part' )
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_column_by_header
|
154
|
-
|
155
|
-
assert( @s.ch( 'Part' )[1] == @s.ch( @s.column(1) )[1] )
|
156
|
-
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_columns
|
160
|
-
|
161
|
-
assert( @s.columns.class == Enumerator )
|
162
|
-
|
163
|
-
assert( @s.columns( 'B' ).count == 4 )
|
164
|
-
|
165
|
-
assert( @s.columns( 'B', 'D' ).to_a[0][1] == 'Ref1' )
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_filter
|
170
|
-
|
171
|
-
assert( @s.filter( 'Part', &/Type[13]/ ).maxrow == 5 )
|
172
|
-
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_get_columns
|
176
|
-
|
177
|
-
assert_equal( @s.get_columns( 'Ref2', 'Part' ).row(1).to_a, [ 'Ref2', 'Part' ] )
|
178
|
-
|
179
|
-
end
|
180
|
-
|
181
|
-
def test_insert_columns
|
182
|
-
|
183
|
-
assert( @s.insert_columns( 2, 2 ).columns.count == 7 )
|
184
|
-
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_insert_rows
|
188
|
-
|
189
|
-
assert( @s.insert_rows( 2, 2 ).rows.count == 10 )
|
190
|
-
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_match
|
194
|
-
|
195
|
-
assert( @s.match( 'Part', &/Type2/ ) == 3 )
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_method_missing
|
200
|
-
|
201
|
-
assert( @s.a1 == 'Part' )
|
202
|
-
|
203
|
-
assert_raise( NoMethodError ) { @s.abcd123 }
|
204
|
-
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_respond_to?
|
208
|
-
|
209
|
-
assert( @s.respond_to?(:A1) )
|
210
|
-
|
211
|
-
end
|
212
|
-
|
213
|
-
def test_partition
|
214
|
-
|
215
|
-
s1, s2 = @s.partition( 'Qty' ) { |v| v > 1 }
|
216
|
-
assert_equal( s1.maxrow + 1, s2.maxrow )
|
217
|
-
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_range
|
221
|
-
|
222
|
-
assert( @s.range( 'A1:A1' ).value == @s.range( @s.cell(1,1), @s.cell( 1,1 ) ).value )
|
223
|
-
|
224
|
-
assert( @s.range( 'A1' ).value == @s.range( @s.cell(1,1) ).value )
|
225
|
-
|
226
|
-
end
|
227
|
-
|
228
|
-
def test_reverse
|
229
|
-
|
230
|
-
assert( @s.reverse_columns!['A1'] == 'Cost' )
|
231
|
-
|
232
|
-
assert( @s.reverse_rows!['A2'] == 104 )
|
233
|
-
|
234
|
-
end
|
235
|
-
|
236
|
-
def test_row
|
237
|
-
|
238
|
-
assert( @s.row(1)['A'] == 'Part' )
|
239
|
-
|
240
|
-
end
|
241
|
-
|
242
|
-
def test_rows
|
243
|
-
|
244
|
-
assert( @s.rows.class == Enumerator )
|
245
|
-
|
246
|
-
assert( @s.rows( 2 ).count == 7 )
|
247
|
-
|
248
|
-
assert( @s.rows( 2, 4 ).to_a[0]['A'] == 'Type1' )
|
249
|
-
|
250
|
-
end
|
251
|
-
|
252
|
-
def test_sumif
|
253
|
-
|
254
|
-
assert( @s.sumif( 'Part', 'Cost', &/Type1/ ) == 169.15 )
|
255
|
-
|
256
|
-
end
|
257
|
-
|
258
|
-
def test_uniq
|
259
|
-
|
260
|
-
assert( @s.uniq( 'Part' ).maxrow == 5 )
|
261
|
-
|
262
|
-
end
|
263
|
-
|
264
|
-
def test_
|
265
|
-
|
266
|
-
assert( @s.vlookup( 'Part', 'Ref2', &/Type1/ ) == '231' )
|
267
|
-
|
268
|
-
end
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
end
|