roo 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/roo.rb +2 -1
- data/lib/roo/openoffice.rb +81 -49
- data/lib/roo/version.rb +1 -1
- data/test/test_roo.rb +57 -4
- data/website/index.html +2 -5
- data/website/index.txt +1 -3
- metadata +2 -2
data/lib/roo.rb
CHANGED
data/lib/roo/openoffice.rb
CHANGED
@@ -7,36 +7,106 @@ require 'zip/zipfilesystem'
|
|
7
7
|
|
8
8
|
class Openoffice
|
9
9
|
|
10
|
-
def initialize(filename
|
10
|
+
def initialize(filename)
|
11
|
+
@cells_read = false
|
11
12
|
@filename = filename
|
12
|
-
|
13
|
-
|
13
|
+
@tmpdir = "oo_"+$$.to_s
|
14
|
+
unless File.exists?(@tmpdir)
|
15
|
+
FileUtils::mkdir(@tmpdir) #TODO:
|
14
16
|
end
|
15
|
-
# `unzip -o "#{filename}"`
|
16
17
|
extract_content
|
17
|
-
file = File.new("roo_content.xml")
|
18
|
+
file = File.new(@tmpdir+"/"+"roo_content.xml") # TODO:
|
18
19
|
@doc = REXML::Document.new file
|
19
20
|
@cell = Hash.new
|
20
21
|
@cell_type = Hash.new
|
22
|
+
FileUtils::rm_r(@tmpdir)
|
23
|
+
@default_sheet = nil
|
21
24
|
end
|
22
25
|
|
26
|
+
# return the content of a spreadsheet-cell
|
27
|
+
# (1,1) is the upper left corner
|
28
|
+
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
|
29
|
+
# cell at first line, first row
|
23
30
|
def cell(row,col)
|
31
|
+
if row.class == String
|
32
|
+
if col.class == Fixnum
|
33
|
+
# ('A',1):
|
34
|
+
# ('B', 5) -> (5, 2)
|
35
|
+
row, col = col, row
|
36
|
+
else
|
37
|
+
raise FormatError
|
38
|
+
end
|
39
|
+
end
|
24
40
|
if col.class == String
|
25
|
-
col = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(col)+1
|
41
|
+
col = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(col.upcase)+1
|
26
42
|
end
|
27
43
|
read_cells unless @cells_read
|
28
44
|
@cell["#{row},#{col}"]
|
29
45
|
end
|
30
46
|
|
47
|
+
# returns the open-office type of a cell
|
31
48
|
def celltype(row,col)
|
32
49
|
read_cells unless @cells_read
|
33
50
|
@cell_type["#{row},#{col}"]
|
34
51
|
end
|
35
52
|
|
53
|
+
|
54
|
+
# returns an array of sheets in the spreadsheet
|
55
|
+
def sheets
|
56
|
+
return_sheets = []
|
57
|
+
# p valid_xml?(doc)
|
58
|
+
oo_document_count = 0
|
59
|
+
@doc.each_element do |oo_document|
|
60
|
+
oo_document_count += 1
|
61
|
+
#p oo_document
|
62
|
+
oo_element_count = 0
|
63
|
+
oo_document.each_element do |oo_element|
|
64
|
+
oo_element_count += 1
|
65
|
+
# p oo_element.name
|
66
|
+
if oo_element.name == "body"
|
67
|
+
# puts "Body gefunden "
|
68
|
+
oo_element.each_element do |be|
|
69
|
+
# p be.name
|
70
|
+
if be.name == "spreadsheet"
|
71
|
+
be.each_element do |se|
|
72
|
+
# p se
|
73
|
+
if se.name == "table"
|
74
|
+
# puts "table gefunden"
|
75
|
+
#se.each_element
|
76
|
+
# p se.attributes['name']
|
77
|
+
return_sheets << se.attributes['name']
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
# puts oo_element_count.to_s+" oo_element_count "
|
85
|
+
end
|
86
|
+
# puts oo_document_count.to_s+" oo_document_count "
|
87
|
+
return_sheets
|
88
|
+
end
|
89
|
+
|
90
|
+
# set the working sheet in the document
|
91
|
+
def default_sheet=(s)
|
92
|
+
@default_sheet = s
|
93
|
+
end
|
94
|
+
|
95
|
+
def officeversion
|
96
|
+
read_cells unless @cells_read
|
97
|
+
@officeversion
|
98
|
+
end
|
99
|
+
|
100
|
+
def to_s
|
101
|
+
@cell.inspect
|
102
|
+
end
|
103
|
+
private
|
104
|
+
|
36
105
|
# read all cells in the selected sheet
|
37
106
|
def read_cells
|
38
107
|
oo_document_count = 0
|
39
108
|
@doc.each_element do |oo_document|
|
109
|
+
@officeversion = oo_document.attributes['version']
|
40
110
|
oo_document_count += 1
|
41
111
|
oo_element_count = 0
|
42
112
|
oo_document.each_element do |oo_element|
|
@@ -67,6 +137,10 @@ class Openoffice
|
|
67
137
|
te.each_element do |tr|
|
68
138
|
# p tr
|
69
139
|
if tr.name == 'table-cell'
|
140
|
+
skip = tr.attributes['number-columns-repeated']
|
141
|
+
if skip
|
142
|
+
x += (skip.to_i - 1)
|
143
|
+
end
|
70
144
|
vt = tr.attributes['value-type']
|
71
145
|
v = tr.attributes['value']
|
72
146
|
# puts "#{vt} #{v}"
|
@@ -107,55 +181,13 @@ class Openoffice
|
|
107
181
|
@cells_read = true
|
108
182
|
end
|
109
183
|
|
110
|
-
# returns a list of sheets in this document
|
111
|
-
def sheets
|
112
|
-
return_sheets = []
|
113
|
-
# p valid_xml?(doc)
|
114
|
-
oo_document_count = 0
|
115
|
-
@doc.each_element do |oo_document|
|
116
|
-
oo_document_count += 1
|
117
|
-
#p oo_document
|
118
|
-
oo_element_count = 0
|
119
|
-
oo_document.each_element do |oo_element|
|
120
|
-
oo_element_count += 1
|
121
|
-
# p oo_element.name
|
122
|
-
if oo_element.name == "body"
|
123
|
-
# puts "Body gefunden "
|
124
|
-
oo_element.each_element do |be|
|
125
|
-
# p be.name
|
126
|
-
if be.name == "spreadsheet"
|
127
|
-
be.each_element do |se|
|
128
|
-
# p se
|
129
|
-
if se.name == "table"
|
130
|
-
# puts "table gefunden"
|
131
|
-
#se.each_element
|
132
|
-
# p se.attributes['name']
|
133
|
-
return_sheets << se.attributes['name']
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
# puts oo_element_count.to_s+" oo_element_count "
|
141
|
-
end
|
142
|
-
# puts oo_document_count.to_s+" oo_document_count "
|
143
|
-
return_sheets
|
144
|
-
end
|
145
|
-
|
146
|
-
def default_sheet=(s)
|
147
|
-
@default_sheet = s
|
148
|
-
end
|
149
|
-
|
150
|
-
private
|
151
|
-
|
152
184
|
def process_zipfile(zip, path='')
|
153
185
|
if zip.file.file? path
|
154
186
|
# puts %{#{path}: "#{zip.read(path)}"}
|
155
187
|
# puts %{#{path}:}
|
156
188
|
|
157
189
|
if path == "content.xml"
|
158
|
-
open('roo_content.xml','w') {|f|
|
190
|
+
open(@tmpdir+'/'+'roo_content.xml','w') {|f|
|
159
191
|
f << zip.read(path)
|
160
192
|
}
|
161
193
|
end
|
data/lib/roo/version.rb
CHANGED
data/test/test_roo.rb
CHANGED
@@ -2,10 +2,63 @@ require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
2
|
|
3
3
|
class TestRoo < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def
|
5
|
+
def test_sheets
|
6
|
+
oo = Openoffice.new("test/numbers1.ods")
|
7
|
+
assert_equal ["Tabelle1","Sheet2","Sheet3"], oo.sheets
|
6
8
|
end
|
7
|
-
|
8
|
-
def
|
9
|
-
|
9
|
+
|
10
|
+
def test_cell
|
11
|
+
oo = Openoffice.new("test/numbers1.ods")
|
12
|
+
oo.default_sheet = oo.sheets.first
|
13
|
+
assert_equal 1, oo.cell(1,1)
|
14
|
+
assert_equal 2, oo.cell(1,2)
|
15
|
+
assert_equal 3, oo.cell(1,3)
|
16
|
+
assert_equal 4, oo.cell(1,4)
|
17
|
+
assert_equal 5, oo.cell(2,1)
|
18
|
+
assert_equal 6, oo.cell(2,2)
|
19
|
+
assert_equal 7, oo.cell(2,3)
|
20
|
+
assert_equal 8, oo.cell(2,4)
|
21
|
+
assert_equal 9, oo.cell(2,5)
|
22
|
+
assert_equal "test", oo.cell(2,6)
|
23
|
+
assert_equal "string", oo.celltype(2,6)
|
24
|
+
assert_equal 11, oo.cell(2,7)
|
25
|
+
|
26
|
+
assert_equal 10, oo.cell(4,1)
|
27
|
+
assert_equal 11, oo.cell(4,2)
|
28
|
+
assert_equal 12, oo.cell(4,3)
|
29
|
+
assert_equal 13, oo.cell(4,4)
|
30
|
+
assert_equal 14, oo.cell(4,5)
|
31
|
+
|
32
|
+
assert_equal 10, oo.cell(4,'A')
|
33
|
+
assert_equal 11, oo.cell(4,'B')
|
34
|
+
assert_equal 12, oo.cell(4,'C')
|
35
|
+
assert_equal 13, oo.cell(4,'D')
|
36
|
+
assert_equal 14, oo.cell(4,'E')
|
37
|
+
|
38
|
+
assert_equal "date", oo.celltype(5,1)
|
39
|
+
assert_equal "1961-11-21", oo.cell(5,1)
|
10
40
|
end
|
41
|
+
|
42
|
+
def test_cell_address
|
43
|
+
oo = Openoffice.new("test/numbers1.ods")
|
44
|
+
oo.default_sheet = oo.sheets.first
|
45
|
+
assert_equal "tata", oo.cell(6,1)
|
46
|
+
assert_equal "tata", oo.cell(6,'A')
|
47
|
+
assert_equal "tata", oo.cell('A',6)
|
48
|
+
assert_equal "tata", oo.cell(6,'a')
|
49
|
+
assert_equal "tata", oo.cell('a',6)
|
50
|
+
assert_equal "thisisc8", oo.cell(8,3)
|
51
|
+
assert_equal "thisisc8", oo.cell(8,'C')
|
52
|
+
assert_equal "thisisc8", oo.cell('C',8)
|
53
|
+
assert_equal "thisisc8", oo.cell(8,'c')
|
54
|
+
assert_equal "thisisc8", oo.cell('c',8)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Version of the (XML) office document
|
58
|
+
# please note that "1.0" is returned even if it was created with OpenOffice V. 2.0
|
59
|
+
def test_officeversion
|
60
|
+
oo = Openoffice.new("test/numbers1.ods")
|
61
|
+
assert_equal "1.0", oo.officeversion
|
62
|
+
end
|
63
|
+
|
11
64
|
end
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>roo</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/roo"; return false'>
|
35
35
|
Get Version
|
36
|
-
<a href="http://rubyforge.org/projects/roo" class="numbers">0.0.
|
36
|
+
<a href="http://rubyforge.org/projects/roo" class="numbers">0.0.2</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘roo’</h1>
|
39
39
|
|
@@ -105,10 +105,7 @@ def test_sheets
|
|
105
105
|
<h2>Forum</h2>
|
106
106
|
|
107
107
|
|
108
|
-
<p><a href="http://groups.google.com/group/roo">http://groups.google.com/group/roo</a></p>
|
109
|
-
|
110
|
-
|
111
|
-
<p><span class="caps">TODO</span> – create Google Group – roo</p>
|
108
|
+
<p><a href="http://groups.google.com/group/ruby-roo">http://groups.google.com/group/ruby-roo</a></p>
|
112
109
|
|
113
110
|
|
114
111
|
<h2>License</h2>
|
data/website/index.txt
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: roo
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-05-
|
6
|
+
version: 0.0.2
|
7
|
+
date: 2007-05-30 00:00:00 +02:00
|
8
8
|
summary: roo can access the contents of OpenOffice-Spreadsheets
|
9
9
|
require_paths:
|
10
10
|
- lib
|