roo 2.0.1 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_roo.rb CHANGED
@@ -1,2088 +1,2091 @@
1
- # encoding: utf-8
2
- # damit keine falschen Vermutungen aufkommen: Ich habe religioes rein gar nichts
3
- # mit diesem Bibelbund zu tun, aber die hatten eine ziemlich grosse
4
- # Spreadsheet-Datei mit ca. 3500 Zeilen oeffentlich im Netz, die sich ganz gut
5
- # zum Testen eignete.
6
- #
7
- #--
8
- # these test cases were developed to run under Linux OS, some commands
9
- # (like 'diff') must be changed (or commented out ;-)) if you want to run
10
- # the tests under another OS
11
- #
12
-
13
- #TODO
14
- # Look at formulas in excel - does not work with date/time
15
-
16
- # Dump warnings that come from the test to open files
17
- # with the wrong spreadsheet class
18
- #STDERR.reopen "/dev/null","w"
19
-
20
- require 'test_helper'
21
- require 'stringio'
22
-
23
- class TestRoo < Minitest::Test
24
-
25
- OPENOFFICE = true # do OpenOffice-Spreadsheet Tests? (.ods files)
26
- EXCELX = true # do Excelx Tests? (.xlsx files)
27
- LIBREOFFICE = true # do LibreOffice tests? (.ods files)
28
- CSV = true # do CSV tests? (.csv files)
29
-
30
- FORMATS = [
31
- :excelx,
32
- :openoffice,
33
- :libreoffice
34
- ]
35
-
36
- ONLINE = false
37
- LONG_RUN = false
38
-
39
- def fixture_filename(name, format)
40
- case format
41
- when :excelx
42
- "#{name}.xlsx"
43
- when :openoffice, :libreoffice
44
- "#{name}.ods"
45
- else
46
- raise ArgumentError, "unexpected format #{format}"
47
- end
48
- end
49
-
50
- # call a block of code for each spreadsheet type
51
- # and yield a reference to the roo object
52
- def with_each_spreadsheet(options)
53
- if options[:format]
54
- formats = Array(options[:format])
55
- invalid_formats = formats - FORMATS
56
- unless invalid_formats.empty?
57
- raise "invalid spreadsheet types: #{invalid_formats.join(', ')}"
58
- end
59
- else
60
- formats = FORMATS
61
- end
62
- formats.each do |format|
63
- begin
64
- yield Roo::Spreadsheet.open(File.join(TESTDIR,
65
- fixture_filename(options[:name], format)))
66
- rescue => e
67
- raise e, "#{e.message} for #{format}", e.backtrace unless options[:ignore_errors]
68
- end
69
- end
70
- end
71
-
72
- def test_sheets_csv
73
- if CSV
74
- oo = Roo::CSV.new(File.join(TESTDIR,'numbers1.csv'))
75
- assert_equal ["default"], oo.sheets
76
- assert_raises(RangeError) { oo.default_sheet = "no_sheet" }
77
- assert_raises(TypeError) { oo.default_sheet = [1,2,3] }
78
- oo.sheets.each { |sh|
79
- oo.default_sheet = sh
80
- assert_equal sh, oo.default_sheet
81
- }
82
- end
83
- end
84
-
85
- def test_sheets
86
- with_each_spreadsheet(:name=>'numbers1') do |oo|
87
- assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], oo.sheets
88
- assert_raises(RangeError) { oo.default_sheet = "no_sheet" }
89
- assert_raises(TypeError) { oo.default_sheet = [1,2,3] }
90
- oo.sheets.each { |sh|
91
- oo.default_sheet = sh
92
- assert_equal sh, oo.default_sheet
93
- }
94
- end
95
- end
96
-
97
- def test_cells
98
- with_each_spreadsheet(:name=>'numbers1') do |oo|
99
- # warum ist Auswaehlen erstes sheet hier nicht
100
- # mehr drin?
101
- oo.default_sheet = oo.sheets.first
102
- assert_equal 1, oo.cell(1,1)
103
- assert_equal 2, oo.cell(1,2)
104
- assert_equal 3, oo.cell(1,3)
105
- assert_equal 4, oo.cell(1,4)
106
- assert_equal 5, oo.cell(2,1)
107
- assert_equal 6, oo.cell(2,2)
108
- assert_equal 7, oo.cell(2,3)
109
- assert_equal 8, oo.cell(2,4)
110
- assert_equal 9, oo.cell(2,5)
111
- assert_equal "test", oo.cell(2,6)
112
- assert_equal :string, oo.celltype(2,6)
113
- assert_equal 11, oo.cell(2,7)
114
- unless oo.kind_of? Roo::CSV
115
- assert_equal :float, oo.celltype(2,7)
116
- end
117
- assert_equal 10, oo.cell(4,1)
118
- assert_equal 11, oo.cell(4,2)
119
- assert_equal 12, oo.cell(4,3)
120
- assert_equal 13, oo.cell(4,4)
121
- assert_equal 14, oo.cell(4,5)
122
- assert_equal 10, oo.cell(4,'A')
123
- assert_equal 11, oo.cell(4,'B')
124
- assert_equal 12, oo.cell(4,'C')
125
- assert_equal 13, oo.cell(4,'D')
126
- assert_equal 14, oo.cell(4,'E')
127
- unless oo.kind_of? Roo::CSV
128
- assert_equal :date, oo.celltype(5,1)
129
- assert_equal Date.new(1961,11,21), oo.cell(5,1)
130
- assert_equal "1961-11-21", oo.cell(5,1).to_s
131
- end
132
- end
133
- end
134
-
135
- def test_celltype
136
- with_each_spreadsheet(:name=>'numbers1') do |oo|
137
- assert_equal :string, oo.celltype(2,6)
138
- end
139
- end
140
-
141
- def test_cell_address
142
- with_each_spreadsheet(:name=>'numbers1') do |oo|
143
- assert_equal "tata", oo.cell(6,1)
144
- assert_equal "tata", oo.cell(6,'A')
145
- assert_equal "tata", oo.cell('A',6)
146
- assert_equal "tata", oo.cell(6,'a')
147
- assert_equal "tata", oo.cell('a',6)
148
- assert_raises(ArgumentError) { assert_equal "tata", oo.cell('a','f') }
149
- assert_raises(ArgumentError) { assert_equal "tata", oo.cell('f','a') }
150
- assert_equal "thisisc8", oo.cell(8,3)
151
- assert_equal "thisisc8", oo.cell(8,'C')
152
- assert_equal "thisisc8", oo.cell('C',8)
153
- assert_equal "thisisc8", oo.cell(8,'c')
154
- assert_equal "thisisc8", oo.cell('c',8)
155
- assert_equal "thisisd9", oo.cell('d',9)
156
- assert_equal "thisisa11", oo.cell('a',11)
157
- end
158
- end
159
-
160
- def test_office_version
161
- with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
162
- assert_equal "1.0", oo.officeversion
163
- end
164
- end
165
-
166
- def test_libre_office
167
- if LIBREOFFICE
168
- oo = Roo::LibreOffice.new(File.join(TESTDIR, "numbers1.ods"))
169
- oo.default_sheet = oo.sheets.first
170
- assert_equal 41, oo.cell('a',12)
171
- end
172
- end
173
-
174
- def test_sheetname
175
- with_each_spreadsheet(:name=>'numbers1') do |oo|
176
- oo.default_sheet = "Name of Sheet 2"
177
- assert_equal 'I am sheet 2', oo.cell('C',5)
178
- assert_raises(RangeError) { oo.default_sheet = "non existing sheet name" }
179
- assert_raises(RangeError) { oo.default_sheet = "non existing sheet name" }
180
- assert_raises(RangeError) { oo.cell('C',5,"non existing sheet name")}
181
- assert_raises(RangeError) { oo.celltype('C',5,"non existing sheet name")}
182
- assert_raises(RangeError) { oo.empty?('C',5,"non existing sheet name")}
183
- assert_raises(RangeError) { oo.formula?('C',5,"non existing sheet name")}
184
- assert_raises(RangeError) { oo.formula('C',5,"non existing sheet name")}
185
- assert_raises(RangeError) { oo.set('C',5,42,"non existing sheet name")}
186
- assert_raises(RangeError) { oo.formulas("non existing sheet name")}
187
- assert_raises(RangeError) { oo.to_yaml({},1,1,1,1,"non existing sheet name")}
188
- end
189
- end
190
-
191
- def test_argument_error
192
- with_each_spreadsheet(:name=>'numbers1') do |oo|
193
- oo.default_sheet = "Tabelle1"
194
- end
195
- end
196
-
197
- def test_bug_contiguous_cells
198
- with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
199
- oo.default_sheet = "Sheet4"
200
- assert_equal Date.new(2007,06,16), oo.cell('a',1)
201
- assert_equal 10, oo.cell('b',1)
202
- assert_equal 10, oo.cell('c',1)
203
- assert_equal 10, oo.cell('d',1)
204
- assert_equal 10, oo.cell('e',1)
205
- end
206
- end
207
-
208
- def test_bug_italo_ve
209
- with_each_spreadsheet(:name=>'numbers1') do |oo|
210
- oo.default_sheet = "Sheet5"
211
- assert_equal 1, oo.cell('A',1)
212
- assert_equal 5, oo.cell('b',1)
213
- assert_equal 5, oo.cell('c',1)
214
- assert_equal 2, oo.cell('a',2)
215
- assert_equal 3, oo.cell('a',3)
216
- end
217
- end
218
-
219
- def test_italo_table
220
- with_each_spreadsheet(:name=>'simple_spreadsheet_from_italo', :format=>:openoffice) do |oo|
221
- assert_equal '1', oo.cell('A',1)
222
- assert_equal '1', oo.cell('B',1)
223
- assert_equal '1', oo.cell('C',1)
224
- assert_equal 1, oo.cell('A',2).to_i
225
- assert_equal 2, oo.cell('B',2).to_i
226
- assert_equal 1, oo.cell('C',2).to_i
227
- assert_equal 1, oo.cell('A',3)
228
- assert_equal 3, oo.cell('B',3)
229
- assert_equal 1, oo.cell('C',3)
230
- assert_equal 'A', oo.cell('A',4)
231
- assert_equal 'A', oo.cell('B',4)
232
- assert_equal 'A', oo.cell('C',4)
233
- assert_equal 0.01, oo.cell('A',5)
234
- assert_equal 0.01, oo.cell('B',5)
235
- assert_equal 0.01, oo.cell('C',5)
236
- assert_equal 0.03, oo.cell('a',5)+oo.cell('b',5)+oo.cell('c',5)
237
-
238
- # Cells values in row 1:
239
- assert_equal "1:string", oo.cell(1, 1)+":"+oo.celltype(1, 1).to_s
240
- assert_equal "1:string",oo.cell(1, 2)+":"+oo.celltype(1, 2).to_s
241
- assert_equal "1:string",oo.cell(1, 3)+":"+oo.celltype(1, 3).to_s
242
-
243
- # Cells values in row 2:
244
- assert_equal "1:string",oo.cell(2, 1)+":"+oo.celltype(2, 1).to_s
245
- assert_equal "2:string",oo.cell(2, 2)+":"+oo.celltype(2, 2).to_s
246
- assert_equal "1:string",oo.cell(2, 3)+":"+oo.celltype(2, 3).to_s
247
-
248
- # Cells values in row 3:
249
- assert_equal "1.0:float",oo.cell(3, 1).to_s+":"+oo.celltype(3, 1).to_s
250
- assert_equal "3.0:float",oo.cell(3, 2).to_s+":"+oo.celltype(3, 2).to_s
251
- assert_equal "1.0:float",oo.cell(3, 3).to_s+":"+oo.celltype(3, 3).to_s
252
-
253
- # Cells values in row 4:
254
- assert_equal "A:string",oo.cell(4, 1)+":"+oo.celltype(4, 1).to_s
255
- assert_equal "A:string",oo.cell(4, 2)+":"+oo.celltype(4, 2).to_s
256
- assert_equal "A:string",oo.cell(4, 3)+":"+oo.celltype(4, 3).to_s
257
-
258
- # Cells values in row 5:
259
- if oo.class == Roo::OpenOffice
260
- assert_equal "0.01:percentage",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
261
- assert_equal "0.01:percentage",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
262
- assert_equal "0.01:percentage",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
263
- else
264
- assert_equal "0.01:float",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
265
- assert_equal "0.01:float",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
266
- assert_equal "0.01:float",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
267
- end
268
- end
269
- end
270
-
271
- def test_formula_openoffice
272
- with_each_spreadsheet(:name=>'formula', :format=>:openoffice) do |oo|
273
- assert_equal 1, oo.cell('A',1)
274
- assert_equal 2, oo.cell('A',2)
275
- assert_equal 3, oo.cell('A',3)
276
- assert_equal 4, oo.cell('A',4)
277
- assert_equal 5, oo.cell('A',5)
278
- assert_equal 6, oo.cell('A',6)
279
- assert_equal 21, oo.cell('A',7)
280
- assert_equal :formula, oo.celltype('A',7)
281
- assert_equal "=[Sheet2.A1]", oo.formula('C',7)
282
- assert_nil oo.formula('A',6)
283
- assert_equal [[7, 1, "=SUM([.A1:.A6])"],
284
- [7, 2, "=SUM([.$A$1:.B6])"],
285
- [7, 3, "=[Sheet2.A1]"],
286
- [8, 2, "=SUM([.$A$1:.B7])"],
287
- ], oo.formulas(oo.sheets.first)
288
-
289
- # setting a cell
290
- oo.set('A',15, 41)
291
- assert_equal 41, oo.cell('A',15)
292
- oo.set('A',16, "41")
293
- assert_equal "41", oo.cell('A',16)
294
- oo.set('A',17, 42.5)
295
- assert_equal 42.5, oo.cell('A',17)
296
- end
297
- end
298
-
299
- def test_header_with_brackets_excelx
300
- with_each_spreadsheet(:name => 'advanced_header', :format => :openoffice) do |oo|
301
- parsed_head = oo.parse(:headers => true)
302
- assert_equal "Date(yyyy-mm-dd)", oo.cell('A',1)
303
- assert_equal parsed_head[0].keys, ["Date(yyyy-mm-dd)"]
304
- assert_equal parsed_head[0].values, ["Date(yyyy-mm-dd)"]
305
- end
306
- end
307
-
308
- def test_formula_excelx
309
- with_each_spreadsheet(:name=>'formula', :format=>:excelx) do |oo|
310
- assert_equal 1, oo.cell('A',1)
311
- assert_equal 2, oo.cell('A',2)
312
- assert_equal 3, oo.cell('A',3)
313
- assert_equal 4, oo.cell('A',4)
314
- assert_equal 5, oo.cell('A',5)
315
- assert_equal 6, oo.cell('A',6)
316
- assert_equal 21, oo.cell('A',7)
317
- assert_equal :formula, oo.celltype('A',7)
318
- #steht nicht in Datei, oder?
319
- #nein, diesen Bezug habe ich nur in der OpenOffice-Datei
320
- #assert_equal "=[Sheet2.A1]", oo.formula('C',7)
321
- assert_nil oo.formula('A',6)
322
- # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
323
- # [7, 2, "=SUM([.$A$1:.B6])"],
324
- #[7, 3, "=[Sheet2.A1]"],
325
- #[8, 2, "=SUM([.$A$1:.B7])"],
326
- #], oo.formulas(oo.sheets.first)
327
- assert_equal [[7, 1, 'SUM(A1:A6)'],
328
- [7, 2, 'SUM($A$1:B6)'],
329
- # [7, 3, "=[Sheet2.A1]"],
330
- # [8, 2, "=SUM([.$A$1:.B7])"],
331
- ], oo.formulas(oo.sheets.first)
332
-
333
- # setting a cell
334
- oo.set('A',15, 41)
335
- assert_equal 41, oo.cell('A',15)
336
- oo.set('A',16, "41")
337
- assert_equal "41", oo.cell('A',16)
338
- oo.set('A',17, 42.5)
339
- assert_equal 42.5, oo.cell('A',17)
340
- end
341
- end
342
-
343
- def test_borders_sheets
344
- with_each_spreadsheet(:name=>'borders') do |oo|
345
- oo.default_sheet = oo.sheets[1]
346
- assert_equal 6, oo.first_row
347
- assert_equal 11, oo.last_row
348
- assert_equal 4, oo.first_column
349
- assert_equal 8, oo.last_column
350
-
351
- oo.default_sheet = oo.sheets.first
352
- assert_equal 5, oo.first_row
353
- assert_equal 10, oo.last_row
354
- assert_equal 3, oo.first_column
355
- assert_equal 7, oo.last_column
356
-
357
- oo.default_sheet = oo.sheets[2]
358
- assert_equal 7, oo.first_row
359
- assert_equal 12, oo.last_row
360
- assert_equal 5, oo.first_column
361
- assert_equal 9, oo.last_column
362
- end
363
- end
364
-
365
- def test_only_one_sheet
366
- with_each_spreadsheet(:name=>'only_one_sheet') do |oo|
367
- assert_equal 42, oo.cell('B',4)
368
- assert_equal 43, oo.cell('C',4)
369
- assert_equal 44, oo.cell('D',4)
370
- oo.default_sheet = oo.sheets.first
371
- assert_equal 42, oo.cell('B',4)
372
- assert_equal 43, oo.cell('C',4)
373
- assert_equal 44, oo.cell('D',4)
374
- end
375
- end
376
-
377
- def test_openoffice_download_uri_and_zipped
378
- if OPENOFFICE
379
- if ONLINE
380
- url = 'http://spazioinwind.libero.it/s2/rata.ods.zip'
381
- sheet = Roo::OpenOffice.new(url, packed: :zip)
382
- #has been changed: assert_equal 'ist "e" im Nenner von H(s)', sheet.cell('b', 5)
383
- assert_in_delta 0.001, 505.14, sheet.cell('c', 33).to_f
384
- end
385
- end
386
- end
387
-
388
- def test_openoffice_zipped
389
- if OPENOFFICE
390
- oo = Roo::OpenOffice.new(File.join(TESTDIR,"bode-v1.ods.zip"), packed: :zip)
391
- assert oo
392
- assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
393
- end
394
- end
395
-
396
- def test_bug_ric
397
- with_each_spreadsheet(:name=>'ric', :format=>:openoffice) do |oo|
398
- assert oo.empty?('A',1)
399
- assert oo.empty?('B',1)
400
- assert oo.empty?('C',1)
401
- assert oo.empty?('D',1)
402
- expected = 1
403
- letter = 'e'
404
- while letter <= 'u'
405
- assert_equal expected, oo.cell(letter,1)
406
- letter.succ!
407
- expected += 1
408
- end
409
- assert_equal 'J', oo.cell('v',1)
410
- assert_equal 'P', oo.cell('w',1)
411
- assert_equal 'B', oo.cell('x',1)
412
- assert_equal 'All', oo.cell('y',1)
413
- assert_equal 0, oo.cell('a',2)
414
- assert oo.empty?('b',2)
415
- assert oo.empty?('c',2)
416
- assert oo.empty?('d',2)
417
- assert_equal 'B', oo.cell('e',2)
418
- assert_equal 'B', oo.cell('f',2)
419
- assert_equal 'B', oo.cell('g',2)
420
- assert_equal 'B', oo.cell('h',2)
421
- assert_equal 'B', oo.cell('i',2)
422
- assert_equal 'B', oo.cell('j',2)
423
- assert_equal 'B', oo.cell('k',2)
424
- assert_equal 'B', oo.cell('l',2)
425
- assert_equal 'B', oo.cell('m',2)
426
- assert_equal 'B', oo.cell('n',2)
427
- assert_equal 'B', oo.cell('o',2)
428
- assert_equal 'B', oo.cell('p',2)
429
- assert_equal 'B', oo.cell('q',2)
430
- assert_equal 'B', oo.cell('r',2)
431
- assert_equal 'B', oo.cell('s',2)
432
- assert oo.empty?('t',2)
433
- assert oo.empty?('u',2)
434
- assert_equal 0 , oo.cell('v',2)
435
- assert_equal 0 , oo.cell('w',2)
436
- assert_equal 15 , oo.cell('x',2)
437
- assert_equal 15 , oo.cell('y',2)
438
- end
439
- end
440
-
441
- def test_mehrteilig
442
- with_each_spreadsheet(:name=>'Bibelbund1', :format=>:openoffice) do |oo|
443
- assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
444
- end
445
- #if EXCELX
446
- # #Datei gibt es noch nicht
447
- # oo = Roo::Excelx.new(File.join(TESTDIR,"Bibelbund1.xlsx"))
448
- # oo.default_sheet = oo.sheets.first
449
- # assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
450
- #end
451
- end
452
-
453
- # "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt
454
- # wird.
455
- # Besser: Methode um temporaeres Dir. portabel zu bestimmen
456
- def test_huge_document_to_csv
457
- if LONG_RUN
458
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[
459
- :openoffice,
460
- :excelx
461
- # Google hier nicht, weil Google-Spreadsheets nicht so gross werden
462
- # duerfen
463
- ]) do |oo|
464
- Dir.mktmpdir do |tempdir|
465
- assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
466
- assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46,'A')
467
- assert_equal "Tagebuch aus Chile Juli 1977", oo.cell(55,'A')
468
- assert oo.to_csv(File.join(tempdir,"Bibelbund.csv"))
469
- assert File.exists?(File.join(tempdir,"Bibelbund.csv"))
470
- assert_equal "", file_diff(File.join(TESTDIR, "Bibelbund.csv"), File.join(tempdir,"Bibelbund.csv")),
471
- "error in class #{oo.class}"
472
- #end
473
- end
474
- end
475
- end
476
- end
477
-
478
- def test_bug_quotes_excelx
479
- if LONG_RUN
480
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice, :excelx]) do |oo|
481
- oo.default_sheet = oo.sheets.first
482
- assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
483
- oo.cell('a',76)
484
- oo.to_csv("csv#{$$}")
485
- assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
486
- oo.cell('a',78)
487
- File.delete_if_exist("csv#{$$}")
488
- end
489
- end
490
- end
491
-
492
- def test_bug_mehrere_datum
493
- with_each_spreadsheet(:name=>'numbers1') do |oo|
494
- oo.default_sheet = 'Sheet5'
495
- assert_equal :date, oo.celltype('A',4)
496
- assert_equal :date, oo.celltype('B',4)
497
- assert_equal :date, oo.celltype('C',4)
498
- assert_equal :date, oo.celltype('D',4)
499
- assert_equal :date, oo.celltype('E',4)
500
- assert_equal Date.new(2007,11,21), oo.cell('A',4)
501
- assert_equal Date.new(2007,11,21), oo.cell('B',4)
502
- assert_equal Date.new(2007,11,21), oo.cell('C',4)
503
- assert_equal Date.new(2007,11,21), oo.cell('D',4)
504
- assert_equal Date.new(2007,11,21), oo.cell('E',4)
505
- assert_equal :float, oo.celltype('A',5)
506
- assert_equal :float, oo.celltype('B',5)
507
- assert_equal :float, oo.celltype('C',5)
508
- assert_equal :float, oo.celltype('D',5)
509
- assert_equal :float, oo.celltype('E',5)
510
- assert_equal 42, oo.cell('A',5)
511
- assert_equal 42, oo.cell('B',5)
512
- assert_equal 42, oo.cell('C',5)
513
- assert_equal 42, oo.cell('D',5)
514
- assert_equal 42, oo.cell('E',5)
515
- assert_equal :string, oo.celltype('A',6)
516
- assert_equal :string, oo.celltype('B',6)
517
- assert_equal :string, oo.celltype('C',6)
518
- assert_equal :string, oo.celltype('D',6)
519
- assert_equal :string, oo.celltype('E',6)
520
- assert_equal "ABC", oo.cell('A',6)
521
- assert_equal "ABC", oo.cell('B',6)
522
- assert_equal "ABC", oo.cell('C',6)
523
- assert_equal "ABC", oo.cell('D',6)
524
- assert_equal "ABC", oo.cell('E',6)
525
- end
526
- end
527
-
528
- def test_multiple_sheets
529
- with_each_spreadsheet(:name=>'numbers1') do |oo|
530
- 2.times do
531
- oo.default_sheet = "Tabelle1"
532
- assert_equal 1, oo.cell(1,1)
533
- assert_equal 1, oo.cell(1,1,"Tabelle1")
534
- assert_equal "I am sheet 2", oo.cell('C',5,"Name of Sheet 2")
535
- sheetname = 'Sheet5'
536
- assert_equal :date, oo.celltype('A',4,sheetname)
537
- assert_equal :date, oo.celltype('B',4,sheetname)
538
- assert_equal :date, oo.celltype('C',4,sheetname)
539
- assert_equal :date, oo.celltype('D',4,sheetname)
540
- assert_equal :date, oo.celltype('E',4,sheetname)
541
- assert_equal Date.new(2007,11,21), oo.cell('A',4,sheetname)
542
- assert_equal Date.new(2007,11,21), oo.cell('B',4,sheetname)
543
- assert_equal Date.new(2007,11,21), oo.cell('C',4,sheetname)
544
- assert_equal Date.new(2007,11,21), oo.cell('D',4,sheetname)
545
- assert_equal Date.new(2007,11,21), oo.cell('E',4,sheetname)
546
- assert_equal :float, oo.celltype('A',5,sheetname)
547
- assert_equal :float, oo.celltype('B',5,sheetname)
548
- assert_equal :float, oo.celltype('C',5,sheetname)
549
- assert_equal :float, oo.celltype('D',5,sheetname)
550
- assert_equal :float, oo.celltype('E',5,sheetname)
551
- assert_equal 42, oo.cell('A',5,sheetname)
552
- assert_equal 42, oo.cell('B',5,sheetname)
553
- assert_equal 42, oo.cell('C',5,sheetname)
554
- assert_equal 42, oo.cell('D',5,sheetname)
555
- assert_equal 42, oo.cell('E',5,sheetname)
556
- assert_equal :string, oo.celltype('A',6,sheetname)
557
- assert_equal :string, oo.celltype('B',6,sheetname)
558
- assert_equal :string, oo.celltype('C',6,sheetname)
559
- assert_equal :string, oo.celltype('D',6,sheetname)
560
- assert_equal :string, oo.celltype('E',6,sheetname)
561
- assert_equal "ABC", oo.cell('A',6,sheetname)
562
- assert_equal "ABC", oo.cell('B',6,sheetname)
563
- assert_equal "ABC", oo.cell('C',6,sheetname)
564
- assert_equal "ABC", oo.cell('D',6,sheetname)
565
- assert_equal "ABC", oo.cell('E',6,sheetname)
566
- oo.reload
567
- end
568
- end
569
- end
570
-
571
-
572
- def test_bug_empty_sheet
573
- with_each_spreadsheet(:name=>'formula', :format=>[:openoffice, :excelx]) do |oo|
574
- oo.default_sheet = 'Sheet3' # is an empty sheet
575
- Dir.mktmpdir do |tempdir|
576
- oo.to_csv(File.join(tempdir,"emptysheet.csv"))
577
- assert_equal "", `cat #{File.join(tempdir,"emptysheet.csv")}`
578
- end
579
- end
580
- end
581
-
582
- def test_find_by_row_huge_document
583
- if LONG_RUN
584
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice, :excelx]) do |oo|
585
- oo.default_sheet = oo.sheets.first
586
- rec = oo.find 20
587
- assert rec
588
- # assert_equal "Brief aus dem Sekretariat", rec[0]
589
- #p rec
590
- assert_equal "Brief aus dem Sekretariat", rec[0]['TITEL']
591
- rec = oo.find 22
592
- assert rec
593
- # assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]
594
- assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]['TITEL']
595
- end
596
- end
597
- end
598
-
599
- def test_find_by_row
600
- with_each_spreadsheet(:name=>'numbers1') do |oo|
601
- oo.header_line = nil
602
- rec = oo.find 16
603
- assert rec
604
- assert_nil oo.header_line
605
- # keine Headerlines in diesem Beispiel definiert
606
- assert_equal "einundvierzig", rec[0]
607
- #assert_equal false, rec
608
- rec = oo.find 15
609
- assert rec
610
- assert_equal 41,rec[0]
611
- end
612
- end
613
-
614
- def test_find_by_row_if_header_line_is_not_nil
615
- with_each_spreadsheet(:name=>'numbers1') do |oo|
616
- oo.header_line = 2
617
- refute_nil oo.header_line
618
- rec = oo.find 1
619
- assert rec
620
- assert_equal 5, rec[0]
621
- assert_equal 6, rec[1]
622
- rec = oo.find 15
623
- assert rec
624
- assert_equal "einundvierzig", rec[0]
625
- end
626
- end
627
-
628
- def test_find_by_conditions
629
- if LONG_RUN
630
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
631
- :excelx]) do |oo|
632
- #-----------------------------------------------------------------
633
- zeilen = oo.find(:all, :conditions => {
634
- 'TITEL' => 'Brief aus dem Sekretariat'
635
- }
636
- )
637
- assert_equal 2, zeilen.size
638
- assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
639
- "INTERNET"=>nil,
640
- "SEITE"=>316.0,
641
- "KENNUNG"=>"Aus dem Bibelbund",
642
- "OBJEKT"=>"Bibel+Gem",
643
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
644
- "NUMMER"=>"1982-3",
645
- "TITEL"=>"Brief aus dem Sekretariat"},
646
- {"VERFASSER"=>"Almassy, Annelene von",
647
- "INTERNET"=>nil,
648
- "SEITE"=>222.0,
649
- "KENNUNG"=>"Aus dem Bibelbund",
650
- "OBJEKT"=>"Bibel+Gem",
651
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
652
- "NUMMER"=>"1983-2",
653
- "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
654
-
655
- #----------------------------------------------------------
656
- zeilen = oo.find(:all,
657
- :conditions => { 'VERFASSER' => 'Almassy, Annelene von' }
658
- )
659
- assert_equal 13, zeilen.size
660
- #----------------------------------------------------------
661
- zeilen = oo.find(:all, :conditions => {
662
- 'TITEL' => 'Brief aus dem Sekretariat',
663
- 'VERFASSER' => 'Almassy, Annelene von',
664
- }
665
- )
666
- assert_equal 2, zeilen.size
667
- assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
668
- "INTERNET"=>nil,
669
- "SEITE"=>316.0,
670
- "KENNUNG"=>"Aus dem Bibelbund",
671
- "OBJEKT"=>"Bibel+Gem",
672
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
673
- "NUMMER"=>"1982-3",
674
- "TITEL"=>"Brief aus dem Sekretariat"},
675
- {"VERFASSER"=>"Almassy, Annelene von",
676
- "INTERNET"=>nil,
677
- "SEITE"=>222.0,
678
- "KENNUNG"=>"Aus dem Bibelbund",
679
- "OBJEKT"=>"Bibel+Gem",
680
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
681
- "NUMMER"=>"1983-2",
682
- "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
683
-
684
- # Result as an array
685
- zeilen = oo.find(:all,
686
- :conditions => {
687
- 'TITEL' => 'Brief aus dem Sekretariat',
688
- 'VERFASSER' => 'Almassy, Annelene von',
689
- }, :array => true)
690
- assert_equal 2, zeilen.size
691
- assert_equal [
692
- [
693
- "Brief aus dem Sekretariat",
694
- "Almassy, Annelene von",
695
- "Bibel+Gem",
696
- "1982-3",
697
- 316.0,
698
- nil,
699
- "#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
700
- "Aus dem Bibelbund",
701
- ],
702
- [
703
- "Brief aus dem Sekretariat",
704
- "Almassy, Annelene von",
705
- "Bibel+Gem",
706
- "1983-2",
707
- 222.0,
708
- nil,
709
- "#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
710
- "Aus dem Bibelbund",
711
- ]] , zeilen
712
- end
713
- end
714
- end
715
-
716
-
717
- #TODO: temporaerer Test
718
- def test_seiten_als_date
719
- if LONG_RUN
720
- with_each_spreadsheet(:name=>'Bibelbund', :format=>:excelx) do |oo|
721
- assert_equal 'Bericht aus dem Sekretariat', oo.cell(13,1)
722
- assert_equal '1981-4', oo.cell(13,'D')
723
- assert_equal String, oo.excelx_type(13,'E')[1].class
724
- assert_equal [:numeric_or_formula,"General"], oo.excelx_type(13,'E')
725
- assert_equal '428', oo.excelx_value(13,'E')
726
- assert_equal 428.0, oo.cell(13,'E')
727
- end
728
- end
729
- end
730
-
731
- def test_column
732
- with_each_spreadsheet(:name=>'numbers1') do |oo|
733
- expected = [1.0,5.0,nil,10.0,Date.new(1961,11,21),'tata',nil,nil,nil,nil,'thisisa11',41.0,nil,nil,41.0,'einundvierzig',nil,Date.new(2007,5,31)]
734
- assert_equal expected, oo.column(1)
735
- assert_equal expected, oo.column('a')
736
- end
737
- end
738
-
739
- def test_column_huge_document
740
- if LONG_RUN
741
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
742
- :excelx]) do |oo|
743
- oo.default_sheet = oo.sheets.first
744
- assert_equal 3735, oo.column('a').size
745
- #assert_equal 499, oo.column('a').size
746
- end
747
- end
748
- end
749
-
750
- def test_simple_spreadsheet_find_by_condition
751
- with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
752
- oo.header_line = 3
753
- # oo.date_format = '%m/%d/%Y' if oo.class == Google
754
- erg = oo.find(:all, :conditions => {'Comment' => 'Task 1'})
755
- assert_equal Date.new(2007,05,07), erg[1]['Date']
756
- assert_equal 10.75 , erg[1]['Start time']
757
- assert_equal 12.50 , erg[1]['End time']
758
- assert_equal 0 , erg[1]['Pause']
759
- assert_equal 1.75 , erg[1]['Sum']
760
- assert_equal "Task 1" , erg[1]['Comment']
761
- end
762
- end
763
-
764
- def get_extension(oo)
765
- case oo
766
- when Roo::OpenOffice
767
- ".ods"
768
- when Roo::Excelx
769
- ".xlsx"
770
- end
771
- end
772
-
773
- def test_info
774
- expected_templ = "File: numbers1%s\n"+
775
- "Number of sheets: 5\n"+
776
- "Sheets: Tabelle1, Name of Sheet 2, Sheet3, Sheet4, Sheet5\n"+
777
- "Sheet 1:\n"+
778
- " First row: 1\n"+
779
- " Last row: 18\n"+
780
- " First column: A\n"+
781
- " Last column: G\n"+
782
- "Sheet 2:\n"+
783
- " First row: 5\n"+
784
- " Last row: 14\n"+
785
- " First column: B\n"+
786
- " Last column: E\n"+
787
- "Sheet 3:\n"+
788
- " First row: 1\n"+
789
- " Last row: 1\n"+
790
- " First column: A\n"+
791
- " Last column: BA\n"+
792
- "Sheet 4:\n"+
793
- " First row: 1\n"+
794
- " Last row: 1\n"+
795
- " First column: A\n"+
796
- " Last column: E\n"+
797
- "Sheet 5:\n"+
798
- " First row: 1\n"+
799
- " Last row: 6\n"+
800
- " First column: A\n"+
801
- " Last column: E"
802
- with_each_spreadsheet(:name=>'numbers1') do |oo|
803
- ext = get_extension(oo)
804
- expected = sprintf(expected_templ,ext)
805
- begin
806
- if oo.class == Google
807
- assert_equal expected.gsub(/numbers1/,key_of("numbers1")), oo.info
808
- else
809
- assert_equal expected, oo.info
810
- end
811
- rescue NameError
812
- #
813
- end
814
- end
815
- end
816
-
817
- def test_info_doesnt_set_default_sheet
818
- with_each_spreadsheet(:name=>'numbers1') do |oo|
819
- oo.default_sheet = 'Sheet3'
820
- oo.info
821
- assert_equal 'Sheet3', oo.default_sheet
822
- end
823
- end
824
-
825
- def test_should_raise_file_not_found_error
826
- if OPENOFFICE
827
- assert_raises(IOError) {
828
- Roo::OpenOffice.new(File.join('testnichtvorhanden','Bibelbund.ods'))
829
- }
830
- end
831
- if EXCELX
832
- assert_raises(IOError) {
833
- Roo::Excelx.new(File.join('testnichtvorhanden','Bibelbund.xlsx'))
834
- }
835
- end
836
- end
837
-
838
- def test_bug_bbu
839
- with_each_spreadsheet(:name=>'bbu', :format=>[:openoffice, :excelx]) do |oo|
840
- assert_equal "File: bbu#{get_extension(oo)}
841
- Number of sheets: 3
842
- Sheets: 2007_12, Tabelle2, Tabelle3
843
- Sheet 1:
844
- First row: 1
845
- Last row: 4
846
- First column: A
847
- Last column: F
848
- Sheet 2:
849
- - empty -
850
- Sheet 3:
851
- - empty -", oo.info
852
-
853
- oo.default_sheet = oo.sheets[1] # empty sheet
854
- assert_nil oo.first_row
855
- assert_nil oo.last_row
856
- assert_nil oo.first_column
857
- assert_nil oo.last_column
858
- end
859
- end
860
-
861
-
862
- def test_bug_time_nil
863
- with_each_spreadsheet(:name=>'time-test') do |oo|
864
- assert_equal 12*3600+13*60+14, oo.cell('B',1) # 12:13:14 (secs since midnight)
865
- assert_equal :time, oo.celltype('B',1)
866
- assert_equal 15*3600+16*60, oo.cell('C',1) # 15:16 (secs since midnight)
867
- assert_equal :time, oo.celltype('C',1)
868
- assert_equal 23*3600, oo.cell('D',1) # 23:00 (secs since midnight)
869
- assert_equal :time, oo.celltype('D',1)
870
- end
871
- end
872
-
873
- def test_date_time_to_csv
874
- with_each_spreadsheet(:name=>'time-test') do |oo|
875
- Dir.mktmpdir do |tempdir|
876
- csv_output = File.join(tempdir,'time_test.csv')
877
- assert oo.to_csv(csv_output)
878
- assert File.exists?(csv_output)
879
- assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}`
880
- # --strip-trailing-cr is needed because the test-file use 0A and
881
- # the test on an windows box generates 0D 0A as line endings
882
- end
883
- end
884
- end
885
-
886
- def test_boolean_to_csv
887
- with_each_spreadsheet(:name=>'boolean') do |oo|
888
- Dir.mktmpdir do |tempdir|
889
- csv_output = File.join(tempdir,'boolean.csv')
890
- assert oo.to_csv(csv_output)
891
- assert File.exists?(csv_output)
892
- assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/boolean.csv #{csv_output}`
893
- # --strip-trailing-cr is needed because the test-file use 0A and
894
- # the test on an windows box generates 0D 0A as line endings
895
- end
896
- end
897
- end
898
- def test_link_to_csv
899
- with_each_spreadsheet(:name=>'link',:format=>:excelx) do |oo|
900
- Dir.mktmpdir do |tempdir|
901
- csv_output = File.join(tempdir,'link.csv')
902
- assert oo.to_csv(csv_output)
903
- assert File.exists?(csv_output)
904
- assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}`
905
- # --strip-trailing-cr is needed because the test-file use 0A and
906
- # the test on an windows box generates 0D 0A as line endings
907
- end
908
- end
909
- end
910
- def test_date_time_yaml
911
- with_each_spreadsheet(:name=>'time-test') do |oo|
912
- expected =
913
- "--- \ncell_1_1: \n row: 1 \n col: 1 \n celltype: string \n value: Mittags: \ncell_1_2: \n row: 1 \n col: 2 \n celltype: time \n value: 12:13:14 \ncell_1_3: \n row: 1 \n col: 3 \n celltype: time \n value: 15:16:00 \ncell_1_4: \n row: 1 \n col: 4 \n celltype: time \n value: 23:00:00 \ncell_2_1: \n row: 2 \n col: 1 \n celltype: date \n value: 2007-11-21 \n"
914
- assert_equal expected, oo.to_yaml
915
- end
916
- end
917
-
918
- # Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
919
- # Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
920
- # nicht immer in der gleichen Reihenfolge erzeugt.
921
- def init_all_cells(oo,sheet)
922
- all = []
923
- oo.first_row(sheet).upto(oo.last_row(sheet)) do |row|
924
- oo.first_column(sheet).upto(oo.last_column(sheet)) do |col|
925
- unless oo.empty?(row,col,sheet)
926
- all << {:row => row.to_s,
927
- :column => col.to_s,
928
- :content => oo.cell(row,col,sheet).to_s,
929
- :type => oo.celltype(row,col,sheet).to_s,
930
- }
931
- end
932
- end
933
- end
934
- all
935
- end
936
-
937
- def test_to_xml
938
- with_each_spreadsheet(:name=>'numbers1', :encoding => 'utf8') do |oo|
939
- skip if defined? JRUBY_VERSION
940
- oo.to_xml
941
- sheetname = oo.sheets.first
942
- doc = Nokogiri::XML(oo.to_xml)
943
- sheet_count = 0
944
- doc.xpath('//spreadsheet/sheet').each {|tmpelem|
945
- sheet_count += 1
946
- }
947
- assert_equal 5, sheet_count
948
- doc.xpath('//spreadsheet/sheet').each { |xml_sheet|
949
- all_cells = init_all_cells(oo, sheetname)
950
- x = 0
951
- assert_equal sheetname, xml_sheet.attributes['name'].value
952
- xml_sheet.children.each {|cell|
953
- if cell.attributes['name']
954
- expected = [all_cells[x][:row],
955
- all_cells[x][:column],
956
- all_cells[x][:content],
957
- all_cells[x][:type],
958
- ]
959
- result = [
960
- cell.attributes['row'],
961
- cell.attributes['column'],
962
- cell.content,
963
- cell.attributes['type'],
964
- ]
965
- assert_equal expected, result
966
- x += 1
967
- end # if
968
- } # end of sheet
969
- sheetname = oo.sheets[oo.sheets.index(sheetname)+1]
970
- }
971
- end
972
- end
973
-
974
- def test_file_warning_default
975
- if OPENOFFICE
976
- assert_raises(TypeError, "test/files/numbers1.xls is not an openoffice spreadsheet") {
977
- Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xls"))
978
- }
979
- assert_raises(TypeError) { Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx")) }
980
- end
981
- if EXCELX
982
- assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods")) }
983
- assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls")) }
984
- end
985
- end
986
-
987
- def test_file_warning_error
988
- if OPENOFFICE
989
- assert_raises(TypeError) {
990
- Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xls"),
991
- packed: false,
992
- file_warning: :error
993
- )
994
- }
995
- assert_raises(TypeError) {
996
- Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx"),
997
- packed: false,
998
- file_warning: :error)
999
- }
1000
- end
1001
- if EXCELX
1002
- assert_raises(TypeError) {
1003
- Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),
1004
- packed: false,
1005
- file_warning: :error)
1006
- }
1007
- assert_raises(TypeError) {
1008
- Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls"),
1009
- packed: false,
1010
- file_warning: :error)
1011
- }
1012
- end
1013
- end
1014
-
1015
- def test_file_warning_warning
1016
- if OPENOFFICE
1017
- assert_raises(ArgumentError) {
1018
- Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx"),
1019
- packed: false,
1020
- file_warning: :warning)
1021
- }
1022
- end
1023
- if EXCELX
1024
- assert_raises(ArgumentError) {
1025
- Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),
1026
- packed: false,
1027
- file_warning: :warning)
1028
- }
1029
- end
1030
- end
1031
-
1032
- def test_file_warning_ignore
1033
- if OPENOFFICE
1034
- # Files, die eigentlich OpenOffice-
1035
- # Files sind, aber die falsche Endung haben.
1036
- # Es soll ohne Fehlermeldung oder Warnung
1037
- # oder Abbruch die Datei geoffnet werden
1038
-
1039
- # xlsx
1040
- Roo::OpenOffice.new(File.join(TESTDIR,"type_openoffice.xlsx"),
1041
- packed: false,
1042
- file_warning: :ignore)
1043
- end
1044
- if EXCELX
1045
- Roo::Excelx.new(File.join(TESTDIR,"type_excelx.ods"),
1046
- packed: false,
1047
- file_warning: :ignore)
1048
- end
1049
- end
1050
-
1051
- def test_bug_to_xml_with_empty_sheets
1052
- with_each_spreadsheet(:name=>'emptysheets', :format=>[:openoffice, :excelx]) do |oo|
1053
- oo.sheets.each { |sheet|
1054
- assert_equal nil, oo.first_row, "first_row not nil in sheet #{sheet}"
1055
- assert_equal nil, oo.last_row, "last_row not nil in sheet #{sheet}"
1056
- assert_equal nil, oo.first_column, "first_column not nil in sheet #{sheet}"
1057
- assert_equal nil, oo.last_column, "last_column not nil in sheet #{sheet}"
1058
- assert_equal nil, oo.first_row(sheet), "first_row not nil in sheet #{sheet}"
1059
- assert_equal nil, oo.last_row(sheet), "last_row not nil in sheet #{sheet}"
1060
- assert_equal nil, oo.first_column(sheet), "first_column not nil in sheet #{sheet}"
1061
- assert_equal nil, oo.last_column(sheet), "last_column not nil in sheet #{sheet}"
1062
- }
1063
- oo.to_xml
1064
- end
1065
- end
1066
-
1067
- def test_bug_simple_spreadsheet_time_bug
1068
- # really a bug? are cells really of type time?
1069
- # No! :float must be the correct type
1070
- with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1071
- # puts oo.cell('B',5).to_s
1072
- # assert_equal :time, oo.celltype('B',5)
1073
- assert_equal :float, oo.celltype('B',5)
1074
- assert_equal 10.75, oo.cell('B',5)
1075
- assert_equal 12.50, oo.cell('C',5)
1076
- assert_equal 0, oo.cell('D',5)
1077
- assert_equal 1.75, oo.cell('E',5)
1078
- assert_equal 'Task 1', oo.cell('F',5)
1079
- assert_equal Date.new(2007,5,7), oo.cell('A',5)
1080
- end
1081
- end
1082
-
1083
- def test_simple2_excelx
1084
- with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1085
- assert_equal [:numeric_or_formula, "yyyy\\-mm\\-dd"], oo.excelx_type('A',4)
1086
- assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('B',4)
1087
- assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('c',4)
1088
- assert_equal [:numeric_or_formula, "General"], oo.excelx_type('d',4)
1089
- assert_equal [:numeric_or_formula, "General"], oo.excelx_type('e',4)
1090
- assert_equal :string, oo.excelx_type('f',4)
1091
-
1092
- assert_equal "39209", oo.excelx_value('a',4)
1093
- assert_equal "yyyy\\-mm\\-dd", oo.excelx_format('a',4)
1094
- assert_equal "9.25", oo.excelx_value('b',4)
1095
- assert_equal "10.25", oo.excelx_value('c',4)
1096
- assert_equal "0", oo.excelx_value('d',4)
1097
- #... Sum-Spalte
1098
- # assert_equal "Task 1", oo.excelx_value('f',4)
1099
- assert_equal "Task 1", oo.cell('f',4)
1100
- assert_equal Date.new(2007,05,07), oo.cell('a',4)
1101
- assert_equal "9.25", oo.excelx_value('b',4)
1102
- assert_equal "#,##0.00", oo.excelx_format('b',4)
1103
- assert_equal 9.25, oo.cell('b',4)
1104
- assert_equal :float, oo.celltype('b',4)
1105
- assert_equal :float, oo.celltype('d',4)
1106
- assert_equal 0, oo.cell('d',4)
1107
- assert_equal :formula, oo.celltype('e',4)
1108
- assert_equal 1, oo.cell('e',4)
1109
- assert_equal 'C4-B4-D4', oo.formula('e',4)
1110
- assert_equal :string, oo.celltype('f',4)
1111
- assert_equal "Task 1", oo.cell('f',4)
1112
- end
1113
- end
1114
-
1115
- def test_datetime
1116
- with_each_spreadsheet(:name=>'datetime') do |oo|
1117
- val = oo.cell('c',3)
1118
- assert_equal :datetime, oo.celltype('c',3)
1119
- assert_equal DateTime.new(1961,11,21,12,17,18), val
1120
- assert_kind_of DateTime, val
1121
- val = oo.cell('a',1)
1122
- assert_equal :date, oo.celltype('a',1)
1123
- assert_kind_of Date, val
1124
- assert_equal Date.new(1961,11,21), val
1125
- assert_equal Date.new(1961,11,21), oo.cell('a',1)
1126
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',3)
1127
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',3)
1128
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',3)
1129
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',4)
1130
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',4)
1131
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',4)
1132
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',5)
1133
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',5)
1134
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',5)
1135
- assert_equal Date.new(1961,11,21), oo.cell('a',6)
1136
- assert_equal Date.new(1961,11,21), oo.cell('b',6)
1137
- assert_equal Date.new(1961,11,21), oo.cell('c',6)
1138
- assert_equal Date.new(1961,11,21), oo.cell('a',7)
1139
- assert_equal Date.new(1961,11,21), oo.cell('b',7)
1140
- assert_equal Date.new(1961,11,21), oo.cell('c',7)
1141
- assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('a',8)
1142
- assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('b',8)
1143
- assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('c',8)
1144
- end
1145
- end
1146
-
1147
- def test_cell_openoffice_html_escape
1148
- with_each_spreadsheet(:name=>'html-escape', :format=>:openoffice) do |oo|
1149
- assert_equal "'", oo.cell(1,1)
1150
- assert_equal "&", oo.cell(2,1)
1151
- assert_equal ">", oo.cell(3,1)
1152
- assert_equal "<", oo.cell(4,1)
1153
- assert_equal "`", oo.cell(5,1)
1154
- # test_openoffice_zipped will catch issues with &quot;
1155
- end
1156
- end
1157
-
1158
- def test_cell_boolean
1159
- with_each_spreadsheet(:name=>'boolean', :format=>[:openoffice, :excelx]) do |oo|
1160
- if oo.class == Roo::Excelx
1161
- assert_equal "TRUE", oo.cell(1,1), "failure in "+oo.class.to_s
1162
- assert_equal "FALSE", oo.cell(2,1), "failure in "+oo.class.to_s
1163
- else
1164
- assert_equal "true", oo.cell(1,1), "failure in "+oo.class.to_s
1165
- assert_equal "false", oo.cell(2,1), "failure in "+oo.class.to_s
1166
- end
1167
- end
1168
- end
1169
-
1170
- def test_cell_multiline
1171
- with_each_spreadsheet(:name=>'paragraph', :format=>[:openoffice, :excelx]) do |oo|
1172
- assert_equal "This is a test\nof a multiline\nCell", oo.cell(1,1)
1173
- assert_equal "This is a test\n¶\nof a multiline\n\nCell", oo.cell(1,2)
1174
- assert_equal "first p\n\nsecond p\n\nlast p", oo.cell(2,1)
1175
- end
1176
- end
1177
-
1178
- def test_cell_styles
1179
- # styles only valid in excel spreadsheets?
1180
- # TODO: what todo with other spreadsheet types
1181
- with_each_spreadsheet(:name=>'style', :format=>[# :openoffice,
1182
- :excelx
1183
- ]) do |oo|
1184
- # bold
1185
- assert_equal true, oo.font(1,1).bold?
1186
- assert_equal false, oo.font(1,1).italic?
1187
- assert_equal false, oo.font(1,1).underline?
1188
-
1189
- # italic
1190
- assert_equal false, oo.font(2,1).bold?
1191
- assert_equal true, oo.font(2,1).italic?
1192
- assert_equal false, oo.font(2,1).underline?
1193
-
1194
- # normal
1195
- assert_equal false, oo.font(3,1).bold?
1196
- assert_equal false, oo.font(3,1).italic?
1197
- assert_equal false, oo.font(3,1).underline?
1198
-
1199
- # underline
1200
- assert_equal false, oo.font(4,1).bold?
1201
- assert_equal false, oo.font(4,1).italic?
1202
- assert_equal true, oo.font(4,1).underline?
1203
-
1204
- # bold italic
1205
- assert_equal true, oo.font(5,1).bold?
1206
- assert_equal true, oo.font(5,1).italic?
1207
- assert_equal false, oo.font(5,1).underline?
1208
-
1209
- # bold underline
1210
- assert_equal true, oo.font(6,1).bold?
1211
- assert_equal false, oo.font(6,1).italic?
1212
- assert_equal true, oo.font(6,1).underline?
1213
-
1214
- # italic underline
1215
- assert_equal false, oo.font(7,1).bold?
1216
- assert_equal true, oo.font(7,1).italic?
1217
- assert_equal true, oo.font(7,1).underline?
1218
-
1219
- # bolded row
1220
- assert_equal true, oo.font(8,1).bold?
1221
- assert_equal false, oo.font(8,1).italic?
1222
- assert_equal false, oo.font(8,1).underline?
1223
-
1224
- # bolded col
1225
- assert_equal true, oo.font(9,2).bold?
1226
- assert_equal false, oo.font(9,2).italic?
1227
- assert_equal false, oo.font(9,2).underline?
1228
-
1229
- # bolded row, italic col
1230
- assert_equal true, oo.font(10,3).bold?
1231
- assert_equal true, oo.font(10,3).italic?
1232
- assert_equal false, oo.font(10,3).underline?
1233
-
1234
- # normal
1235
- assert_equal false, oo.font(11,4).bold?
1236
- assert_equal false, oo.font(11,4).italic?
1237
- assert_equal false, oo.font(11,4).underline?
1238
- end
1239
- end
1240
-
1241
- # Need to extend to other formats
1242
- def test_row_whitespace
1243
- # auf dieses Dokument habe ich keinen Zugriff TODO:
1244
- with_each_spreadsheet(:name=>'whitespace') do |oo|
1245
- oo.default_sheet = "Sheet1"
1246
- assert_equal [nil, nil, nil, nil, nil, nil], oo.row(1)
1247
- assert_equal [nil, nil, nil, nil, nil, nil], oo.row(2)
1248
- assert_equal ["Date", "Start time", "End time", "Pause", "Sum", "Comment"], oo.row(3)
1249
- assert_equal [Date.new(2007,5,7), 9.25, 10.25, 0.0, 1.0, "Task 1"], oo.row(4)
1250
- assert_equal [nil, nil, nil, nil, nil, nil], oo.row(5)
1251
- assert_equal [Date.new(2007,5,7), 10.75, 10.75, 0.0, 0.0, "Task 1"], oo.row(6)
1252
- oo.default_sheet = "Sheet2"
1253
- assert_equal ["Date", nil, "Start time"], oo.row(1)
1254
- assert_equal [Date.new(2007,5,7), nil, 9.25], oo.row(2)
1255
- assert_equal [Date.new(2007,5,7), nil, 10.75], oo.row(3)
1256
- end
1257
- end
1258
-
1259
- def test_col_whitespace
1260
- #TODO:
1261
- # kein Zugriff auf Dokument whitespace
1262
- with_each_spreadsheet(:name=>'whitespace') do |oo|
1263
- oo.default_sheet = "Sheet1"
1264
- assert_equal ["Date", Date.new(2007,5,7), nil, Date.new(2007,5,7)], oo.column(1)
1265
- assert_equal ["Start time", 9.25, nil, 10.75], oo.column(2)
1266
- assert_equal ["End time", 10.25, nil, 10.75], oo.column(3)
1267
- assert_equal ["Pause", 0.0, nil, 0.0], oo.column(4)
1268
- assert_equal ["Sum", 1.0, nil, 0.0], oo.column(5)
1269
- assert_equal ["Comment","Task 1", nil, "Task 1"], oo.column(6)
1270
- oo.default_sheet = "Sheet2"
1271
- assert_equal [nil, nil, nil], oo.column(1)
1272
- assert_equal [nil, nil, nil], oo.column(2)
1273
- assert_equal ["Date", Date.new(2007,5,7), Date.new(2007,5,7)], oo.column(3)
1274
- assert_equal [nil, nil, nil], oo.column(4)
1275
- assert_equal [ "Start time", 9.25, 10.75], oo.column(5)
1276
- end
1277
- end
1278
-
1279
- def test_excelx_links
1280
- with_each_spreadsheet(:name=>'link', :format=>:excelx) do |oo|
1281
- assert_equal 'Google', oo.cell(1,1)
1282
- assert_equal 'http://www.google.com', oo.cell(1,1).href
1283
- end
1284
- end
1285
-
1286
- # Excel has two base date formats one from 1900 and the other from 1904.
1287
- # see #test_base_dates_in_excel
1288
- def test_base_dates_in_excelx
1289
- with_each_spreadsheet(:name=>'1900_base', :format=>:excelx) do |oo|
1290
- assert_equal Date.new(2009,06,15), oo.cell(1,1)
1291
- assert_equal :date, oo.celltype(1,1)
1292
- end
1293
- with_each_spreadsheet(:name=>'1904_base', :format=>:excelx) do |oo|
1294
- assert_equal Date.new(2009,06,15), oo.cell(1,1)
1295
- assert_equal :date, oo.celltype(1,1)
1296
- end
1297
- end
1298
-
1299
- def test_cell_methods
1300
- with_each_spreadsheet(:name=>'numbers1') do |oo|
1301
- assert_equal 10, oo.a4 # cell(4,'A')
1302
- assert_equal 11, oo.b4 # cell(4,'B')
1303
- assert_equal 12, oo.c4 # cell(4,'C')
1304
- assert_equal 13, oo.d4 # cell(4,'D')
1305
- assert_equal 14, oo.e4 # cell(4,'E')
1306
- assert_equal 'ABC', oo.c6('Sheet5')
1307
-
1308
- #assert_raises(ArgumentError) {
1309
- assert_raises(NoMethodError) {
1310
- # a42a is not a valid cell name, should raise ArgumentError
1311
- assert_equal 9999, oo.a42a
1312
- }
1313
- end
1314
- end
1315
-
1316
-
1317
- # compare large spreadsheets
1318
- def test_compare_large_spreadsheets
1319
- # problematisch, weil Formeln in Excel nicht unterstützt werden
1320
- if LONG_RUN
1321
- qq = Roo::OpenOffice.new(File.join('test',"Bibelbund.ods"))
1322
- with_each_spreadsheet(:name=>'Bibelbund') do |oo|
1323
- # p "comparing Bibelbund.ods with #{oo.class}"
1324
- oo.sheets.each do |sh|
1325
- oo.first_row.upto(oo.last_row) do |row|
1326
- oo.first_column.upto(oo.last_column) do |col|
1327
- c1 = qq.cell(row,col,sh)
1328
- c1.force_encoding("UTF-8") if c1.class == String
1329
- c2 = oo.cell(row,col,sh)
1330
- c2.force_encoding("UTF-8") if c2.class == String
1331
- assert_equal c1, c2, "diff in #{sh}/#{row}/#{col}}"
1332
- assert_equal qq.celltype(row,col,sh), oo.celltype(row,col,sh)
1333
- assert_equal qq.formula?(row,col,sh), oo.formula?(row,col,sh) if oo.class != Roo::Excel
1334
- end
1335
- end
1336
- end
1337
- end
1338
- end # LONG_RUN
1339
- end
1340
-
1341
- def test_label
1342
- with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1343
- # oo.default_sheet = oo.sheets.first
1344
- begin
1345
- row,col = oo.label('anton')
1346
- rescue ArgumentError
1347
- puts "labels error at #{oo.class}"
1348
- raise
1349
- end
1350
- assert_equal 5, row, "error with label in class #{oo.class}"
1351
- assert_equal 3, col, "error with label in class #{oo.class}"
1352
-
1353
- row,col = oo.label('anton')
1354
- assert_equal 'Anton', oo.cell(row,col), "error with label in class #{oo.class}"
1355
-
1356
- row,col = oo.label('berta')
1357
- assert_equal 'Bertha', oo.cell(row,col), "error with label in class #{oo.class}"
1358
-
1359
- row,col = oo.label('caesar')
1360
- assert_equal 'Cäsar', oo.cell(row,col),"error with label in class #{oo.class}"
1361
-
1362
- row,col = oo.label('never')
1363
- assert_nil row
1364
- assert_nil col
1365
-
1366
- row,col,sheet = oo.label('anton')
1367
- assert_equal 5, row
1368
- assert_equal 3, col
1369
- assert_equal "Sheet1", sheet
1370
- end
1371
- end
1372
-
1373
- def test_method_missing_anton
1374
- with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1375
- # oo.default_sheet = oo.sheets.first
1376
- assert_equal "Anton", oo.anton
1377
- assert_raises(NoMethodError) {
1378
- oo.never
1379
- }
1380
- end
1381
- end
1382
-
1383
- def test_labels
1384
- with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1385
- # oo.default_sheet = oo.sheets.first
1386
- assert_equal [
1387
- ['anton',[5,3,'Sheet1']],
1388
- ['berta',[4,2,'Sheet1']],
1389
- ['caesar',[7,2,'Sheet1']],
1390
- ], oo.labels, "error with labels array in class #{oo.class}"
1391
- end
1392
- end
1393
-
1394
- def test_labeled_cells
1395
- with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1396
- oo.default_sheet = oo.sheets.first
1397
- begin
1398
- row,col = oo.label('anton')
1399
- rescue ArgumentError
1400
- puts "labels error at #{oo.class}"
1401
- raise
1402
- end
1403
- assert_equal 5, row
1404
- assert_equal 3, col
1405
-
1406
- row,col = oo.label('anton')
1407
- assert_equal 'Anton', oo.cell(row,col)
1408
-
1409
- row,col = oo.label('berta')
1410
- assert_equal 'Bertha', oo.cell(row,col)
1411
-
1412
- row,col = oo.label('caesar')
1413
- assert_equal 'Cäsar', oo.cell(row,col)
1414
-
1415
- row,col = oo.label('never')
1416
- assert_nil row
1417
- assert_nil col
1418
-
1419
- row,col,sheet = oo.label('anton')
1420
- assert_equal 5, row
1421
- assert_equal 3, col
1422
- assert_equal "Sheet1", sheet
1423
-
1424
- assert_equal "Anton", oo.anton
1425
- assert_raises(NoMethodError) {
1426
- row,col = oo.never
1427
- }
1428
-
1429
- # Reihenfolge row,col,sheet analog zu #label
1430
- assert_equal [
1431
- ['anton',[5,3,'Sheet1']],
1432
- ['berta',[4,2,'Sheet1']],
1433
- ['caesar',[7,2,'Sheet1']],
1434
- ], oo.labels, "error with labels array in class #{oo.class}"
1435
- end
1436
- end
1437
-
1438
- require 'matrix'
1439
- def test_matrix
1440
- with_each_spreadsheet(:name => 'matrix', :format => :openoffice) do |oo|
1441
- oo.default_sheet = oo.sheets.first
1442
- assert_equal Matrix[
1443
- [1.0, 2.0, 3.0],
1444
- [4.0, 5.0, 6.0],
1445
- [7.0, 8.0, 9.0] ], oo.to_matrix
1446
- end
1447
- end
1448
-
1449
- def test_matrix_selected_range
1450
- with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
1451
- oo.default_sheet = 'Sheet2'
1452
- assert_equal Matrix[
1453
- [1.0, 2.0, 3.0],
1454
- [4.0, 5.0, 6.0],
1455
- [7.0, 8.0, 9.0] ], oo.to_matrix(3,4,5,6)
1456
- end
1457
- end
1458
-
1459
- def test_matrix_all_nil
1460
- with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
1461
- oo.default_sheet = 'Sheet2'
1462
- assert_equal Matrix[
1463
- [nil, nil, nil],
1464
- [nil, nil, nil],
1465
- [nil, nil, nil] ], oo.to_matrix(10,10,12,12)
1466
- end
1467
- end
1468
-
1469
- def test_matrix_values_and_nil
1470
- with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
1471
- oo.default_sheet = 'Sheet3'
1472
- assert_equal Matrix[
1473
- [1.0, nil, 3.0],
1474
- [4.0, 5.0, 6.0],
1475
- [7.0, 8.0, nil] ], oo.to_matrix(1,1,3,3)
1476
- end
1477
- end
1478
-
1479
- def test_matrix_specifying_sheet
1480
- with_each_spreadsheet(:name => 'matrix', :format => :openoffice) do |oo|
1481
- oo.default_sheet = oo.sheets.first
1482
- assert_equal Matrix[
1483
- [1.0, nil, 3.0],
1484
- [4.0, 5.0, 6.0],
1485
- [7.0, 8.0, nil] ], oo.to_matrix(nil, nil, nil, nil, 'Sheet3')
1486
- end
1487
- end
1488
-
1489
- # unter Windows soll es laut Bug-Reports nicht moeglich sein, eine Excel-Datei, die
1490
- # mit Excel.new geoeffnet wurde nach dem Processing anschliessend zu loeschen.
1491
- # Anmerkung: Das Spreadsheet-Gem erlaubt kein explizites Close von Spreadsheet-Dateien,
1492
- # was verhindern koennte, das die Datei geloescht werden kann.
1493
- # def test_bug_cannot_delete_opened_excel_sheet
1494
- # with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
1495
- # 'kopiere nach temporaere Datei und versuche diese zu oeffnen und zu loeschen'
1496
- # end
1497
- # end
1498
-
1499
- def test_bug_xlsx_reference_cell
1500
-
1501
- if EXCELX
1502
- =begin
1503
- If cell A contains a string and cell B references cell A. When reading the value of cell B, the result will be
1504
- "0.0" instead of the value of cell A.
1505
-
1506
- With the attached test case, I ran the following code:
1507
- spreadsheet = Roo::Excelx.new('formula_string_error.xlsx')
1508
- spreadsheet.default_sheet = 'sheet1'
1509
- p "A: #{spreadsheet.cell(1, 1)}"
1510
- p "B: #{spreadsheet.cell(2, 1)}"
1511
-
1512
- with the following results
1513
- "A: TestString"
1514
- "B: 0.0"
1515
-
1516
- where the expected result is
1517
- "A: TestString"
1518
- "B: TestString"
1519
- =end
1520
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "formula_string_error.xlsx"))
1521
- xlsx.default_sheet = xlsx.sheets.first
1522
- assert_equal 'Teststring', xlsx.cell('a',1)
1523
- assert_equal 'Teststring', xlsx.cell('a',2)
1524
- end
1525
- end
1526
-
1527
- # #formulas of an empty sheet should return an empty array and not result in
1528
- # an error message
1529
- # 2011-06-24
1530
- def test_bug_formulas_empty_sheet
1531
- with_each_spreadsheet(:name =>'emptysheets',
1532
- :format=>[:openoffice,:excelx]) do |oo|
1533
- oo.default_sheet = oo.sheets.first
1534
- oo.formulas
1535
- assert_equal([], oo.formulas)
1536
- end
1537
- end
1538
-
1539
- # #to_yaml of an empty sheet should return an empty string and not result in
1540
- # an error message
1541
- # 2011-06-24
1542
- def test_bug_to_yaml_empty_sheet
1543
- with_each_spreadsheet(:name =>'emptysheets',
1544
- :format=>[:openoffice,:excelx]) do |oo|
1545
- oo.default_sheet = oo.sheets.first
1546
- oo.to_yaml
1547
- assert_equal('', oo.to_yaml)
1548
- end
1549
- end
1550
-
1551
- # #to_matrix of an empty sheet should return an empty matrix and not result in
1552
- # an error message
1553
- # 2011-06-25
1554
- def test_bug_to_matrix_empty_sheet
1555
- with_each_spreadsheet(:name =>'emptysheets',
1556
- :format=>[:openoffice,:excelx]) do |oo|
1557
- oo.default_sheet = oo.sheets.first
1558
- oo.to_matrix
1559
- assert_equal(Matrix.empty(0,0), oo.to_matrix)
1560
- end
1561
- end
1562
-
1563
- # 2011-08-03
1564
- def test_bug_datetime_to_csv
1565
- with_each_spreadsheet(:name=>'datetime') do |oo|
1566
- Dir.mktmpdir do |tempdir|
1567
- datetime_csv_file = File.join(tempdir,"datetime.csv")
1568
-
1569
- assert oo.to_csv(datetime_csv_file)
1570
- assert File.exists?(datetime_csv_file)
1571
- assert_equal "", file_diff('test/files/so_datetime.csv', datetime_csv_file)
1572
- end
1573
- end
1574
- end
1575
-
1576
- # 2011-08-11
1577
- def test_bug_openoffice_formula_missing_letters
1578
- if LIBREOFFICE
1579
- # Dieses Dokument wurde mit LibreOffice angelegt.
1580
- # Keine Ahnung, ob es damit zusammenhaengt, das diese
1581
- # Formeln anders sind, als in der Datei formula.ods, welche
1582
- # mit OpenOffice angelegt wurde.
1583
- # Bei den OpenOffice-Dateien ist in diesem Feld in der XML-
1584
- # Datei of: als Prefix enthalten, waehrend in dieser Datei
1585
- # irgendetwas mit oooc: als Prefix verwendet wird.
1586
- oo = Roo::OpenOffice.new(File.join(TESTDIR,'dreimalvier.ods'))
1587
- oo.default_sheet = oo.sheets.first
1588
- assert_equal '=SUM([.A1:.D1])', oo.formula('e',1)
1589
- assert_equal '=SUM([.A2:.D2])', oo.formula('e',2)
1590
- assert_equal '=SUM([.A3:.D3])', oo.formula('e',3)
1591
- assert_equal [
1592
- [1,5,'=SUM([.A1:.D1])'],
1593
- [2,5,'=SUM([.A2:.D2])'],
1594
- [3,5,'=SUM([.A3:.D3])'],
1595
- ], oo.formulas
1596
-
1597
- end
1598
- end
1599
-
1600
- =begin
1601
- def test_postprocessing_and_types_in_csv
1602
- if CSV
1603
- oo = CSV.new(File.join(TESTDIR,'csvtypes.csv'))
1604
- oo.default_sheet = oo.sheets.first
1605
- assert_equal(1,oo.a1)
1606
- assert_equal(:float,oo.celltype('A',1))
1607
- assert_equal("2",oo.b1)
1608
- assert_equal(:string,oo.celltype('B',1))
1609
- assert_equal("Mayer",oo.c1)
1610
- assert_equal(:string,oo.celltype('C',1))
1611
- end
1612
- end
1613
- =end
1614
-
1615
- =begin
1616
- def test_postprocessing_with_callback_function
1617
- if CSV
1618
- oo = CSV.new(File.join(TESTDIR,'csvtypes.csv'))
1619
- oo.default_sheet = oo.sheets.first
1620
-
1621
- #
1622
- assert_equal(1, oo.last_column)
1623
- end
1624
- end
1625
- =end
1626
-
1627
- =begin
1628
- def x_123
1629
- class ::CSV
1630
- def cell_postprocessing(row,col,value)
1631
- if row < 3
1632
- return nil
1633
- end
1634
- return value
1635
- end
1636
- end
1637
- end
1638
- =end
1639
-
1640
- def test_nil_rows_and_lines_csv
1641
- # x_123
1642
- if CSV
1643
- oo = Roo::CSV.new(File.join(TESTDIR,'Bibelbund.csv'))
1644
- oo.default_sheet = oo.sheets.first
1645
- assert_equal 1, oo.first_row
1646
- end
1647
- end
1648
-
1649
- def test_bug_pfand_from_windows_phone_xlsx
1650
- return if defined? JRUBY_VERSION
1651
- with_each_spreadsheet(:name=>'Pfand_from_windows_phone', :format=>:excelx) do |oo|
1652
- oo.default_sheet = oo.sheets.first
1653
- assert_equal ['Blatt1','Blatt2','Blatt3'], oo.sheets
1654
- assert_equal 'Summe', oo.cell('b',1)
1655
-
1656
- assert_equal Date.new(2011,9,14), oo.cell('a',2)
1657
- assert_equal :date, oo.celltype('a',2)
1658
- assert_equal Date.new(2011,9,15), oo.cell('a',3)
1659
- assert_equal :date, oo.celltype('a',3)
1660
-
1661
- assert_equal 3.81, oo.cell('b',2)
1662
- assert_equal "SUM(C2:L2)", oo.formula('b',2)
1663
- assert_equal 0.7, oo.cell('c',2)
1664
- end # each
1665
- end
1666
-
1667
- def test_comment
1668
- with_each_spreadsheet(:name=>'comments', :format=>[:openoffice,:libreoffice,
1669
- :excelx]) do |oo|
1670
- oo.default_sheet = oo.sheets.first
1671
- assert_equal 'Kommentar fuer B4',oo.comment('b',4)
1672
- assert_equal 'Kommentar fuer B5',oo.comment('b',5)
1673
- assert_nil oo.comment('b',99)
1674
- # no comment at the second page
1675
- oo.default_sheet = oo.sheets[1]
1676
- assert_nil oo.comment('b',4)
1677
- end
1678
- end
1679
-
1680
- def test_comments
1681
- with_each_spreadsheet(:name=>'comments', :format=>[:openoffice,:libreoffice,
1682
- :excelx]) do |oo|
1683
- oo.default_sheet = oo.sheets.first
1684
- assert_equal [
1685
- [4, 2, "Kommentar fuer B4"],
1686
- [5, 2, "Kommentar fuer B5"],
1687
- ], oo.comments(oo.sheets.first), "comments error in class #{oo.class}"
1688
- # no comments at the second page
1689
- oo.default_sheet = oo.sheets[1]
1690
- assert_equal [], oo.comments, "comments error in class #{oo.class}"
1691
- end
1692
-
1693
- with_each_spreadsheet(:name=>'comments-google', :format=>[:excelx]) do |oo|
1694
- oo.default_sheet = oo.sheets.first
1695
- assert_equal [[1, 1, "this is a comment\n\t-Steven Daniels"]], oo.comments(oo.sheets.first), "comments error in class #{oo.class}"
1696
- end
1697
- end
1698
-
1699
- ## PREVIOUSLY SKIPPED
1700
-
1701
- # don't have these test files so removing. We can easily add in
1702
- # by modifying with_each_spreadsheet
1703
- GNUMERIC_ODS = false # do gnumeric with ods files Tests?
1704
- OPENOFFICEWRITE = false # experimental: write access with OO-Documents
1705
-
1706
- def test_writeopenoffice
1707
- if OPENOFFICEWRITE
1708
- File.cp(File.join(TESTDIR,"numbers1.ods"),
1709
- File.join(TESTDIR,"numbers2.ods"))
1710
- File.cp(File.join(TESTDIR,"numbers2.ods"),
1711
- File.join(TESTDIR,"bak_numbers2.ods"))
1712
- oo = OpenOffice.new(File.join(TESTDIR,"numbers2.ods"))
1713
- oo.default_sheet = oo.sheets.first
1714
- oo.first_row.upto(oo.last_row) {|y|
1715
- oo.first_column.upto(oo.last_column) {|x|
1716
- unless oo.empty?(y,x)
1717
- # oo.set(y, x, oo.cell(y,x) + 7) if oo.celltype(y,x) == "float"
1718
- oo.set(y, x, oo.cell(y,x) + 7) if oo.celltype(y,x) == :float
1719
- end
1720
- }
1721
- }
1722
- oo.save
1723
-
1724
- oo1 = Roo::OpenOffice.new(File.join(TESTDIR,"numbers2.ods"))
1725
- oo2 = Roo::OpenOffice.new(File.join(TESTDIR,"bak_numbers2.ods"))
1726
- #p oo2.to_s
1727
- assert_equal 999, oo2.cell('a',1), oo2.cell('a',1)
1728
- assert_equal oo2.cell('a',1) + 7, oo1.cell('a',1)
1729
- assert_equal oo2.cell('b',1)+7, oo1.cell('b',1)
1730
- assert_equal oo2.cell('c',1)+7, oo1.cell('c',1)
1731
- assert_equal oo2.cell('d',1)+7, oo1.cell('d',1)
1732
- assert_equal oo2.cell('a',2)+7, oo1.cell('a',2)
1733
- assert_equal oo2.cell('b',2)+7, oo1.cell('b',2)
1734
- assert_equal oo2.cell('c',2)+7, oo1.cell('c',2)
1735
- assert_equal oo2.cell('d',2)+7, oo1.cell('d',2)
1736
- assert_equal oo2.cell('e',2)+7, oo1.cell('e',2)
1737
-
1738
- File.cp(File.join(TESTDIR,"bak_numbers2.ods"),
1739
- File.join(TESTDIR,"numbers2.ods"))
1740
- end
1741
- end
1742
-
1743
- def common_possible_bug_snowboard_cells(ss)
1744
- assert_equal "A.", ss.cell(13,'A'), ss.class
1745
- assert_equal 147, ss.cell(13,'f'), ss.class
1746
- assert_equal 152, ss.cell(13,'g'), ss.class
1747
- assert_equal 156, ss.cell(13,'h'), ss.class
1748
- assert_equal 158, ss.cell(13,'i'), ss.class
1749
- assert_equal 160, ss.cell(13,'j'), ss.class
1750
- assert_equal 164, ss.cell(13,'k'), ss.class
1751
- assert_equal 168, ss.cell(13,'l'), ss.class
1752
- assert_equal :string, ss.celltype(13,'m'), ss.class
1753
- assert_equal "159W", ss.cell(13,'m'), ss.class
1754
- assert_equal "164W", ss.cell(13,'n'), ss.class
1755
- assert_equal "168W", ss.cell(13,'o'), ss.class
1756
- end
1757
-
1758
- # def test_false_encoding
1759
- # ex = Roo::Excel.new(File.join(TESTDIR,'false_encoding.xls'))
1760
- # ex.default_sheet = ex.sheets.first
1761
- # assert_equal "Sheet1", ex.sheets.first
1762
- # ex.first_row.upto(ex.last_row) do |row|
1763
- # ex.first_column.upto(ex.last_column) do |col|
1764
- # content = ex.cell(row,col)
1765
- # puts "#{row}/#{col}"
1766
- # #puts content if ! ex.empty?(row,col) or ex.formula?(row,col)
1767
- # if ex.formula?(row,col)
1768
- # #! ex.empty?(row,col)
1769
- # puts content
1770
- # end
1771
- # end
1772
- # end
1773
- # end
1774
-
1775
- def test_download_uri
1776
- if ONLINE
1777
- if OPENOFFICE
1778
- assert_raises(RuntimeError) {
1779
- Roo::OpenOffice.new("http://gibbsnichtdomainxxxxx.com/file.ods")
1780
- }
1781
- end
1782
- if EXCELX
1783
- assert_raises(RuntimeError) {
1784
- Roo::Excelx.new("http://gibbsnichtdomainxxxxx.com/file.xlsx")
1785
- }
1786
- end
1787
- end
1788
- end
1789
-
1790
- def test_download_uri_with_query_string
1791
- dir = File.expand_path("#{File.dirname __FILE__}/files")
1792
- { xlsx: [EXCELX, Roo::Excelx],
1793
- ods: [OPENOFFICE, Roo::OpenOffice]}.each do |extension, (flag, type)|
1794
- if flag
1795
- file = "#{dir}/simple_spreadsheet.#{extension}"
1796
- url = "http://test.example.com/simple_spreadsheet.#{extension}?query-param=value"
1797
- stub_request(:any, url).to_return(body: File.read(file))
1798
- spreadsheet = type.new(url)
1799
- spreadsheet.default_sheet = spreadsheet.sheets.first
1800
- assert_equal 'Task 1', spreadsheet.cell('f', 4)
1801
- end
1802
- end
1803
- end
1804
-
1805
- # def test_soap_server
1806
- # #threads = []
1807
- # #threads << Thread.new("serverthread") do
1808
- # fork do
1809
- # p "serverthread started"
1810
- # puts "in child, pid = #$$"
1811
- # puts `/usr/bin/ruby rooserver.rb`
1812
- # p "serverthread finished"
1813
- # end
1814
- # #threads << Thread.new("clientthread") do
1815
- # p "clientthread started"
1816
- # sleep 10
1817
- # proxy = SOAP::RPC::Driver.new("http://localhost:12321","spreadsheetserver")
1818
- # proxy.add_method('cell','row','col')
1819
- # proxy.add_method('officeversion')
1820
- # proxy.add_method('last_row')
1821
- # proxy.add_method('last_column')
1822
- # proxy.add_method('first_row')
1823
- # proxy.add_method('first_column')
1824
- # proxy.add_method('sheets')
1825
- # proxy.add_method('set_default_sheet','s')
1826
- # proxy.add_method('ferien_fuer_region', 'region')
1827
-
1828
- # sheets = proxy.sheets
1829
- # p sheets
1830
- # proxy.set_default_sheet(sheets.first)
1831
-
1832
- # assert_equal 1, proxy.first_row
1833
- # assert_equal 1, proxy.first_column
1834
- # assert_equal 187, proxy.last_row
1835
- # assert_equal 7, proxy.last_column
1836
- # assert_equal 42, proxy.cell('C',8)
1837
- # assert_equal 43, proxy.cell('F',12)
1838
- # assert_equal "1.0", proxy.officeversion
1839
- # p "clientthread finished"
1840
- # #end
1841
- # #threads.each {|t| t.join }
1842
- # puts "fertig"
1843
- # Process.kill("INT",pid)
1844
- # pid = Process.wait
1845
- # puts "child terminated, pid= #{pid}, status= #{$?.exitstatus}"
1846
- # end
1847
-
1848
- def split_coord(s)
1849
- letter = ""
1850
- number = 0
1851
- i = 0
1852
- while i<s.length and "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".include?(s[i,1])
1853
- letter += s[i,1]
1854
- i+=1
1855
- end
1856
- while i<s.length and "01234567890".include?(s[i,1])
1857
- number = number*10 + s[i,1].to_i
1858
- i+=1
1859
- end
1860
- if letter=="" or number==0
1861
- raise ArgumentError
1862
- end
1863
- return letter,number
1864
- end
1865
-
1866
- #def sum(s,expression)
1867
- # arg = expression.split(':')
1868
- # b,z = split_coord(arg[0])
1869
- # first_row = z
1870
- # first_col = OpenOffice.letter_to_number(b)
1871
- # b,z = split_coord(arg[1])
1872
- # last_row = z
1873
- # last_col = OpenOffice.letter_to_number(b)
1874
- # result = 0
1875
- # first_row.upto(last_row) {|row|
1876
- # first_col.upto(last_col) {|col|
1877
- # result = result + s.cell(row,col)
1878
- # }
1879
- # }
1880
- # result
1881
- #end
1882
-
1883
- #def test_dsl
1884
- # s = OpenOffice.new(File.join(TESTDIR,"numbers1.ods"))
1885
- # s.default_sheet = s.sheets.first
1886
- #
1887
- # s.set 'a',1, 5
1888
- # s.set 'b',1, 3
1889
- # s.set 'c',1, 7
1890
- # s.set('a',2, s.cell('a',1)+s.cell('b',1))
1891
- # assert_equal 8, s.cell('a',2)
1892
- #
1893
- # assert_equal 15, sum(s,'A1:C1')
1894
- # end
1895
-
1896
- #def test_create_spreadsheet1
1897
- # name = File.join(TESTDIR,'createdspreadsheet.ods')
1898
- # rm(name) if File.exists?(File.join(TESTDIR,'createdspreadsheet.ods'))
1899
- # # anlegen, falls noch nicht existierend
1900
- # s = OpenOffice.new(name,true)
1901
- # assert File.exists?(name)
1902
- #end
1903
-
1904
- #def test_create_spreadsheet2
1905
- # # anlegen, falls noch nicht existierend
1906
- # s = OpenOffice.new(File.join(TESTDIR,"createdspreadsheet.ods"),true)
1907
- # s.set 'a',1,42
1908
- # s.set 'b',1,43
1909
- # s.set 'c',1,44
1910
- # s.save
1911
- #
1912
- # t = OpenOffice.new(File.join(TESTDIR,"createdspreadsheet.ods"))
1913
- # assert_equal 42, t.cell(1,'a')
1914
- # assert_equal 43, t.cell('b',1)
1915
- # assert_equal 44, t.cell('c',3)
1916
- #end
1917
-
1918
- # We don't have the bode-v1.xlsx test file
1919
- # #TODO: xlsx-Datei anpassen!
1920
- # def test_excelx_download_uri_and_zipped
1921
- # #TODO: gezippte xlsx Datei online zum Testen suchen
1922
- # if EXCELX
1923
- # if ONLINE
1924
- # url = 'http://stiny-leonhard.de/bode-v1.xlsx.zip'
1925
- # excel = Roo::Excelx.new(url, :zip)
1926
- # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1927
- # end
1928
- # end
1929
- # end
1930
-
1931
- # def test_excelx_zipped
1932
- # # TODO: bode...xls bei Gelegenheit nach .xlsx konverieren lassen und zippen!
1933
- # if EXCELX
1934
- # # diese Datei gibt es noch nicht gezippt
1935
- # excel = Roo::Excelx.new(File.join(TESTDIR,"bode-v1.xlsx.zip"), :zip)
1936
- # assert excel
1937
- # assert_raises(ArgumentError) {
1938
- # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1939
- # }
1940
- # excel.default_sheet = excel.sheets.first
1941
- # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1942
- # end
1943
- # end
1944
-
1945
- def test_csv_parsing_with_headers
1946
- return unless CSV
1947
- headers = ["TITEL", "VERFASSER", "OBJEKT", "NUMMER", "SEITE", "INTERNET", "PC", "KENNUNG"]
1948
-
1949
- oo = Roo::Spreadsheet.open(File.join(TESTDIR, 'Bibelbund.csv'))
1950
- parsed = oo.parse(:headers => true)
1951
- assert_equal headers, parsed[1].keys
1952
- end
1953
-
1954
- def test_bug_numbered_sheet_names
1955
- with_each_spreadsheet(:name=>'bug-numbered-sheet-names', :format=>:excelx) do |oo|
1956
- oo.each_with_pagename { }
1957
- end
1958
- end
1959
-
1960
- def test_parsing_xslx_from_numbers
1961
- return unless EXCELX
1962
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "numbers-export.xlsx"))
1963
-
1964
- xlsx.default_sheet = xlsx.sheets.first
1965
- assert_equal 'Sheet 1', xlsx.cell('a',1)
1966
-
1967
- # Another buggy behavior of Numbers 3.1: if a warkbook has more than a
1968
- # single sheet, all sheets except the first one will have an extra row and
1969
- # column added to the beginning. That's why we assert against cell B2 and
1970
- # not A1
1971
- xlsx.default_sheet = xlsx.sheets.last
1972
- assert_equal 'Sheet 2', xlsx.cell('b',2)
1973
- end
1974
-
1975
- def test_openoffice_encryption
1976
- if OPENOFFICE
1977
- assert_raises(ArgumentError) { Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods")) }
1978
- assert_raises(ArgumentError) { Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods"), :password => "badpassword") }
1979
- oo = Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods"), :password => "letmein")
1980
- oo.default_sheet = oo.sheets.first
1981
- assert_equal "Hello World", oo.cell('a',1)
1982
- end
1983
- end
1984
-
1985
- def test_expand_merged_range
1986
- return unless EXCELX
1987
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"), {:expand_merged_ranges => true})
1988
- for row in 3..7 do
1989
- for col in 'a'..'b'
1990
- if row > 3 && row < 7 && col == 'a'
1991
- assert_equal 'vertical1', xlsx.cell(col,row)
1992
- else
1993
- assert_nil xlsx.cell(col,row)
1994
- end
1995
- end
1996
- end
1997
- for row in 3..11 do
1998
- for col in 'f'..'h'
1999
- if row > 3 && row < 11 && col == 'g'
2000
- assert_equal 'vertical2', xlsx.cell(col,row)
2001
- else
2002
- assert_nil xlsx.cell(col,row)
2003
- end
2004
- end
2005
- end
2006
- for row in 3..5 do
2007
- for col in 'b'..'f'
2008
- if row == 4 && col > 'b' && col < 'f'
2009
- assert_equal 'horizontal', xlsx.cell(col,row)
2010
- else
2011
- assert_nil xlsx.cell(col,row)
2012
- end
2013
- end
2014
- end
2015
- for row in 8..13 do
2016
- for col in 'a'..'e'
2017
- if row > 8 && row < 13 && col > 'a' && col < 'e'
2018
- assert_equal 'block', xlsx.cell(col,row)
2019
- else
2020
- assert_nil xlsx.cell(col,row)
2021
- end
2022
- end
2023
- end
2024
- end
2025
-
2026
- def test_noexpand_merged_range
2027
- return unless EXCELX
2028
- xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"))
2029
- for row in 3..7 do
2030
- for col in 'a'..'b'
2031
- if row == 4 && col == 'a'
2032
- assert_equal 'vertical1', xlsx.cell(col,row)
2033
- else
2034
- assert_nil xlsx.cell(col,row)
2035
- end
2036
- end
2037
- end
2038
- for row in 3..11 do
2039
- for col in 'f'..'h'
2040
- if row == 4 && col == 'g'
2041
- assert_equal 'vertical2', xlsx.cell(col,row)
2042
- else
2043
- assert_nil xlsx.cell(col,row)
2044
- end
2045
- end
2046
- end
2047
- for row in 3..5 do
2048
- for col in 'b'..'f'
2049
- if row == 4 && col == 'c'
2050
- assert_equal 'horizontal', xlsx.cell(col,row)
2051
- else
2052
- assert_nil xlsx.cell(col,row)
2053
- end
2054
- end
2055
- end
2056
- for row in 8..13 do
2057
- for col in 'a'..'e'
2058
- if row == 9 && col == 'b'
2059
- assert_equal 'block', xlsx.cell(col,row)
2060
- else
2061
- assert_nil xlsx.cell(col,row)
2062
- end
2063
- end
2064
- end
2065
- end
2066
-
2067
- def test_open_stream
2068
- return unless EXCELX
2069
- file_contents = File.read File.join(TESTDIR, fixture_filename(:numbers1, :excelx))
2070
- stream = StringIO.new(file_contents)
2071
- xlsx = Roo::Excelx.new(stream)
2072
- assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], xlsx.sheets
2073
- end
2074
-
2075
- def test_close
2076
- with_each_spreadsheet(:name=>'numbers1') do |oo|
2077
- next unless (tempdir = oo.instance_variable_get('@tmpdir'))
2078
- oo.close
2079
- assert !File.exists?(tempdir), "Expected #{tempdir} to be cleaned up, but it still exists"
2080
- end
2081
- end
2082
-
2083
- def test_cleanup_on_error
2084
- old_temp_files = Dir.open(Dir.tmpdir).to_a
2085
- with_each_spreadsheet(:name=>'non_existent_file', :ignore_errors=>true) do |oo|; end
2086
- assert_equal Dir.open(Dir.tmpdir).to_a, old_temp_files
2087
- end
2088
- end # class
1
+ # encoding: utf-8
2
+ # damit keine falschen Vermutungen aufkommen: Ich habe religioes rein gar nichts
3
+ # mit diesem Bibelbund zu tun, aber die hatten eine ziemlich grosse
4
+ # Spreadsheet-Datei mit ca. 3500 Zeilen oeffentlich im Netz, die sich ganz gut
5
+ # zum Testen eignete.
6
+ #
7
+ #--
8
+ # these test cases were developed to run under Linux OS, some commands
9
+ # (like 'diff') must be changed (or commented out ;-)) if you want to run
10
+ # the tests under another OS
11
+ #
12
+
13
+ #TODO
14
+ # Look at formulas in excel - does not work with date/time
15
+
16
+ # Dump warnings that come from the test to open files
17
+ # with the wrong spreadsheet class
18
+ #STDERR.reopen "/dev/null","w"
19
+
20
+ require 'test_helper'
21
+ require 'stringio'
22
+
23
+ class TestRoo < Minitest::Test
24
+
25
+ OPENOFFICE = true # do OpenOffice-Spreadsheet Tests? (.ods files)
26
+ EXCELX = true # do Excelx Tests? (.xlsx files)
27
+ LIBREOFFICE = true # do LibreOffice tests? (.ods files)
28
+ CSV = true # do CSV tests? (.csv files)
29
+
30
+ FORMATS = [
31
+ :excelx,
32
+ :excelxm,
33
+ :openoffice,
34
+ :libreoffice
35
+ ]
36
+
37
+ ONLINE = false
38
+ LONG_RUN = false
39
+
40
+ def fixture_filename(name, format)
41
+ case format
42
+ when :excelx
43
+ "#{name}.xlsx"
44
+ when :excelxm
45
+ "#{name}.xlsm"
46
+ when :openoffice, :libreoffice
47
+ "#{name}.ods"
48
+ else
49
+ raise ArgumentError, "unexpected format #{format}"
50
+ end
51
+ end
52
+
53
+ # call a block of code for each spreadsheet type
54
+ # and yield a reference to the roo object
55
+ def with_each_spreadsheet(options)
56
+ if options[:format]
57
+ formats = Array(options[:format])
58
+ invalid_formats = formats - FORMATS
59
+ unless invalid_formats.empty?
60
+ raise "invalid spreadsheet types: #{invalid_formats.join(', ')}"
61
+ end
62
+ else
63
+ formats = FORMATS
64
+ end
65
+ formats.each do |format|
66
+ begin
67
+ yield Roo::Spreadsheet.open(File.join(TESTDIR,
68
+ fixture_filename(options[:name], format)))
69
+ rescue => e
70
+ raise e, "#{e.message} for #{format}", e.backtrace unless options[:ignore_errors]
71
+ end
72
+ end
73
+ end
74
+
75
+ def test_sheets_csv
76
+ if CSV
77
+ oo = Roo::CSV.new(File.join(TESTDIR,'numbers1.csv'))
78
+ assert_equal ["default"], oo.sheets
79
+ assert_raises(RangeError) { oo.default_sheet = "no_sheet" }
80
+ assert_raises(TypeError) { oo.default_sheet = [1,2,3] }
81
+ oo.sheets.each { |sh|
82
+ oo.default_sheet = sh
83
+ assert_equal sh, oo.default_sheet
84
+ }
85
+ end
86
+ end
87
+
88
+ def test_sheets
89
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
90
+ assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], oo.sheets
91
+ assert_raises(RangeError) { oo.default_sheet = "no_sheet" }
92
+ assert_raises(TypeError) { oo.default_sheet = [1,2,3] }
93
+ oo.sheets.each { |sh|
94
+ oo.default_sheet = sh
95
+ assert_equal sh, oo.default_sheet
96
+ }
97
+ end
98
+ end
99
+
100
+ def test_cells
101
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
102
+ # warum ist Auswaehlen erstes sheet hier nicht
103
+ # mehr drin?
104
+ oo.default_sheet = oo.sheets.first
105
+ assert_equal 1, oo.cell(1,1)
106
+ assert_equal 2, oo.cell(1,2)
107
+ assert_equal 3, oo.cell(1,3)
108
+ assert_equal 4, oo.cell(1,4)
109
+ assert_equal 5, oo.cell(2,1)
110
+ assert_equal 6, oo.cell(2,2)
111
+ assert_equal 7, oo.cell(2,3)
112
+ assert_equal 8, oo.cell(2,4)
113
+ assert_equal 9, oo.cell(2,5)
114
+ assert_equal "test", oo.cell(2,6)
115
+ assert_equal :string, oo.celltype(2,6)
116
+ assert_equal 11, oo.cell(2,7)
117
+ unless oo.kind_of? Roo::CSV
118
+ assert_equal :float, oo.celltype(2,7)
119
+ end
120
+ assert_equal 10, oo.cell(4,1)
121
+ assert_equal 11, oo.cell(4,2)
122
+ assert_equal 12, oo.cell(4,3)
123
+ assert_equal 13, oo.cell(4,4)
124
+ assert_equal 14, oo.cell(4,5)
125
+ assert_equal 10, oo.cell(4,'A')
126
+ assert_equal 11, oo.cell(4,'B')
127
+ assert_equal 12, oo.cell(4,'C')
128
+ assert_equal 13, oo.cell(4,'D')
129
+ assert_equal 14, oo.cell(4,'E')
130
+ unless oo.kind_of? Roo::CSV
131
+ assert_equal :date, oo.celltype(5,1)
132
+ assert_equal Date.new(1961,11,21), oo.cell(5,1)
133
+ assert_equal "1961-11-21", oo.cell(5,1).to_s
134
+ end
135
+ end
136
+ end
137
+
138
+ def test_celltype
139
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
140
+ assert_equal :string, oo.celltype(2,6)
141
+ end
142
+ end
143
+
144
+ def test_cell_address
145
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
146
+ assert_equal "tata", oo.cell(6,1)
147
+ assert_equal "tata", oo.cell(6,'A')
148
+ assert_equal "tata", oo.cell('A',6)
149
+ assert_equal "tata", oo.cell(6,'a')
150
+ assert_equal "tata", oo.cell('a',6)
151
+ assert_raises(ArgumentError) { assert_equal "tata", oo.cell('a','f') }
152
+ assert_raises(ArgumentError) { assert_equal "tata", oo.cell('f','a') }
153
+ assert_equal "thisisc8", oo.cell(8,3)
154
+ assert_equal "thisisc8", oo.cell(8,'C')
155
+ assert_equal "thisisc8", oo.cell('C',8)
156
+ assert_equal "thisisc8", oo.cell(8,'c')
157
+ assert_equal "thisisc8", oo.cell('c',8)
158
+ assert_equal "thisisd9", oo.cell('d',9)
159
+ assert_equal "thisisa11", oo.cell('a',11)
160
+ end
161
+ end
162
+
163
+ def test_office_version
164
+ with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
165
+ assert_equal "1.0", oo.officeversion
166
+ end
167
+ end
168
+
169
+ def test_libre_office
170
+ if LIBREOFFICE
171
+ oo = Roo::LibreOffice.new(File.join(TESTDIR, "numbers1.ods"))
172
+ oo.default_sheet = oo.sheets.first
173
+ assert_equal 41, oo.cell('a',12)
174
+ end
175
+ end
176
+
177
+ def test_sheetname
178
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
179
+ oo.default_sheet = "Name of Sheet 2"
180
+ assert_equal 'I am sheet 2', oo.cell('C',5)
181
+ assert_raises(RangeError) { oo.default_sheet = "non existing sheet name" }
182
+ assert_raises(RangeError) { oo.default_sheet = "non existing sheet name" }
183
+ assert_raises(RangeError) { oo.cell('C',5,"non existing sheet name")}
184
+ assert_raises(RangeError) { oo.celltype('C',5,"non existing sheet name")}
185
+ assert_raises(RangeError) { oo.empty?('C',5,"non existing sheet name")}
186
+ assert_raises(RangeError) { oo.formula?('C',5,"non existing sheet name")}
187
+ assert_raises(RangeError) { oo.formula('C',5,"non existing sheet name")}
188
+ assert_raises(RangeError) { oo.set('C',5,42,"non existing sheet name")}
189
+ assert_raises(RangeError) { oo.formulas("non existing sheet name")}
190
+ assert_raises(RangeError) { oo.to_yaml({},1,1,1,1,"non existing sheet name")}
191
+ end
192
+ end
193
+
194
+ def test_argument_error
195
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
196
+ oo.default_sheet = "Tabelle1"
197
+ end
198
+ end
199
+
200
+ def test_bug_contiguous_cells
201
+ with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
202
+ oo.default_sheet = "Sheet4"
203
+ assert_equal Date.new(2007,06,16), oo.cell('a',1)
204
+ assert_equal 10, oo.cell('b',1)
205
+ assert_equal 10, oo.cell('c',1)
206
+ assert_equal 10, oo.cell('d',1)
207
+ assert_equal 10, oo.cell('e',1)
208
+ end
209
+ end
210
+
211
+ def test_bug_italo_ve
212
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
213
+ oo.default_sheet = "Sheet5"
214
+ assert_equal 1, oo.cell('A',1)
215
+ assert_equal 5, oo.cell('b',1)
216
+ assert_equal 5, oo.cell('c',1)
217
+ assert_equal 2, oo.cell('a',2)
218
+ assert_equal 3, oo.cell('a',3)
219
+ end
220
+ end
221
+
222
+ def test_italo_table
223
+ with_each_spreadsheet(:name=>'simple_spreadsheet_from_italo', :format=>:openoffice) do |oo|
224
+ assert_equal '1', oo.cell('A',1)
225
+ assert_equal '1', oo.cell('B',1)
226
+ assert_equal '1', oo.cell('C',1)
227
+ assert_equal 1, oo.cell('A',2).to_i
228
+ assert_equal 2, oo.cell('B',2).to_i
229
+ assert_equal 1, oo.cell('C',2).to_i
230
+ assert_equal 1, oo.cell('A',3)
231
+ assert_equal 3, oo.cell('B',3)
232
+ assert_equal 1, oo.cell('C',3)
233
+ assert_equal 'A', oo.cell('A',4)
234
+ assert_equal 'A', oo.cell('B',4)
235
+ assert_equal 'A', oo.cell('C',4)
236
+ assert_equal 0.01, oo.cell('A',5)
237
+ assert_equal 0.01, oo.cell('B',5)
238
+ assert_equal 0.01, oo.cell('C',5)
239
+ assert_equal 0.03, oo.cell('a',5)+oo.cell('b',5)+oo.cell('c',5)
240
+
241
+ # Cells values in row 1:
242
+ assert_equal "1:string", oo.cell(1, 1)+":"+oo.celltype(1, 1).to_s
243
+ assert_equal "1:string",oo.cell(1, 2)+":"+oo.celltype(1, 2).to_s
244
+ assert_equal "1:string",oo.cell(1, 3)+":"+oo.celltype(1, 3).to_s
245
+
246
+ # Cells values in row 2:
247
+ assert_equal "1:string",oo.cell(2, 1)+":"+oo.celltype(2, 1).to_s
248
+ assert_equal "2:string",oo.cell(2, 2)+":"+oo.celltype(2, 2).to_s
249
+ assert_equal "1:string",oo.cell(2, 3)+":"+oo.celltype(2, 3).to_s
250
+
251
+ # Cells values in row 3:
252
+ assert_equal "1.0:float",oo.cell(3, 1).to_s+":"+oo.celltype(3, 1).to_s
253
+ assert_equal "3.0:float",oo.cell(3, 2).to_s+":"+oo.celltype(3, 2).to_s
254
+ assert_equal "1.0:float",oo.cell(3, 3).to_s+":"+oo.celltype(3, 3).to_s
255
+
256
+ # Cells values in row 4:
257
+ assert_equal "A:string",oo.cell(4, 1)+":"+oo.celltype(4, 1).to_s
258
+ assert_equal "A:string",oo.cell(4, 2)+":"+oo.celltype(4, 2).to_s
259
+ assert_equal "A:string",oo.cell(4, 3)+":"+oo.celltype(4, 3).to_s
260
+
261
+ # Cells values in row 5:
262
+ if oo.class == Roo::OpenOffice
263
+ assert_equal "0.01:percentage",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
264
+ assert_equal "0.01:percentage",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
265
+ assert_equal "0.01:percentage",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
266
+ else
267
+ assert_equal "0.01:float",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
268
+ assert_equal "0.01:float",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
269
+ assert_equal "0.01:float",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
270
+ end
271
+ end
272
+ end
273
+
274
+ def test_formula_openoffice
275
+ with_each_spreadsheet(:name=>'formula', :format=>:openoffice) do |oo|
276
+ assert_equal 1, oo.cell('A',1)
277
+ assert_equal 2, oo.cell('A',2)
278
+ assert_equal 3, oo.cell('A',3)
279
+ assert_equal 4, oo.cell('A',4)
280
+ assert_equal 5, oo.cell('A',5)
281
+ assert_equal 6, oo.cell('A',6)
282
+ assert_equal 21, oo.cell('A',7)
283
+ assert_equal :formula, oo.celltype('A',7)
284
+ assert_equal "=[Sheet2.A1]", oo.formula('C',7)
285
+ assert_nil oo.formula('A',6)
286
+ assert_equal [[7, 1, "=SUM([.A1:.A6])"],
287
+ [7, 2, "=SUM([.$A$1:.B6])"],
288
+ [7, 3, "=[Sheet2.A1]"],
289
+ [8, 2, "=SUM([.$A$1:.B7])"],
290
+ ], oo.formulas(oo.sheets.first)
291
+
292
+ # setting a cell
293
+ oo.set('A',15, 41)
294
+ assert_equal 41, oo.cell('A',15)
295
+ oo.set('A',16, "41")
296
+ assert_equal "41", oo.cell('A',16)
297
+ oo.set('A',17, 42.5)
298
+ assert_equal 42.5, oo.cell('A',17)
299
+ end
300
+ end
301
+
302
+ def test_header_with_brackets_excelx
303
+ with_each_spreadsheet(:name => 'advanced_header', :format => :openoffice) do |oo|
304
+ parsed_head = oo.parse(:headers => true)
305
+ assert_equal "Date(yyyy-mm-dd)", oo.cell('A',1)
306
+ assert_equal parsed_head[0].keys, ["Date(yyyy-mm-dd)"]
307
+ assert_equal parsed_head[0].values, ["Date(yyyy-mm-dd)"]
308
+ end
309
+ end
310
+
311
+ def test_formula_excelx
312
+ with_each_spreadsheet(:name=>'formula', :format=>:excelx) do |oo|
313
+ assert_equal 1, oo.cell('A',1)
314
+ assert_equal 2, oo.cell('A',2)
315
+ assert_equal 3, oo.cell('A',3)
316
+ assert_equal 4, oo.cell('A',4)
317
+ assert_equal 5, oo.cell('A',5)
318
+ assert_equal 6, oo.cell('A',6)
319
+ assert_equal 21, oo.cell('A',7)
320
+ assert_equal :formula, oo.celltype('A',7)
321
+ #steht nicht in Datei, oder?
322
+ #nein, diesen Bezug habe ich nur in der OpenOffice-Datei
323
+ #assert_equal "=[Sheet2.A1]", oo.formula('C',7)
324
+ assert_nil oo.formula('A',6)
325
+ # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
326
+ # [7, 2, "=SUM([.$A$1:.B6])"],
327
+ #[7, 3, "=[Sheet2.A1]"],
328
+ #[8, 2, "=SUM([.$A$1:.B7])"],
329
+ #], oo.formulas(oo.sheets.first)
330
+ assert_equal [[7, 1, 'SUM(A1:A6)'],
331
+ [7, 2, 'SUM($A$1:B6)'],
332
+ # [7, 3, "=[Sheet2.A1]"],
333
+ # [8, 2, "=SUM([.$A$1:.B7])"],
334
+ ], oo.formulas(oo.sheets.first)
335
+
336
+ # setting a cell
337
+ oo.set('A',15, 41)
338
+ assert_equal 41, oo.cell('A',15)
339
+ oo.set('A',16, "41")
340
+ assert_equal "41", oo.cell('A',16)
341
+ oo.set('A',17, 42.5)
342
+ assert_equal 42.5, oo.cell('A',17)
343
+ end
344
+ end
345
+
346
+ def test_borders_sheets
347
+ with_each_spreadsheet(:name=>'borders') do |oo|
348
+ oo.default_sheet = oo.sheets[1]
349
+ assert_equal 6, oo.first_row
350
+ assert_equal 11, oo.last_row
351
+ assert_equal 4, oo.first_column
352
+ assert_equal 8, oo.last_column
353
+
354
+ oo.default_sheet = oo.sheets.first
355
+ assert_equal 5, oo.first_row
356
+ assert_equal 10, oo.last_row
357
+ assert_equal 3, oo.first_column
358
+ assert_equal 7, oo.last_column
359
+
360
+ oo.default_sheet = oo.sheets[2]
361
+ assert_equal 7, oo.first_row
362
+ assert_equal 12, oo.last_row
363
+ assert_equal 5, oo.first_column
364
+ assert_equal 9, oo.last_column
365
+ end
366
+ end
367
+
368
+ def test_only_one_sheet
369
+ with_each_spreadsheet(:name=>'only_one_sheet') do |oo|
370
+ assert_equal 42, oo.cell('B',4)
371
+ assert_equal 43, oo.cell('C',4)
372
+ assert_equal 44, oo.cell('D',4)
373
+ oo.default_sheet = oo.sheets.first
374
+ assert_equal 42, oo.cell('B',4)
375
+ assert_equal 43, oo.cell('C',4)
376
+ assert_equal 44, oo.cell('D',4)
377
+ end
378
+ end
379
+
380
+ def test_openoffice_download_uri_and_zipped
381
+ if OPENOFFICE
382
+ if ONLINE
383
+ url = 'http://spazioinwind.libero.it/s2/rata.ods.zip'
384
+ sheet = Roo::OpenOffice.new(url, packed: :zip)
385
+ #has been changed: assert_equal 'ist "e" im Nenner von H(s)', sheet.cell('b', 5)
386
+ assert_in_delta 0.001, 505.14, sheet.cell('c', 33).to_f
387
+ end
388
+ end
389
+ end
390
+
391
+ def test_openoffice_zipped
392
+ if OPENOFFICE
393
+ oo = Roo::OpenOffice.new(File.join(TESTDIR,"bode-v1.ods.zip"), packed: :zip)
394
+ assert oo
395
+ assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
396
+ end
397
+ end
398
+
399
+ def test_bug_ric
400
+ with_each_spreadsheet(:name=>'ric', :format=>:openoffice) do |oo|
401
+ assert oo.empty?('A',1)
402
+ assert oo.empty?('B',1)
403
+ assert oo.empty?('C',1)
404
+ assert oo.empty?('D',1)
405
+ expected = 1
406
+ letter = 'e'
407
+ while letter <= 'u'
408
+ assert_equal expected, oo.cell(letter,1)
409
+ letter.succ!
410
+ expected += 1
411
+ end
412
+ assert_equal 'J', oo.cell('v',1)
413
+ assert_equal 'P', oo.cell('w',1)
414
+ assert_equal 'B', oo.cell('x',1)
415
+ assert_equal 'All', oo.cell('y',1)
416
+ assert_equal 0, oo.cell('a',2)
417
+ assert oo.empty?('b',2)
418
+ assert oo.empty?('c',2)
419
+ assert oo.empty?('d',2)
420
+ assert_equal 'B', oo.cell('e',2)
421
+ assert_equal 'B', oo.cell('f',2)
422
+ assert_equal 'B', oo.cell('g',2)
423
+ assert_equal 'B', oo.cell('h',2)
424
+ assert_equal 'B', oo.cell('i',2)
425
+ assert_equal 'B', oo.cell('j',2)
426
+ assert_equal 'B', oo.cell('k',2)
427
+ assert_equal 'B', oo.cell('l',2)
428
+ assert_equal 'B', oo.cell('m',2)
429
+ assert_equal 'B', oo.cell('n',2)
430
+ assert_equal 'B', oo.cell('o',2)
431
+ assert_equal 'B', oo.cell('p',2)
432
+ assert_equal 'B', oo.cell('q',2)
433
+ assert_equal 'B', oo.cell('r',2)
434
+ assert_equal 'B', oo.cell('s',2)
435
+ assert oo.empty?('t',2)
436
+ assert oo.empty?('u',2)
437
+ assert_equal 0 , oo.cell('v',2)
438
+ assert_equal 0 , oo.cell('w',2)
439
+ assert_equal 15 , oo.cell('x',2)
440
+ assert_equal 15 , oo.cell('y',2)
441
+ end
442
+ end
443
+
444
+ def test_mehrteilig
445
+ with_each_spreadsheet(:name=>'Bibelbund1', :format=>:openoffice) do |oo|
446
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
447
+ end
448
+ #if EXCELX
449
+ # #Datei gibt es noch nicht
450
+ # oo = Roo::Excelx.new(File.join(TESTDIR,"Bibelbund1.xlsx"))
451
+ # oo.default_sheet = oo.sheets.first
452
+ # assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
453
+ #end
454
+ end
455
+
456
+ # "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt
457
+ # wird.
458
+ # Besser: Methode um temporaeres Dir. portabel zu bestimmen
459
+ def test_huge_document_to_csv
460
+ if LONG_RUN
461
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[
462
+ :openoffice,
463
+ :excelx
464
+ # Google hier nicht, weil Google-Spreadsheets nicht so gross werden
465
+ # duerfen
466
+ ]) do |oo|
467
+ Dir.mktmpdir do |tempdir|
468
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
469
+ assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46,'A')
470
+ assert_equal "Tagebuch aus Chile Juli 1977", oo.cell(55,'A')
471
+ assert oo.to_csv(File.join(tempdir,"Bibelbund.csv"))
472
+ assert File.exists?(File.join(tempdir,"Bibelbund.csv"))
473
+ assert_equal "", file_diff(File.join(TESTDIR, "Bibelbund.csv"), File.join(tempdir,"Bibelbund.csv")),
474
+ "error in class #{oo.class}"
475
+ #end
476
+ end
477
+ end
478
+ end
479
+ end
480
+
481
+ def test_bug_quotes_excelx
482
+ if LONG_RUN
483
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice, :excelx]) do |oo|
484
+ oo.default_sheet = oo.sheets.first
485
+ assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
486
+ oo.cell('a',76)
487
+ oo.to_csv("csv#{$$}")
488
+ assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
489
+ oo.cell('a',78)
490
+ File.delete_if_exist("csv#{$$}")
491
+ end
492
+ end
493
+ end
494
+
495
+ def test_bug_mehrere_datum
496
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
497
+ oo.default_sheet = 'Sheet5'
498
+ assert_equal :date, oo.celltype('A',4)
499
+ assert_equal :date, oo.celltype('B',4)
500
+ assert_equal :date, oo.celltype('C',4)
501
+ assert_equal :date, oo.celltype('D',4)
502
+ assert_equal :date, oo.celltype('E',4)
503
+ assert_equal Date.new(2007,11,21), oo.cell('A',4)
504
+ assert_equal Date.new(2007,11,21), oo.cell('B',4)
505
+ assert_equal Date.new(2007,11,21), oo.cell('C',4)
506
+ assert_equal Date.new(2007,11,21), oo.cell('D',4)
507
+ assert_equal Date.new(2007,11,21), oo.cell('E',4)
508
+ assert_equal :float, oo.celltype('A',5)
509
+ assert_equal :float, oo.celltype('B',5)
510
+ assert_equal :float, oo.celltype('C',5)
511
+ assert_equal :float, oo.celltype('D',5)
512
+ assert_equal :float, oo.celltype('E',5)
513
+ assert_equal 42, oo.cell('A',5)
514
+ assert_equal 42, oo.cell('B',5)
515
+ assert_equal 42, oo.cell('C',5)
516
+ assert_equal 42, oo.cell('D',5)
517
+ assert_equal 42, oo.cell('E',5)
518
+ assert_equal :string, oo.celltype('A',6)
519
+ assert_equal :string, oo.celltype('B',6)
520
+ assert_equal :string, oo.celltype('C',6)
521
+ assert_equal :string, oo.celltype('D',6)
522
+ assert_equal :string, oo.celltype('E',6)
523
+ assert_equal "ABC", oo.cell('A',6)
524
+ assert_equal "ABC", oo.cell('B',6)
525
+ assert_equal "ABC", oo.cell('C',6)
526
+ assert_equal "ABC", oo.cell('D',6)
527
+ assert_equal "ABC", oo.cell('E',6)
528
+ end
529
+ end
530
+
531
+ def test_multiple_sheets
532
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
533
+ 2.times do
534
+ oo.default_sheet = "Tabelle1"
535
+ assert_equal 1, oo.cell(1,1)
536
+ assert_equal 1, oo.cell(1,1,"Tabelle1")
537
+ assert_equal "I am sheet 2", oo.cell('C',5,"Name of Sheet 2")
538
+ sheetname = 'Sheet5'
539
+ assert_equal :date, oo.celltype('A',4,sheetname)
540
+ assert_equal :date, oo.celltype('B',4,sheetname)
541
+ assert_equal :date, oo.celltype('C',4,sheetname)
542
+ assert_equal :date, oo.celltype('D',4,sheetname)
543
+ assert_equal :date, oo.celltype('E',4,sheetname)
544
+ assert_equal Date.new(2007,11,21), oo.cell('A',4,sheetname)
545
+ assert_equal Date.new(2007,11,21), oo.cell('B',4,sheetname)
546
+ assert_equal Date.new(2007,11,21), oo.cell('C',4,sheetname)
547
+ assert_equal Date.new(2007,11,21), oo.cell('D',4,sheetname)
548
+ assert_equal Date.new(2007,11,21), oo.cell('E',4,sheetname)
549
+ assert_equal :float, oo.celltype('A',5,sheetname)
550
+ assert_equal :float, oo.celltype('B',5,sheetname)
551
+ assert_equal :float, oo.celltype('C',5,sheetname)
552
+ assert_equal :float, oo.celltype('D',5,sheetname)
553
+ assert_equal :float, oo.celltype('E',5,sheetname)
554
+ assert_equal 42, oo.cell('A',5,sheetname)
555
+ assert_equal 42, oo.cell('B',5,sheetname)
556
+ assert_equal 42, oo.cell('C',5,sheetname)
557
+ assert_equal 42, oo.cell('D',5,sheetname)
558
+ assert_equal 42, oo.cell('E',5,sheetname)
559
+ assert_equal :string, oo.celltype('A',6,sheetname)
560
+ assert_equal :string, oo.celltype('B',6,sheetname)
561
+ assert_equal :string, oo.celltype('C',6,sheetname)
562
+ assert_equal :string, oo.celltype('D',6,sheetname)
563
+ assert_equal :string, oo.celltype('E',6,sheetname)
564
+ assert_equal "ABC", oo.cell('A',6,sheetname)
565
+ assert_equal "ABC", oo.cell('B',6,sheetname)
566
+ assert_equal "ABC", oo.cell('C',6,sheetname)
567
+ assert_equal "ABC", oo.cell('D',6,sheetname)
568
+ assert_equal "ABC", oo.cell('E',6,sheetname)
569
+ oo.reload
570
+ end
571
+ end
572
+ end
573
+
574
+
575
+ def test_bug_empty_sheet
576
+ with_each_spreadsheet(:name=>'formula', :format=>[:openoffice, :excelx]) do |oo|
577
+ oo.default_sheet = 'Sheet3' # is an empty sheet
578
+ Dir.mktmpdir do |tempdir|
579
+ oo.to_csv(File.join(tempdir,"emptysheet.csv"))
580
+ assert_equal "", `cat #{File.join(tempdir,"emptysheet.csv")}`
581
+ end
582
+ end
583
+ end
584
+
585
+ def test_find_by_row_huge_document
586
+ if LONG_RUN
587
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice, :excelx]) do |oo|
588
+ oo.default_sheet = oo.sheets.first
589
+ rec = oo.find 20
590
+ assert rec
591
+ # assert_equal "Brief aus dem Sekretariat", rec[0]
592
+ #p rec
593
+ assert_equal "Brief aus dem Sekretariat", rec[0]['TITEL']
594
+ rec = oo.find 22
595
+ assert rec
596
+ # assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]
597
+ assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]['TITEL']
598
+ end
599
+ end
600
+ end
601
+
602
+ def test_find_by_row
603
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
604
+ oo.header_line = nil
605
+ rec = oo.find 16
606
+ assert rec
607
+ assert_nil oo.header_line
608
+ # keine Headerlines in diesem Beispiel definiert
609
+ assert_equal "einundvierzig", rec[0]
610
+ #assert_equal false, rec
611
+ rec = oo.find 15
612
+ assert rec
613
+ assert_equal 41,rec[0]
614
+ end
615
+ end
616
+
617
+ def test_find_by_row_if_header_line_is_not_nil
618
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
619
+ oo.header_line = 2
620
+ refute_nil oo.header_line
621
+ rec = oo.find 1
622
+ assert rec
623
+ assert_equal 5, rec[0]
624
+ assert_equal 6, rec[1]
625
+ rec = oo.find 15
626
+ assert rec
627
+ assert_equal "einundvierzig", rec[0]
628
+ end
629
+ end
630
+
631
+ def test_find_by_conditions
632
+ if LONG_RUN
633
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
634
+ :excelx]) do |oo|
635
+ #-----------------------------------------------------------------
636
+ zeilen = oo.find(:all, :conditions => {
637
+ 'TITEL' => 'Brief aus dem Sekretariat'
638
+ }
639
+ )
640
+ assert_equal 2, zeilen.size
641
+ assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
642
+ "INTERNET"=>nil,
643
+ "SEITE"=>316.0,
644
+ "KENNUNG"=>"Aus dem Bibelbund",
645
+ "OBJEKT"=>"Bibel+Gem",
646
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
647
+ "NUMMER"=>"1982-3",
648
+ "TITEL"=>"Brief aus dem Sekretariat"},
649
+ {"VERFASSER"=>"Almassy, Annelene von",
650
+ "INTERNET"=>nil,
651
+ "SEITE"=>222.0,
652
+ "KENNUNG"=>"Aus dem Bibelbund",
653
+ "OBJEKT"=>"Bibel+Gem",
654
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
655
+ "NUMMER"=>"1983-2",
656
+ "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
657
+
658
+ #----------------------------------------------------------
659
+ zeilen = oo.find(:all,
660
+ :conditions => { 'VERFASSER' => 'Almassy, Annelene von' }
661
+ )
662
+ assert_equal 13, zeilen.size
663
+ #----------------------------------------------------------
664
+ zeilen = oo.find(:all, :conditions => {
665
+ 'TITEL' => 'Brief aus dem Sekretariat',
666
+ 'VERFASSER' => 'Almassy, Annelene von',
667
+ }
668
+ )
669
+ assert_equal 2, zeilen.size
670
+ assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
671
+ "INTERNET"=>nil,
672
+ "SEITE"=>316.0,
673
+ "KENNUNG"=>"Aus dem Bibelbund",
674
+ "OBJEKT"=>"Bibel+Gem",
675
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
676
+ "NUMMER"=>"1982-3",
677
+ "TITEL"=>"Brief aus dem Sekretariat"},
678
+ {"VERFASSER"=>"Almassy, Annelene von",
679
+ "INTERNET"=>nil,
680
+ "SEITE"=>222.0,
681
+ "KENNUNG"=>"Aus dem Bibelbund",
682
+ "OBJEKT"=>"Bibel+Gem",
683
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
684
+ "NUMMER"=>"1983-2",
685
+ "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
686
+
687
+ # Result as an array
688
+ zeilen = oo.find(:all,
689
+ :conditions => {
690
+ 'TITEL' => 'Brief aus dem Sekretariat',
691
+ 'VERFASSER' => 'Almassy, Annelene von',
692
+ }, :array => true)
693
+ assert_equal 2, zeilen.size
694
+ assert_equal [
695
+ [
696
+ "Brief aus dem Sekretariat",
697
+ "Almassy, Annelene von",
698
+ "Bibel+Gem",
699
+ "1982-3",
700
+ 316.0,
701
+ nil,
702
+ "#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
703
+ "Aus dem Bibelbund",
704
+ ],
705
+ [
706
+ "Brief aus dem Sekretariat",
707
+ "Almassy, Annelene von",
708
+ "Bibel+Gem",
709
+ "1983-2",
710
+ 222.0,
711
+ nil,
712
+ "#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
713
+ "Aus dem Bibelbund",
714
+ ]] , zeilen
715
+ end
716
+ end
717
+ end
718
+
719
+
720
+ #TODO: temporaerer Test
721
+ def test_seiten_als_date
722
+ if LONG_RUN
723
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>:excelx) do |oo|
724
+ assert_equal 'Bericht aus dem Sekretariat', oo.cell(13,1)
725
+ assert_equal '1981-4', oo.cell(13,'D')
726
+ assert_equal String, oo.excelx_type(13,'E')[1].class
727
+ assert_equal [:numeric_or_formula,"General"], oo.excelx_type(13,'E')
728
+ assert_equal '428', oo.excelx_value(13,'E')
729
+ assert_equal 428.0, oo.cell(13,'E')
730
+ end
731
+ end
732
+ end
733
+
734
+ def test_column
735
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
736
+ expected = [1.0,5.0,nil,10.0,Date.new(1961,11,21),'tata',nil,nil,nil,nil,'thisisa11',41.0,nil,nil,41.0,'einundvierzig',nil,Date.new(2007,5,31)]
737
+ assert_equal expected, oo.column(1)
738
+ assert_equal expected, oo.column('a')
739
+ end
740
+ end
741
+
742
+ def test_column_huge_document
743
+ if LONG_RUN
744
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
745
+ :excelx]) do |oo|
746
+ oo.default_sheet = oo.sheets.first
747
+ assert_equal 3735, oo.column('a').size
748
+ #assert_equal 499, oo.column('a').size
749
+ end
750
+ end
751
+ end
752
+
753
+ def test_simple_spreadsheet_find_by_condition
754
+ with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
755
+ oo.header_line = 3
756
+ # oo.date_format = '%m/%d/%Y' if oo.class == Google
757
+ erg = oo.find(:all, :conditions => {'Comment' => 'Task 1'})
758
+ assert_equal Date.new(2007,05,07), erg[1]['Date']
759
+ assert_equal 10.75 , erg[1]['Start time']
760
+ assert_equal 12.50 , erg[1]['End time']
761
+ assert_equal 0 , erg[1]['Pause']
762
+ assert_equal 1.75 , erg[1]['Sum']
763
+ assert_equal "Task 1" , erg[1]['Comment']
764
+ end
765
+ end
766
+
767
+ def get_extension(oo)
768
+ case oo
769
+ when Roo::OpenOffice
770
+ ".ods"
771
+ when Roo::Excelx
772
+ ".xlsx"
773
+ end
774
+ end
775
+
776
+ def test_info
777
+ expected_templ = "File: numbers1%s\n"+
778
+ "Number of sheets: 5\n"+
779
+ "Sheets: Tabelle1, Name of Sheet 2, Sheet3, Sheet4, Sheet5\n"+
780
+ "Sheet 1:\n"+
781
+ " First row: 1\n"+
782
+ " Last row: 18\n"+
783
+ " First column: A\n"+
784
+ " Last column: G\n"+
785
+ "Sheet 2:\n"+
786
+ " First row: 5\n"+
787
+ " Last row: 14\n"+
788
+ " First column: B\n"+
789
+ " Last column: E\n"+
790
+ "Sheet 3:\n"+
791
+ " First row: 1\n"+
792
+ " Last row: 1\n"+
793
+ " First column: A\n"+
794
+ " Last column: BA\n"+
795
+ "Sheet 4:\n"+
796
+ " First row: 1\n"+
797
+ " Last row: 1\n"+
798
+ " First column: A\n"+
799
+ " Last column: E\n"+
800
+ "Sheet 5:\n"+
801
+ " First row: 1\n"+
802
+ " Last row: 6\n"+
803
+ " First column: A\n"+
804
+ " Last column: E"
805
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
806
+ ext = get_extension(oo)
807
+ expected = sprintf(expected_templ,ext)
808
+ begin
809
+ if oo.class == Google
810
+ assert_equal expected.gsub(/numbers1/,key_of("numbers1")), oo.info
811
+ else
812
+ assert_equal expected, oo.info
813
+ end
814
+ rescue NameError
815
+ #
816
+ end
817
+ end
818
+ end
819
+
820
+ def test_info_doesnt_set_default_sheet
821
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
822
+ oo.default_sheet = 'Sheet3'
823
+ oo.info
824
+ assert_equal 'Sheet3', oo.default_sheet
825
+ end
826
+ end
827
+
828
+ def test_should_raise_file_not_found_error
829
+ if OPENOFFICE
830
+ assert_raises(IOError) {
831
+ Roo::OpenOffice.new(File.join('testnichtvorhanden','Bibelbund.ods'))
832
+ }
833
+ end
834
+ if EXCELX
835
+ assert_raises(IOError) {
836
+ Roo::Excelx.new(File.join('testnichtvorhanden','Bibelbund.xlsx'))
837
+ }
838
+ end
839
+ end
840
+
841
+ def test_bug_bbu
842
+ with_each_spreadsheet(:name=>'bbu', :format=>[:openoffice, :excelx]) do |oo|
843
+ assert_equal "File: bbu#{get_extension(oo)}
844
+ Number of sheets: 3
845
+ Sheets: 2007_12, Tabelle2, Tabelle3
846
+ Sheet 1:
847
+ First row: 1
848
+ Last row: 4
849
+ First column: A
850
+ Last column: F
851
+ Sheet 2:
852
+ - empty -
853
+ Sheet 3:
854
+ - empty -", oo.info
855
+
856
+ oo.default_sheet = oo.sheets[1] # empty sheet
857
+ assert_nil oo.first_row
858
+ assert_nil oo.last_row
859
+ assert_nil oo.first_column
860
+ assert_nil oo.last_column
861
+ end
862
+ end
863
+
864
+
865
+ def test_bug_time_nil
866
+ with_each_spreadsheet(:name=>'time-test') do |oo|
867
+ assert_equal 12*3600+13*60+14, oo.cell('B',1) # 12:13:14 (secs since midnight)
868
+ assert_equal :time, oo.celltype('B',1)
869
+ assert_equal 15*3600+16*60, oo.cell('C',1) # 15:16 (secs since midnight)
870
+ assert_equal :time, oo.celltype('C',1)
871
+ assert_equal 23*3600, oo.cell('D',1) # 23:00 (secs since midnight)
872
+ assert_equal :time, oo.celltype('D',1)
873
+ end
874
+ end
875
+
876
+ def test_date_time_to_csv
877
+ with_each_spreadsheet(:name=>'time-test') do |oo|
878
+ Dir.mktmpdir do |tempdir|
879
+ csv_output = File.join(tempdir,'time_test.csv')
880
+ assert oo.to_csv(csv_output)
881
+ assert File.exists?(csv_output)
882
+ assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}`
883
+ # --strip-trailing-cr is needed because the test-file use 0A and
884
+ # the test on an windows box generates 0D 0A as line endings
885
+ end
886
+ end
887
+ end
888
+
889
+ def test_boolean_to_csv
890
+ with_each_spreadsheet(:name=>'boolean') do |oo|
891
+ Dir.mktmpdir do |tempdir|
892
+ csv_output = File.join(tempdir,'boolean.csv')
893
+ assert oo.to_csv(csv_output)
894
+ assert File.exists?(csv_output)
895
+ assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/boolean.csv #{csv_output}`
896
+ # --strip-trailing-cr is needed because the test-file use 0A and
897
+ # the test on an windows box generates 0D 0A as line endings
898
+ end
899
+ end
900
+ end
901
+ def test_link_to_csv
902
+ with_each_spreadsheet(:name=>'link',:format=>:excelx) do |oo|
903
+ Dir.mktmpdir do |tempdir|
904
+ csv_output = File.join(tempdir,'link.csv')
905
+ assert oo.to_csv(csv_output)
906
+ assert File.exists?(csv_output)
907
+ assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}`
908
+ # --strip-trailing-cr is needed because the test-file use 0A and
909
+ # the test on an windows box generates 0D 0A as line endings
910
+ end
911
+ end
912
+ end
913
+ def test_date_time_yaml
914
+ with_each_spreadsheet(:name=>'time-test') do |oo|
915
+ expected =
916
+ "--- \ncell_1_1: \n row: 1 \n col: 1 \n celltype: string \n value: Mittags: \ncell_1_2: \n row: 1 \n col: 2 \n celltype: time \n value: 12:13:14 \ncell_1_3: \n row: 1 \n col: 3 \n celltype: time \n value: 15:16:00 \ncell_1_4: \n row: 1 \n col: 4 \n celltype: time \n value: 23:00:00 \ncell_2_1: \n row: 2 \n col: 1 \n celltype: date \n value: 2007-11-21 \n"
917
+ assert_equal expected, oo.to_yaml
918
+ end
919
+ end
920
+
921
+ # Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
922
+ # Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
923
+ # nicht immer in der gleichen Reihenfolge erzeugt.
924
+ def init_all_cells(oo,sheet)
925
+ all = []
926
+ oo.first_row(sheet).upto(oo.last_row(sheet)) do |row|
927
+ oo.first_column(sheet).upto(oo.last_column(sheet)) do |col|
928
+ unless oo.empty?(row,col,sheet)
929
+ all << {:row => row.to_s,
930
+ :column => col.to_s,
931
+ :content => oo.cell(row,col,sheet).to_s,
932
+ :type => oo.celltype(row,col,sheet).to_s,
933
+ }
934
+ end
935
+ end
936
+ end
937
+ all
938
+ end
939
+
940
+ def test_to_xml
941
+ with_each_spreadsheet(:name=>'numbers1', :encoding => 'utf8') do |oo|
942
+ skip if defined? JRUBY_VERSION
943
+ oo.to_xml
944
+ sheetname = oo.sheets.first
945
+ doc = Nokogiri::XML(oo.to_xml)
946
+ sheet_count = 0
947
+ doc.xpath('//spreadsheet/sheet').each {|tmpelem|
948
+ sheet_count += 1
949
+ }
950
+ assert_equal 5, sheet_count
951
+ doc.xpath('//spreadsheet/sheet').each { |xml_sheet|
952
+ all_cells = init_all_cells(oo, sheetname)
953
+ x = 0
954
+ assert_equal sheetname, xml_sheet.attributes['name'].value
955
+ xml_sheet.children.each {|cell|
956
+ if cell.attributes['name']
957
+ expected = [all_cells[x][:row],
958
+ all_cells[x][:column],
959
+ all_cells[x][:content],
960
+ all_cells[x][:type],
961
+ ]
962
+ result = [
963
+ cell.attributes['row'],
964
+ cell.attributes['column'],
965
+ cell.content,
966
+ cell.attributes['type'],
967
+ ]
968
+ assert_equal expected, result
969
+ x += 1
970
+ end # if
971
+ } # end of sheet
972
+ sheetname = oo.sheets[oo.sheets.index(sheetname)+1]
973
+ }
974
+ end
975
+ end
976
+
977
+ def test_file_warning_default
978
+ if OPENOFFICE
979
+ assert_raises(TypeError, "test/files/numbers1.xls is not an openoffice spreadsheet") {
980
+ Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xls"))
981
+ }
982
+ assert_raises(TypeError) { Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx")) }
983
+ end
984
+ if EXCELX
985
+ assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods")) }
986
+ assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls")) }
987
+ end
988
+ end
989
+
990
+ def test_file_warning_error
991
+ if OPENOFFICE
992
+ assert_raises(TypeError) {
993
+ Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xls"),
994
+ packed: false,
995
+ file_warning: :error
996
+ )
997
+ }
998
+ assert_raises(TypeError) {
999
+ Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx"),
1000
+ packed: false,
1001
+ file_warning: :error)
1002
+ }
1003
+ end
1004
+ if EXCELX
1005
+ assert_raises(TypeError) {
1006
+ Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),
1007
+ packed: false,
1008
+ file_warning: :error)
1009
+ }
1010
+ assert_raises(TypeError) {
1011
+ Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls"),
1012
+ packed: false,
1013
+ file_warning: :error)
1014
+ }
1015
+ end
1016
+ end
1017
+
1018
+ def test_file_warning_warning
1019
+ if OPENOFFICE
1020
+ assert_raises(ArgumentError) {
1021
+ Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx"),
1022
+ packed: false,
1023
+ file_warning: :warning)
1024
+ }
1025
+ end
1026
+ if EXCELX
1027
+ assert_raises(ArgumentError) {
1028
+ Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),
1029
+ packed: false,
1030
+ file_warning: :warning)
1031
+ }
1032
+ end
1033
+ end
1034
+
1035
+ def test_file_warning_ignore
1036
+ if OPENOFFICE
1037
+ # Files, die eigentlich OpenOffice-
1038
+ # Files sind, aber die falsche Endung haben.
1039
+ # Es soll ohne Fehlermeldung oder Warnung
1040
+ # oder Abbruch die Datei geoffnet werden
1041
+
1042
+ # xlsx
1043
+ Roo::OpenOffice.new(File.join(TESTDIR,"type_openoffice.xlsx"),
1044
+ packed: false,
1045
+ file_warning: :ignore)
1046
+ end
1047
+ if EXCELX
1048
+ Roo::Excelx.new(File.join(TESTDIR,"type_excelx.ods"),
1049
+ packed: false,
1050
+ file_warning: :ignore)
1051
+ end
1052
+ end
1053
+
1054
+ def test_bug_to_xml_with_empty_sheets
1055
+ with_each_spreadsheet(:name=>'emptysheets', :format=>[:openoffice, :excelx]) do |oo|
1056
+ oo.sheets.each { |sheet|
1057
+ assert_equal nil, oo.first_row, "first_row not nil in sheet #{sheet}"
1058
+ assert_equal nil, oo.last_row, "last_row not nil in sheet #{sheet}"
1059
+ assert_equal nil, oo.first_column, "first_column not nil in sheet #{sheet}"
1060
+ assert_equal nil, oo.last_column, "last_column not nil in sheet #{sheet}"
1061
+ assert_equal nil, oo.first_row(sheet), "first_row not nil in sheet #{sheet}"
1062
+ assert_equal nil, oo.last_row(sheet), "last_row not nil in sheet #{sheet}"
1063
+ assert_equal nil, oo.first_column(sheet), "first_column not nil in sheet #{sheet}"
1064
+ assert_equal nil, oo.last_column(sheet), "last_column not nil in sheet #{sheet}"
1065
+ }
1066
+ oo.to_xml
1067
+ end
1068
+ end
1069
+
1070
+ def test_bug_simple_spreadsheet_time_bug
1071
+ # really a bug? are cells really of type time?
1072
+ # No! :float must be the correct type
1073
+ with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1074
+ # puts oo.cell('B',5).to_s
1075
+ # assert_equal :time, oo.celltype('B',5)
1076
+ assert_equal :float, oo.celltype('B',5)
1077
+ assert_equal 10.75, oo.cell('B',5)
1078
+ assert_equal 12.50, oo.cell('C',5)
1079
+ assert_equal 0, oo.cell('D',5)
1080
+ assert_equal 1.75, oo.cell('E',5)
1081
+ assert_equal 'Task 1', oo.cell('F',5)
1082
+ assert_equal Date.new(2007,5,7), oo.cell('A',5)
1083
+ end
1084
+ end
1085
+
1086
+ def test_simple2_excelx
1087
+ with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1088
+ assert_equal [:numeric_or_formula, "yyyy\\-mm\\-dd"], oo.excelx_type('A',4)
1089
+ assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('B',4)
1090
+ assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('c',4)
1091
+ assert_equal [:numeric_or_formula, "General"], oo.excelx_type('d',4)
1092
+ assert_equal [:numeric_or_formula, "General"], oo.excelx_type('e',4)
1093
+ assert_equal :string, oo.excelx_type('f',4)
1094
+
1095
+ assert_equal "39209", oo.excelx_value('a',4)
1096
+ assert_equal "yyyy\\-mm\\-dd", oo.excelx_format('a',4)
1097
+ assert_equal "9.25", oo.excelx_value('b',4)
1098
+ assert_equal "10.25", oo.excelx_value('c',4)
1099
+ assert_equal "0", oo.excelx_value('d',4)
1100
+ #... Sum-Spalte
1101
+ # assert_equal "Task 1", oo.excelx_value('f',4)
1102
+ assert_equal "Task 1", oo.cell('f',4)
1103
+ assert_equal Date.new(2007,05,07), oo.cell('a',4)
1104
+ assert_equal "9.25", oo.excelx_value('b',4)
1105
+ assert_equal "#,##0.00", oo.excelx_format('b',4)
1106
+ assert_equal 9.25, oo.cell('b',4)
1107
+ assert_equal :float, oo.celltype('b',4)
1108
+ assert_equal :float, oo.celltype('d',4)
1109
+ assert_equal 0, oo.cell('d',4)
1110
+ assert_equal :formula, oo.celltype('e',4)
1111
+ assert_equal 1, oo.cell('e',4)
1112
+ assert_equal 'C4-B4-D4', oo.formula('e',4)
1113
+ assert_equal :string, oo.celltype('f',4)
1114
+ assert_equal "Task 1", oo.cell('f',4)
1115
+ end
1116
+ end
1117
+
1118
+ def test_datetime
1119
+ with_each_spreadsheet(:name=>'datetime') do |oo|
1120
+ val = oo.cell('c',3)
1121
+ assert_equal :datetime, oo.celltype('c',3)
1122
+ assert_equal DateTime.new(1961,11,21,12,17,18), val
1123
+ assert_kind_of DateTime, val
1124
+ val = oo.cell('a',1)
1125
+ assert_equal :date, oo.celltype('a',1)
1126
+ assert_kind_of Date, val
1127
+ assert_equal Date.new(1961,11,21), val
1128
+ assert_equal Date.new(1961,11,21), oo.cell('a',1)
1129
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',3)
1130
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',3)
1131
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',3)
1132
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',4)
1133
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',4)
1134
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',4)
1135
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',5)
1136
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',5)
1137
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',5)
1138
+ assert_equal Date.new(1961,11,21), oo.cell('a',6)
1139
+ assert_equal Date.new(1961,11,21), oo.cell('b',6)
1140
+ assert_equal Date.new(1961,11,21), oo.cell('c',6)
1141
+ assert_equal Date.new(1961,11,21), oo.cell('a',7)
1142
+ assert_equal Date.new(1961,11,21), oo.cell('b',7)
1143
+ assert_equal Date.new(1961,11,21), oo.cell('c',7)
1144
+ assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('a',8)
1145
+ assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('b',8)
1146
+ assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('c',8)
1147
+ end
1148
+ end
1149
+
1150
+ def test_cell_openoffice_html_escape
1151
+ with_each_spreadsheet(:name=>'html-escape', :format=>:openoffice) do |oo|
1152
+ assert_equal "'", oo.cell(1,1)
1153
+ assert_equal "&", oo.cell(2,1)
1154
+ assert_equal ">", oo.cell(3,1)
1155
+ assert_equal "<", oo.cell(4,1)
1156
+ assert_equal "`", oo.cell(5,1)
1157
+ # test_openoffice_zipped will catch issues with &quot;
1158
+ end
1159
+ end
1160
+
1161
+ def test_cell_boolean
1162
+ with_each_spreadsheet(:name=>'boolean', :format=>[:openoffice, :excelx]) do |oo|
1163
+ if oo.class == Roo::Excelx
1164
+ assert_equal "TRUE", oo.cell(1,1), "failure in "+oo.class.to_s
1165
+ assert_equal "FALSE", oo.cell(2,1), "failure in "+oo.class.to_s
1166
+ else
1167
+ assert_equal "true", oo.cell(1,1), "failure in "+oo.class.to_s
1168
+ assert_equal "false", oo.cell(2,1), "failure in "+oo.class.to_s
1169
+ end
1170
+ end
1171
+ end
1172
+
1173
+ def test_cell_multiline
1174
+ with_each_spreadsheet(:name=>'paragraph', :format=>[:openoffice, :excelx]) do |oo|
1175
+ assert_equal "This is a test\nof a multiline\nCell", oo.cell(1,1)
1176
+ assert_equal "This is a test\n¶\nof a multiline\n\nCell", oo.cell(1,2)
1177
+ assert_equal "first p\n\nsecond p\n\nlast p", oo.cell(2,1)
1178
+ end
1179
+ end
1180
+
1181
+ def test_cell_styles
1182
+ # styles only valid in excel spreadsheets?
1183
+ # TODO: what todo with other spreadsheet types
1184
+ with_each_spreadsheet(:name=>'style', :format=>[# :openoffice,
1185
+ :excelx
1186
+ ]) do |oo|
1187
+ # bold
1188
+ assert_equal true, oo.font(1,1).bold?
1189
+ assert_equal false, oo.font(1,1).italic?
1190
+ assert_equal false, oo.font(1,1).underline?
1191
+
1192
+ # italic
1193
+ assert_equal false, oo.font(2,1).bold?
1194
+ assert_equal true, oo.font(2,1).italic?
1195
+ assert_equal false, oo.font(2,1).underline?
1196
+
1197
+ # normal
1198
+ assert_equal false, oo.font(3,1).bold?
1199
+ assert_equal false, oo.font(3,1).italic?
1200
+ assert_equal false, oo.font(3,1).underline?
1201
+
1202
+ # underline
1203
+ assert_equal false, oo.font(4,1).bold?
1204
+ assert_equal false, oo.font(4,1).italic?
1205
+ assert_equal true, oo.font(4,1).underline?
1206
+
1207
+ # bold italic
1208
+ assert_equal true, oo.font(5,1).bold?
1209
+ assert_equal true, oo.font(5,1).italic?
1210
+ assert_equal false, oo.font(5,1).underline?
1211
+
1212
+ # bold underline
1213
+ assert_equal true, oo.font(6,1).bold?
1214
+ assert_equal false, oo.font(6,1).italic?
1215
+ assert_equal true, oo.font(6,1).underline?
1216
+
1217
+ # italic underline
1218
+ assert_equal false, oo.font(7,1).bold?
1219
+ assert_equal true, oo.font(7,1).italic?
1220
+ assert_equal true, oo.font(7,1).underline?
1221
+
1222
+ # bolded row
1223
+ assert_equal true, oo.font(8,1).bold?
1224
+ assert_equal false, oo.font(8,1).italic?
1225
+ assert_equal false, oo.font(8,1).underline?
1226
+
1227
+ # bolded col
1228
+ assert_equal true, oo.font(9,2).bold?
1229
+ assert_equal false, oo.font(9,2).italic?
1230
+ assert_equal false, oo.font(9,2).underline?
1231
+
1232
+ # bolded row, italic col
1233
+ assert_equal true, oo.font(10,3).bold?
1234
+ assert_equal true, oo.font(10,3).italic?
1235
+ assert_equal false, oo.font(10,3).underline?
1236
+
1237
+ # normal
1238
+ assert_equal false, oo.font(11,4).bold?
1239
+ assert_equal false, oo.font(11,4).italic?
1240
+ assert_equal false, oo.font(11,4).underline?
1241
+ end
1242
+ end
1243
+
1244
+ # Need to extend to other formats
1245
+ def test_row_whitespace
1246
+ # auf dieses Dokument habe ich keinen Zugriff TODO:
1247
+ with_each_spreadsheet(:name=>'whitespace') do |oo|
1248
+ oo.default_sheet = "Sheet1"
1249
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(1)
1250
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(2)
1251
+ assert_equal ["Date", "Start time", "End time", "Pause", "Sum", "Comment"], oo.row(3)
1252
+ assert_equal [Date.new(2007,5,7), 9.25, 10.25, 0.0, 1.0, "Task 1"], oo.row(4)
1253
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(5)
1254
+ assert_equal [Date.new(2007,5,7), 10.75, 10.75, 0.0, 0.0, "Task 1"], oo.row(6)
1255
+ oo.default_sheet = "Sheet2"
1256
+ assert_equal ["Date", nil, "Start time"], oo.row(1)
1257
+ assert_equal [Date.new(2007,5,7), nil, 9.25], oo.row(2)
1258
+ assert_equal [Date.new(2007,5,7), nil, 10.75], oo.row(3)
1259
+ end
1260
+ end
1261
+
1262
+ def test_col_whitespace
1263
+ #TODO:
1264
+ # kein Zugriff auf Dokument whitespace
1265
+ with_each_spreadsheet(:name=>'whitespace') do |oo|
1266
+ oo.default_sheet = "Sheet1"
1267
+ assert_equal ["Date", Date.new(2007,5,7), nil, Date.new(2007,5,7)], oo.column(1)
1268
+ assert_equal ["Start time", 9.25, nil, 10.75], oo.column(2)
1269
+ assert_equal ["End time", 10.25, nil, 10.75], oo.column(3)
1270
+ assert_equal ["Pause", 0.0, nil, 0.0], oo.column(4)
1271
+ assert_equal ["Sum", 1.0, nil, 0.0], oo.column(5)
1272
+ assert_equal ["Comment","Task 1", nil, "Task 1"], oo.column(6)
1273
+ oo.default_sheet = "Sheet2"
1274
+ assert_equal [nil, nil, nil], oo.column(1)
1275
+ assert_equal [nil, nil, nil], oo.column(2)
1276
+ assert_equal ["Date", Date.new(2007,5,7), Date.new(2007,5,7)], oo.column(3)
1277
+ assert_equal [nil, nil, nil], oo.column(4)
1278
+ assert_equal [ "Start time", 9.25, 10.75], oo.column(5)
1279
+ end
1280
+ end
1281
+
1282
+ def test_excelx_links
1283
+ with_each_spreadsheet(:name=>'link', :format=>:excelx) do |oo|
1284
+ assert_equal 'Google', oo.cell(1,1)
1285
+ assert_equal 'http://www.google.com', oo.cell(1,1).href
1286
+ end
1287
+ end
1288
+
1289
+ # Excel has two base date formats one from 1900 and the other from 1904.
1290
+ # see #test_base_dates_in_excel
1291
+ def test_base_dates_in_excelx
1292
+ with_each_spreadsheet(:name=>'1900_base', :format=>:excelx) do |oo|
1293
+ assert_equal Date.new(2009,06,15), oo.cell(1,1)
1294
+ assert_equal :date, oo.celltype(1,1)
1295
+ end
1296
+ with_each_spreadsheet(:name=>'1904_base', :format=>:excelx) do |oo|
1297
+ assert_equal Date.new(2009,06,15), oo.cell(1,1)
1298
+ assert_equal :date, oo.celltype(1,1)
1299
+ end
1300
+ end
1301
+
1302
+ def test_cell_methods
1303
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
1304
+ assert_equal 10, oo.a4 # cell(4,'A')
1305
+ assert_equal 11, oo.b4 # cell(4,'B')
1306
+ assert_equal 12, oo.c4 # cell(4,'C')
1307
+ assert_equal 13, oo.d4 # cell(4,'D')
1308
+ assert_equal 14, oo.e4 # cell(4,'E')
1309
+ assert_equal 'ABC', oo.c6('Sheet5')
1310
+
1311
+ #assert_raises(ArgumentError) {
1312
+ assert_raises(NoMethodError) {
1313
+ # a42a is not a valid cell name, should raise ArgumentError
1314
+ assert_equal 9999, oo.a42a
1315
+ }
1316
+ end
1317
+ end
1318
+
1319
+
1320
+ # compare large spreadsheets
1321
+ def test_compare_large_spreadsheets
1322
+ # problematisch, weil Formeln in Excel nicht unterstützt werden
1323
+ if LONG_RUN
1324
+ qq = Roo::OpenOffice.new(File.join('test',"Bibelbund.ods"))
1325
+ with_each_spreadsheet(:name=>'Bibelbund') do |oo|
1326
+ # p "comparing Bibelbund.ods with #{oo.class}"
1327
+ oo.sheets.each do |sh|
1328
+ oo.first_row.upto(oo.last_row) do |row|
1329
+ oo.first_column.upto(oo.last_column) do |col|
1330
+ c1 = qq.cell(row,col,sh)
1331
+ c1.force_encoding("UTF-8") if c1.class == String
1332
+ c2 = oo.cell(row,col,sh)
1333
+ c2.force_encoding("UTF-8") if c2.class == String
1334
+ assert_equal c1, c2, "diff in #{sh}/#{row}/#{col}}"
1335
+ assert_equal qq.celltype(row,col,sh), oo.celltype(row,col,sh)
1336
+ assert_equal qq.formula?(row,col,sh), oo.formula?(row,col,sh) if oo.class != Roo::Excel
1337
+ end
1338
+ end
1339
+ end
1340
+ end
1341
+ end # LONG_RUN
1342
+ end
1343
+
1344
+ def test_label
1345
+ with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1346
+ # oo.default_sheet = oo.sheets.first
1347
+ begin
1348
+ row,col = oo.label('anton')
1349
+ rescue ArgumentError
1350
+ puts "labels error at #{oo.class}"
1351
+ raise
1352
+ end
1353
+ assert_equal 5, row, "error with label in class #{oo.class}"
1354
+ assert_equal 3, col, "error with label in class #{oo.class}"
1355
+
1356
+ row,col = oo.label('anton')
1357
+ assert_equal 'Anton', oo.cell(row,col), "error with label in class #{oo.class}"
1358
+
1359
+ row,col = oo.label('berta')
1360
+ assert_equal 'Bertha', oo.cell(row,col), "error with label in class #{oo.class}"
1361
+
1362
+ row,col = oo.label('caesar')
1363
+ assert_equal 'Cäsar', oo.cell(row,col),"error with label in class #{oo.class}"
1364
+
1365
+ row,col = oo.label('never')
1366
+ assert_nil row
1367
+ assert_nil col
1368
+
1369
+ row,col,sheet = oo.label('anton')
1370
+ assert_equal 5, row
1371
+ assert_equal 3, col
1372
+ assert_equal "Sheet1", sheet
1373
+ end
1374
+ end
1375
+
1376
+ def test_method_missing_anton
1377
+ with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1378
+ # oo.default_sheet = oo.sheets.first
1379
+ assert_equal "Anton", oo.anton
1380
+ assert_raises(NoMethodError) {
1381
+ oo.never
1382
+ }
1383
+ end
1384
+ end
1385
+
1386
+ def test_labels
1387
+ with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1388
+ # oo.default_sheet = oo.sheets.first
1389
+ assert_equal [
1390
+ ['anton',[5,3,'Sheet1']],
1391
+ ['berta',[4,2,'Sheet1']],
1392
+ ['caesar',[7,2,'Sheet1']],
1393
+ ], oo.labels, "error with labels array in class #{oo.class}"
1394
+ end
1395
+ end
1396
+
1397
+ def test_labeled_cells
1398
+ with_each_spreadsheet(:name=>'named_cells', :format=>[:openoffice,:excelx,:libreoffice]) do |oo|
1399
+ oo.default_sheet = oo.sheets.first
1400
+ begin
1401
+ row,col = oo.label('anton')
1402
+ rescue ArgumentError
1403
+ puts "labels error at #{oo.class}"
1404
+ raise
1405
+ end
1406
+ assert_equal 5, row
1407
+ assert_equal 3, col
1408
+
1409
+ row,col = oo.label('anton')
1410
+ assert_equal 'Anton', oo.cell(row,col)
1411
+
1412
+ row,col = oo.label('berta')
1413
+ assert_equal 'Bertha', oo.cell(row,col)
1414
+
1415
+ row,col = oo.label('caesar')
1416
+ assert_equal 'Cäsar', oo.cell(row,col)
1417
+
1418
+ row,col = oo.label('never')
1419
+ assert_nil row
1420
+ assert_nil col
1421
+
1422
+ row,col,sheet = oo.label('anton')
1423
+ assert_equal 5, row
1424
+ assert_equal 3, col
1425
+ assert_equal "Sheet1", sheet
1426
+
1427
+ assert_equal "Anton", oo.anton
1428
+ assert_raises(NoMethodError) {
1429
+ row,col = oo.never
1430
+ }
1431
+
1432
+ # Reihenfolge row,col,sheet analog zu #label
1433
+ assert_equal [
1434
+ ['anton',[5,3,'Sheet1']],
1435
+ ['berta',[4,2,'Sheet1']],
1436
+ ['caesar',[7,2,'Sheet1']],
1437
+ ], oo.labels, "error with labels array in class #{oo.class}"
1438
+ end
1439
+ end
1440
+
1441
+ require 'matrix'
1442
+ def test_matrix
1443
+ with_each_spreadsheet(:name => 'matrix', :format => :openoffice) do |oo|
1444
+ oo.default_sheet = oo.sheets.first
1445
+ assert_equal Matrix[
1446
+ [1.0, 2.0, 3.0],
1447
+ [4.0, 5.0, 6.0],
1448
+ [7.0, 8.0, 9.0] ], oo.to_matrix
1449
+ end
1450
+ end
1451
+
1452
+ def test_matrix_selected_range
1453
+ with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
1454
+ oo.default_sheet = 'Sheet2'
1455
+ assert_equal Matrix[
1456
+ [1.0, 2.0, 3.0],
1457
+ [4.0, 5.0, 6.0],
1458
+ [7.0, 8.0, 9.0] ], oo.to_matrix(3,4,5,6)
1459
+ end
1460
+ end
1461
+
1462
+ def test_matrix_all_nil
1463
+ with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
1464
+ oo.default_sheet = 'Sheet2'
1465
+ assert_equal Matrix[
1466
+ [nil, nil, nil],
1467
+ [nil, nil, nil],
1468
+ [nil, nil, nil] ], oo.to_matrix(10,10,12,12)
1469
+ end
1470
+ end
1471
+
1472
+ def test_matrix_values_and_nil
1473
+ with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
1474
+ oo.default_sheet = 'Sheet3'
1475
+ assert_equal Matrix[
1476
+ [1.0, nil, 3.0],
1477
+ [4.0, 5.0, 6.0],
1478
+ [7.0, 8.0, nil] ], oo.to_matrix(1,1,3,3)
1479
+ end
1480
+ end
1481
+
1482
+ def test_matrix_specifying_sheet
1483
+ with_each_spreadsheet(:name => 'matrix', :format => :openoffice) do |oo|
1484
+ oo.default_sheet = oo.sheets.first
1485
+ assert_equal Matrix[
1486
+ [1.0, nil, 3.0],
1487
+ [4.0, 5.0, 6.0],
1488
+ [7.0, 8.0, nil] ], oo.to_matrix(nil, nil, nil, nil, 'Sheet3')
1489
+ end
1490
+ end
1491
+
1492
+ # unter Windows soll es laut Bug-Reports nicht moeglich sein, eine Excel-Datei, die
1493
+ # mit Excel.new geoeffnet wurde nach dem Processing anschliessend zu loeschen.
1494
+ # Anmerkung: Das Spreadsheet-Gem erlaubt kein explizites Close von Spreadsheet-Dateien,
1495
+ # was verhindern koennte, das die Datei geloescht werden kann.
1496
+ # def test_bug_cannot_delete_opened_excel_sheet
1497
+ # with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
1498
+ # 'kopiere nach temporaere Datei und versuche diese zu oeffnen und zu loeschen'
1499
+ # end
1500
+ # end
1501
+
1502
+ def test_bug_xlsx_reference_cell
1503
+
1504
+ if EXCELX
1505
+ =begin
1506
+ If cell A contains a string and cell B references cell A. When reading the value of cell B, the result will be
1507
+ "0.0" instead of the value of cell A.
1508
+
1509
+ With the attached test case, I ran the following code:
1510
+ spreadsheet = Roo::Excelx.new('formula_string_error.xlsx')
1511
+ spreadsheet.default_sheet = 'sheet1'
1512
+ p "A: #{spreadsheet.cell(1, 1)}"
1513
+ p "B: #{spreadsheet.cell(2, 1)}"
1514
+
1515
+ with the following results
1516
+ "A: TestString"
1517
+ "B: 0.0"
1518
+
1519
+ where the expected result is
1520
+ "A: TestString"
1521
+ "B: TestString"
1522
+ =end
1523
+ xlsx = Roo::Excelx.new(File.join(TESTDIR, "formula_string_error.xlsx"))
1524
+ xlsx.default_sheet = xlsx.sheets.first
1525
+ assert_equal 'Teststring', xlsx.cell('a',1)
1526
+ assert_equal 'Teststring', xlsx.cell('a',2)
1527
+ end
1528
+ end
1529
+
1530
+ # #formulas of an empty sheet should return an empty array and not result in
1531
+ # an error message
1532
+ # 2011-06-24
1533
+ def test_bug_formulas_empty_sheet
1534
+ with_each_spreadsheet(:name =>'emptysheets',
1535
+ :format=>[:openoffice,:excelx]) do |oo|
1536
+ oo.default_sheet = oo.sheets.first
1537
+ oo.formulas
1538
+ assert_equal([], oo.formulas)
1539
+ end
1540
+ end
1541
+
1542
+ # #to_yaml of an empty sheet should return an empty string and not result in
1543
+ # an error message
1544
+ # 2011-06-24
1545
+ def test_bug_to_yaml_empty_sheet
1546
+ with_each_spreadsheet(:name =>'emptysheets',
1547
+ :format=>[:openoffice,:excelx]) do |oo|
1548
+ oo.default_sheet = oo.sheets.first
1549
+ oo.to_yaml
1550
+ assert_equal('', oo.to_yaml)
1551
+ end
1552
+ end
1553
+
1554
+ # #to_matrix of an empty sheet should return an empty matrix and not result in
1555
+ # an error message
1556
+ # 2011-06-25
1557
+ def test_bug_to_matrix_empty_sheet
1558
+ with_each_spreadsheet(:name =>'emptysheets',
1559
+ :format=>[:openoffice,:excelx]) do |oo|
1560
+ oo.default_sheet = oo.sheets.first
1561
+ oo.to_matrix
1562
+ assert_equal(Matrix.empty(0,0), oo.to_matrix)
1563
+ end
1564
+ end
1565
+
1566
+ # 2011-08-03
1567
+ def test_bug_datetime_to_csv
1568
+ with_each_spreadsheet(:name=>'datetime') do |oo|
1569
+ Dir.mktmpdir do |tempdir|
1570
+ datetime_csv_file = File.join(tempdir,"datetime.csv")
1571
+
1572
+ assert oo.to_csv(datetime_csv_file)
1573
+ assert File.exists?(datetime_csv_file)
1574
+ assert_equal "", file_diff('test/files/so_datetime.csv', datetime_csv_file)
1575
+ end
1576
+ end
1577
+ end
1578
+
1579
+ # 2011-08-11
1580
+ def test_bug_openoffice_formula_missing_letters
1581
+ if LIBREOFFICE
1582
+ # Dieses Dokument wurde mit LibreOffice angelegt.
1583
+ # Keine Ahnung, ob es damit zusammenhaengt, das diese
1584
+ # Formeln anders sind, als in der Datei formula.ods, welche
1585
+ # mit OpenOffice angelegt wurde.
1586
+ # Bei den OpenOffice-Dateien ist in diesem Feld in der XML-
1587
+ # Datei of: als Prefix enthalten, waehrend in dieser Datei
1588
+ # irgendetwas mit oooc: als Prefix verwendet wird.
1589
+ oo = Roo::OpenOffice.new(File.join(TESTDIR,'dreimalvier.ods'))
1590
+ oo.default_sheet = oo.sheets.first
1591
+ assert_equal '=SUM([.A1:.D1])', oo.formula('e',1)
1592
+ assert_equal '=SUM([.A2:.D2])', oo.formula('e',2)
1593
+ assert_equal '=SUM([.A3:.D3])', oo.formula('e',3)
1594
+ assert_equal [
1595
+ [1,5,'=SUM([.A1:.D1])'],
1596
+ [2,5,'=SUM([.A2:.D2])'],
1597
+ [3,5,'=SUM([.A3:.D3])'],
1598
+ ], oo.formulas
1599
+
1600
+ end
1601
+ end
1602
+
1603
+ =begin
1604
+ def test_postprocessing_and_types_in_csv
1605
+ if CSV
1606
+ oo = CSV.new(File.join(TESTDIR,'csvtypes.csv'))
1607
+ oo.default_sheet = oo.sheets.first
1608
+ assert_equal(1,oo.a1)
1609
+ assert_equal(:float,oo.celltype('A',1))
1610
+ assert_equal("2",oo.b1)
1611
+ assert_equal(:string,oo.celltype('B',1))
1612
+ assert_equal("Mayer",oo.c1)
1613
+ assert_equal(:string,oo.celltype('C',1))
1614
+ end
1615
+ end
1616
+ =end
1617
+
1618
+ =begin
1619
+ def test_postprocessing_with_callback_function
1620
+ if CSV
1621
+ oo = CSV.new(File.join(TESTDIR,'csvtypes.csv'))
1622
+ oo.default_sheet = oo.sheets.first
1623
+
1624
+ #
1625
+ assert_equal(1, oo.last_column)
1626
+ end
1627
+ end
1628
+ =end
1629
+
1630
+ =begin
1631
+ def x_123
1632
+ class ::CSV
1633
+ def cell_postprocessing(row,col,value)
1634
+ if row < 3
1635
+ return nil
1636
+ end
1637
+ return value
1638
+ end
1639
+ end
1640
+ end
1641
+ =end
1642
+
1643
+ def test_nil_rows_and_lines_csv
1644
+ # x_123
1645
+ if CSV
1646
+ oo = Roo::CSV.new(File.join(TESTDIR,'Bibelbund.csv'))
1647
+ oo.default_sheet = oo.sheets.first
1648
+ assert_equal 1, oo.first_row
1649
+ end
1650
+ end
1651
+
1652
+ def test_bug_pfand_from_windows_phone_xlsx
1653
+ return if defined? JRUBY_VERSION
1654
+ with_each_spreadsheet(:name=>'Pfand_from_windows_phone', :format=>:excelx) do |oo|
1655
+ oo.default_sheet = oo.sheets.first
1656
+ assert_equal ['Blatt1','Blatt2','Blatt3'], oo.sheets
1657
+ assert_equal 'Summe', oo.cell('b',1)
1658
+
1659
+ assert_equal Date.new(2011,9,14), oo.cell('a',2)
1660
+ assert_equal :date, oo.celltype('a',2)
1661
+ assert_equal Date.new(2011,9,15), oo.cell('a',3)
1662
+ assert_equal :date, oo.celltype('a',3)
1663
+
1664
+ assert_equal 3.81, oo.cell('b',2)
1665
+ assert_equal "SUM(C2:L2)", oo.formula('b',2)
1666
+ assert_equal 0.7, oo.cell('c',2)
1667
+ end # each
1668
+ end
1669
+
1670
+ def test_comment
1671
+ with_each_spreadsheet(:name=>'comments', :format=>[:openoffice,:libreoffice,
1672
+ :excelx]) do |oo|
1673
+ oo.default_sheet = oo.sheets.first
1674
+ assert_equal 'Kommentar fuer B4',oo.comment('b',4)
1675
+ assert_equal 'Kommentar fuer B5',oo.comment('b',5)
1676
+ assert_nil oo.comment('b',99)
1677
+ # no comment at the second page
1678
+ oo.default_sheet = oo.sheets[1]
1679
+ assert_nil oo.comment('b',4)
1680
+ end
1681
+ end
1682
+
1683
+ def test_comments
1684
+ with_each_spreadsheet(:name=>'comments', :format=>[:openoffice,:libreoffice,
1685
+ :excelx]) do |oo|
1686
+ oo.default_sheet = oo.sheets.first
1687
+ assert_equal [
1688
+ [4, 2, "Kommentar fuer B4"],
1689
+ [5, 2, "Kommentar fuer B5"],
1690
+ ], oo.comments(oo.sheets.first), "comments error in class #{oo.class}"
1691
+ # no comments at the second page
1692
+ oo.default_sheet = oo.sheets[1]
1693
+ assert_equal [], oo.comments, "comments error in class #{oo.class}"
1694
+ end
1695
+
1696
+ with_each_spreadsheet(:name=>'comments-google', :format=>[:excelx]) do |oo|
1697
+ oo.default_sheet = oo.sheets.first
1698
+ assert_equal [[1, 1, "this is a comment\n\t-Steven Daniels"]], oo.comments(oo.sheets.first), "comments error in class #{oo.class}"
1699
+ end
1700
+ end
1701
+
1702
+ ## PREVIOUSLY SKIPPED
1703
+
1704
+ # don't have these test files so removing. We can easily add in
1705
+ # by modifying with_each_spreadsheet
1706
+ GNUMERIC_ODS = false # do gnumeric with ods files Tests?
1707
+ OPENOFFICEWRITE = false # experimental: write access with OO-Documents
1708
+
1709
+ def test_writeopenoffice
1710
+ if OPENOFFICEWRITE
1711
+ File.cp(File.join(TESTDIR,"numbers1.ods"),
1712
+ File.join(TESTDIR,"numbers2.ods"))
1713
+ File.cp(File.join(TESTDIR,"numbers2.ods"),
1714
+ File.join(TESTDIR,"bak_numbers2.ods"))
1715
+ oo = OpenOffice.new(File.join(TESTDIR,"numbers2.ods"))
1716
+ oo.default_sheet = oo.sheets.first
1717
+ oo.first_row.upto(oo.last_row) {|y|
1718
+ oo.first_column.upto(oo.last_column) {|x|
1719
+ unless oo.empty?(y,x)
1720
+ # oo.set(y, x, oo.cell(y,x) + 7) if oo.celltype(y,x) == "float"
1721
+ oo.set(y, x, oo.cell(y,x) + 7) if oo.celltype(y,x) == :float
1722
+ end
1723
+ }
1724
+ }
1725
+ oo.save
1726
+
1727
+ oo1 = Roo::OpenOffice.new(File.join(TESTDIR,"numbers2.ods"))
1728
+ oo2 = Roo::OpenOffice.new(File.join(TESTDIR,"bak_numbers2.ods"))
1729
+ #p oo2.to_s
1730
+ assert_equal 999, oo2.cell('a',1), oo2.cell('a',1)
1731
+ assert_equal oo2.cell('a',1) + 7, oo1.cell('a',1)
1732
+ assert_equal oo2.cell('b',1)+7, oo1.cell('b',1)
1733
+ assert_equal oo2.cell('c',1)+7, oo1.cell('c',1)
1734
+ assert_equal oo2.cell('d',1)+7, oo1.cell('d',1)
1735
+ assert_equal oo2.cell('a',2)+7, oo1.cell('a',2)
1736
+ assert_equal oo2.cell('b',2)+7, oo1.cell('b',2)
1737
+ assert_equal oo2.cell('c',2)+7, oo1.cell('c',2)
1738
+ assert_equal oo2.cell('d',2)+7, oo1.cell('d',2)
1739
+ assert_equal oo2.cell('e',2)+7, oo1.cell('e',2)
1740
+
1741
+ File.cp(File.join(TESTDIR,"bak_numbers2.ods"),
1742
+ File.join(TESTDIR,"numbers2.ods"))
1743
+ end
1744
+ end
1745
+
1746
+ def common_possible_bug_snowboard_cells(ss)
1747
+ assert_equal "A.", ss.cell(13,'A'), ss.class
1748
+ assert_equal 147, ss.cell(13,'f'), ss.class
1749
+ assert_equal 152, ss.cell(13,'g'), ss.class
1750
+ assert_equal 156, ss.cell(13,'h'), ss.class
1751
+ assert_equal 158, ss.cell(13,'i'), ss.class
1752
+ assert_equal 160, ss.cell(13,'j'), ss.class
1753
+ assert_equal 164, ss.cell(13,'k'), ss.class
1754
+ assert_equal 168, ss.cell(13,'l'), ss.class
1755
+ assert_equal :string, ss.celltype(13,'m'), ss.class
1756
+ assert_equal "159W", ss.cell(13,'m'), ss.class
1757
+ assert_equal "164W", ss.cell(13,'n'), ss.class
1758
+ assert_equal "168W", ss.cell(13,'o'), ss.class
1759
+ end
1760
+
1761
+ # def test_false_encoding
1762
+ # ex = Roo::Excel.new(File.join(TESTDIR,'false_encoding.xls'))
1763
+ # ex.default_sheet = ex.sheets.first
1764
+ # assert_equal "Sheet1", ex.sheets.first
1765
+ # ex.first_row.upto(ex.last_row) do |row|
1766
+ # ex.first_column.upto(ex.last_column) do |col|
1767
+ # content = ex.cell(row,col)
1768
+ # puts "#{row}/#{col}"
1769
+ # #puts content if ! ex.empty?(row,col) or ex.formula?(row,col)
1770
+ # if ex.formula?(row,col)
1771
+ # #! ex.empty?(row,col)
1772
+ # puts content
1773
+ # end
1774
+ # end
1775
+ # end
1776
+ # end
1777
+
1778
+ def test_download_uri
1779
+ if ONLINE
1780
+ if OPENOFFICE
1781
+ assert_raises(RuntimeError) {
1782
+ Roo::OpenOffice.new("http://gibbsnichtdomainxxxxx.com/file.ods")
1783
+ }
1784
+ end
1785
+ if EXCELX
1786
+ assert_raises(RuntimeError) {
1787
+ Roo::Excelx.new("http://gibbsnichtdomainxxxxx.com/file.xlsx")
1788
+ }
1789
+ end
1790
+ end
1791
+ end
1792
+
1793
+ def test_download_uri_with_query_string
1794
+ dir = File.expand_path("#{File.dirname __FILE__}/files")
1795
+ { xlsx: [EXCELX, Roo::Excelx],
1796
+ ods: [OPENOFFICE, Roo::OpenOffice]}.each do |extension, (flag, type)|
1797
+ if flag
1798
+ file = "#{dir}/simple_spreadsheet.#{extension}"
1799
+ url = "http://test.example.com/simple_spreadsheet.#{extension}?query-param=value"
1800
+ stub_request(:any, url).to_return(body: File.read(file))
1801
+ spreadsheet = type.new(url)
1802
+ spreadsheet.default_sheet = spreadsheet.sheets.first
1803
+ assert_equal 'Task 1', spreadsheet.cell('f', 4)
1804
+ end
1805
+ end
1806
+ end
1807
+
1808
+ # def test_soap_server
1809
+ # #threads = []
1810
+ # #threads << Thread.new("serverthread") do
1811
+ # fork do
1812
+ # p "serverthread started"
1813
+ # puts "in child, pid = #$$"
1814
+ # puts `/usr/bin/ruby rooserver.rb`
1815
+ # p "serverthread finished"
1816
+ # end
1817
+ # #threads << Thread.new("clientthread") do
1818
+ # p "clientthread started"
1819
+ # sleep 10
1820
+ # proxy = SOAP::RPC::Driver.new("http://localhost:12321","spreadsheetserver")
1821
+ # proxy.add_method('cell','row','col')
1822
+ # proxy.add_method('officeversion')
1823
+ # proxy.add_method('last_row')
1824
+ # proxy.add_method('last_column')
1825
+ # proxy.add_method('first_row')
1826
+ # proxy.add_method('first_column')
1827
+ # proxy.add_method('sheets')
1828
+ # proxy.add_method('set_default_sheet','s')
1829
+ # proxy.add_method('ferien_fuer_region', 'region')
1830
+
1831
+ # sheets = proxy.sheets
1832
+ # p sheets
1833
+ # proxy.set_default_sheet(sheets.first)
1834
+
1835
+ # assert_equal 1, proxy.first_row
1836
+ # assert_equal 1, proxy.first_column
1837
+ # assert_equal 187, proxy.last_row
1838
+ # assert_equal 7, proxy.last_column
1839
+ # assert_equal 42, proxy.cell('C',8)
1840
+ # assert_equal 43, proxy.cell('F',12)
1841
+ # assert_equal "1.0", proxy.officeversion
1842
+ # p "clientthread finished"
1843
+ # #end
1844
+ # #threads.each {|t| t.join }
1845
+ # puts "fertig"
1846
+ # Process.kill("INT",pid)
1847
+ # pid = Process.wait
1848
+ # puts "child terminated, pid= #{pid}, status= #{$?.exitstatus}"
1849
+ # end
1850
+
1851
+ def split_coord(s)
1852
+ letter = ""
1853
+ number = 0
1854
+ i = 0
1855
+ while i<s.length and "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".include?(s[i,1])
1856
+ letter += s[i,1]
1857
+ i+=1
1858
+ end
1859
+ while i<s.length and "01234567890".include?(s[i,1])
1860
+ number = number*10 + s[i,1].to_i
1861
+ i+=1
1862
+ end
1863
+ if letter=="" or number==0
1864
+ raise ArgumentError
1865
+ end
1866
+ return letter,number
1867
+ end
1868
+
1869
+ #def sum(s,expression)
1870
+ # arg = expression.split(':')
1871
+ # b,z = split_coord(arg[0])
1872
+ # first_row = z
1873
+ # first_col = OpenOffice.letter_to_number(b)
1874
+ # b,z = split_coord(arg[1])
1875
+ # last_row = z
1876
+ # last_col = OpenOffice.letter_to_number(b)
1877
+ # result = 0
1878
+ # first_row.upto(last_row) {|row|
1879
+ # first_col.upto(last_col) {|col|
1880
+ # result = result + s.cell(row,col)
1881
+ # }
1882
+ # }
1883
+ # result
1884
+ #end
1885
+
1886
+ #def test_dsl
1887
+ # s = OpenOffice.new(File.join(TESTDIR,"numbers1.ods"))
1888
+ # s.default_sheet = s.sheets.first
1889
+ #
1890
+ # s.set 'a',1, 5
1891
+ # s.set 'b',1, 3
1892
+ # s.set 'c',1, 7
1893
+ # s.set('a',2, s.cell('a',1)+s.cell('b',1))
1894
+ # assert_equal 8, s.cell('a',2)
1895
+ #
1896
+ # assert_equal 15, sum(s,'A1:C1')
1897
+ # end
1898
+
1899
+ #def test_create_spreadsheet1
1900
+ # name = File.join(TESTDIR,'createdspreadsheet.ods')
1901
+ # rm(name) if File.exists?(File.join(TESTDIR,'createdspreadsheet.ods'))
1902
+ # # anlegen, falls noch nicht existierend
1903
+ # s = OpenOffice.new(name,true)
1904
+ # assert File.exists?(name)
1905
+ #end
1906
+
1907
+ #def test_create_spreadsheet2
1908
+ # # anlegen, falls noch nicht existierend
1909
+ # s = OpenOffice.new(File.join(TESTDIR,"createdspreadsheet.ods"),true)
1910
+ # s.set 'a',1,42
1911
+ # s.set 'b',1,43
1912
+ # s.set 'c',1,44
1913
+ # s.save
1914
+ #
1915
+ # t = OpenOffice.new(File.join(TESTDIR,"createdspreadsheet.ods"))
1916
+ # assert_equal 42, t.cell(1,'a')
1917
+ # assert_equal 43, t.cell('b',1)
1918
+ # assert_equal 44, t.cell('c',3)
1919
+ #end
1920
+
1921
+ # We don't have the bode-v1.xlsx test file
1922
+ # #TODO: xlsx-Datei anpassen!
1923
+ # def test_excelx_download_uri_and_zipped
1924
+ # #TODO: gezippte xlsx Datei online zum Testen suchen
1925
+ # if EXCELX
1926
+ # if ONLINE
1927
+ # url = 'http://stiny-leonhard.de/bode-v1.xlsx.zip'
1928
+ # excel = Roo::Excelx.new(url, :zip)
1929
+ # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1930
+ # end
1931
+ # end
1932
+ # end
1933
+
1934
+ # def test_excelx_zipped
1935
+ # # TODO: bode...xls bei Gelegenheit nach .xlsx konverieren lassen und zippen!
1936
+ # if EXCELX
1937
+ # # diese Datei gibt es noch nicht gezippt
1938
+ # excel = Roo::Excelx.new(File.join(TESTDIR,"bode-v1.xlsx.zip"), :zip)
1939
+ # assert excel
1940
+ # assert_raises(ArgumentError) {
1941
+ # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1942
+ # }
1943
+ # excel.default_sheet = excel.sheets.first
1944
+ # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1945
+ # end
1946
+ # end
1947
+
1948
+ def test_csv_parsing_with_headers
1949
+ return unless CSV
1950
+ headers = ["TITEL", "VERFASSER", "OBJEKT", "NUMMER", "SEITE", "INTERNET", "PC", "KENNUNG"]
1951
+
1952
+ oo = Roo::Spreadsheet.open(File.join(TESTDIR, 'Bibelbund.csv'))
1953
+ parsed = oo.parse(:headers => true)
1954
+ assert_equal headers, parsed[1].keys
1955
+ end
1956
+
1957
+ def test_bug_numbered_sheet_names
1958
+ with_each_spreadsheet(:name=>'bug-numbered-sheet-names', :format=>:excelx) do |oo|
1959
+ oo.each_with_pagename { }
1960
+ end
1961
+ end
1962
+
1963
+ def test_parsing_xslx_from_numbers
1964
+ return unless EXCELX
1965
+ xlsx = Roo::Excelx.new(File.join(TESTDIR, "numbers-export.xlsx"))
1966
+
1967
+ xlsx.default_sheet = xlsx.sheets.first
1968
+ assert_equal 'Sheet 1', xlsx.cell('a',1)
1969
+
1970
+ # Another buggy behavior of Numbers 3.1: if a warkbook has more than a
1971
+ # single sheet, all sheets except the first one will have an extra row and
1972
+ # column added to the beginning. That's why we assert against cell B2 and
1973
+ # not A1
1974
+ xlsx.default_sheet = xlsx.sheets.last
1975
+ assert_equal 'Sheet 2', xlsx.cell('b',2)
1976
+ end
1977
+
1978
+ def test_openoffice_encryption
1979
+ if OPENOFFICE
1980
+ assert_raises(ArgumentError) { Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods")) }
1981
+ assert_raises(ArgumentError) { Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods"), :password => "badpassword") }
1982
+ oo = Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods"), :password => "letmein")
1983
+ oo.default_sheet = oo.sheets.first
1984
+ assert_equal "Hello World", oo.cell('a',1)
1985
+ end
1986
+ end
1987
+
1988
+ def test_expand_merged_range
1989
+ return unless EXCELX
1990
+ xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"), {:expand_merged_ranges => true})
1991
+ for row in 3..7 do
1992
+ for col in 'a'..'b'
1993
+ if row > 3 && row < 7 && col == 'a'
1994
+ assert_equal 'vertical1', xlsx.cell(col,row)
1995
+ else
1996
+ assert_nil xlsx.cell(col,row)
1997
+ end
1998
+ end
1999
+ end
2000
+ for row in 3..11 do
2001
+ for col in 'f'..'h'
2002
+ if row > 3 && row < 11 && col == 'g'
2003
+ assert_equal 'vertical2', xlsx.cell(col,row)
2004
+ else
2005
+ assert_nil xlsx.cell(col,row)
2006
+ end
2007
+ end
2008
+ end
2009
+ for row in 3..5 do
2010
+ for col in 'b'..'f'
2011
+ if row == 4 && col > 'b' && col < 'f'
2012
+ assert_equal 'horizontal', xlsx.cell(col,row)
2013
+ else
2014
+ assert_nil xlsx.cell(col,row)
2015
+ end
2016
+ end
2017
+ end
2018
+ for row in 8..13 do
2019
+ for col in 'a'..'e'
2020
+ if row > 8 && row < 13 && col > 'a' && col < 'e'
2021
+ assert_equal 'block', xlsx.cell(col,row)
2022
+ else
2023
+ assert_nil xlsx.cell(col,row)
2024
+ end
2025
+ end
2026
+ end
2027
+ end
2028
+
2029
+ def test_noexpand_merged_range
2030
+ return unless EXCELX
2031
+ xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"))
2032
+ for row in 3..7 do
2033
+ for col in 'a'..'b'
2034
+ if row == 4 && col == 'a'
2035
+ assert_equal 'vertical1', xlsx.cell(col,row)
2036
+ else
2037
+ assert_nil xlsx.cell(col,row)
2038
+ end
2039
+ end
2040
+ end
2041
+ for row in 3..11 do
2042
+ for col in 'f'..'h'
2043
+ if row == 4 && col == 'g'
2044
+ assert_equal 'vertical2', xlsx.cell(col,row)
2045
+ else
2046
+ assert_nil xlsx.cell(col,row)
2047
+ end
2048
+ end
2049
+ end
2050
+ for row in 3..5 do
2051
+ for col in 'b'..'f'
2052
+ if row == 4 && col == 'c'
2053
+ assert_equal 'horizontal', xlsx.cell(col,row)
2054
+ else
2055
+ assert_nil xlsx.cell(col,row)
2056
+ end
2057
+ end
2058
+ end
2059
+ for row in 8..13 do
2060
+ for col in 'a'..'e'
2061
+ if row == 9 && col == 'b'
2062
+ assert_equal 'block', xlsx.cell(col,row)
2063
+ else
2064
+ assert_nil xlsx.cell(col,row)
2065
+ end
2066
+ end
2067
+ end
2068
+ end
2069
+
2070
+ def test_open_stream
2071
+ return unless EXCELX
2072
+ file_contents = File.read File.join(TESTDIR, fixture_filename(:numbers1, :excelx)), encoding: 'BINARY'
2073
+ stream = StringIO.new(file_contents)
2074
+ xlsx = Roo::Excelx.new(stream)
2075
+ assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], xlsx.sheets
2076
+ end
2077
+
2078
+ def test_close
2079
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
2080
+ next unless (tempdir = oo.instance_variable_get('@tmpdir'))
2081
+ oo.close
2082
+ assert !File.exists?(tempdir), "Expected #{tempdir} to be cleaned up, but it still exists"
2083
+ end
2084
+ end
2085
+
2086
+ def test_cleanup_on_error
2087
+ old_temp_files = Dir.open(Dir.tmpdir).to_a
2088
+ with_each_spreadsheet(:name=>'non_existent_file', :ignore_errors=>true) do |oo|; end
2089
+ assert_equal Dir.open(Dir.tmpdir).to_a, old_temp_files
2090
+ end
2091
+ end # class