rxls 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rxls/base.rb +76 -0
- data/lib/rxls/version.rb +1 -1
- data/spec/fixtures/x.xls +0 -0
- data/spec/rxls_spec.rb +26 -0
- data/spec/spec_helper.rb +1 -0
- metadata +6 -2
data/lib/rxls/base.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'win32ole'
|
2
|
+
module Rxls
|
3
|
+
class Base
|
4
|
+
|
5
|
+
def initialize file
|
6
|
+
@obj = begin
|
7
|
+
WIN32OLE.connect('excel.application')
|
8
|
+
rescue
|
9
|
+
WIN32OLE.new('excel.application')
|
10
|
+
end
|
11
|
+
file = File.expand_path "../../../spec/fixtures/#{file}.xls", __FILE__ if file.kind_of? Symbol
|
12
|
+
@obj.workbooks.open file
|
13
|
+
end
|
14
|
+
|
15
|
+
def quit
|
16
|
+
@obj.quit
|
17
|
+
end
|
18
|
+
|
19
|
+
def worksheet(n,mapping=nil)
|
20
|
+
Worksheet.new(@obj,n,mapping)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class Worksheet
|
26
|
+
include Enumerable
|
27
|
+
|
28
|
+
# 'a' => [0]
|
29
|
+
# 'a b' => [0,1]
|
30
|
+
def to_row_numbers(s)
|
31
|
+
s.split(' ').map do |r|
|
32
|
+
r = 96.chr + r if r.length == 1
|
33
|
+
s = nil
|
34
|
+
r.each_byte do |b|
|
35
|
+
s = s ? s + (b-97) : (b-96) * 26
|
36
|
+
end
|
37
|
+
s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(o,n,mapping)
|
42
|
+
@columns = to_row_numbers mapping
|
43
|
+
@values = o.Worksheets(n).UsedRange.Value
|
44
|
+
headers = @values.shift # skip 1st row
|
45
|
+
@headers = @columns.map{|c| headers[c]}
|
46
|
+
end
|
47
|
+
|
48
|
+
def each
|
49
|
+
@values.each_with_index do |row,column|
|
50
|
+
values = @columns ? @columns.map{|r| row[r]} : row
|
51
|
+
yield values.unshift(column)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def dump
|
56
|
+
vcs = @columns.map{|column| ValueCounter.new}
|
57
|
+
each do |row_with_number|
|
58
|
+
row_with_number[1..-1].each_with_index do |value,index|
|
59
|
+
vcs[index] << dump_value(value)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
vcs.each_with_index{|vc,index| puts "#{@headers[index]}:#{vc}"}
|
63
|
+
end
|
64
|
+
|
65
|
+
def dump_value(v)
|
66
|
+
case v
|
67
|
+
when String
|
68
|
+
v == '' ? "String(empty)" : "String"
|
69
|
+
else
|
70
|
+
v.class
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/lib/rxls/version.rb
CHANGED
data/spec/fixtures/x.xls
ADDED
Binary file
|
data/spec/rxls_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Rxls
|
3
|
+
describe Base do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@b = Base.new(:x)
|
7
|
+
@w = @b.worksheet 1,'a c'
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'converts exel colum names to array indexes' do
|
11
|
+
@w.to_row_numbers('a').should == [0]
|
12
|
+
@w.to_row_numbers('a b').should == [0,1]
|
13
|
+
@w.to_row_numbers('aa').should == [26]
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'iterates over worksheet' do
|
17
|
+
@w.map{ |row,number,nachname| nachname}.should == ["Behresn", "Schnell"]
|
18
|
+
end
|
19
|
+
|
20
|
+
after(:all) do
|
21
|
+
@b.quit
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rxls'
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Frank Behrens
|
@@ -32,8 +32,12 @@ files:
|
|
32
32
|
- Gemfile
|
33
33
|
- Rakefile
|
34
34
|
- lib/rxls.rb
|
35
|
+
- lib/rxls/base.rb
|
35
36
|
- lib/rxls/version.rb
|
36
37
|
- rxls.gemspec
|
38
|
+
- spec/fixtures/x.xls
|
39
|
+
- spec/rxls_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
37
41
|
has_rdoc: true
|
38
42
|
homepage: ""
|
39
43
|
licenses: []
|