roo 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,17 @@
1
+ == 0.2.3 2007-06-02
2
+ * 3 enhancements:
3
+ * more robust call att Excel#default_sheet= when called with a name
4
+ * new method empty?
5
+ * refactoring
6
+ * 1 bugfix:
7
+ * bugfix in Excel#celltype
8
+ * bugfix (running under windows only) in closing the temp file before removing it
9
+
10
+ == 0.2.2 2007-06-01
11
+ * 1 bugfix:
12
+ * correct pathname for running with windows
13
+
14
+
1
15
  == 0.2.2 2007-06-01
2
16
  * 1 bugfix:
3
17
  * incorrect dependencies fixed
@@ -11,6 +11,8 @@ scripts/txt2html
11
11
  setup.rb
12
12
  test/test_helper.rb
13
13
  test/test_roo.rb
14
+ test/numbers1.ods
15
+ test/numbers1.xls
14
16
  website/index.html
15
17
  website/index.txt
16
18
  website/javascripts/rounded_corners_lite.inc.js
data/Rakefile CHANGED
@@ -76,6 +76,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
76
76
  # ['ruport', '>= 1.0.0'],
77
77
  # ['ruport-util', '>= 0.5.0'],
78
78
  ['parseexcel', '>= 0.5.1.1'],
79
+ ['rubyzip', '>= 0.9.1'],
79
80
  ]
80
81
  #p.spec_extras = {} # A hash of extra values to set in the gemspec.
81
82
  end
@@ -21,33 +21,29 @@ class Excel < Openoffice
21
21
  end
22
22
  end
23
23
 
24
+ # sets the working sheet (1,2,3,..)
25
+ #--
24
26
  # im Excel-Bereich muesste man wahrscheinlich intern mit Nummern arbeiten
25
27
  # von aussen arbeite ich mit (1,2,3... intern wird Index 0,1,2,...
26
28
  # verwendet.
29
+
27
30
  def default_sheet=(n)
31
+ unless n.kind_of?(Fixnum)
32
+ fail ArgumentError.new("Number expected")
33
+ end
28
34
  @default_sheet = n-1
29
35
  end
30
36
 
31
37
  def cell(row,col)
32
- #TODO: refactoring with normalize
33
- if row.class == String
34
- if col.class == Fixnum
35
- # ('A',1):
36
- # ('B', 5) -> (5, 2)
37
- row, col = col, row
38
- else
39
- raise FormatError
40
- end
41
- end
42
- if col.class == String
43
- col = Openoffice.letter_to_number(col)
44
- end
45
- #--
38
+ row,col = normalize(row,col)
46
39
  worksheet = @workbook.worksheet(@default_sheet)
47
40
  skip = 0
48
41
  line = 1
49
42
  worksheet.each(skip) { |row_par|
50
43
  if line == row
44
+ if row_par == nil
45
+ return nil
46
+ end
51
47
  cell = row_par.at(col-1)
52
48
  # p "celltype: "
53
49
  # p cell.type
@@ -64,6 +60,7 @@ class Excel < Openoffice
64
60
  end
65
61
 
66
62
  def celltype(row,col)
63
+ row,col = normalize(row,col)
67
64
  worksheet = @workbook.worksheet(@default_sheet)
68
65
  skip = 0
69
66
  line = 1
@@ -71,10 +68,10 @@ class Excel < Openoffice
71
68
  if line == row
72
69
  cell = row_par.at(col-1)
73
70
  case cell.type
74
- when :numeric then return cell.to_i
71
+ when :numeric then return "float"
75
72
  when :text then return "string"
76
73
  when :date then return "date"
77
- else return cell.to_s
74
+ else return cell.type
78
75
  end
79
76
  end
80
77
  line += 1
@@ -107,12 +104,18 @@ class Excel < Openoffice
107
104
  lr
108
105
  end
109
106
 
110
- def first_column_as_letter
111
- number_to_letter(first_column)
112
- end
113
-
114
- def last_column_as_letter
115
- number_to_letter(last_column)
107
+ def empty?(row, col)
108
+ row,col = normalize(row,col)
109
+ return true if row < first_row || row > last_row || col < first_column || col > last_column
110
+ # read_cells unless @cells_read
111
+ return true unless cell(row, col)
112
+ # p celltype(row,col)
113
+ #p cell(row,col)
114
+ return true if celltype(row, col) == "string" && cell(row, col) == ""
115
+ #when :text then return cell.to_s('latin1')
116
+ # p celltype(row,col)
117
+ # return true if cell(row, col) == ""
118
+ false
116
119
  end
117
120
 
118
121
  private
@@ -126,7 +129,7 @@ private
126
129
  worksheet.each(skip) { |row_par|
127
130
  if row_par
128
131
  row_par.each_with_index {|cell,i|
129
- # nicht beruechsichtigen, wenn nil und vorher noch nichts war
132
+ # nicht beruecksichtigen, wenn nil und vorher noch nichts war
130
133
  # p cell
131
134
  if !cell
132
135
  # nix
@@ -1,7 +1,6 @@
1
1
 
2
2
  require 'rubygems'
3
3
  require 'rexml/document'
4
- # require 'matrix'
5
4
  require 'fileutils'
6
5
  require 'zip/zipfilesystem'
7
6
  require 'date'
@@ -14,17 +13,17 @@ end
14
13
 
15
14
  class Openoffice
16
15
 
17
-
18
16
  def initialize(filename)
19
17
  @cells_read = false
20
18
  @filename = filename
21
19
  @tmpdir = "oo_"+$$.to_s
22
20
  unless File.exists?(@tmpdir)
23
- FileUtils::mkdir(@tmpdir) #TODO:
21
+ FileUtils::mkdir(@tmpdir)
24
22
  end
25
23
  extract_content
26
- file = File.new(@tmpdir+"/"+"roo_content.xml") # TODO:
24
+ file = File.new(File.join(@tmpdir, "roo_content.xml"))
27
25
  @doc = REXML::Document.new file
26
+ file.close
28
27
  @cell = Hash.new
29
28
  @cell_type = Hash.new
30
29
  if DateTime.now > Date.new(2007,5,31)
@@ -38,25 +37,15 @@ class Openoffice
38
37
  # (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
39
38
  # cell at first line, first row
40
39
  def cell(row,col)
41
- if row.class == String
42
- if col.class == Fixnum
43
- # ('A',1):
44
- # ('B', 5) -> (5, 2)
45
- row, col = col, row
46
- else
47
- raise FormatError
48
- end
49
- end
50
- if col.class == String
51
- col = Openoffice.letter_to_number(col)
52
- end
53
40
  read_cells unless @cells_read
41
+ row,col = normalize(row,col)
54
42
  @cell["#{row},#{col}"]
55
43
  end
56
44
 
57
45
  # returns the open-office type of a cell
58
46
  def celltype(row,col)
59
47
  read_cells unless @cells_read
48
+ row,col = normalize(row,col)
60
49
  @cell_type["#{row},#{col}"]
61
50
  end
62
51
 
@@ -97,6 +86,8 @@ class Openoffice
97
86
  @officeversion
98
87
  end
99
88
 
89
+ # shows the internal representation of all cells
90
+ # mainly for debugging purposes
100
91
  def to_s
101
92
  read_cells unless @cells_read
102
93
  @cell.inspect
@@ -194,7 +185,14 @@ class Openoffice
194
185
  n = n.div(26)
195
186
  end
196
187
  letters
197
- end
188
+ end
189
+
190
+ def empty?(row, col)
191
+ read_cells unless @cells_read
192
+ return true unless cell(row, col)
193
+ return true if celltype(row, col) == "string" && cell(row, col).empty?
194
+ false
195
+ end
198
196
 
199
197
  private
200
198
 
@@ -310,5 +308,22 @@ private
310
308
  end
311
309
  result
312
310
  end
313
-
311
+
312
+ # converts cell coordinate to numeric values of row,col
313
+ def normalize(row,col)
314
+ if row.class == String
315
+ if col.class == Fixnum
316
+ # ('A',1):
317
+ # ('B', 5) -> (5, 2)
318
+ row, col = col, row
319
+ else
320
+ raise FormatError
321
+ end
322
+ end
323
+ if col.class == String
324
+ col = Openoffice.letter_to_number(col)
325
+ end
326
+ return row,col
327
+ end
328
+
314
329
  end
@@ -2,7 +2,7 @@ module Roo #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
Binary file
Binary file
@@ -11,15 +11,15 @@ class TestRoo < Test::Unit::TestCase
11
11
  end
12
12
 
13
13
  def test_sheets
14
- oo = Openoffice.new("test/numbers1.ods")
14
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
15
15
  assert_equal ["Tabelle1","Name of Sheet 2","Sheet3"], oo.sheets
16
16
  #--
17
- oo = Excel.new("test/numbers1.xls")
17
+ oo = Excel.new(File.join("test","numbers1.xls"))
18
18
  assert_equal ["Tabelle1","Name of Sheet 2","Sheet3"], oo.sheets
19
19
  end
20
20
 
21
21
  def test_cell
22
- oo = Openoffice.new("test/numbers1.ods")
22
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
23
23
  oo.default_sheet = oo.sheets.first
24
24
  assert_equal 1, oo.cell(1,1)
25
25
  assert_equal 2, oo.cell(1,2)
@@ -33,6 +33,7 @@ class TestRoo < Test::Unit::TestCase
33
33
  assert_equal "test", oo.cell(2,6)
34
34
  assert_equal "string", oo.celltype(2,6)
35
35
  assert_equal 11, oo.cell(2,7)
36
+ assert_equal "float", oo.celltype(2,7)
36
37
 
37
38
  assert_equal 10, oo.cell(4,1)
38
39
  assert_equal 11, oo.cell(4,2)
@@ -49,7 +50,7 @@ class TestRoo < Test::Unit::TestCase
49
50
  assert_equal "date", oo.celltype(5,1)
50
51
  assert_equal "1961-11-21", oo.cell(5,1)
51
52
 
52
- oo = Excel.new("test/numbers1.xls")
53
+ oo = Excel.new(File.join("test","numbers1.xls"))
53
54
  oo.default_sheet = 1 # oo.sheets.first
54
55
  assert_equal 1, oo.cell(1,1)
55
56
  assert_equal 2, oo.cell(1,2)
@@ -63,6 +64,7 @@ class TestRoo < Test::Unit::TestCase
63
64
  assert_equal "test", oo.cell(2,6)
64
65
  assert_equal "string", oo.celltype(2,6)
65
66
  assert_equal 11, oo.cell(2,7)
67
+ assert_equal "float", oo.celltype(2,7)
66
68
 
67
69
  assert_equal 10, oo.cell(4,1)
68
70
  assert_equal 11, oo.cell(4,2)
@@ -81,7 +83,7 @@ class TestRoo < Test::Unit::TestCase
81
83
  end
82
84
 
83
85
  def test_cell_address
84
- oo = Openoffice.new("test/numbers1.ods")
86
+ oo = Openoffice.new(File.join("test/numbers1.ods"))
85
87
  oo.default_sheet = oo.sheets.first
86
88
  assert_equal "tata", oo.cell(6,1)
87
89
  assert_equal "tata", oo.cell(6,'A')
@@ -98,7 +100,7 @@ class TestRoo < Test::Unit::TestCase
98
100
  assert_equal "thisisd9", oo.cell('d',9)
99
101
  assert_equal "thisisa11", oo.cell('a',11)
100
102
 
101
- oo = Excel.new("test/numbers1.xls")
103
+ oo = Excel.new(File.join("test","numbers1.xls"))
102
104
  oo.default_sheet = 1 # oo.sheets.first
103
105
  assert_equal "tata", oo.cell(6,1)
104
106
  assert_equal "tata", oo.cell(6,'A')
@@ -120,18 +122,18 @@ class TestRoo < Test::Unit::TestCase
120
122
  # please note that "1.0" is returned even if it was created with OpenOffice V. 2.0
121
123
  def test_officeversion
122
124
  #-- OpenOffice
123
- oo = Openoffice.new("test/numbers1.ods")
125
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
124
126
  assert_equal "1.0", oo.officeversion
125
127
  #-- Excel
126
128
  if DateTime.now > Date.new(2007,6,15)
127
- oo = Excel.new("test/numbers1.xls")
129
+ oo = Excel.new(File.join("test","numbers1.xls"))
128
130
  assert_equal "1.0", oo.officeversion
129
131
  end
130
132
  end
131
133
 
132
134
  def test_rows
133
135
  #-- OpenOffice
134
- oo = Openoffice.new("test/numbers1.ods")
136
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
135
137
  oo.default_sheet = oo.sheets.first
136
138
  assert_equal 41, oo.cell('a',12)
137
139
  assert_equal 42, oo.cell('b',12)
@@ -141,7 +143,7 @@ class TestRoo < Test::Unit::TestCase
141
143
  assert_equal [41.0,42.0,43.0,44.0,45.0], oo.row(12)
142
144
  if DateTime.now > Date.new(2007,6,17)
143
145
  #-- Excel
144
- oo = Excel.new("test/numbers1.xls")
146
+ oo = Excel.new(File.join("test","numbers1.xls"))
145
147
  oo.default_sheet = 1 # oo.sheets.first
146
148
  assert_equal 41, oo.cell('a',12)
147
149
  assert_equal 42, oo.cell('b',12)
@@ -154,78 +156,78 @@ class TestRoo < Test::Unit::TestCase
154
156
 
155
157
  def test_last_row
156
158
  #-- OpenOffice
157
- oo = Openoffice.new("test/numbers1.ods")
159
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
158
160
  oo.default_sheet = oo.sheets.first
159
161
  assert_equal 18, oo.last_row
160
162
  #-- Excel
161
- oo = Excel.new("test/numbers1.xls")
163
+ oo = Excel.new(File.join("test","numbers1.xls"))
162
164
  oo.default_sheet = 1 # oo.sheets.first
163
165
  assert_equal 18, oo.last_row
164
166
  end
165
167
 
166
168
  def test_last_column
167
169
  #-- OpenOffice
168
- oo = Openoffice.new("test/numbers1.ods")
170
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
169
171
  oo.default_sheet = oo.sheets.first
170
172
  assert_equal 7, oo.last_column
171
173
  #-- Excel
172
- oo = Excel.new("test/numbers1.xls")
174
+ oo = Excel.new(File.join("test","numbers1.xls"))
173
175
  oo.default_sheet = 1 # oo.sheets.first
174
176
  assert_equal 7, oo.last_column
175
177
  end
176
178
 
177
179
  def test_last_column_as_letter
178
180
  #-- OpenOffice
179
- oo = Openoffice.new("test/numbers1.ods")
181
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
180
182
  oo.default_sheet = oo.sheets.first
181
183
  assert_equal 'G', oo.last_column_as_letter
182
184
  #-- Excel
183
- oo = Excel.new("test/numbers1.xls")
185
+ oo = Excel.new(File.join("test","numbers1.xls"))
184
186
  oo.default_sheet = 1 # oo.sheets.first
185
187
  assert_equal 'G', oo.last_column_as_letter
186
188
  end
187
189
 
188
190
  def test_first_row
189
191
  #-- OpenOffice
190
- oo = Openoffice.new("test/numbers1.ods")
192
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
191
193
  oo.default_sheet = oo.sheets.first
192
194
  assert_equal 1, oo.first_row
193
195
  #-- Excel
194
- oo = Excel.new("test/numbers1.xls")
196
+ oo = Excel.new(File.join("test","numbers1.xls"))
195
197
  oo.default_sheet = 1 # oo.sheets.first
196
198
  assert_equal 1, oo.first_row
197
199
  end
198
200
 
199
201
  def test_first_column
200
202
  #-- OpenOffice
201
- oo = Openoffice.new("test/numbers1.ods")
203
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
202
204
  oo.default_sheet = oo.sheets.first
203
205
  assert_equal 1, oo.first_column
204
206
  #-- Excel
205
- oo = Excel.new("test/numbers1.xls")
207
+ oo = Excel.new(File.join("test","numbers1.xls"))
206
208
  oo.default_sheet = 1 # oo.sheets.first
207
209
  assert_equal 1, oo.first_column
208
210
  end
209
211
 
210
212
  def test_first_column_as_letter
211
213
  #-- OpenOffice
212
- oo = Openoffice.new("test/numbers1.ods")
214
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
213
215
  oo.default_sheet = oo.sheets.first
214
216
  assert_equal 'A', oo.first_column_as_letter
215
217
  #-- Excel
216
- oo = Excel.new("test/numbers1.xls")
218
+ oo = Excel.new(File.join("test","numbers1.xls"))
217
219
  oo.default_sheet = 1 # oo.sheets.first
218
220
  assert_equal 'A', oo.first_column_as_letter
219
221
  end
220
222
 
221
223
  def test_sheetname
222
224
  #-- OpenOffice
223
- oo = Openoffice.new("test/numbers1.ods")
225
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
224
226
  oo.default_sheet = "Name of Sheet 2"
225
227
  assert_equal 'I am sheet 2', oo.cell('C',5)
226
228
  if DateTime.now > Date.new(2007,6,16)
227
229
  #-- Excel
228
- oo = Excel.new("test/numbers1.xls")
230
+ oo = Excel.new(File.join("test","numbers1.xls"))
229
231
  oo.default_sheet = "Name of Sheet 2"
230
232
  assert_equal 'I am sheet 2', oo.cell('C',5)
231
233
  end
@@ -233,7 +235,7 @@ class TestRoo < Test::Unit::TestCase
233
235
 
234
236
  def test_boundaries
235
237
  #-- OpenOffice
236
- oo = Openoffice.new("test/numbers1.ods")
238
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
237
239
  oo.default_sheet = "Name of Sheet 2"
238
240
  assert_equal 2, oo.first_column
239
241
  assert_equal 'B', oo.first_column_as_letter
@@ -242,9 +244,8 @@ class TestRoo < Test::Unit::TestCase
242
244
  assert_equal 14, oo.last_row
243
245
  assert_equal 'E', oo.first_row_as_letter
244
246
  assert_equal 'N', oo.last_row_as_letter
245
- assert_equal 'N', oo.last_row.as_letter
246
247
  #-- Excel
247
- oo = Excel.new("test/numbers1.xls")
248
+ oo = Excel.new(File.join("test","numbers1.xls"))
248
249
  oo.default_sheet = 2 # "Name of Sheet 2"
249
250
  assert_equal 2, oo.first_column
250
251
  assert_equal 'B', oo.first_column_as_letter
@@ -253,12 +254,11 @@ class TestRoo < Test::Unit::TestCase
253
254
  assert_equal 14, oo.last_row
254
255
  assert_equal 'E', oo.first_row_as_letter
255
256
  assert_equal 'N', oo.last_row_as_letter
256
- assert_equal 'N', oo.last_row.as_letter
257
257
  end
258
258
 
259
259
  def test_multiple_letters
260
260
  #-- OpenOffice
261
- oo = Openoffice.new("test/numbers1.ods")
261
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
262
262
  oo.default_sheet = "Sheet3"
263
263
  assert_equal "i am AA", oo.cell('AA',1)
264
264
  assert_equal "i am AB", oo.cell('AB',1)
@@ -266,7 +266,7 @@ class TestRoo < Test::Unit::TestCase
266
266
  assert_equal 'BA', oo.last_column_as_letter
267
267
  assert_equal "i am BA", oo.cell(1,'BA')
268
268
  #-- Excel
269
- oo = Excel.new("test/numbers1.xls")
269
+ oo = Excel.new(File.join("test","numbers1.xls"))
270
270
  oo.default_sheet = 3 # "Sheet3"
271
271
  assert_equal "i am AA", oo.cell('AA',1)
272
272
  assert_equal "i am AB", oo.cell('AB',1)
@@ -275,4 +275,33 @@ class TestRoo < Test::Unit::TestCase
275
275
  assert_equal "i am BA", oo.cell(1,'BA')
276
276
  end
277
277
 
278
+ def test_setting_cell
279
+ assert true
280
+ end
281
+
282
+ def test_argument_error
283
+ oo = Excel.new(File.join("test","numbers1.xls"))
284
+ assert_raise(ArgumentError) {
285
+ oo.default_sheet = "first sheet"
286
+ }
287
+ assert_nothing_raised(ArgumentError) {
288
+ oo.default_sheet = 1
289
+ }
290
+ end
291
+
292
+ def test_empty_eh
293
+ #-- OpenOffice
294
+ oo = Openoffice.new(File.join("test","numbers1.ods"))
295
+ oo.default_sheet = oo.sheets.first
296
+ assert oo.empty?('a',14)
297
+ assert ! oo.empty?('a',15)
298
+ assert oo.empty?('a',20)
299
+ #-- Excel
300
+ oo = Excel.new(File.join("test","numbers1.xls"))
301
+ oo.default_sheet = 1
302
+ assert oo.empty?('a',14)
303
+ assert ! oo.empty?('a',15)
304
+ assert oo.empty?('a',20)
305
+
306
+ end
278
307
  end
@@ -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.2.2</a>
36
+ <a href="http://rubyforge.org/projects/roo" class="numbers">0.2.3</a>
37
37
  </div>
38
38
  <h2>What</h2>
39
39
 
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.2.2
7
- date: 2007-06-01 00:00:00 +02:00
6
+ version: 0.2.3
7
+ date: 2007-06-02 00:00:00 +02:00
8
8
  summary: roo can access the contents of OpenOffice-Spreadsheets
9
9
  require_paths:
10
10
  - lib
@@ -42,6 +42,8 @@ files:
42
42
  - setup.rb
43
43
  - test/test_helper.rb
44
44
  - test/test_roo.rb
45
+ - test/numbers1.ods
46
+ - test/numbers1.xls
45
47
  - website/index.html
46
48
  - website/index.txt
47
49
  - website/javascripts/rounded_corners_lite.inc.js
@@ -75,3 +77,12 @@ dependencies:
75
77
  - !ruby/object:Gem::Version
76
78
  version: 0.5.1.1
77
79
  version:
80
+ - !ruby/object:Gem::Dependency
81
+ name: rubyzip
82
+ version_requirement:
83
+ version_requirements: !ruby/object:Gem::Version::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 0.9.1
88
+ version: