roo 1.9.3 → 1.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ # Loeschen von Dateien, wenn mit Excel geoeffnet
2
+ require 'rubygems'
3
+ require 'roo'
4
+
5
+ oo = Excel.new("tmp.xls")
6
+ #oo = Openoffice.new("tmp.ods")
7
+ oo.default_sheet = oo.sheets.first
8
+ oo.first_row.upto(oo.last_row) do |row|
9
+ oo.first_column.upto(oo.last_column) do |col|
10
+ p oo.cell(row,col)
11
+ end
12
+ end
13
+
@@ -0,0 +1,20 @@
1
+ =begin
2
+ require 'rubygems'
3
+ require 'roo'
4
+
5
+ oo = Excel.new("tmp.xls")
6
+ oo.default_sheet = oo.sheets.first
7
+ oo.first_row.upto(oo.last_row) do |row|
8
+ oo.first_column.upto(oo.last_column) do |col|
9
+ p oo.cell(row,col)
10
+ end
11
+ end
12
+ FileUtils.rm_f("tmp.xls", {:verbose => true, :force => true})
13
+ =end
14
+ require 'spreadsheet'
15
+
16
+ book = Spreadsheet.open 'tmp.xls'
17
+ sheet = book.worksheet 0
18
+ sheet.each do |row| puts row[0] end
19
+
20
+ FileUtils.rm("tmp.xls")
File without changes
File without changes
File without changes
File without changes
@@ -17,3 +17,33 @@ def local_only
17
17
  yield
18
18
  end
19
19
  end
20
+
21
+ # very simple diff implementation
22
+ # output is an empty string if the files are equal
23
+ # otherwise differences a printen (not compatible to
24
+ # the diff command)
25
+ def diff(fn1,fn2)
26
+ result = ''
27
+ f1 = File.open(fn1)
28
+ f2 = File.open(fn2)
29
+ while f1.eof? == false and f2.eof? == false
30
+ line1 = f1.gets
31
+ line2 = f2.gets
32
+ result << "<#{line1}\n>#{line2}\n" if line1 != line2
33
+ end
34
+ if f1.eof? == false
35
+ while f1.eof? == false
36
+ line1 = f1.gets
37
+ result << "<#{line1}\n"
38
+ end
39
+ end
40
+ if f2.eof? == false
41
+ while f2.eof? == false
42
+ line2 = f2.gets
43
+ result ">#{line2}\n"
44
+ end
45
+ end
46
+ f1.close
47
+ f2.close
48
+ result
49
+ end
@@ -1,2022 +1,2254 @@
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
- require './lib/roo'
13
- #TODO
14
- # Look at formulas in excel - does not work with date/time
15
-
16
-
17
- # Dump warnings that come from the test to open files
18
- # with the wrong spreadsheet class
19
- #STDERR.reopen "/dev/null","w"
20
-
21
- TESTDIR = File.dirname(__FILE__)
22
- require TESTDIR + '/test_helper.rb'
23
- #require 'soap/rpc/driver'
24
- require 'fileutils'
25
- require 'timeout'
26
- require 'logger'
27
- $log = Logger.new(File.join(ENV['HOME'],"roo.log"))
28
- #$log.level = Logger::WARN
29
- $log.level = Logger::DEBUG
30
-
31
- DISPLAY_LOG = false
32
- DB_LOG = false
33
-
34
- if DB_LOG
35
- require 'activerecord'
36
- end
37
-
38
- include FileUtils
39
-
40
- if DB_LOG
41
- def activerecord_connect
42
- ActiveRecord::Base.establish_connection(:adapter => "mysql",
43
- :database => "test_runs",
44
- :host => "localhost",
45
- :username => "root",
46
- :socket => "/var/run/mysqld/mysqld.sock")
47
- end
48
-
49
- class Testrun < ActiveRecord::Base
50
- end
51
- end
52
-
53
- class Test::Unit::TestCase
54
- def key_of(spreadsheetname)
55
- begin
56
-
57
- return {
58
- #'formula' => 'rt4Pw1WmjxFtyfrqqy94wPw',
59
- 'formula' => 'o10837434939102457526.3022866619437760118',
60
- #"write.me" => 'r6m7HFlUOwst0RTUTuhQ0Ow',
61
- "write.me" => '0AkCuGANLc3jFcHR1NmJiYWhOWnBZME4wUnJ4UWJXZHc',
62
- #'numbers1' => "rYraCzjxTtkxw1NxHJgDU8Q",
63
- 'numbers1' => 'o10837434939102457526.4784396906364855777',
64
- #'borders' => "r_nLYMft6uWg_PT9Rc2urXw",
65
- 'borders' => "o10837434939102457526.664868920231926255",
66
- #'simple_spreadsheet' => "r3aMMCBCA153TmU_wyIaxfw",
67
- 'simple_spreadsheet' => "ptu6bbahNZpYe-L1vEBmgGA",
68
- 'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
69
- #"only_one_sheet" => "rqRtkcPJ97nhQ0m9ksDw2rA",
70
- "only_one_sheet" => "o10837434939102457526.762705759906130135",
71
- #'time-test' => 'r2XfDBJMrLPjmuLrPQQrEYw',
72
- 'time-test' => 'ptu6bbahNZpYBMhk01UfXSg',
73
- #'datetime' => "r2kQpXWr6xOSUpw9MyXavYg",
74
- 'datetime' => "ptu6bbahNZpYQEtZwzL_dZQ",
75
- 'whitespace' => "rZyQaoFebVGeHKzjG6e9gRQ",
76
- 'matrix' => '0AkCuGANLc3jFdHY3cWtYUkM4bVdadjZ5VGpfTzFEUEE',
77
- }[spreadsheetname]
78
- # 'numbers1' => "o10837434939102457526.4784396906364855777",
79
- # 'borders' => "o10837434939102457526.664868920231926255",
80
- # 'simple_spreadsheet' => "ptu6bbahNZpYe-L1vEBmgGA",
81
- # 'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
82
- # "only_one_sheet" => "o10837434939102457526.762705759906130135",
83
- # "write.me" => 'ptu6bbahNZpY0N0RrxQbWdw&hl',
84
- # 'formula' => 'o10837434939102457526.3022866619437760118',
85
- # 'time-test' => 'ptu6bbahNZpYBMhk01UfXSg',
86
- # 'datetime' => "ptu6bbahNZpYQEtZwzL_dZQ",
87
- rescue
88
- raise "unknown spreadsheetname: #{spreadsheetname}"
89
- end
90
- end
91
-
92
- if DB_LOG
93
- if ! (defined?(@connected) and @connected)
94
- activerecord_connect
95
- else
96
- @connected = true
97
- end
98
- end
99
- alias unlogged_run run
100
- def run(result, &block)
101
- t1 = Time.now
102
- #RAILS_DEFAULT_LOGGER.debug "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
103
- if DISPLAY_LOG
104
- print "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
105
- STDOUT.flush
106
- end
107
- unlogged_run result, &block
108
- t2 = Time.now
109
- if DISPLAY_LOG
110
- puts "\t#{t2-t1} seconds"
111
- end
112
- if DB_LOG
113
- domain = Testrun.create(
114
- :class_name => self.class.to_s,
115
- :test_name => @method_name,
116
- :start => t1,
117
- :duration => t2-t1
118
- )
119
- end
120
- end
121
- end
122
-
123
- class File
124
- def File.delete_if_exist(filename)
125
- if File.exist?(filename)
126
- File.delete(filename)
127
- end
128
- end
129
- end
130
-
131
- # :nodoc
132
- class Fixnum
133
- def minutes
134
- self * 60
135
- end
136
- end
137
-
138
- class TestRoo < Test::Unit::TestCase
139
-
140
- OPENOFFICE = true # do Openoffice-Spreadsheet Tests?
141
- EXCEL = true # do Excel Tests?
142
- GOOGLE = true # do Google-Spreadsheet Tests?
143
- EXCELX = true # do Excel-X Tests? (.xlsx-files)
144
-
145
- ONLINE = true
146
- LONG_RUN = true
147
- GLOBAL_TIMEOUT = 48.minutes #*60 # 2*12*60 # seconds
148
-
149
- def setup
150
- #if DISPLAY_LOG
151
- # puts " GLOBAL_TIMEOUT = #{GLOBAL_TIMEOUT}"
152
- #end
153
- end
154
-
155
- def test_internal_minutes
156
- assert_equal 42*60, 42.minutes
157
- end
158
-
159
- # call a block of code for each spreadsheet type
160
- # and yield a reference to the roo object
161
- def with_each_spreadsheet(options)
162
- # test if the spreadsheet type is valid :nodoc
163
- if options[:format]
164
- if options[:format].is_a? Symbol
165
- options[:format] = [options[:format]]
166
- end
167
- options[:format].each do |formatname|
168
- unless [:openoffice,:excel,:excelx,:google].include?(formatname)
169
- raise "invalid spreadsheet type #{formatname}"
170
- end
171
- end
172
- end
173
- # end test spreadsheet type :nodoc
174
- options[:format] ||= [:excel, :excelx, :openoffice, :google]
175
- options[:format] = [options[:format]] if options[:format].class == Symbol
176
- yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xls')) if EXCEL && options[:format].include?(:excel)
177
- yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xlsx')) if EXCELX && options[:format].include?(:excelx)
178
- yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.ods')) if OPENOFFICE && options[:format].include?(:openoffice)
179
- yield Roo::Spreadsheet.open(key_of(options[:name]) || options[:name]) if GOOGLE && options[:format].include?(:google)
180
- end
181
- # Using Date.strptime so check that it's using the method
182
- # with the value set in date_format
183
- def test_date
184
- with_each_spreadsheet(:name=>'numbers1', :format=>:google) do |oo|
185
- # should default to DDMMYYYY
186
- assert oo.date?("21/11/1962") == true
187
- assert oo.date?("11/21/1962") == false
188
- oo.date_format = '%m/%d/%Y'
189
- assert oo.date?("21/11/1962") == false
190
- assert oo.date?("11/21/1962") == true
191
- oo.date_format = '%Y-%m-%d'
192
- assert oo.date?("1962-11-21") == true
193
- assert oo.date?("1962-21-11") == false
194
- end
195
- end
196
-
197
- def test_classes
198
- if OPENOFFICE
199
- oo = Openoffice.new(File.join(TESTDIR,"numbers1.ods"))
200
- assert_kind_of Openoffice, oo
201
- end
202
- if EXCEL
203
- oo = Excel.new(File.join(TESTDIR,"numbers1.xls"))
204
- assert_kind_of Excel, oo
205
- end
206
- if GOOGLE
207
- oo = Google.new(key_of("numbers1"))
208
- assert_kind_of Google, oo
209
- end
210
- if EXCELX
211
- oo = Excelx.new(File.join(TESTDIR,"numbers1.xlsx"))
212
- assert_kind_of Excelx, oo
213
- end
214
- end
215
-
216
- def test_letters
217
- assert_equal 1, GenericSpreadsheet.letter_to_number('A')
218
- assert_equal 1, GenericSpreadsheet.letter_to_number('a')
219
- assert_equal 2, GenericSpreadsheet.letter_to_number('B')
220
- assert_equal 26, GenericSpreadsheet.letter_to_number('Z')
221
- assert_equal 27, GenericSpreadsheet.letter_to_number('AA')
222
- assert_equal 27, GenericSpreadsheet.letter_to_number('aA')
223
- assert_equal 27, GenericSpreadsheet.letter_to_number('Aa')
224
- assert_equal 27, GenericSpreadsheet.letter_to_number('aa')
225
- end
226
-
227
- def test_sheets
228
- with_each_spreadsheet(:name=>'numbers1') do |oo|
229
- assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], oo.sheets
230
- assert_raise(RangeError) { oo.default_sheet = "no_sheet" }
231
- assert_raise(TypeError) { oo.default_sheet = [1,2,3] }
232
- oo.sheets.each { |sh|
233
- oo.default_sheet = sh
234
- assert_equal sh, oo.default_sheet
235
- }
236
- end
237
- end
238
-
239
- def test_cells
240
- with_each_spreadsheet(:name=>'numbers1') do |oo|
241
- # warum ist Auswaehlen erstes sheet hier nicht
242
- # mehr drin?
243
- oo.default_sheet = oo.sheets.first
244
- assert_equal 1, oo.cell(1,1)
245
- assert_equal 2, oo.cell(1,2)
246
- assert_equal 3, oo.cell(1,3)
247
- assert_equal 4, oo.cell(1,4)
248
- assert_equal 5, oo.cell(2,1)
249
- assert_equal 6, oo.cell(2,2)
250
- assert_equal 7, oo.cell(2,3)
251
- assert_equal 8, oo.cell(2,4)
252
- assert_equal 9, oo.cell(2,5)
253
- assert_equal "test", oo.cell(2,6)
254
- assert_equal :string, oo.celltype(2,6)
255
- assert_equal 11, oo.cell(2,7)
256
- assert_equal :float, oo.celltype(2,7)
257
- assert_equal 10, oo.cell(4,1)
258
- assert_equal 11, oo.cell(4,2)
259
- assert_equal 12, oo.cell(4,3)
260
- assert_equal 13, oo.cell(4,4)
261
- assert_equal 14, oo.cell(4,5)
262
- assert_equal 10, oo.cell(4,'A')
263
- assert_equal 11, oo.cell(4,'B')
264
- assert_equal 12, oo.cell(4,'C')
265
- assert_equal 13, oo.cell(4,'D')
266
- assert_equal 14, oo.cell(4,'E')
267
- assert_equal :date, oo.celltype(5,1)
268
- assert_equal Date.new(1961,11,21), oo.cell(5,1)
269
- assert_equal "1961-11-21", oo.cell(5,1).to_s
270
- end
271
- end
272
-
273
- def test_celltype
274
- with_each_spreadsheet(:name=>'numbers1') do |oo|
275
- assert_equal :string, oo.celltype(2,6)
276
- end
277
- end
278
-
279
- def test_cell_address
280
- with_each_spreadsheet(:name=>'numbers1') do |oo|
281
- assert_equal "tata", oo.cell(6,1)
282
- assert_equal "tata", oo.cell(6,'A')
283
- assert_equal "tata", oo.cell('A',6)
284
- assert_equal "tata", oo.cell(6,'a')
285
- assert_equal "tata", oo.cell('a',6)
286
- assert_raise(ArgumentError) { assert_equal "tata", oo.cell('a','f') }
287
- assert_raise(ArgumentError) { assert_equal "tata", oo.cell('f','a') }
288
- assert_equal "thisisc8", oo.cell(8,3)
289
- assert_equal "thisisc8", oo.cell(8,'C')
290
- assert_equal "thisisc8", oo.cell('C',8)
291
- assert_equal "thisisc8", oo.cell(8,'c')
292
- assert_equal "thisisc8", oo.cell('c',8)
293
- assert_equal "thisisd9", oo.cell('d',9)
294
- assert_equal "thisisa11", oo.cell('a',11)
295
- end
296
- end
297
-
298
- def test_office_version
299
- with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
300
- assert_equal "1.0", oo.officeversion
301
- end
302
- end
303
-
304
- #TODO: inkonsequente Lieferung Fixnum/Float
305
- def test_rows
306
- with_each_spreadsheet(:name=>'numbers1') do |oo|
307
- oo.default_sheet = oo.sheets.first
308
- assert_equal 41, oo.cell('a',12)
309
- assert_equal 42, oo.cell('b',12)
310
- assert_equal 43, oo.cell('c',12)
311
- assert_equal 44, oo.cell('d',12)
312
- assert_equal 45, oo.cell('e',12)
313
- assert_equal [41.0,42.0,43.0,44.0,45.0, nil, nil], oo.row(12)
314
- assert_equal "einundvierzig", oo.cell('a',16)
315
- assert_equal "zweiundvierzig", oo.cell('b',16)
316
- assert_equal "dreiundvierzig", oo.cell('c',16)
317
- assert_equal "vierundvierzig", oo.cell('d',16)
318
- assert_equal "fuenfundvierzig", oo.cell('e',16)
319
- assert_equal ["einundvierzig", "zweiundvierzig", "dreiundvierzig", "vierundvierzig", "fuenfundvierzig", nil, nil], oo.row(16)
320
- end
321
- end
322
-
323
- def test_last_row
324
- with_each_spreadsheet(:name=>'numbers1') do |oo|
325
- oo.default_sheet = oo.sheets.first
326
- assert_equal 18, oo.last_row
327
- end
328
- end
329
-
330
- def test_last_column
331
- with_each_spreadsheet(:name=>'numbers1') do |oo|
332
- oo.default_sheet = oo.sheets.first
333
- assert_equal 7, oo.last_column
334
- end
335
- end
336
-
337
- def test_last_column_as_letter
338
- with_each_spreadsheet(:name=>'numbers1') do |oo|
339
- oo.default_sheet = oo.sheets.first
340
- assert_equal 'G', oo.last_column_as_letter
341
- end
342
- end
343
-
344
- def test_first_row
345
- with_each_spreadsheet(:name=>'numbers1') do |oo|
346
- oo.default_sheet = oo.sheets.first
347
- assert_equal 1, oo.first_row
348
- end
349
- end
350
-
351
- def test_first_column
352
- with_each_spreadsheet(:name=>'numbers1') do |oo|
353
- oo.default_sheet = oo.sheets.first
354
- assert_equal 1, oo.first_column
355
- end
356
- end
357
-
358
- def test_first_column_as_letter
359
- with_each_spreadsheet(:name=>'numbers1') do |oo|
360
- oo.default_sheet = oo.sheets.first
361
- assert_equal 'A', oo.first_column_as_letter
362
- end
363
- end
364
-
365
- def test_sheetname
366
- with_each_spreadsheet(:name=>'numbers1') do |oo|
367
- oo.default_sheet = "Name of Sheet 2"
368
- assert_equal 'I am sheet 2', oo.cell('C',5)
369
- assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
370
- assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
371
- assert_raise(RangeError) { dummy = oo.cell('C',5,"non existing sheet name")}
372
- assert_raise(RangeError) { dummy = oo.celltype('C',5,"non existing sheet name")}
373
- assert_raise(RangeError) { dummy = oo.empty?('C',5,"non existing sheet name")}
374
- if oo.class == Excel
375
- assert_raise(RuntimeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
376
- assert_raise(RuntimeError) { dummy = oo.formula('C',5,"non existing sheet name")}
377
- else
378
- assert_raise(RangeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
379
- assert_raise(RangeError) { dummy = oo.formula('C',5,"non existing sheet name")}
380
- begin
381
- assert_raise(RangeError) { dummy = oo.set('C',5,42,"non existing sheet name")} unless oo.class == Google
382
- rescue NameError
383
- #
384
- end
385
- assert_raise(RangeError) { dummy = oo.formulas("non existing sheet name")}
386
- end
387
- assert_raise(RangeError) { dummy = oo.to_yaml({},1,1,1,1,"non existing sheet name")}
388
- end
389
- end
390
-
391
- def test_boundaries
392
- with_each_spreadsheet(:name=>'numbers1') do |oo|
393
- oo.default_sheet = "Name of Sheet 2"
394
- assert_equal 2, oo.first_column
395
- assert_equal 'B', oo.first_column_as_letter
396
- assert_equal 5, oo.first_row
397
- assert_equal 'E', oo.last_column_as_letter
398
- assert_equal 14, oo.last_row
399
- end
400
- end
401
-
402
- def test_multiple_letters
403
- with_each_spreadsheet(:name=>'numbers1') do |oo|
404
- oo.default_sheet = "Sheet3"
405
- assert_equal "i am AA", oo.cell('AA',1)
406
- assert_equal "i am AB", oo.cell('AB',1)
407
- assert_equal "i am BA", oo.cell('BA',1)
408
- assert_equal 'BA', oo.last_column_as_letter
409
- assert_equal "i am BA", oo.cell(1,'BA')
410
- end
411
- end
412
-
413
- def test_argument_error
414
- with_each_spreadsheet(:name=>'numbers1') do |oo|
415
- assert_nothing_raised(ArgumentError) { oo.default_sheet = "Tabelle1" }
416
- end
417
- end
418
-
419
- def test_empty_eh
420
- with_each_spreadsheet(:name=>'numbers1') do |oo|
421
- assert oo.empty?('a',14)
422
- assert ! oo.empty?('a',15)
423
- assert oo.empty?('a',20)
424
- end
425
- end
426
-
427
- def test_reload
428
- with_each_spreadsheet(:name=>'numbers1') do |oo|
429
- oo.default_sheet = oo.sheets.first
430
- assert_equal 1, oo.cell(1,1)
431
- oo.reload
432
- assert_equal 1, oo.cell(1,1)
433
- end
434
- end
435
-
436
- def test_bug_contiguous_cells
437
- with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
438
- oo.default_sheet = "Sheet4"
439
- assert_equal Date.new(2007,06,16), oo.cell('a',1)
440
- assert_equal 10, oo.cell('b',1)
441
- assert_equal 10, oo.cell('c',1)
442
- assert_equal 10, oo.cell('d',1)
443
- assert_equal 10, oo.cell('e',1)
444
- end
445
- end
446
-
447
- def test_bug_italo_ve
448
- with_each_spreadsheet(:name=>'numbers1') do |oo|
449
- oo.default_sheet = "Sheet5"
450
- assert_equal 1, oo.cell('A',1)
451
- assert_equal 5, oo.cell('b',1)
452
- assert_equal 5, oo.cell('c',1)
453
- assert_equal 2, oo.cell('a',2)
454
- assert_equal 3, oo.cell('a',3)
455
- end
456
- end
457
-
458
- def test_italo_table
459
- with_each_spreadsheet(:name=>'simple_spreadsheet_from_italo', :format=>[:openoffice, :excel]) do |oo|
460
- assert_equal '1', oo.cell('A',1)
461
- assert_equal '1', oo.cell('B',1)
462
- assert_equal '1', oo.cell('C',1)
463
- assert_equal 1, oo.cell('A',2).to_i
464
- assert_equal 2, oo.cell('B',2).to_i
465
- assert_equal 1, oo.cell('C',2).to_i
466
- assert_equal 1, oo.cell('A',3)
467
- assert_equal 3, oo.cell('B',3)
468
- assert_equal 1, oo.cell('C',3)
469
- assert_equal 'A', oo.cell('A',4)
470
- assert_equal 'A', oo.cell('B',4)
471
- assert_equal 'A', oo.cell('C',4)
472
- assert_equal 0.01, oo.cell('A',5)
473
- assert_equal 0.01, oo.cell('B',5)
474
- assert_equal 0.01, oo.cell('C',5)
475
- assert_equal 0.03, oo.cell('a',5)+oo.cell('b',5)+oo.cell('c',5)
476
-
477
- # Cells values in row 1:
478
- assert_equal "1:string", oo.cell(1, 1)+":"+oo.celltype(1, 1).to_s
479
- assert_equal "1:string",oo.cell(1, 2)+":"+oo.celltype(1, 2).to_s
480
- assert_equal "1:string",oo.cell(1, 3)+":"+oo.celltype(1, 3).to_s
481
-
482
- # Cells values in row 2:
483
- assert_equal "1:string",oo.cell(2, 1)+":"+oo.celltype(2, 1).to_s
484
- assert_equal "2:string",oo.cell(2, 2)+":"+oo.celltype(2, 2).to_s
485
- assert_equal "1:string",oo.cell(2, 3)+":"+oo.celltype(2, 3).to_s
486
-
487
- # Cells values in row 3:
488
- assert_equal "1.0:float",oo.cell(3, 1).to_s+":"+oo.celltype(3, 1).to_s
489
- assert_equal "3.0:float",oo.cell(3, 2).to_s+":"+oo.celltype(3, 2).to_s
490
- assert_equal "1.0:float",oo.cell(3, 3).to_s+":"+oo.celltype(3, 3).to_s
491
-
492
- # Cells values in row 4:
493
- assert_equal "A:string",oo.cell(4, 1)+":"+oo.celltype(4, 1).to_s
494
- assert_equal "A:string",oo.cell(4, 2)+":"+oo.celltype(4, 2).to_s
495
- assert_equal "A:string",oo.cell(4, 3)+":"+oo.celltype(4, 3).to_s
496
-
497
- # Cells values in row 5:
498
- if oo.class == Openoffice
499
- assert_equal "0.01:percentage",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
500
- assert_equal "0.01:percentage",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
501
- assert_equal "0.01:percentage",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
502
- else
503
- assert_equal "0.01:float",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
504
- assert_equal "0.01:float",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
505
- assert_equal "0.01:float",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
506
- end
507
- end
508
- end
509
-
510
- def test_formula_openoffice
511
- with_each_spreadsheet(:name=>'formula', :format=>:openoffice) do |oo|
512
- assert_equal 1, oo.cell('A',1)
513
- assert_equal 2, oo.cell('A',2)
514
- assert_equal 3, oo.cell('A',3)
515
- assert_equal 4, oo.cell('A',4)
516
- assert_equal 5, oo.cell('A',5)
517
- assert_equal 6, oo.cell('A',6)
518
- assert_equal 21, oo.cell('A',7)
519
- assert_equal :formula, oo.celltype('A',7)
520
- assert_equal "=[Sheet2.A1]", oo.formula('C',7)
521
- assert_nil oo.formula('A',6)
522
- assert_equal [[7, 1, "=SUM([.A1:.A6])"],
523
- [7, 2, "=SUM([.$A$1:.B6])"],
524
- [7, 3, "=[Sheet2.A1]"],
525
- [8, 2, "=SUM([.$A$1:.B7])"],
526
- ], oo.formulas(oo.sheets.first)
527
-
528
- # setting a cell
529
- oo.set('A',15, 41)
530
- assert_equal 41, oo.cell('A',15)
531
- oo.set('A',16, "41")
532
- assert_equal "41", oo.cell('A',16)
533
- oo.set('A',17, 42.5)
534
- assert_equal 42.5, oo.cell('A',17)
535
- end
536
- end
537
-
538
- def test_formula_google
539
- with_each_spreadsheet(:name=>'formula', :format=>:google) do |oo|
540
- oo.default_sheet = oo.sheets.first
541
- assert_equal 1, oo.cell('A',1)
542
- assert_equal 2, oo.cell('A',2)
543
- assert_equal 3, oo.cell('A',3)
544
- assert_equal 4, oo.cell('A',4)
545
- assert_equal 5, oo.cell('A',5)
546
- assert_equal 6, oo.cell('A',6)
547
- # assert_equal 21, oo.cell('A',7)
548
- assert_equal 21.0, oo.cell('A',7) #TODO: better solution Fixnum/Float
549
- assert_equal :formula, oo.celltype('A',7)
550
- # assert_equal "=[Sheet2.A1]", oo.formula('C',7)
551
- # !!! different from formulas in Openoffice
552
- #was: assert_equal "=sheet2!R[-6]C[-2]", oo.formula('C',7)
553
- # has Google changed their format of formulas/references to other sheets?
554
- assert_equal "=Sheet2!R[-6]C[-2]", oo.formula('C',7)
555
- assert_nil oo.formula('A',6)
556
- # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
557
- # [7, 2, "=SUM([.$A$1:.B6])"],
558
- # [7, 3, "=[Sheet2.A1]"],
559
- # [8, 2, "=SUM([.$A$1:.B7])"],
560
- # ], oo.formulas(oo.sheets.first)
561
- # different format than in openoffice spreadsheets:
562
- #was:
563
- # assert_equal [[7, 1, "=SUM(R[-6]C[0]:R[-1]C[0])"],
564
- # [7, 2, "=SUM(R1C1:R[-1]C[0])"],
565
- # [7, 3, "=sheet2!R[-6]C[-2]"],
566
- # [8, 2, "=SUM(R1C1:R[-1]C[0])"]],
567
- # oo.formulas(oo.sheets.first)
568
- assert_equal [[7, 1, "=SUM(R[-6]C:R[-1]C)"],
569
- [7, 2, "=SUM(R1C1:R[-1]C)"],
570
- [7, 3, "=Sheet2!R[-6]C[-2]"],
571
- [8, 2, "=SUM(R1C1:R[-1]C)"]],
572
- oo.formulas(oo.sheets.first)
573
- end
574
- end
575
-
576
- def test_formula_excelx
577
- with_each_spreadsheet(:name=>'formula', :format=>:excelx) do |oo|
578
- assert_equal 1, oo.cell('A',1)
579
- assert_equal 2, oo.cell('A',2)
580
- assert_equal 3, oo.cell('A',3)
581
- assert_equal 4, oo.cell('A',4)
582
- assert_equal 5, oo.cell('A',5)
583
- assert_equal 6, oo.cell('A',6)
584
- assert_equal 21, oo.cell('A',7)
585
- assert_equal :formula, oo.celltype('A',7)
586
- #steht nicht in Datei, oder?
587
- #nein, diesen Bezug habe ich nur in der Openoffice-Datei
588
- #assert_equal "=[Sheet2.A1]", oo.formula('C',7)
589
- assert_nil oo.formula('A',6)
590
- # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
591
- # [7, 2, "=SUM([.$A$1:.B6])"],
592
- #[7, 3, "=[Sheet2.A1]"],
593
- #[8, 2, "=SUM([.$A$1:.B7])"],
594
- #], oo.formulas(oo.sheets.first)
595
- assert_equal [[7, 1, 'SUM(A1:A6)'],
596
- [7, 2, 'SUM($A$1:B6)'],
597
- # [7, 3, "=[Sheet2.A1]"],
598
- # [8, 2, "=SUM([.$A$1:.B7])"],
599
- ], oo.formulas(oo.sheets.first)
600
-
601
- # setting a cell
602
- oo.set('A',15, 41)
603
- assert_equal 41, oo.cell('A',15)
604
- oo.set('A',16, "41")
605
- assert_equal "41", oo.cell('A',16)
606
- oo.set('A',17, 42.5)
607
- assert_equal 42.5, oo.cell('A',17)
608
- end
609
- end
610
-
611
- # Excel can only read the cell's value
612
- def test_formula_excel
613
- with_each_spreadsheet(:name=>'formula', :format=>:excel) do |oo|
614
- assert_equal 21, oo.cell('A',7)
615
- assert_equal 21, oo.cell('B',7)
616
- end
617
- end
618
-
619
-
620
- def test_borders_sheets
621
- with_each_spreadsheet(:name=>'borders') do |oo|
622
- oo.default_sheet = oo.sheets[1]
623
- assert_equal 6, oo.first_row
624
- assert_equal 11, oo.last_row
625
- assert_equal 4, oo.first_column
626
- assert_equal 8, oo.last_column
627
-
628
- oo.default_sheet = oo.sheets.first
629
- assert_equal 5, oo.first_row
630
- assert_equal 10, oo.last_row
631
- assert_equal 3, oo.first_column
632
- assert_equal 7, oo.last_column
633
-
634
- oo.default_sheet = oo.sheets[2]
635
- assert_equal 7, oo.first_row
636
- assert_equal 12, oo.last_row
637
- assert_equal 5, oo.first_column
638
- assert_equal 9, oo.last_column
639
- end
640
- end
641
-
642
- def yaml_entry(row,col,type,value)
643
- "cell_#{row}_#{col}: \n row: #{row} \n col: #{col} \n celltype: #{type} \n value: #{value} \n"
644
- end
645
-
646
- def test_to_yaml
647
- with_each_spreadsheet(:name=>'numbers1') do |oo|
648
- oo.default_sheet = oo.sheets.first
649
- assert_equal "--- \n"+yaml_entry(5,1,"date","1961-11-21"), oo.to_yaml({}, 5,1,5,1)
650
- assert_equal "--- \n"+yaml_entry(8,3,"string","thisisc8"), oo.to_yaml({}, 8,3,8,3)
651
- assert_equal "--- \n"+yaml_entry(12,3,"float",43.0), oo.to_yaml({}, 12,3,12,3)
652
- assert_equal \
653
- "--- \n"+yaml_entry(12,3,"float",43.0) +
654
- yaml_entry(12,4,"float",44.0) +
655
- yaml_entry(12,5,"float",45.0), oo.to_yaml({}, 12,3,12)
656
- assert_equal \
657
- "--- \n"+yaml_entry(12,3,"float",43.0)+
658
- yaml_entry(12,4,"float",44.0)+
659
- yaml_entry(12,5,"float",45.0)+
660
- yaml_entry(15,3,"float",43.0)+
661
- yaml_entry(15,4,"float",44.0)+
662
- yaml_entry(15,5,"float",45.0)+
663
- yaml_entry(16,3,"string","dreiundvierzig")+
664
- yaml_entry(16,4,"string","vierundvierzig")+
665
- yaml_entry(16,5,"string","fuenfundvierzig"), oo.to_yaml({}, 12,3)
666
- end
667
- end
668
-
669
- def test_only_one_sheet
670
- with_each_spreadsheet(:name=>'only_one_sheet') do |oo|
671
- assert_equal 42, oo.cell('B',4)
672
- assert_equal 43, oo.cell('C',4)
673
- assert_equal 44, oo.cell('D',4)
674
- oo.default_sheet = oo.sheets.first
675
- assert_equal 42, oo.cell('B',4)
676
- assert_equal 43, oo.cell('C',4)
677
- assert_equal 44, oo.cell('D',4)
678
- end
679
- end
680
-
681
- def test_excel_open_from_uri_and_zipped
682
- if EXCEL
683
- if ONLINE
684
- begin
685
- url = 'http://stiny-leonhard.de/bode-v1.xls.zip'
686
- excel = Excel.new(url, :zip)
687
- excel.default_sheet = excel.sheets.first
688
- assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
689
- ensure
690
- excel.remove_tmp
691
- end
692
- end
693
- end
694
- end
695
-
696
- def test_openoffice_open_from_uri_and_zipped
697
- if OPENOFFICE
698
- if ONLINE
699
- begin
700
- url = 'http://spazioinwind.libero.it/s2/rata.ods.zip'
701
- sheet = Openoffice.new(url, :zip)
702
- #has been changed: assert_equal 'ist "e" im Nenner von H(s)', sheet.cell('b', 5)
703
- assert_in_delta 0.001, 505.14, sheet.cell('c', 33).to_f
704
- ensure
705
- sheet.remove_tmp
706
- end
707
- end
708
- end
709
- end
710
-
711
- def test_excel_zipped
712
- if EXCEL
713
- begin
714
- oo = Excel.new(File.join(TESTDIR,"bode-v1.xls.zip"), :zip)
715
- assert oo
716
- assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
717
- ensure
718
- oo.remove_tmp if oo
719
- end
720
- end
721
- end
722
-
723
- def test_openoffice_zipped
724
- if OPENOFFICE
725
- begin
726
- oo = Openoffice.new(File.join(TESTDIR,"bode-v1.ods.zip"), :zip)
727
- assert oo
728
- assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
729
- ensure
730
- oo.remove_tmp
731
- end
732
- end
733
- end
734
-
735
- def test_bug_ric
736
- with_each_spreadsheet(:name=>'ric', :format=>:openoffice) do |oo|
737
- assert oo.empty?('A',1)
738
- assert oo.empty?('B',1)
739
- assert oo.empty?('C',1)
740
- assert oo.empty?('D',1)
741
- expected = 1
742
- letter = 'e'
743
- while letter <= 'u'
744
- assert_equal expected, oo.cell(letter,1)
745
- letter.succ!
746
- expected += 1
747
- end
748
- assert_equal 'J', oo.cell('v',1)
749
- assert_equal 'P', oo.cell('w',1)
750
- assert_equal 'B', oo.cell('x',1)
751
- assert_equal 'All', oo.cell('y',1)
752
- assert_equal 0, oo.cell('a',2)
753
- assert oo.empty?('b',2)
754
- assert oo.empty?('c',2)
755
- assert oo.empty?('d',2)
756
- assert_equal 'B', oo.cell('e',2)
757
- assert_equal 'B', oo.cell('f',2)
758
- assert_equal 'B', oo.cell('g',2)
759
- assert_equal 'B', oo.cell('h',2)
760
- assert_equal 'B', oo.cell('i',2)
761
- assert_equal 'B', oo.cell('j',2)
762
- assert_equal 'B', oo.cell('k',2)
763
- assert_equal 'B', oo.cell('l',2)
764
- assert_equal 'B', oo.cell('m',2)
765
- assert_equal 'B', oo.cell('n',2)
766
- assert_equal 'B', oo.cell('o',2)
767
- assert_equal 'B', oo.cell('p',2)
768
- assert_equal 'B', oo.cell('q',2)
769
- assert_equal 'B', oo.cell('r',2)
770
- assert_equal 'B', oo.cell('s',2)
771
- assert oo.empty?('t',2)
772
- assert oo.empty?('u',2)
773
- assert_equal 0 , oo.cell('v',2)
774
- assert_equal 0 , oo.cell('w',2)
775
- assert_equal 15 , oo.cell('x',2)
776
- assert_equal 15 , oo.cell('y',2)
777
- end
778
- end
779
-
780
- def test_mehrteilig
781
- with_each_spreadsheet(:name=>'Bibelbund1', :format=>:openoffice) do |oo|
782
- assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
783
- end
784
- #if EXCELX
785
- # #Datei gibt es noch nicht
786
- # oo = Excelx.new(File.join(TESTDIR,"Bibelbund1.xlsx"))
787
- # oo.default_sheet = oo.sheets.first
788
- # assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
789
- #end
790
- end
791
-
792
- def test_huge_document_to_csv
793
- after Date.new(2009,10,1) do
794
- if LONG_RUN
795
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
796
- :excel,
797
- :excelx
798
- ]) do |oo|
799
- assert_nothing_raised(Timeout::Error) {
800
- Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
801
- File.delete_if_exist("/tmp/Bibelbund.csv")
802
- assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
803
- assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46,'A')
804
- assert_equal "Tagebuch aus Chile Juli 1977", oo.cell(55,'A')
805
- assert oo.to_csv("/tmp/Bibelbund.csv")
806
- assert File.exists?("/tmp/Bibelbund.csv")
807
- assert_equal "", `diff test/Bibelbund.csv /tmp/Bibelbund.csv`, "error in class #{oo.class}"
808
- end
809
- }
810
- end
811
- end
812
- end
813
- end
814
-
815
- def test_bug_quotes_excelx
816
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
817
- :excel,
818
- :excelx]) do |oo|
819
- oo.default_sheet = oo.sheets.first
820
- assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
821
- oo.cell('a',76)
822
- dummy = oo.to_csv("csv#{$$}")
823
- assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
824
- oo.cell('a',78)
825
- File.delete_if_exist("csv#{$$}")
826
- end
827
- end
828
-
829
- def test_to_csv
830
- with_each_spreadsheet(:name=>'numbers1') do |oo|
831
- master = "#{TESTDIR}/numbers1.csv"
832
- File.delete_if_exist("/tmp/numbers1.csv")
833
- assert oo.to_csv("/tmp/numbers1.csv",oo.sheets.first)
834
- assert File.exists?("/tmp/numbers1.csv")
835
- assert_equal "", `diff #{master} /tmp/numbers1.csv`
836
- assert oo.to_csv("/tmp/numbers1.csv")
837
- assert File.exists?("/tmp/numbers1.csv")
838
- assert_equal "", `diff #{master} /tmp/numbers1.csv`
839
- end
840
- end
841
-
842
- def test_bug_mehrere_datum
843
- with_each_spreadsheet(:name=>'numbers1') do |oo|
844
- oo.default_sheet = 'Sheet5'
845
- assert_equal :date, oo.celltype('A',4)
846
- assert_equal :date, oo.celltype('B',4)
847
- assert_equal :date, oo.celltype('C',4)
848
- assert_equal :date, oo.celltype('D',4)
849
- assert_equal :date, oo.celltype('E',4)
850
- assert_equal Date.new(2007,11,21), oo.cell('A',4)
851
- assert_equal Date.new(2007,11,21), oo.cell('B',4)
852
- assert_equal Date.new(2007,11,21), oo.cell('C',4)
853
- assert_equal Date.new(2007,11,21), oo.cell('D',4)
854
- assert_equal Date.new(2007,11,21), oo.cell('E',4)
855
- assert_equal :float, oo.celltype('A',5)
856
- assert_equal :float, oo.celltype('B',5)
857
- assert_equal :float, oo.celltype('C',5)
858
- assert_equal :float, oo.celltype('D',5)
859
- assert_equal :float, oo.celltype('E',5)
860
- assert_equal 42, oo.cell('A',5)
861
- assert_equal 42, oo.cell('B',5)
862
- assert_equal 42, oo.cell('C',5)
863
- assert_equal 42, oo.cell('D',5)
864
- assert_equal 42, oo.cell('E',5)
865
- assert_equal :string, oo.celltype('A',6)
866
- assert_equal :string, oo.celltype('B',6)
867
- assert_equal :string, oo.celltype('C',6)
868
- assert_equal :string, oo.celltype('D',6)
869
- assert_equal :string, oo.celltype('E',6)
870
- assert_equal "ABC", oo.cell('A',6)
871
- assert_equal "ABC", oo.cell('B',6)
872
- assert_equal "ABC", oo.cell('C',6)
873
- assert_equal "ABC", oo.cell('D',6)
874
- assert_equal "ABC", oo.cell('E',6)
875
- end
876
- end
877
-
878
- def test_multiple_sheets
879
- with_each_spreadsheet(:name=>'numbers1') do |oo|
880
- 2.times do
881
- oo.default_sheet = "Tabelle1"
882
- assert_equal 1, oo.cell(1,1)
883
- assert_equal 1, oo.cell(1,1,"Tabelle1")
884
- assert_equal "I am sheet 2", oo.cell('C',5,"Name of Sheet 2")
885
- sheetname = 'Sheet5'
886
- assert_equal :date, oo.celltype('A',4,sheetname)
887
- assert_equal :date, oo.celltype('B',4,sheetname)
888
- assert_equal :date, oo.celltype('C',4,sheetname)
889
- assert_equal :date, oo.celltype('D',4,sheetname)
890
- assert_equal :date, oo.celltype('E',4,sheetname)
891
- assert_equal Date.new(2007,11,21), oo.cell('A',4,sheetname)
892
- assert_equal Date.new(2007,11,21), oo.cell('B',4,sheetname)
893
- assert_equal Date.new(2007,11,21), oo.cell('C',4,sheetname)
894
- assert_equal Date.new(2007,11,21), oo.cell('D',4,sheetname)
895
- assert_equal Date.new(2007,11,21), oo.cell('E',4,sheetname)
896
- assert_equal :float, oo.celltype('A',5,sheetname)
897
- assert_equal :float, oo.celltype('B',5,sheetname)
898
- assert_equal :float, oo.celltype('C',5,sheetname)
899
- assert_equal :float, oo.celltype('D',5,sheetname)
900
- assert_equal :float, oo.celltype('E',5,sheetname)
901
- assert_equal 42, oo.cell('A',5,sheetname)
902
- assert_equal 42, oo.cell('B',5,sheetname)
903
- assert_equal 42, oo.cell('C',5,sheetname)
904
- assert_equal 42, oo.cell('D',5,sheetname)
905
- assert_equal 42, oo.cell('E',5,sheetname)
906
- assert_equal :string, oo.celltype('A',6,sheetname)
907
- assert_equal :string, oo.celltype('B',6,sheetname)
908
- assert_equal :string, oo.celltype('C',6,sheetname)
909
- assert_equal :string, oo.celltype('D',6,sheetname)
910
- assert_equal :string, oo.celltype('E',6,sheetname)
911
- assert_equal "ABC", oo.cell('A',6,sheetname)
912
- assert_equal "ABC", oo.cell('B',6,sheetname)
913
- assert_equal "ABC", oo.cell('C',6,sheetname)
914
- assert_equal "ABC", oo.cell('D',6,sheetname)
915
- assert_equal "ABC", oo.cell('E',6,sheetname)
916
- oo.reload
917
- end
918
- end
919
- end
920
-
921
-
922
- def test_bug_empty_sheet
923
- with_each_spreadsheet(:name=>'formula', :format=>[:openoffice, :excelx]) do |oo|
924
- oo.default_sheet = 'Sheet3' # is an empty sheet
925
- assert_nothing_raised(NoMethodError) { oo.to_csv(File.join("/","tmp","emptysheet.csv")) }
926
- assert_equal "", `cat /tmp/emptysheet.csv`
927
- end
928
- end
929
-
930
- def test_find_by_row_huge_document
931
- if LONG_RUN
932
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
933
- :excel,
934
- :excelx]) do |oo|
935
- Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
936
- oo.default_sheet = oo.sheets.first
937
- rec = oo.find 20
938
- assert rec
939
- # assert_equal "Brief aus dem Sekretariat", rec[0]
940
- #p rec
941
- assert_equal "Brief aus dem Sekretariat", rec[0]['TITEL']
942
- rec = oo.find 22
943
- assert rec
944
- # assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]
945
- assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]['TITEL']
946
- end
947
- end
948
- end
949
- end
950
-
951
- def test_find_by_row
952
- with_each_spreadsheet(:name=>'numbers1') do |oo|
953
- oo.header_line = nil
954
- rec = oo.find 16
955
- assert rec
956
- assert_nil oo.header_line
957
- # keine Headerlines in diesem Beispiel definiert
958
- assert_equal "einundvierzig", rec[0]
959
- #assert_equal false, rec
960
- rec = oo.find 15
961
- assert rec
962
- assert_equal 41,rec[0]
963
- end
964
- end
965
-
966
- def test_find_by_conditions
967
- if LONG_RUN
968
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
969
- :excel,
970
- :excelx]) do |oo|
971
- assert_nothing_raised(Timeout::Error) {
972
- Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
973
- #-----------------------------------------------------------------
974
- zeilen = oo.find(:all, :conditions => {
975
- 'TITEL' => 'Brief aus dem Sekretariat'
976
- }
977
- )
978
- assert_equal 2, zeilen.size
979
- assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
980
- "INTERNET"=>nil,
981
- "SEITE"=>316.0,
982
- "KENNUNG"=>"Aus dem Bibelbund",
983
- "OBJEKT"=>"Bibel+Gem",
984
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
985
- "NUMMER"=>"1982-3",
986
- "TITEL"=>"Brief aus dem Sekretariat"},
987
- {"VERFASSER"=>"Almassy, Annelene von",
988
- "INTERNET"=>nil,
989
- "SEITE"=>222.0,
990
- "KENNUNG"=>"Aus dem Bibelbund",
991
- "OBJEKT"=>"Bibel+Gem",
992
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
993
- "NUMMER"=>"1983-2",
994
- "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
995
-
996
- #----------------------------------------------------------
997
- zeilen = oo.find(:all,
998
- :conditions => { 'VERFASSER' => 'Almassy, Annelene von' }
999
- )
1000
- assert_equal 13, zeilen.size
1001
- #----------------------------------------------------------
1002
- zeilen = oo.find(:all, :conditions => {
1003
- 'TITEL' => 'Brief aus dem Sekretariat',
1004
- 'VERFASSER' => 'Almassy, Annelene von',
1005
- }
1006
- )
1007
- assert_equal 2, zeilen.size
1008
- assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
1009
- "INTERNET"=>nil,
1010
- "SEITE"=>316.0,
1011
- "KENNUNG"=>"Aus dem Bibelbund",
1012
- "OBJEKT"=>"Bibel+Gem",
1013
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
1014
- "NUMMER"=>"1982-3",
1015
- "TITEL"=>"Brief aus dem Sekretariat"},
1016
- {"VERFASSER"=>"Almassy, Annelene von",
1017
- "INTERNET"=>nil,
1018
- "SEITE"=>222.0,
1019
- "KENNUNG"=>"Aus dem Bibelbund",
1020
- "OBJEKT"=>"Bibel+Gem",
1021
- "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
1022
- "NUMMER"=>"1983-2",
1023
- "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
1024
-
1025
- # Result as an array
1026
- zeilen = oo.find(:all,
1027
- :conditions => {
1028
- 'TITEL' => 'Brief aus dem Sekretariat',
1029
- 'VERFASSER' => 'Almassy, Annelene von',
1030
- }, :array => true)
1031
- assert_equal 2, zeilen.size
1032
- assert_equal [
1033
- [
1034
- "Brief aus dem Sekretariat",
1035
- "Almassy, Annelene von",
1036
- "Bibel+Gem",
1037
- "1982-3",
1038
- 316.0,
1039
- nil,
1040
- "#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
1041
- "Aus dem Bibelbund",
1042
- ],
1043
- [
1044
- "Brief aus dem Sekretariat",
1045
- "Almassy, Annelene von",
1046
- "Bibel+Gem",
1047
- "1983-2",
1048
- 222.0,
1049
- nil,
1050
- "#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
1051
- "Aus dem Bibelbund",
1052
- ]] , zeilen
1053
- end # Timeout
1054
- } # nothing_raised
1055
- end
1056
- end
1057
- end
1058
-
1059
-
1060
- #TODO: temporaerer Test
1061
- def test_seiten_als_date
1062
- with_each_spreadsheet(:name=>'Bibelbund', :format=>:excelx) do |oo|
1063
- assert_equal 'Bericht aus dem Sekretariat', oo.cell(13,1)
1064
- assert_equal '1981-4', oo.cell(13,'D')
1065
- assert_equal String, oo.excelx_type(13,'E')[1].class
1066
- assert_equal [:numeric_or_formula,"General"], oo.excelx_type(13,'E')
1067
- assert_equal '428', oo.excelx_value(13,'E')
1068
- assert_equal 428.0, oo.cell(13,'E')
1069
- end
1070
- end
1071
-
1072
- def test_column
1073
- with_each_spreadsheet(:name=>'numbers1') do |oo|
1074
- 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)]
1075
- assert_equal expected, oo.column(1)
1076
- assert_equal expected, oo.column('a')
1077
- end
1078
- end
1079
-
1080
- def test_column_huge_document
1081
- if LONG_RUN
1082
- with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
1083
- :excel,
1084
- :excelx]) do |oo|
1085
- assert_nothing_raised(Timeout::Error) {
1086
- Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
1087
- oo.default_sheet = oo.sheets.first
1088
- assert_equal 3735, oo.column('a').size
1089
- #assert_equal 499, oo.column('a').size
1090
- end
1091
- }
1092
- end
1093
- end
1094
- end
1095
-
1096
- def test_simple_spreadsheet_find_by_condition
1097
- with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
1098
- oo.header_line = 3
1099
- # oo.date_format = '%m/%d/%Y' if oo.class == Google
1100
- erg = oo.find(:all, :conditions => {'Comment' => 'Task 1'})
1101
- assert_equal Date.new(2007,05,07), erg[1]['Date']
1102
- assert_equal 10.75 , erg[1]['Start time']
1103
- assert_equal 12.50 , erg[1]['End time']
1104
- assert_equal 0 , erg[1]['Pause']
1105
- assert_equal 1.75 , erg[1]['Sum'] unless oo.class == Excel
1106
- assert_equal "Task 1" , erg[1]['Comment']
1107
- end
1108
- end
1109
-
1110
- # Ruby-spreadsheet now allows us to at least give the current value
1111
- # from a cell with a formula (no possible with parseexcel)
1112
- def test_bug_false_borders_with_formulas
1113
- with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1114
- assert_equal 1, oo.first_row
1115
- assert_equal 3, oo.last_row
1116
- assert_equal 1, oo.first_column
1117
- assert_equal 4, oo.last_column
1118
- end
1119
- end
1120
-
1121
- # We'ce added minimal formula support so we can now read these
1122
- # though not sure how the spreadsheet reports older values....
1123
- def test_fe
1124
- with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1125
- assert_equal Date.new(2007,11,1), oo.cell('a',1)
1126
- #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('a',1)
1127
- #DOES NOT WORK IN EXCEL FILES: assert_equal '=TODAY()', oo.formula('a',1)
1128
-
1129
- assert_equal Date.new(2008,2,9), oo.cell('B',1)
1130
- #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('B',1)
1131
- #DOES NOT WORK IN EXCEL FILES: assert_equal "=A1+100", oo.formula('B',1)
1132
-
1133
- assert_kind_of DateTime, oo.cell('C',1)
1134
- #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('C',1)
1135
- #DOES NOT WORK IN EXCEL FILES: assert_equal "=C1", oo.formula('C',1)
1136
-
1137
- assert_equal 'H1', oo.cell('A',2)
1138
- assert_equal 'H2', oo.cell('B',2)
1139
- assert_equal 'H3', oo.cell('C',2)
1140
- assert_equal 'H4', oo.cell('D',2)
1141
- assert_equal 'R1', oo.cell('A',3)
1142
- assert_equal 'R2', oo.cell('B',3)
1143
- assert_equal 'R3', oo.cell('C',3)
1144
- assert_equal 'R4', oo.cell('D',3)
1145
- end
1146
- end
1147
-
1148
- def test_excel_does_not_support_formulas
1149
- with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1150
- assert_raise(RuntimeError) { void = oo.formula('a',1) }
1151
- assert_raise(RuntimeError) { void = oo.formula?('a',1) }
1152
- assert_raise(RuntimeError) { void = oo.formulas(oo.sheets.first) }
1153
- end
1154
- end
1155
-
1156
- def get_extension(oo)
1157
- case oo
1158
- when Openoffice
1159
- ".ods"
1160
- when Excel
1161
- ".xls"
1162
- when Excelx
1163
- ".xlsx"
1164
- when Google
1165
- ""
1166
- end
1167
- end
1168
-
1169
- def test_info
1170
- expected_templ = "File: numbers1%s\n"+
1171
- "Number of sheets: 5\n"+
1172
- "Sheets: Tabelle1, Name of Sheet 2, Sheet3, Sheet4, Sheet5\n"+
1173
- "Sheet 1:\n"+
1174
- " First row: 1\n"+
1175
- " Last row: 18\n"+
1176
- " First column: A\n"+
1177
- " Last column: G\n"+
1178
- "Sheet 2:\n"+
1179
- " First row: 5\n"+
1180
- " Last row: 14\n"+
1181
- " First column: B\n"+
1182
- " Last column: E\n"+
1183
- "Sheet 3:\n"+
1184
- " First row: 1\n"+
1185
- " Last row: 1\n"+
1186
- " First column: A\n"+
1187
- " Last column: BA\n"+
1188
- "Sheet 4:\n"+
1189
- " First row: 1\n"+
1190
- " Last row: 1\n"+
1191
- " First column: A\n"+
1192
- " Last column: E\n"+
1193
- "Sheet 5:\n"+
1194
- " First row: 1\n"+
1195
- " Last row: 6\n"+
1196
- " First column: A\n"+
1197
- " Last column: E"
1198
- with_each_spreadsheet(:name=>'numbers1') do |oo|
1199
- ext = get_extension(oo)
1200
- expected = sprintf(expected_templ,ext)
1201
- begin
1202
- if oo.class == Google
1203
- assert_equal expected.gsub(/numbers1/,key_of("numbers1")), oo.info
1204
- else
1205
- assert_equal expected, oo.info
1206
- end
1207
- rescue NameError
1208
- #
1209
- end
1210
- end
1211
- end
1212
-
1213
- def test_bug_excel_numbers1_sheet5_last_row
1214
- with_each_spreadsheet(:name=>'numbers1', :format=>:excel) do |oo|
1215
- oo.default_sheet = "Tabelle1"
1216
- assert_equal 1, oo.first_row
1217
- assert_equal 18, oo.last_row
1218
- assert_equal Openoffice.letter_to_number('A'), oo.first_column
1219
- assert_equal Openoffice.letter_to_number('G'), oo.last_column
1220
- oo.default_sheet = "Name of Sheet 2"
1221
- assert_equal 5, oo.first_row
1222
- assert_equal 14, oo.last_row
1223
- assert_equal Openoffice.letter_to_number('B'), oo.first_column
1224
- assert_equal Openoffice.letter_to_number('E'), oo.last_column
1225
- oo.default_sheet = "Sheet3"
1226
- assert_equal 1, oo.first_row
1227
- assert_equal 1, oo.last_row
1228
- assert_equal Openoffice.letter_to_number('A'), oo.first_column
1229
- assert_equal Openoffice.letter_to_number('BA'), oo.last_column
1230
- oo.default_sheet = "Sheet4"
1231
- assert_equal 1, oo.first_row
1232
- assert_equal 1, oo.last_row
1233
- assert_equal Openoffice.letter_to_number('A'), oo.first_column
1234
- assert_equal Openoffice.letter_to_number('E'), oo.last_column
1235
- oo.default_sheet = "Sheet5"
1236
- assert_equal 1, oo.first_row
1237
- assert_equal 6, oo.last_row
1238
- assert_equal Openoffice.letter_to_number('A'), oo.first_column
1239
- assert_equal Openoffice.letter_to_number('E'), oo.last_column
1240
- end
1241
- end
1242
-
1243
- def test_should_raise_file_not_found_error
1244
- if OPENOFFICE
1245
- assert_raise(IOError) {
1246
- oo = Openoffice.new(File.join('testnichtvorhanden','Bibelbund.ods'))
1247
- }
1248
- end
1249
- if EXCEL
1250
- assert_raise(IOError) {
1251
- oo = Excel.new(File.join('testnichtvorhanden','Bibelbund.xls'))
1252
- }
1253
- end
1254
- if EXCELX
1255
- assert_raise(IOError) {
1256
- oo = Excelx.new(File.join('testnichtvorhanden','Bibelbund.xlsx'))
1257
- }
1258
- end
1259
- if GOOGLE
1260
- # assert_raise(Net::HTTPServerException) {
1261
- # oo = Google.new(key_of('testnichtvorhanden'+'Bibelbund.ods'))
1262
- # oo = Google.new('testnichtvorhanden')
1263
- # }
1264
- end
1265
- end
1266
-
1267
- def test_write_google
1268
- after Date.new(2010,3,10) do
1269
- # write.me: http://spreadsheets.google.com/ccc?key=ptu6bbahNZpY0N0RrxQbWdw&hl=en_GB
1270
- with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1271
- oo.default_sheet = oo.sheets.first
1272
- oo.set_value(1,1,"hello from the tests")
1273
- assert_equal "hello from the tests", oo.cell(1,1)
1274
- oo.set_value(1,1, 1.0)
1275
- assert_equal 1.0, oo.cell(1,1)
1276
- end
1277
- end
1278
- end
1279
-
1280
- def test_bug_set_value_with_more_than_one_sheet_google
1281
- # write.me: http://spreadsheets.google.com/ccc?key=ptu6bbahNZpY0N0RrxQbWdw&hl=en_GB
1282
- with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1283
- content1 = 'AAA'
1284
- content2 = 'BBB'
1285
- oo.default_sheet = oo.sheets.first
1286
- oo.set_value(1,1,content1)
1287
- oo.default_sheet = oo.sheets[1]
1288
- oo.set_value(1,1,content2) # in the second sheet
1289
- oo.default_sheet = oo.sheets.first
1290
- assert_equal content1, oo.cell(1,1)
1291
- oo.default_sheet = oo.sheets[1]
1292
- assert_equal content2, oo.cell(1,1)
1293
- end
1294
- end
1295
-
1296
- def test_set_value_with_sheet_argument_google
1297
- with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1298
- random_row = rand(10)+1
1299
- random_column = rand(10)+1
1300
- content1 = 'ABC'
1301
- content2 = 'DEF'
1302
- oo.set_value(random_row,random_column,content1,oo.sheets.first)
1303
- oo.set_value(random_row,random_column,content2,oo.sheets[1])
1304
- assert_equal content1, oo.cell(random_row,random_column,oo.sheets.first)
1305
- assert_equal content2, oo.cell(random_row,random_column,oo.sheets[1])
1306
- end
1307
- end
1308
-
1309
- def test_set_value_for_non_existing_sheet_google
1310
- with_each_spreadsheet(:name=>'ptu6bbahNZpY0N0RrxQbWdw', :format=>:google) do |oo|
1311
- assert_raise(RangeError) { oo.set_value(1,1,"dummy","no_sheet") }
1312
- end
1313
- end
1314
-
1315
- def test_bug_bbu
1316
- with_each_spreadsheet(:name=>'bbu', :format=>[:openoffice, :excelx, :excel]) do |oo|
1317
- assert_nothing_raised() {
1318
- assert_equal "File: bbu#{get_extension(oo)}
1319
- Number of sheets: 3
1320
- Sheets: 2007_12, Tabelle2, Tabelle3
1321
- Sheet 1:
1322
- First row: 1
1323
- Last row: 4
1324
- First column: A
1325
- Last column: F
1326
- Sheet 2:
1327
- - empty -
1328
- Sheet 3:
1329
- - empty -", oo.info
1330
- }
1331
-
1332
- oo.default_sheet = oo.sheets[1] # empty sheet
1333
- assert_nil oo.first_row
1334
- assert_nil oo.last_row
1335
- assert_nil oo.first_column
1336
- assert_nil oo.last_column
1337
- end
1338
- end
1339
-
1340
-
1341
- def test_bug_time_nil
1342
- with_each_spreadsheet(:name=>'time-test') do |oo|
1343
- assert_equal 12*3600+13*60+14, oo.cell('B',1) # 12:13:14 (secs since midnight)
1344
- assert_equal :time, oo.celltype('B',1)
1345
- assert_equal 15*3600+16*60, oo.cell('C',1) # 15:16 (secs since midnight)
1346
- assert_equal :time, oo.celltype('C',1)
1347
- assert_equal 23*3600, oo.cell('D',1) # 23:00 (secs since midnight)
1348
- assert_equal :time, oo.celltype('D',1)
1349
- end
1350
- end
1351
-
1352
- def test_date_time_to_csv
1353
- with_each_spreadsheet(:name=>'time-test') do |oo|
1354
- begin
1355
- assert oo.to_csv("/tmp/time-test.csv")
1356
- assert File.exists?("/tmp/time-test.csv")
1357
- assert_equal "", `diff #{TESTDIR}/time-test.csv /tmp/time-test.csv`
1358
- ensure
1359
- File.delete_if_exist("/tmp/time-test.csv")
1360
- end
1361
- end
1362
- end
1363
-
1364
- def test_date_time_yaml
1365
- with_each_spreadsheet(:name=>'time-test') do |oo|
1366
- expected =
1367
- "--- \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"
1368
- assert_equal expected, oo.to_yaml
1369
- end
1370
- end
1371
-
1372
- def test_no_remaining_tmp_files_openoffice
1373
- if OPENOFFICE
1374
- assert_raise(Zip::ZipError) { #TODO: besseres Fehlerkriterium bei
1375
- # oo = Openoffice.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1376
- # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1377
- oo = Openoffice.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1378
- false,
1379
- :ignore)
1380
- }
1381
- a=Dir.glob("oo_*")
1382
- assert_equal [], a
1383
- end
1384
- end
1385
-
1386
- def test_no_remaining_tmp_files_excel
1387
- if EXCEL
1388
- assert_raise(Ole::Storage::FormatError) {
1389
- # oo = Excel.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1390
- # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1391
- oo = Excel.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1392
- false,
1393
- :ignore)
1394
- }
1395
- a=Dir.glob("oo_*")
1396
- assert_equal [], a
1397
- end
1398
- end
1399
-
1400
- def test_no_remaining_tmp_files_excelx
1401
- if EXCELX
1402
- assert_raise(Zip::ZipError) { #TODO: besseres Fehlerkriterium bei
1403
-
1404
- # oo = Excelx.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1405
- # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1406
- oo = Excelx.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1407
- false,
1408
- :ignore)
1409
-
1410
- }
1411
- a=Dir.glob("oo_*")
1412
- assert_equal [], a
1413
- end
1414
- end
1415
-
1416
- def test_no_remaining_tmp_files_google
1417
- after Date.new(2010,4,1) do
1418
- # Exception ist irgendwie anders, nochmal ansehen TODO:
1419
- if GOOGLE
1420
- assert_nothing_raised() {
1421
- oo = Google.new(key_of("no_spreadsheet_file.txt"))
1422
- }
1423
- a=Dir.glob("oo_*")
1424
- assert_equal [], a
1425
- end
1426
- end
1427
- end
1428
-
1429
- # Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
1430
- # Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
1431
- # nicht immer in der gleichen Reihenfolge erzeugt.
1432
- def init_all_cells(oo,sheet)
1433
- all = []
1434
- oo.first_row(sheet).upto(oo.last_row(sheet)) do |row|
1435
- oo.first_column(sheet).upto(oo.last_column(sheet)) do |col|
1436
- unless oo.empty?(row,col,sheet)
1437
- all << {:row => row.to_s,
1438
- :column => col.to_s,
1439
- :content => oo.cell(row,col,sheet).to_s,
1440
- :type => oo.celltype(row,col,sheet).to_s,
1441
- }
1442
- end
1443
- end
1444
- end
1445
- all
1446
- end
1447
-
1448
- def test_to_xml
1449
- after Date.new(2009,10,1) do
1450
- with_each_spreadsheet(:name=>'numbers1', :encoding => 'utf8') do |oo|
1451
- assert_nothing_raised {oo.to_xml}
1452
- sheetname = oo.sheets.first
1453
- # doc = XML::Parser.string(oo.to_xml).parse
1454
- doc = Nokogiri::XML(oo.to_xml)
1455
- # doc.root.each_element {|xml_sheet|
1456
- doc.root.each {|xml_sheet|
1457
- all_cells = init_all_cells(oo, sheetname)
1458
- x = 0
1459
- assert_equal sheetname, xml_sheet.attributes['name']
1460
- xml_sheet.each_element {|cell|
1461
- expected = [all_cells[x][:row],
1462
- all_cells[x][:column],
1463
- all_cells[x][:content],
1464
- all_cells[x][:type],
1465
- ]
1466
- result = [
1467
- cell.attributes['row'],
1468
- cell.attributes['column'],
1469
- cell.content,
1470
- cell.attributes['type'],
1471
- ]
1472
- assert_equal expected, result
1473
- x += 1
1474
- } # end of sheet
1475
- sheetname = oo.sheets[oo.sheets.index(sheetname)+1]
1476
- }
1477
- end
1478
- end
1479
- end
1480
-
1481
- def test_bug_row_column_fixnum_float
1482
- with_each_spreadsheet(:name=>'bug-row-column-fixnum-float', :format=>:excel) do |oo|
1483
- assert_equal 42.5, oo.cell('b',2)
1484
- assert_equal 43 , oo.cell('c',2)
1485
- assert_equal ['hij',42.5, 43], oo.row(2)
1486
- assert_equal ['def',42.5, 'nop'], oo.column(2)
1487
- end
1488
- end
1489
-
1490
- def test_file_warning_default
1491
- if OPENOFFICE
1492
- assert_raises(TypeError) { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls")) }
1493
- assert_raises(TypeError) { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx")) }
1494
- assert_equal [], Dir.glob("oo_*")
1495
- end
1496
- if EXCEL
1497
- assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.ods")) }
1498
- assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx")) }
1499
- assert_equal [], Dir.glob("oo_*")
1500
- end
1501
- if EXCELX
1502
- assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.ods")) }
1503
- assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.xls")) }
1504
- assert_equal [], Dir.glob("oo_*")
1505
- end
1506
- end
1507
-
1508
- def test_file_warning_error
1509
- if OPENOFFICE
1510
- assert_raises(TypeError) { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false,:error) }
1511
- assert_raises(TypeError) { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false,:error) }
1512
- assert_equal [], Dir.glob("oo_*")
1513
- end
1514
- if EXCEL
1515
- assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.ods"),false,:error) }
1516
- assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false,:error) }
1517
- assert_equal [], Dir.glob("oo_*")
1518
- end
1519
- if EXCELX
1520
- assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.ods"),false,:error) }
1521
- assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.xls"),false,:error) }
1522
- assert_equal [], Dir.glob("oo_*")
1523
- end
1524
- end
1525
-
1526
- def test_file_warning_warning
1527
- if OPENOFFICE
1528
- assert_nothing_raised(TypeError) {
1529
- assert_raises(Zip::ZipError) {
1530
- oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false, :warning)
1531
- }
1532
- }
1533
- assert_nothing_raised(TypeError) {
1534
- assert_raises(Errno::ENOENT) {
1535
- oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false, :warning)
1536
- }
1537
- }
1538
- assert_equal [], Dir.glob("oo_*")
1539
- end
1540
- if EXCEL
1541
- assert_nothing_raised(TypeError) {
1542
- assert_raises(Ole::Storage::FormatError) {
1543
- oo = Excel.new(File.join(TESTDIR,"numbers1.ods"),false, :warning) }
1544
- }
1545
- assert_nothing_raised(TypeError) {
1546
- assert_raises(Ole::Storage::FormatError) {
1547
- oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false, :warning) }
1548
- }
1549
- assert_equal [], Dir.glob("oo_*")
1550
- end
1551
- if EXCELX
1552
- assert_nothing_raised(TypeError) {
1553
- assert_raises(Errno::ENOENT) {
1554
- oo = Excelx.new(File.join(TESTDIR,"numbers1.ods"),false, :warning) }
1555
- }
1556
- assert_nothing_raised(TypeError) {
1557
- assert_raises(Zip::ZipError) {
1558
- oo = Excelx.new(File.join(TESTDIR,"numbers1.xls"),false, :warning) }
1559
- }
1560
- assert_equal [], Dir.glob("oo_*")
1561
- end
1562
- end
1563
-
1564
- def test_file_warning_ignore
1565
- if OPENOFFICE
1566
- assert_nothing_raised(TypeError) {
1567
- assert_raises(Zip::ZipError) {
1568
- oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false, :ignore) }
1569
- }
1570
- assert_nothing_raised(TypeError) {
1571
- assert_raises(Errno::ENOENT) {
1572
- oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false, :ignore) }
1573
- }
1574
- assert_equal [], Dir.glob("oo_*")
1575
- end
1576
- if EXCEL
1577
- assert_nothing_raised(TypeError) {
1578
- assert_raises(Ole::Storage::FormatError) {
1579
- oo = Excel.new(File.join(TESTDIR,"numbers1.ods"),false, :ignore) }
1580
- }
1581
- assert_nothing_raised(TypeError) {
1582
- assert_raises(Ole::Storage::FormatError) {oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false, :ignore) }}
1583
- assert_equal [], Dir.glob("oo_*")
1584
- end
1585
- if EXCELX
1586
- assert_nothing_raised(TypeError) {
1587
- assert_raises(Errno::ENOENT) {
1588
- oo = Excelx.new(File.join(TESTDIR,"numbers1.ods"),false, :ignore)
1589
- }
1590
- }
1591
- assert_nothing_raised(TypeError) {
1592
- assert_raises(Zip::ZipError) {
1593
- oo = Excelx.new(File.join(TESTDIR,"numbers1.xls"),false, :ignore)
1594
- }
1595
- }
1596
- assert_equal [], Dir.glob("oo_*")
1597
- end
1598
- end
1599
-
1600
- def test_bug_last_row_excel
1601
- with_each_spreadsheet(:name=>'time-test', :format=>:excel) do |oo|
1602
- assert_equal 2, oo.last_row
1603
- end
1604
- end
1605
-
1606
- def test_bug_to_xml_with_empty_sheets
1607
- with_each_spreadsheet(:name=>'emptysheets', :format=>[:openoffice, :excel]) do |oo|
1608
- oo.sheets.each { |sheet|
1609
- assert_equal nil, oo.first_row, "first_row not nil in sheet #{sheet}"
1610
- assert_equal nil, oo.last_row, "last_row not nil in sheet #{sheet}"
1611
- assert_equal nil, oo.first_column, "first_column not nil in sheet #{sheet}"
1612
- assert_equal nil, oo.last_column, "last_column not nil in sheet #{sheet}"
1613
- assert_equal nil, oo.first_row(sheet), "first_row not nil in sheet #{sheet}"
1614
- assert_equal nil, oo.last_row(sheet), "last_row not nil in sheet #{sheet}"
1615
- assert_equal nil, oo.first_column(sheet), "first_column not nil in sheet #{sheet}"
1616
- assert_equal nil, oo.last_column(sheet), "last_column not nil in sheet #{sheet}"
1617
- }
1618
- assert_nothing_raised() { result = oo.to_xml }
1619
- end
1620
- end
1621
-
1622
- def test_bug_simple_spreadsheet_time_bug
1623
- # really a bug? are cells really of type time?
1624
- # No! :float must be the correct type
1625
- with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1626
- # puts oo.cell('B',5).to_s
1627
- # assert_equal :time, oo.celltype('B',5)
1628
- assert_equal :float, oo.celltype('B',5)
1629
- assert_equal 10.75, oo.cell('B',5)
1630
- assert_equal 12.50, oo.cell('C',5)
1631
- assert_equal 0, oo.cell('D',5)
1632
- assert_equal 1.75, oo.cell('E',5)
1633
- assert_equal 'Task 1', oo.cell('F',5)
1634
- assert_equal Date.new(2007,5,7), oo.cell('A',5)
1635
- end
1636
- end
1637
-
1638
- def test_simple2_excelx
1639
- after Date.new(2009,10,15) do
1640
- with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1641
- assert_equal [:numeric_or_formula, "yyyy\\-mm\\-dd"], oo.excelx_type('A',4)
1642
- assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('B',4)
1643
- assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('c',4)
1644
- assert_equal [:numeric_or_formula, "General"], oo.excelx_type('d',4)
1645
- assert_equal [:numeric_or_formula, "General"], oo.excelx_type('e',4)
1646
- assert_equal :string, oo.excelx_type('f',4)
1647
-
1648
- assert_equal "39209", oo.excelx_value('a',4)
1649
- assert_equal "yyyy\\-mm\\-dd", oo.excelx_format('a',4)
1650
- assert_equal "9.25", oo.excelx_value('b',4)
1651
- assert_equal "10.25", oo.excelx_value('c',4)
1652
- assert_equal "0", oo.excelx_value('d',4)
1653
- #... Sum-Spalte
1654
- # assert_equal "Task 1", oo.excelx_value('f',4)
1655
- assert_equal "Task 1", oo.cell('f',4)
1656
- assert_equal Date.new(2007,05,07), oo.cell('a',4)
1657
- assert_equal "9.25", oo.excelx_value('b',4)
1658
- assert_equal "#,##0.00", oo.excelx_format('b',4)
1659
- assert_equal 9.25, oo.cell('b',4)
1660
- assert_equal :float, oo.celltype('b',4)
1661
- assert_equal :float, oo.celltype('d',4)
1662
- assert_equal 0, oo.cell('d',4)
1663
- assert_equal :formula, oo.celltype('e',4)
1664
- assert_equal 1, oo.cell('e',4)
1665
- assert_equal 'C4-B4-D4', oo.formula('e',4)
1666
- assert_equal :string, oo.celltype('f',4)
1667
- assert_equal "Task 1", oo.cell('f',4)
1668
- end
1669
- end
1670
- end
1671
-
1672
- def test_datetime
1673
- with_each_spreadsheet(:name=>'datetime') do |oo|
1674
- val = oo.cell('c',3)
1675
- assert_kind_of DateTime, val
1676
- assert_equal :datetime, oo.celltype('c',3)
1677
- assert_equal DateTime.new(1961,11,21,12,17,18), val
1678
- val = oo.cell('a',1)
1679
- assert_kind_of Date, val
1680
- assert_equal :date, oo.celltype('a',1)
1681
- assert_equal Date.new(1961,11,21), val
1682
- assert_equal Date.new(1961,11,21), oo.cell('a',1)
1683
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',3)
1684
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',3)
1685
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',3)
1686
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',4)
1687
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',4)
1688
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',4)
1689
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',5)
1690
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',5)
1691
- assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',5)
1692
- assert_equal Date.new(1961,11,21), oo.cell('a',6)
1693
- assert_equal Date.new(1961,11,21), oo.cell('b',6)
1694
- assert_equal Date.new(1961,11,21), oo.cell('c',6)
1695
- assert_equal Date.new(1961,11,21), oo.cell('a',7)
1696
- assert_equal Date.new(1961,11,21), oo.cell('b',7)
1697
- assert_equal Date.new(1961,11,21), oo.cell('c',7)
1698
- end
1699
- end
1700
-
1701
- def test_cell_openoffice_html_escape
1702
- with_each_spreadsheet(:name=>'html-escape', :format=>:openoffice) do |oo|
1703
- assert_equal "'", oo.cell(1,1)
1704
- assert_equal "&", oo.cell(2,1)
1705
- assert_equal ">", oo.cell(3,1)
1706
- assert_equal "<", oo.cell(4,1)
1707
- assert_equal "`", oo.cell(5,1)
1708
- # test_openoffice_zipped will catch issues with &quot;
1709
- end
1710
- end
1711
-
1712
- def test_cell_boolean
1713
- with_each_spreadsheet(:name=>'boolean', :format=>[:openoffice, :excel, :excelx]) do |oo|
1714
- if oo.class == Excelx
1715
- assert_equal "TRUE", oo.cell(1,1), "failure in "+oo.class.to_s
1716
- assert_equal "FALSE", oo.cell(2,1), "failure in "+oo.class.to_s
1717
- else
1718
- assert_equal "true", oo.cell(1,1), "failure in "+oo.class.to_s
1719
- assert_equal "false", oo.cell(2,1), "failure in "+oo.class.to_s
1720
- end
1721
- end
1722
- end
1723
-
1724
- def test_cell_multiline
1725
- with_each_spreadsheet(:name=>'paragraph', :format=>[:openoffice, :excel, :excelx]) do |oo|
1726
- assert_equal "This is a test\nof a multiline\nCell", oo.cell(1,1)
1727
- assert_equal "This is a test\n¶\nof a multiline\n\nCell", oo.cell(1,2)
1728
- assert_equal "first p\n\nsecond p\n\nlast p", oo.cell(2,1)
1729
- end
1730
- end
1731
-
1732
- def test_cell_styles
1733
- # styles only valid in excel spreadsheets?
1734
- # TODO: what todo with other spreadsheet types
1735
- with_each_spreadsheet(:name=>'style', :format=>[# :openoffice,
1736
- :excel,
1737
- # :excelx
1738
- ]) do |oo|
1739
- # bold
1740
- assert_equal true, oo.font(1,1).bold?
1741
- assert_equal false, oo.font(1,1).italic?
1742
- assert_equal false, oo.font(1,1).underline?
1743
-
1744
- # italic
1745
- assert_equal false, oo.font(2,1).bold?
1746
- assert_equal true, oo.font(2,1).italic?
1747
- assert_equal false, oo.font(2,1).underline?
1748
-
1749
- # normal
1750
- assert_equal false, oo.font(3,1).bold?
1751
- assert_equal false, oo.font(3,1).italic?
1752
- assert_equal false, oo.font(3,1).underline?
1753
-
1754
- # underline
1755
- assert_equal false, oo.font(4,1).bold?
1756
- assert_equal false, oo.font(4,1).italic?
1757
- assert_equal true, oo.font(4,1).underline?
1758
-
1759
- # bold italic
1760
- assert_equal true, oo.font(5,1).bold?
1761
- assert_equal true, oo.font(5,1).italic?
1762
- assert_equal false, oo.font(5,1).underline?
1763
-
1764
- # bold underline
1765
- assert_equal true, oo.font(6,1).bold?
1766
- assert_equal false, oo.font(6,1).italic?
1767
- assert_equal true, oo.font(6,1).underline?
1768
-
1769
- # italic underline
1770
- assert_equal false, oo.font(7,1).bold?
1771
- assert_equal true, oo.font(7,1).italic?
1772
- assert_equal true, oo.font(7,1).underline?
1773
-
1774
- # bolded row
1775
- assert_equal true, oo.font(8,1).bold?
1776
- assert_equal false, oo.font(8,1).italic?
1777
- assert_equal false, oo.font(8,1).underline?
1778
-
1779
- # bolded col
1780
- assert_equal true, oo.font(9,2).bold?
1781
- assert_equal false, oo.font(9,2).italic?
1782
- assert_equal false, oo.font(9,2).underline?
1783
-
1784
- # bolded row, italic col
1785
- assert_equal true, oo.font(10,3).bold?
1786
- assert_equal true, oo.font(10,3).italic?
1787
- assert_equal false, oo.font(10,3).underline?
1788
-
1789
- # normal
1790
- assert_equal false, oo.font(11,4).bold?
1791
- assert_equal false, oo.font(11,4).italic?
1792
- assert_equal false, oo.font(11,4).underline?
1793
- end
1794
- end
1795
-
1796
- # If a cell has a date-like string but is preceeded by a '
1797
- # to force that date to be treated like a string, we were getting an exception.
1798
- # This test just checks for that exception to make sure it's not raised in this case
1799
- def test_date_to_float_conversion
1800
- with_each_spreadsheet(:name=>'datetime_floatconv', :format=>:excel) do |oo|
1801
- assert_nothing_raised(NoMethodError) do
1802
- oo.cell('a',1)
1803
- oo.cell('a',2)
1804
- end
1805
- end
1806
- end
1807
-
1808
- # Need to extend to other formats
1809
- def test_row_whitespace
1810
- # auf dieses Dokument habe ich keinen Zugriff TODO:
1811
- after Date.new(2010,4,1) do
1812
- with_each_spreadsheet(:name=>'whitespace') do |oo|
1813
- oo.default_sheet = "Sheet1"
1814
- assert_equal [nil, nil, nil, nil, nil, nil], oo.row(1)
1815
- assert_equal [nil, nil, nil, nil, nil, nil], oo.row(2)
1816
- assert_equal ["Date", "Start time", "End time", "Pause", "Sum", "Comment"], oo.row(3)
1817
- assert_equal [Date.new(2007,5,7), 9.25, 10.25, 0.0, 1.0, "Task 1"], oo.row(4)
1818
- assert_equal [nil, nil, nil, nil, nil, nil], oo.row(5)
1819
- assert_equal [Date.new(2007,5,7), 10.75, 10.75, 0.0, 0.0, "Task 1"], oo.row(6)
1820
- oo.default_sheet = "Sheet2"
1821
- assert_equal ["Date", nil, "Start time"], oo.row(1)
1822
- assert_equal [Date.new(2007,5,7), nil, 9.25], oo.row(2)
1823
- assert_equal [Date.new(2007,5,7), nil, 10.75], oo.row(3)
1824
- end
1825
- end
1826
- end
1827
-
1828
- def test_col_whitespace
1829
- after Date.new(2010,2,20) do
1830
- #TODO:
1831
- # kein Zugriff auf Dokument whitespace
1832
- with_each_spreadsheet(:name=>'whitespace') do |oo|
1833
- oo.default_sheet = "Sheet1"
1834
- assert_equal ["Date", Date.new(2007,5,7), nil, Date.new(2007,5,7)], oo.column(1)
1835
- assert_equal ["Start time", 9.25, nil, 10.75], oo.column(2)
1836
- assert_equal ["End time", 10.25, nil, 10.75], oo.column(3)
1837
- assert_equal ["Pause", 0.0, nil, 0.0], oo.column(4)
1838
- assert_equal ["Sum", 1.0, nil, 0.0], oo.column(5)
1839
- assert_equal ["Comment","Task 1", nil, "Task 1"], oo.column(6)
1840
- oo.default_sheet = "Sheet2"
1841
- assert_equal [nil, nil, nil], oo.column(1)
1842
- assert_equal [nil, nil, nil], oo.column(2)
1843
- assert_equal ["Date", Date.new(2007,5,7), Date.new(2007,5,7)], oo.column(3)
1844
- assert_equal [nil, nil, nil], oo.column(4)
1845
- assert_equal [ "Start time", 9.25, 10.75], oo.column(5)
1846
- end
1847
- end
1848
- end
1849
-
1850
- # Excel has two base date formats one from 1900 and the other from 1904.
1851
- # There's a MS bug that 1900 base dates include an extra day due to erroneously
1852
- # including 1900 as a leap yar.
1853
- def test_base_dates_in_excel
1854
- with_each_spreadsheet(:name=>'1900_base', :format=>:excel) do |oo|
1855
- assert_equal Date.new(2009,06,15), oo.cell(1,1)
1856
- #we don't want to to 'interpret' formulas assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
1857
- # if we test TODAY() we have also have to calculate
1858
- # other date calculations
1859
- #
1860
- assert_equal :date, oo.celltype(1,1)
1861
- end
1862
- with_each_spreadsheet(:name=>'1904_base', :format=>:excel) do |oo|
1863
- assert_equal Date.new(2009,06,15), oo.cell(1,1)
1864
- # see comment above
1865
- # assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
1866
- assert_equal :date, oo.celltype(1,1)
1867
- end
1868
- end
1869
-
1870
- def test_bad_date
1871
- with_each_spreadsheet(:name=>'prova', :format=>:excel) do |oo|
1872
- assert_nothing_raised(ArgumentError) {
1873
- assert_equal DateTime.new(2006,2,2,10,0,0), oo.cell('a',1)
1874
- }
1875
- end # each
1876
- end
1877
-
1878
- def test_cell_methods
1879
- after Date.new(2010,3,30) do
1880
- with_each_spreadsheet(:name=>'numbers1') do |oo|
1881
- assert_equal 10, oo.a4 # cell(4,'A')
1882
- assert_equal 11, oo.b4 # cell(4,'B')
1883
- assert_equal 12, oo.c4 # cell(4,'C')
1884
- assert_equal 13, oo.d4 # cell(4,'D')
1885
- assert_equal 14, oo.e4 # cell(4,'E')
1886
- assert_equal 'ABC', oo.c6('Sheet5')
1887
-
1888
- assert_raises(ArgumentError) {
1889
- # a42a is not a valid cell name, should raise ArgumentError
1890
- assert_equal 9999, oo.a42a
1891
- }
1892
- end
1893
- end
1894
- end
1895
-
1896
-
1897
- # compare large spreadsheets
1898
- def test_compare_large_spreadsheets
1899
- after Date.new(2010,11,1) do
1900
- # problematisch, weil Formeln in Excel nicht unterstützt werden
1901
- if LONG_RUN
1902
- qq = Openoffice.new(File.join('test',"Bibelbund.ods"))
1903
- with_each_spreadsheet(:name=>'Bibelbund') do |oo|
1904
- # p "comparing Bibelbund.ods with #{oo.class}"
1905
- oo.sheets.each do |sh|
1906
- oo.first_row.upto(oo.last_row) do |row|
1907
- oo.first_column.upto(oo.last_column) do |col|
1908
- c1 = qq.cell(row,col,sh)
1909
- c1.force_encoding("UTF-8") if c1.class == String
1910
- c2 = oo.cell(row,col,sh)
1911
- c2.force_encoding("UTF-8") if c2.class == String
1912
- assert_equal c1, c2, "diff in #{sh}/#{row}/#{col}}"
1913
- assert_equal qq.celltype(row,col,sh), oo.celltype(row,col,sh)
1914
- assert_equal qq.formula?(row,col,sh), oo.formula?(row,col,sh)
1915
- end
1916
- end
1917
- end
1918
- end
1919
- end # LONG_RUN
1920
- end
1921
- end
1922
-
1923
- def test_labeled_cells
1924
- after Date.new(2010,3,25) do
1925
- # TODO: more spreadsheet types
1926
- with_each_spreadsheet(:name=>'named_cells', :format=>:openoffice) do |oo|
1927
- oo.default_sheet = oo.sheets.first
1928
- begin
1929
- row,col = oo.label('anton')
1930
- rescue ArgumentError
1931
- puts "labels error at #{oo.class}"
1932
- raise
1933
- end
1934
- assert_equal 5, row
1935
- assert_equal 3, col
1936
-
1937
- row,col = oo.label('anton')
1938
- assert_equal 'Anton', oo.cell(row,col)
1939
-
1940
- row,col = oo.label('berta')
1941
- assert_equal 'Bertha', oo.cell(row,col)
1942
-
1943
- row,col = oo.label('caesar')
1944
- assert_equal 'Cäsar', oo.cell(row,col)
1945
-
1946
- # a not defined label:
1947
- row,col = oo.label('never')
1948
- assert_nil row
1949
- assert_nil col
1950
-
1951
- row,col,sheet = oo.label('anton')
1952
- assert_equal 5, row
1953
- assert_equal 3, col
1954
- assert_equal "Sheet1", sheet
1955
- #
1956
- # assert_equal "Anton", oo.label('anton')
1957
- after Date.new(2009,12,12) do
1958
- assert_equal "Anton", oo.anton
1959
- end
1960
- end
1961
- end
1962
- end
1963
-
1964
-
1965
- def test_bug_excel_last_row_255
1966
- after Date.new(2010,2,20) do
1967
- oo = Excel.new(File.join('test','ScienceStaff.xls'))
1968
- oo.default_sheet = oo.sheets.first
1969
- assert_equal "COMSCI", oo.cell(255,1)
1970
- assert_equal "lala", oo.cell(256,1)
1971
- assert_equal 1537, oo.last_row
1972
- end
1973
- end
1974
-
1975
- def test_bug_excel_last_row_255_modified
1976
- oo = Excel.new(File.join('test','ScienceStaff_modified.xls'))
1977
- oo.default_sheet = oo.sheets.first
1978
- assert_equal 1537, oo.last_row
1979
- end
1980
-
1981
- require 'matrix'
1982
- def test_matrix
1983
- with_each_spreadsheet(:name => 'matrix', :format => [:openoffice, :excel, :google]) do |oo|
1984
- oo.default_sheet = oo.sheets.first
1985
- assert_equal Matrix[
1986
- [1.0, 2.0, 3.0],
1987
- [4.0, 5.0, 6.0],
1988
- [7.0, 8.0, 9.0] ], oo.to_matrix
1989
- end
1990
- end
1991
-
1992
- def test_matrix_selected_range
1993
- with_each_spreadsheet(:name => 'matrix', :format=>[:excel,:openoffice,:google]) do |oo|
1994
- oo.default_sheet = 'Sheet2'
1995
- assert_equal Matrix[
1996
- [1.0, 2.0, 3.0],
1997
- [4.0, 5.0, 6.0],
1998
- [7.0, 8.0, 9.0] ], oo.to_matrix(3,4,5,6)
1999
- end
2000
- end
2001
-
2002
- def test_matrix_all_nil
2003
- with_each_spreadsheet(:name => 'matrix', :format=>[:excel,:openoffice,:google]) do |oo|
2004
- oo.default_sheet = 'Sheet2'
2005
- assert_equal Matrix[
2006
- [nil, nil, nil],
2007
- [nil, nil, nil],
2008
- [nil, nil, nil] ], oo.to_matrix(10,10,12,12)
2009
- end
2010
- end
2011
-
2012
- def test_matrix_values_and_nil
2013
- with_each_spreadsheet(:name => 'matrix', :format=>[:excel,:openoffice,:google]) do |oo|
2014
- oo.default_sheet = 'Sheet3'
2015
- assert_equal Matrix[
2016
- [1.0, nil, 3.0],
2017
- [4.0, 5.0, 6.0],
2018
- [7.0, 8.0, nil] ], oo.to_matrix(1,1,3,3)
2019
- end
2020
- end
2021
-
2022
- 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
+ require 'tmpdir'
13
+ require './lib/roo'
14
+ #TODO
15
+ # Look at formulas in excel - does not work with date/time
16
+
17
+
18
+ # Dump warnings that come from the test to open files
19
+ # with the wrong spreadsheet class
20
+ #STDERR.reopen "/dev/null","w"
21
+
22
+ TESTDIR = File.dirname(__FILE__)
23
+ TMP_PREFIX = 'oo_*'
24
+ # require './' + TESTDIR + '/test_helper.rb'
25
+ require TESTDIR + '/test_helper.rb'
26
+
27
+ #require 'soap/rpc/driver'
28
+ require 'fileutils'
29
+ require 'timeout'
30
+ require 'logger'
31
+ $log = Logger.new(File.join(ENV['HOME'],"roo.log"))
32
+ #$log.level = Logger::WARN
33
+ $log.level = Logger::DEBUG
34
+
35
+ DISPLAY_LOG = false
36
+ DB_LOG = false
37
+ if DB_LOG
38
+ require 'activerecord'
39
+ end
40
+
41
+ include FileUtils
42
+ def running_windows?
43
+ # to do
44
+ # "besser loesen"
45
+ # end
46
+ File.exists? "C:\\"
47
+ end
48
+
49
+ if DB_LOG
50
+ def activerecord_connect
51
+ ActiveRecord::Base.establish_connection(:adapter => "mysql",
52
+ :database => "test_runs",
53
+ :host => "localhost",
54
+ :username => "root",
55
+ :socket => "/var/run/mysqld/mysqld.sock")
56
+ end
57
+
58
+ class Testrun < ActiveRecord::Base
59
+ end
60
+ end
61
+
62
+ class Test::Unit::TestCase
63
+ def key_of(spreadsheetname)
64
+ begin
65
+
66
+ return {
67
+ #'formula' => 'rt4Pw1WmjxFtyfrqqy94wPw',
68
+ 'formula' => 'o10837434939102457526.3022866619437760118',
69
+ #"write.me" => 'r6m7HFlUOwst0RTUTuhQ0Ow',
70
+ "write.me" => '0AkCuGANLc3jFcHR1NmJiYWhOWnBZME4wUnJ4UWJXZHc',
71
+ #'numbers1' => "rYraCzjxTtkxw1NxHJgDU8Q",
72
+ 'numbers1' => 'o10837434939102457526.4784396906364855777',
73
+ #'borders' => "r_nLYMft6uWg_PT9Rc2urXw",
74
+ 'borders' => "o10837434939102457526.664868920231926255",
75
+ #'simple_spreadsheet' => "r3aMMCBCA153TmU_wyIaxfw",
76
+ 'simple_spreadsheet' => "ptu6bbahNZpYe-L1vEBmgGA",
77
+ 'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
78
+ #"only_one_sheet" => "rqRtkcPJ97nhQ0m9ksDw2rA",
79
+ "only_one_sheet" => "o10837434939102457526.762705759906130135",
80
+ #'time-test' => 'r2XfDBJMrLPjmuLrPQQrEYw',
81
+ 'time-test' => 'ptu6bbahNZpYBMhk01UfXSg',
82
+ #'datetime' => "r2kQpXWr6xOSUpw9MyXavYg",
83
+ 'datetime' => "ptu6bbahNZpYQEtZwzL_dZQ",
84
+ 'whitespace' => "rZyQaoFebVGeHKzjG6e9gRQ",
85
+ 'matrix' => '0AkCuGANLc3jFdHY3cWtYUkM4bVdadjZ5VGpfTzFEUEE',
86
+ }[spreadsheetname]
87
+ # 'numbers1' => "o10837434939102457526.4784396906364855777",
88
+ # 'borders' => "o10837434939102457526.664868920231926255",
89
+ # 'simple_spreadsheet' => "ptu6bbahNZpYe-L1vEBmgGA",
90
+ # 'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
91
+ # "only_one_sheet" => "o10837434939102457526.762705759906130135",
92
+ # "write.me" => 'ptu6bbahNZpY0N0RrxQbWdw&hl',
93
+ # 'formula' => 'o10837434939102457526.3022866619437760118',
94
+ # 'time-test' => 'ptu6bbahNZpYBMhk01UfXSg',
95
+ # 'datetime' => "ptu6bbahNZpYQEtZwzL_dZQ",
96
+ rescue
97
+ raise "unknown spreadsheetname: #{spreadsheetname}"
98
+ end
99
+ end
100
+
101
+ if DB_LOG
102
+ if ! (defined?(@connected) and @connected)
103
+ activerecord_connect
104
+ else
105
+ @connected = true
106
+ end
107
+ end
108
+ alias unlogged_run run
109
+ def run(result, &block)
110
+ t1 = Time.now
111
+ #RAILS_DEFAULT_LOGGER.debug "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
112
+ if DISPLAY_LOG
113
+ v1,v2,v3 = RUBY_VERSION.split('.')
114
+ if v1.to_i > 1 or
115
+ (v1.to_i == 1 and v2.to_i > 8)
116
+ # Ruby 1.9.x
117
+ print "RUNNING #{self.class} #{self.__name__} \t#{Time.now.to_s}"
118
+ else
119
+ # Ruby < 1.9.x
120
+ print "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
121
+ end
122
+ STDOUT.flush
123
+ end
124
+ unlogged_run result, &block
125
+ t2 = Time.now
126
+ if DISPLAY_LOG
127
+ puts "\t#{t2-t1} seconds"
128
+ end
129
+ if DB_LOG
130
+ domain = Testrun.create(
131
+ :class_name => self.class.to_s,
132
+ :test_name => @method_name,
133
+ :start => t1,
134
+ :duration => t2-t1
135
+ )
136
+ end
137
+ end
138
+ end
139
+
140
+ class File
141
+ def File.delete_if_exist(filename)
142
+ if File.exist?(filename)
143
+ File.delete(filename)
144
+ end
145
+ end
146
+ end
147
+
148
+ # :nodoc
149
+ class Fixnum
150
+ def minutes
151
+ self * 60
152
+ end
153
+ end
154
+
155
+ class TestRoo < Test::Unit::TestCase
156
+
157
+ OPENOFFICE = true # do Openoffice-Spreadsheet Tests? (.ods files)
158
+ EXCEL = true # do Excel Tests? (.xls files)
159
+ GOOGLE = true # do Google-Spreadsheet Tests?
160
+ EXCELX = true # do Excelx Tests? (.xlsx files)
161
+
162
+ ONLINE = true
163
+ LONG_RUN = true
164
+ GLOBAL_TIMEOUT = 48.minutes
165
+
166
+ def setup
167
+ #if DISPLAY_LOG
168
+ # puts " GLOBAL_TIMEOUT = #{GLOBAL_TIMEOUT}"
169
+ #end
170
+ end
171
+
172
+ def test_internal_minutes
173
+ assert_equal 42*60, 42.minutes
174
+ end
175
+
176
+ # call a block of code for each spreadsheet type
177
+ # and yield a reference to the roo object
178
+ def with_each_spreadsheet(options)
179
+ # test if the spreadsheet type is valid :nodoc
180
+ if options[:format]
181
+ if options[:format].is_a? Symbol
182
+ options[:format] = [options[:format]]
183
+ end
184
+ options[:format].each do |formatname|
185
+ unless [:openoffice,:excel,:excelx,:google].include?(formatname)
186
+ raise "invalid spreadsheet type #{formatname}"
187
+ end
188
+ end
189
+ end
190
+ # end test spreadsheet type :nodoc
191
+ options[:format] ||= [:excel, :excelx, :openoffice, :google]
192
+ options[:format] = [options[:format]] if options[:format].class == Symbol
193
+ yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xls')) if EXCEL && options[:format].include?(:excel)
194
+ yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xlsx')) if EXCELX && options[:format].include?(:excelx)
195
+ yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.ods')) if OPENOFFICE && options[:format].include?(:openoffice)
196
+ yield Roo::Spreadsheet.open(key_of(options[:name]) || options[:name]) if GOOGLE && options[:format].include?(:google)
197
+ end
198
+ # Using Date.strptime so check that it's using the method
199
+ # with the value set in date_format
200
+ def test_date
201
+ with_each_spreadsheet(:name=>'numbers1', :format=>:google) do |oo|
202
+ # should default to DDMMYYYY
203
+ assert oo.date?("21/11/1962")
204
+ assert !oo.date?("11/21/1962")
205
+ oo.date_format = '%m/%d/%Y'
206
+ assert !oo.date?("21/11/1962")
207
+ assert oo.date?("11/21/1962")
208
+ oo.date_format = '%Y-%m-%d'
209
+ assert(oo.date?("1962-11-21"))
210
+ assert(!oo.date?("1962-21-11"))
211
+ end
212
+ end
213
+
214
+ def test_classes
215
+ if OPENOFFICE
216
+ oo = Openoffice.new(File.join(TESTDIR,"numbers1.ods"))
217
+ assert_kind_of Openoffice, oo
218
+ end
219
+ if EXCEL
220
+ oo = Excel.new(File.join(TESTDIR,"numbers1.xls"))
221
+ assert_kind_of Excel, oo
222
+ end
223
+ if GOOGLE
224
+ oo = Google.new(key_of("numbers1"))
225
+ assert_kind_of Google, oo
226
+ end
227
+ if EXCELX
228
+ oo = Excelx.new(File.join(TESTDIR,"numbers1.xlsx"))
229
+ assert_kind_of Excelx, oo
230
+ end
231
+ end
232
+
233
+ def test_letters
234
+ assert_equal 1, GenericSpreadsheet.letter_to_number('A')
235
+ assert_equal 1, GenericSpreadsheet.letter_to_number('a')
236
+ assert_equal 2, GenericSpreadsheet.letter_to_number('B')
237
+ assert_equal 26, GenericSpreadsheet.letter_to_number('Z')
238
+ assert_equal 27, GenericSpreadsheet.letter_to_number('AA')
239
+ assert_equal 27, GenericSpreadsheet.letter_to_number('aA')
240
+ assert_equal 27, GenericSpreadsheet.letter_to_number('Aa')
241
+ assert_equal 27, GenericSpreadsheet.letter_to_number('aa')
242
+ end
243
+ def test_sheets
244
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
245
+ assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], oo.sheets
246
+ assert_raise(RangeError) { oo.default_sheet = "no_sheet" }
247
+ assert_raise(TypeError) { oo.default_sheet = [1,2,3] }
248
+ oo.sheets.each { |sh|
249
+ oo.default_sheet = sh
250
+ assert_equal sh, oo.default_sheet
251
+ }
252
+ end
253
+ end
254
+
255
+ def test_cells
256
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
257
+ # warum ist Auswaehlen erstes sheet hier nicht
258
+ # mehr drin?
259
+ oo.default_sheet = oo.sheets.first
260
+ assert_equal 1, oo.cell(1,1)
261
+ assert_equal 2, oo.cell(1,2)
262
+ assert_equal 3, oo.cell(1,3)
263
+ assert_equal 4, oo.cell(1,4)
264
+ assert_equal 5, oo.cell(2,1)
265
+ assert_equal 6, oo.cell(2,2)
266
+ assert_equal 7, oo.cell(2,3)
267
+ assert_equal 8, oo.cell(2,4)
268
+ assert_equal 9, oo.cell(2,5)
269
+ assert_equal "test", oo.cell(2,6)
270
+ assert_equal :string, oo.celltype(2,6)
271
+ assert_equal 11, oo.cell(2,7)
272
+ assert_equal :float, oo.celltype(2,7)
273
+ assert_equal 10, oo.cell(4,1)
274
+ assert_equal 11, oo.cell(4,2)
275
+ assert_equal 12, oo.cell(4,3)
276
+ assert_equal 13, oo.cell(4,4)
277
+ assert_equal 14, oo.cell(4,5)
278
+ assert_equal 10, oo.cell(4,'A')
279
+ assert_equal 11, oo.cell(4,'B')
280
+ assert_equal 12, oo.cell(4,'C')
281
+ assert_equal 13, oo.cell(4,'D')
282
+ assert_equal 14, oo.cell(4,'E')
283
+ assert_equal :date, oo.celltype(5,1)
284
+ assert_equal Date.new(1961,11,21), oo.cell(5,1)
285
+ assert_equal "1961-11-21", oo.cell(5,1).to_s
286
+ end
287
+ end
288
+
289
+ def test_celltype
290
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
291
+ assert_equal :string, oo.celltype(2,6)
292
+ end
293
+ end
294
+
295
+ def test_cell_address
296
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
297
+ assert_equal "tata", oo.cell(6,1)
298
+ assert_equal "tata", oo.cell(6,'A')
299
+ assert_equal "tata", oo.cell('A',6)
300
+ assert_equal "tata", oo.cell(6,'a')
301
+ assert_equal "tata", oo.cell('a',6)
302
+ assert_raise(ArgumentError) { assert_equal "tata", oo.cell('a','f') }
303
+ assert_raise(ArgumentError) { assert_equal "tata", oo.cell('f','a') }
304
+ assert_equal "thisisc8", oo.cell(8,3)
305
+ assert_equal "thisisc8", oo.cell(8,'C')
306
+ assert_equal "thisisc8", oo.cell('C',8)
307
+ assert_equal "thisisc8", oo.cell(8,'c')
308
+ assert_equal "thisisc8", oo.cell('c',8)
309
+ assert_equal "thisisd9", oo.cell('d',9)
310
+ assert_equal "thisisa11", oo.cell('a',11)
311
+ end
312
+ end
313
+
314
+ def test_office_version
315
+ with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
316
+ assert_equal "1.0", oo.officeversion
317
+ end
318
+ end
319
+
320
+ #TODO: inkonsequente Lieferung Fixnum/Float
321
+ def test_rows
322
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
323
+ oo.default_sheet = oo.sheets.first
324
+ assert_equal 41, oo.cell('a',12)
325
+ assert_equal 42, oo.cell('b',12)
326
+ assert_equal 43, oo.cell('c',12)
327
+ assert_equal 44, oo.cell('d',12)
328
+ assert_equal 45, oo.cell('e',12)
329
+ assert_equal [41.0,42.0,43.0,44.0,45.0, nil, nil], oo.row(12)
330
+ assert_equal "einundvierzig", oo.cell('a',16)
331
+ assert_equal "zweiundvierzig", oo.cell('b',16)
332
+ assert_equal "dreiundvierzig", oo.cell('c',16)
333
+ assert_equal "vierundvierzig", oo.cell('d',16)
334
+ assert_equal "fuenfundvierzig", oo.cell('e',16)
335
+ assert_equal ["einundvierzig", "zweiundvierzig", "dreiundvierzig", "vierundvierzig", "fuenfundvierzig", nil, nil], oo.row(16)
336
+ end
337
+ end
338
+
339
+ def test_last_row
340
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
341
+ oo.default_sheet = oo.sheets.first
342
+ assert_equal 18, oo.last_row
343
+ end
344
+ end
345
+
346
+ def test_last_column
347
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
348
+ oo.default_sheet = oo.sheets.first
349
+ assert_equal 7, oo.last_column
350
+ end
351
+ end
352
+
353
+ def test_last_column_as_letter
354
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
355
+ oo.default_sheet = oo.sheets.first
356
+ assert_equal 'G', oo.last_column_as_letter
357
+ end
358
+ end
359
+
360
+ def test_first_row
361
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
362
+ oo.default_sheet = oo.sheets.first
363
+ assert_equal 1, oo.first_row
364
+ end
365
+ end
366
+
367
+ def test_first_column
368
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
369
+ oo.default_sheet = oo.sheets.first
370
+ assert_equal 1, oo.first_column
371
+ end
372
+ end
373
+
374
+ def test_first_column_as_letter
375
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
376
+ oo.default_sheet = oo.sheets.first
377
+ assert_equal 'A', oo.first_column_as_letter
378
+ end
379
+ end
380
+
381
+ def test_sheetname
382
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
383
+ oo.default_sheet = "Name of Sheet 2"
384
+ assert_equal 'I am sheet 2', oo.cell('C',5)
385
+ assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
386
+ assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
387
+ assert_raise(RangeError) { dummy = oo.cell('C',5,"non existing sheet name")}
388
+ assert_raise(RangeError) { dummy = oo.celltype('C',5,"non existing sheet name")}
389
+ assert_raise(RangeError) { dummy = oo.empty?('C',5,"non existing sheet name")}
390
+ if oo.class == Excel
391
+ assert_raise(RuntimeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
392
+ assert_raise(RuntimeError) { dummy = oo.formula('C',5,"non existing sheet name")}
393
+ else
394
+ assert_raise(RangeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
395
+ assert_raise(RangeError) { dummy = oo.formula('C',5,"non existing sheet name")}
396
+ begin
397
+ assert_raise(RangeError) { dummy = oo.set('C',5,42,"non existing sheet name")} unless oo.class == Google
398
+ rescue NameError
399
+ #
400
+ end
401
+ assert_raise(RangeError) { dummy = oo.formulas("non existing sheet name")}
402
+ end
403
+ assert_raise(RangeError) { dummy = oo.to_yaml({},1,1,1,1,"non existing sheet name")}
404
+ end
405
+ end
406
+
407
+ def test_boundaries
408
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
409
+ oo.default_sheet = "Name of Sheet 2"
410
+ assert_equal 2, oo.first_column
411
+ assert_equal 'B', oo.first_column_as_letter
412
+ assert_equal 5, oo.first_row
413
+ assert_equal 'E', oo.last_column_as_letter
414
+ assert_equal 14, oo.last_row
415
+ end
416
+ end
417
+
418
+ def test_multiple_letters
419
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
420
+ oo.default_sheet = "Sheet3"
421
+ assert_equal "i am AA", oo.cell('AA',1)
422
+ assert_equal "i am AB", oo.cell('AB',1)
423
+ assert_equal "i am BA", oo.cell('BA',1)
424
+ assert_equal 'BA', oo.last_column_as_letter
425
+ assert_equal "i am BA", oo.cell(1,'BA')
426
+ end
427
+ end
428
+
429
+ def test_argument_error
430
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
431
+ assert_nothing_raised(ArgumentError) { oo.default_sheet = "Tabelle1" }
432
+ end
433
+ end
434
+
435
+ def test_empty_eh
436
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
437
+ assert oo.empty?('a',14)
438
+ assert ! oo.empty?('a',15)
439
+ assert oo.empty?('a',20)
440
+ end
441
+ end
442
+
443
+ def test_reload
444
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
445
+ oo.default_sheet = oo.sheets.first
446
+ assert_equal 1, oo.cell(1,1)
447
+ oo.reload
448
+ assert_equal 1, oo.cell(1,1)
449
+ end
450
+ end
451
+
452
+ def test_bug_contiguous_cells
453
+ with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
454
+ oo.default_sheet = "Sheet4"
455
+ assert_equal Date.new(2007,06,16), oo.cell('a',1)
456
+ assert_equal 10, oo.cell('b',1)
457
+ assert_equal 10, oo.cell('c',1)
458
+ assert_equal 10, oo.cell('d',1)
459
+ assert_equal 10, oo.cell('e',1)
460
+ end
461
+ end
462
+
463
+ def test_bug_italo_ve
464
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
465
+ oo.default_sheet = "Sheet5"
466
+ assert_equal 1, oo.cell('A',1)
467
+ assert_equal 5, oo.cell('b',1)
468
+ assert_equal 5, oo.cell('c',1)
469
+ assert_equal 2, oo.cell('a',2)
470
+ assert_equal 3, oo.cell('a',3)
471
+ end
472
+ end
473
+
474
+ def test_italo_table
475
+ with_each_spreadsheet(:name=>'simple_spreadsheet_from_italo', :format=>[:openoffice, :excel]) do |oo|
476
+ assert_equal '1', oo.cell('A',1)
477
+ assert_equal '1', oo.cell('B',1)
478
+ assert_equal '1', oo.cell('C',1)
479
+ assert_equal 1, oo.cell('A',2).to_i
480
+ assert_equal 2, oo.cell('B',2).to_i
481
+ assert_equal 1, oo.cell('C',2).to_i
482
+ assert_equal 1, oo.cell('A',3)
483
+ assert_equal 3, oo.cell('B',3)
484
+ assert_equal 1, oo.cell('C',3)
485
+ assert_equal 'A', oo.cell('A',4)
486
+ assert_equal 'A', oo.cell('B',4)
487
+ assert_equal 'A', oo.cell('C',4)
488
+ assert_equal 0.01, oo.cell('A',5)
489
+ assert_equal 0.01, oo.cell('B',5)
490
+ assert_equal 0.01, oo.cell('C',5)
491
+ assert_equal 0.03, oo.cell('a',5)+oo.cell('b',5)+oo.cell('c',5)
492
+
493
+ # Cells values in row 1:
494
+ assert_equal "1:string", oo.cell(1, 1)+":"+oo.celltype(1, 1).to_s
495
+ assert_equal "1:string",oo.cell(1, 2)+":"+oo.celltype(1, 2).to_s
496
+ assert_equal "1:string",oo.cell(1, 3)+":"+oo.celltype(1, 3).to_s
497
+
498
+ # Cells values in row 2:
499
+ assert_equal "1:string",oo.cell(2, 1)+":"+oo.celltype(2, 1).to_s
500
+ assert_equal "2:string",oo.cell(2, 2)+":"+oo.celltype(2, 2).to_s
501
+ assert_equal "1:string",oo.cell(2, 3)+":"+oo.celltype(2, 3).to_s
502
+
503
+ # Cells values in row 3:
504
+ assert_equal "1.0:float",oo.cell(3, 1).to_s+":"+oo.celltype(3, 1).to_s
505
+ assert_equal "3.0:float",oo.cell(3, 2).to_s+":"+oo.celltype(3, 2).to_s
506
+ assert_equal "1.0:float",oo.cell(3, 3).to_s+":"+oo.celltype(3, 3).to_s
507
+
508
+ # Cells values in row 4:
509
+ assert_equal "A:string",oo.cell(4, 1)+":"+oo.celltype(4, 1).to_s
510
+ assert_equal "A:string",oo.cell(4, 2)+":"+oo.celltype(4, 2).to_s
511
+ assert_equal "A:string",oo.cell(4, 3)+":"+oo.celltype(4, 3).to_s
512
+
513
+ # Cells values in row 5:
514
+ if oo.class == Openoffice
515
+ assert_equal "0.01:percentage",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
516
+ assert_equal "0.01:percentage",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
517
+ assert_equal "0.01:percentage",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
518
+ else
519
+ assert_equal "0.01:float",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
520
+ assert_equal "0.01:float",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
521
+ assert_equal "0.01:float",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
522
+ end
523
+ end
524
+ end
525
+
526
+ def test_formula_openoffice
527
+ with_each_spreadsheet(:name=>'formula', :format=>:openoffice) do |oo|
528
+ assert_equal 1, oo.cell('A',1)
529
+ assert_equal 2, oo.cell('A',2)
530
+ assert_equal 3, oo.cell('A',3)
531
+ assert_equal 4, oo.cell('A',4)
532
+ assert_equal 5, oo.cell('A',5)
533
+ assert_equal 6, oo.cell('A',6)
534
+ assert_equal 21, oo.cell('A',7)
535
+ assert_equal :formula, oo.celltype('A',7)
536
+ assert_equal "=[Sheet2.A1]", oo.formula('C',7)
537
+ assert_nil oo.formula('A',6)
538
+ assert_equal [[7, 1, "=SUM([.A1:.A6])"],
539
+ [7, 2, "=SUM([.$A$1:.B6])"],
540
+ [7, 3, "=[Sheet2.A1]"],
541
+ [8, 2, "=SUM([.$A$1:.B7])"],
542
+ ], oo.formulas(oo.sheets.first)
543
+
544
+ # setting a cell
545
+ oo.set('A',15, 41)
546
+ assert_equal 41, oo.cell('A',15)
547
+ oo.set('A',16, "41")
548
+ assert_equal "41", oo.cell('A',16)
549
+ oo.set('A',17, 42.5)
550
+ assert_equal 42.5, oo.cell('A',17)
551
+ end
552
+ end
553
+
554
+ def test_formula_google
555
+ with_each_spreadsheet(:name=>'formula', :format=>:google) do |oo|
556
+ oo.default_sheet = oo.sheets.first
557
+ assert_equal 1, oo.cell('A',1)
558
+ assert_equal 2, oo.cell('A',2)
559
+ assert_equal 3, oo.cell('A',3)
560
+ assert_equal 4, oo.cell('A',4)
561
+ assert_equal 5, oo.cell('A',5)
562
+ assert_equal 6, oo.cell('A',6)
563
+ # assert_equal 21, oo.cell('A',7)
564
+ assert_equal 21.0, oo.cell('A',7) #TODO: better solution Fixnum/Float
565
+ assert_equal :formula, oo.celltype('A',7)
566
+ # assert_equal "=[Sheet2.A1]", oo.formula('C',7)
567
+ # !!! different from formulas in Openoffice
568
+ #was: assert_equal "=sheet2!R[-6]C[-2]", oo.formula('C',7)
569
+ # has Google changed their format of formulas/references to other sheets?
570
+ assert_equal "=Sheet2!R[-6]C[-2]", oo.formula('C',7)
571
+ assert_nil oo.formula('A',6)
572
+ # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
573
+ # [7, 2, "=SUM([.$A$1:.B6])"],
574
+ # [7, 3, "=[Sheet2.A1]"],
575
+ # [8, 2, "=SUM([.$A$1:.B7])"],
576
+ # ], oo.formulas(oo.sheets.first)
577
+ # different format than in openoffice spreadsheets:
578
+ #was:
579
+ # assert_equal [[7, 1, "=SUM(R[-6]C[0]:R[-1]C[0])"],
580
+ # [7, 2, "=SUM(R1C1:R[-1]C[0])"],
581
+ # [7, 3, "=sheet2!R[-6]C[-2]"],
582
+ # [8, 2, "=SUM(R1C1:R[-1]C[0])"]],
583
+ # oo.formulas(oo.sheets.first)
584
+ assert_equal [[7, 1, "=SUM(R[-6]C:R[-1]C)"],
585
+ [7, 2, "=SUM(R1C1:R[-1]C)"],
586
+ [7, 3, "=Sheet2!R[-6]C[-2]"],
587
+ [8, 2, "=SUM(R1C1:R[-1]C)"]],
588
+ oo.formulas(oo.sheets.first)
589
+ end
590
+ end
591
+
592
+ def test_formula_excelx
593
+ with_each_spreadsheet(:name=>'formula', :format=>:excelx) do |oo|
594
+ assert_equal 1, oo.cell('A',1)
595
+ assert_equal 2, oo.cell('A',2)
596
+ assert_equal 3, oo.cell('A',3)
597
+ assert_equal 4, oo.cell('A',4)
598
+ assert_equal 5, oo.cell('A',5)
599
+ assert_equal 6, oo.cell('A',6)
600
+ assert_equal 21, oo.cell('A',7)
601
+ assert_equal :formula, oo.celltype('A',7)
602
+ #steht nicht in Datei, oder?
603
+ #nein, diesen Bezug habe ich nur in der Openoffice-Datei
604
+ #assert_equal "=[Sheet2.A1]", oo.formula('C',7)
605
+ assert_nil oo.formula('A',6)
606
+ # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
607
+ # [7, 2, "=SUM([.$A$1:.B6])"],
608
+ #[7, 3, "=[Sheet2.A1]"],
609
+ #[8, 2, "=SUM([.$A$1:.B7])"],
610
+ #], oo.formulas(oo.sheets.first)
611
+ assert_equal [[7, 1, 'SUM(A1:A6)'],
612
+ [7, 2, 'SUM($A$1:B6)'],
613
+ # [7, 3, "=[Sheet2.A1]"],
614
+ # [8, 2, "=SUM([.$A$1:.B7])"],
615
+ ], oo.formulas(oo.sheets.first)
616
+
617
+ # setting a cell
618
+ oo.set('A',15, 41)
619
+ assert_equal 41, oo.cell('A',15)
620
+ oo.set('A',16, "41")
621
+ assert_equal "41", oo.cell('A',16)
622
+ oo.set('A',17, 42.5)
623
+ assert_equal 42.5, oo.cell('A',17)
624
+ end
625
+ end
626
+
627
+ # Excel can only read the cell's value
628
+ def test_formula_excel
629
+ with_each_spreadsheet(:name=>'formula', :format=>:excel) do |oo|
630
+ assert_equal 21, oo.cell('A',7)
631
+ assert_equal 21, oo.cell('B',7)
632
+ end
633
+ end
634
+
635
+
636
+ def test_borders_sheets
637
+ with_each_spreadsheet(:name=>'borders') do |oo|
638
+ oo.default_sheet = oo.sheets[1]
639
+ assert_equal 6, oo.first_row
640
+ assert_equal 11, oo.last_row
641
+ assert_equal 4, oo.first_column
642
+ assert_equal 8, oo.last_column
643
+
644
+ oo.default_sheet = oo.sheets.first
645
+ assert_equal 5, oo.first_row
646
+ assert_equal 10, oo.last_row
647
+ assert_equal 3, oo.first_column
648
+ assert_equal 7, oo.last_column
649
+
650
+ oo.default_sheet = oo.sheets[2]
651
+ assert_equal 7, oo.first_row
652
+ assert_equal 12, oo.last_row
653
+ assert_equal 5, oo.first_column
654
+ assert_equal 9, oo.last_column
655
+ end
656
+ end
657
+
658
+ def yaml_entry(row,col,type,value)
659
+ "cell_#{row}_#{col}: \n row: #{row} \n col: #{col} \n celltype: #{type} \n value: #{value} \n"
660
+ end
661
+
662
+ def test_to_yaml
663
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
664
+ oo.default_sheet = oo.sheets.first
665
+ assert_equal "--- \n"+yaml_entry(5,1,"date","1961-11-21"), oo.to_yaml({}, 5,1,5,1)
666
+ assert_equal "--- \n"+yaml_entry(8,3,"string","thisisc8"), oo.to_yaml({}, 8,3,8,3)
667
+ assert_equal "--- \n"+yaml_entry(12,3,"float",43.0), oo.to_yaml({}, 12,3,12,3)
668
+ assert_equal \
669
+ "--- \n"+yaml_entry(12,3,"float",43.0) +
670
+ yaml_entry(12,4,"float",44.0) +
671
+ yaml_entry(12,5,"float",45.0), oo.to_yaml({}, 12,3,12)
672
+ assert_equal \
673
+ "--- \n"+yaml_entry(12,3,"float",43.0)+
674
+ yaml_entry(12,4,"float",44.0)+
675
+ yaml_entry(12,5,"float",45.0)+
676
+ yaml_entry(15,3,"float",43.0)+
677
+ yaml_entry(15,4,"float",44.0)+
678
+ yaml_entry(15,5,"float",45.0)+
679
+ yaml_entry(16,3,"string","dreiundvierzig")+
680
+ yaml_entry(16,4,"string","vierundvierzig")+
681
+ yaml_entry(16,5,"string","fuenfundvierzig"), oo.to_yaml({}, 12,3)
682
+ end
683
+ end
684
+
685
+ def test_only_one_sheet
686
+ with_each_spreadsheet(:name=>'only_one_sheet') do |oo|
687
+ assert_equal 42, oo.cell('B',4)
688
+ assert_equal 43, oo.cell('C',4)
689
+ assert_equal 44, oo.cell('D',4)
690
+ oo.default_sheet = oo.sheets.first
691
+ assert_equal 42, oo.cell('B',4)
692
+ assert_equal 43, oo.cell('C',4)
693
+ assert_equal 44, oo.cell('D',4)
694
+ end
695
+ end
696
+
697
+ def test_excel_open_from_uri_and_zipped
698
+ if EXCEL
699
+ if ONLINE
700
+ begin
701
+ url = 'http://stiny-leonhard.de/bode-v1.xls.zip'
702
+ excel = Excel.new(url, :zip)
703
+ excel.default_sheet = excel.sheets.first
704
+ assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
705
+ end
706
+ end
707
+ end
708
+ end
709
+
710
+ def test_openoffice_open_from_uri_and_zipped
711
+ if OPENOFFICE
712
+ if ONLINE
713
+ begin
714
+ url = 'http://spazioinwind.libero.it/s2/rata.ods.zip'
715
+ sheet = Openoffice.new(url, :zip)
716
+ #has been changed: assert_equal 'ist "e" im Nenner von H(s)', sheet.cell('b', 5)
717
+ assert_in_delta 0.001, 505.14, sheet.cell('c', 33).to_f
718
+ end
719
+ end
720
+ end
721
+ end
722
+
723
+ def test_excel_zipped
724
+ if EXCEL
725
+ begin
726
+ oo = Excel.new(File.join(TESTDIR,"bode-v1.xls.zip"), :zip)
727
+ assert oo
728
+ assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
729
+ end
730
+ end
731
+ end
732
+
733
+ def test_openoffice_zipped
734
+ if OPENOFFICE
735
+ begin
736
+ oo = Openoffice.new(File.join(TESTDIR,"bode-v1.ods.zip"), :zip)
737
+ assert oo
738
+ assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
739
+ end
740
+ end
741
+ end
742
+
743
+ def test_bug_ric
744
+ with_each_spreadsheet(:name=>'ric', :format=>:openoffice) do |oo|
745
+ assert oo.empty?('A',1)
746
+ assert oo.empty?('B',1)
747
+ assert oo.empty?('C',1)
748
+ assert oo.empty?('D',1)
749
+ expected = 1
750
+ letter = 'e'
751
+ while letter <= 'u'
752
+ assert_equal expected, oo.cell(letter,1)
753
+ letter.succ!
754
+ expected += 1
755
+ end
756
+ assert_equal 'J', oo.cell('v',1)
757
+ assert_equal 'P', oo.cell('w',1)
758
+ assert_equal 'B', oo.cell('x',1)
759
+ assert_equal 'All', oo.cell('y',1)
760
+ assert_equal 0, oo.cell('a',2)
761
+ assert oo.empty?('b',2)
762
+ assert oo.empty?('c',2)
763
+ assert oo.empty?('d',2)
764
+ assert_equal 'B', oo.cell('e',2)
765
+ assert_equal 'B', oo.cell('f',2)
766
+ assert_equal 'B', oo.cell('g',2)
767
+ assert_equal 'B', oo.cell('h',2)
768
+ assert_equal 'B', oo.cell('i',2)
769
+ assert_equal 'B', oo.cell('j',2)
770
+ assert_equal 'B', oo.cell('k',2)
771
+ assert_equal 'B', oo.cell('l',2)
772
+ assert_equal 'B', oo.cell('m',2)
773
+ assert_equal 'B', oo.cell('n',2)
774
+ assert_equal 'B', oo.cell('o',2)
775
+ assert_equal 'B', oo.cell('p',2)
776
+ assert_equal 'B', oo.cell('q',2)
777
+ assert_equal 'B', oo.cell('r',2)
778
+ assert_equal 'B', oo.cell('s',2)
779
+ assert oo.empty?('t',2)
780
+ assert oo.empty?('u',2)
781
+ assert_equal 0 , oo.cell('v',2)
782
+ assert_equal 0 , oo.cell('w',2)
783
+ assert_equal 15 , oo.cell('x',2)
784
+ assert_equal 15 , oo.cell('y',2)
785
+ end
786
+ end
787
+
788
+ def test_mehrteilig
789
+ with_each_spreadsheet(:name=>'Bibelbund1', :format=>:openoffice) do |oo|
790
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
791
+ end
792
+ #if EXCELX
793
+ # #Datei gibt es noch nicht
794
+ # oo = Excelx.new(File.join(TESTDIR,"Bibelbund1.xlsx"))
795
+ # oo.default_sheet = oo.sheets.first
796
+ # assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
797
+ #end
798
+ end
799
+
800
+ def tempdir
801
+ Dir::tmpdir
802
+ end
803
+
804
+ # "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt
805
+ # wird.
806
+ # Besser: Methode um temporaeres Dir. portabel zu bestimmen
807
+ def test_huge_document_to_csv
808
+ if LONG_RUN
809
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
810
+ :excel,
811
+ :excelx
812
+ # Google hier nicht, weil Google-Spreadsheets nicht so gross werden
813
+ # duerfen
814
+ ]) do |oo|
815
+ assert_nothing_raised(Timeout::Error) {
816
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
817
+ File.delete_if_exist(File.join(tempdir,"Bibelbund.csv"))
818
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
819
+ assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46,'A')
820
+ assert_equal "Tagebuch aus Chile Juli 1977", oo.cell(55,'A')
821
+ assert oo.to_csv(File.join(tempdir,"Bibelbund.csv"))
822
+ assert File.exists?(File.join(tempdir,"Bibelbund.csv"))
823
+ #if running_windows?
824
+ # to do
825
+ # "diff is not available under windows"
826
+ # end
827
+ #else
828
+ # assert_equal "", `diff test/Bibelbund.csv /tmp/Bibelbund.csv`, "error in class #{oo.class}"
829
+ # assert_equal "", `diff test/Bibelbund.csv #{File.join(tempdir,"Bibelbund.csv")}`, "error in class #{oo.class}"
830
+ assert_equal "",
831
+ diff("test/Bibelbund.csv","#{File.join(tempdir,"Bibelbund.csv")}"),
832
+ "error in class #{oo.class}"
833
+ #end
834
+ end
835
+ }
836
+ end
837
+ end
838
+ end
839
+
840
+ def test_bug_quotes_excelx
841
+ if LONG_RUN
842
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
843
+ :excel,
844
+ :excelx]) do |oo|
845
+ oo.default_sheet = oo.sheets.first
846
+ assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
847
+ oo.cell('a',76)
848
+ dummy = oo.to_csv("csv#{$$}")
849
+ assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
850
+ oo.cell('a',78)
851
+ File.delete_if_exist("csv#{$$}")
852
+ end
853
+ end
854
+ end
855
+
856
+ def test_to_csv
857
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
858
+ master = "#{TESTDIR}/numbers1.csv"
859
+ File.delete_if_exist(File.join(tempdir,"numbers1.csv"))
860
+ assert oo.to_csv(File.join(tempdir,"numbers1.csv"),oo.sheets.first)
861
+ assert(File.exists?(File.join(tempdir,"numbers1.csv")), "Datei #{tempdir}/numbers1.csv existiert nicht")
862
+ if running_windows?
863
+ warn "no 'diff' in windows"
864
+ else
865
+ assert_equal "", `diff #{master} #{File.join(tempdir,"numbers1.csv")}`
866
+ assert oo.to_csv(File.join(tempdir,"numbers1.csv"))
867
+ assert File.exists?(File.join(tempdir,"numbers1.csv"))
868
+ assert_equal "", `diff #{master} #{File.join(tempdir,"numbers1.csv")}`
869
+ end
870
+ end
871
+ end
872
+
873
+ def test_bug_mehrere_datum
874
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
875
+ oo.default_sheet = 'Sheet5'
876
+ assert_equal :date, oo.celltype('A',4)
877
+ assert_equal :date, oo.celltype('B',4)
878
+ assert_equal :date, oo.celltype('C',4)
879
+ assert_equal :date, oo.celltype('D',4)
880
+ assert_equal :date, oo.celltype('E',4)
881
+ assert_equal Date.new(2007,11,21), oo.cell('A',4)
882
+ assert_equal Date.new(2007,11,21), oo.cell('B',4)
883
+ assert_equal Date.new(2007,11,21), oo.cell('C',4)
884
+ assert_equal Date.new(2007,11,21), oo.cell('D',4)
885
+ assert_equal Date.new(2007,11,21), oo.cell('E',4)
886
+ assert_equal :float, oo.celltype('A',5)
887
+ assert_equal :float, oo.celltype('B',5)
888
+ assert_equal :float, oo.celltype('C',5)
889
+ assert_equal :float, oo.celltype('D',5)
890
+ assert_equal :float, oo.celltype('E',5)
891
+ assert_equal 42, oo.cell('A',5)
892
+ assert_equal 42, oo.cell('B',5)
893
+ assert_equal 42, oo.cell('C',5)
894
+ assert_equal 42, oo.cell('D',5)
895
+ assert_equal 42, oo.cell('E',5)
896
+ assert_equal :string, oo.celltype('A',6)
897
+ assert_equal :string, oo.celltype('B',6)
898
+ assert_equal :string, oo.celltype('C',6)
899
+ assert_equal :string, oo.celltype('D',6)
900
+ assert_equal :string, oo.celltype('E',6)
901
+ assert_equal "ABC", oo.cell('A',6)
902
+ assert_equal "ABC", oo.cell('B',6)
903
+ assert_equal "ABC", oo.cell('C',6)
904
+ assert_equal "ABC", oo.cell('D',6)
905
+ assert_equal "ABC", oo.cell('E',6)
906
+ end
907
+ end
908
+
909
+ def test_multiple_sheets
910
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
911
+ 2.times do
912
+ oo.default_sheet = "Tabelle1"
913
+ assert_equal 1, oo.cell(1,1)
914
+ assert_equal 1, oo.cell(1,1,"Tabelle1")
915
+ assert_equal "I am sheet 2", oo.cell('C',5,"Name of Sheet 2")
916
+ sheetname = 'Sheet5'
917
+ assert_equal :date, oo.celltype('A',4,sheetname)
918
+ assert_equal :date, oo.celltype('B',4,sheetname)
919
+ assert_equal :date, oo.celltype('C',4,sheetname)
920
+ assert_equal :date, oo.celltype('D',4,sheetname)
921
+ assert_equal :date, oo.celltype('E',4,sheetname)
922
+ assert_equal Date.new(2007,11,21), oo.cell('A',4,sheetname)
923
+ assert_equal Date.new(2007,11,21), oo.cell('B',4,sheetname)
924
+ assert_equal Date.new(2007,11,21), oo.cell('C',4,sheetname)
925
+ assert_equal Date.new(2007,11,21), oo.cell('D',4,sheetname)
926
+ assert_equal Date.new(2007,11,21), oo.cell('E',4,sheetname)
927
+ assert_equal :float, oo.celltype('A',5,sheetname)
928
+ assert_equal :float, oo.celltype('B',5,sheetname)
929
+ assert_equal :float, oo.celltype('C',5,sheetname)
930
+ assert_equal :float, oo.celltype('D',5,sheetname)
931
+ assert_equal :float, oo.celltype('E',5,sheetname)
932
+ assert_equal 42, oo.cell('A',5,sheetname)
933
+ assert_equal 42, oo.cell('B',5,sheetname)
934
+ assert_equal 42, oo.cell('C',5,sheetname)
935
+ assert_equal 42, oo.cell('D',5,sheetname)
936
+ assert_equal 42, oo.cell('E',5,sheetname)
937
+ assert_equal :string, oo.celltype('A',6,sheetname)
938
+ assert_equal :string, oo.celltype('B',6,sheetname)
939
+ assert_equal :string, oo.celltype('C',6,sheetname)
940
+ assert_equal :string, oo.celltype('D',6,sheetname)
941
+ assert_equal :string, oo.celltype('E',6,sheetname)
942
+ assert_equal "ABC", oo.cell('A',6,sheetname)
943
+ assert_equal "ABC", oo.cell('B',6,sheetname)
944
+ assert_equal "ABC", oo.cell('C',6,sheetname)
945
+ assert_equal "ABC", oo.cell('D',6,sheetname)
946
+ assert_equal "ABC", oo.cell('E',6,sheetname)
947
+ oo.reload
948
+ end
949
+ end
950
+ end
951
+
952
+
953
+ def test_bug_empty_sheet
954
+ with_each_spreadsheet(:name=>'formula', :format=>[:openoffice, :excelx]) do |oo|
955
+ oo.default_sheet = 'Sheet3' # is an empty sheet
956
+ assert_nothing_raised() { oo.to_csv(File.join(tempdir,"emptysheet.csv")) }
957
+ assert_equal "", `cat #{File.join(tempdir,"emptysheet.csv")}`
958
+ end
959
+ end
960
+
961
+ def test_find_by_row_huge_document
962
+ if LONG_RUN
963
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
964
+ :excel,
965
+ :excelx]) do |oo|
966
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
967
+ oo.default_sheet = oo.sheets.first
968
+ rec = oo.find 20
969
+ assert rec
970
+ # assert_equal "Brief aus dem Sekretariat", rec[0]
971
+ #p rec
972
+ assert_equal "Brief aus dem Sekretariat", rec[0]['TITEL']
973
+ rec = oo.find 22
974
+ assert rec
975
+ # assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]
976
+ assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]['TITEL']
977
+ end
978
+ end
979
+ end
980
+ end
981
+
982
+ def test_find_by_row
983
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
984
+ oo.header_line = nil
985
+ rec = oo.find 16
986
+ assert rec
987
+ assert_nil oo.header_line
988
+ # keine Headerlines in diesem Beispiel definiert
989
+ assert_equal "einundvierzig", rec[0]
990
+ #assert_equal false, rec
991
+ rec = oo.find 15
992
+ assert rec
993
+ assert_equal 41,rec[0]
994
+ end
995
+ end
996
+
997
+ def test_find_by_conditions
998
+ if LONG_RUN
999
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
1000
+ :excel,
1001
+ :excelx]) do |oo|
1002
+ assert_nothing_raised(Timeout::Error) {
1003
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
1004
+ #-----------------------------------------------------------------
1005
+ zeilen = oo.find(:all, :conditions => {
1006
+ 'TITEL' => 'Brief aus dem Sekretariat'
1007
+ }
1008
+ )
1009
+ assert_equal 2, zeilen.size
1010
+ assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
1011
+ "INTERNET"=>nil,
1012
+ "SEITE"=>316.0,
1013
+ "KENNUNG"=>"Aus dem Bibelbund",
1014
+ "OBJEKT"=>"Bibel+Gem",
1015
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
1016
+ "NUMMER"=>"1982-3",
1017
+ "TITEL"=>"Brief aus dem Sekretariat"},
1018
+ {"VERFASSER"=>"Almassy, Annelene von",
1019
+ "INTERNET"=>nil,
1020
+ "SEITE"=>222.0,
1021
+ "KENNUNG"=>"Aus dem Bibelbund",
1022
+ "OBJEKT"=>"Bibel+Gem",
1023
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
1024
+ "NUMMER"=>"1983-2",
1025
+ "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
1026
+
1027
+ #----------------------------------------------------------
1028
+ zeilen = oo.find(:all,
1029
+ :conditions => { 'VERFASSER' => 'Almassy, Annelene von' }
1030
+ )
1031
+ assert_equal 13, zeilen.size
1032
+ #----------------------------------------------------------
1033
+ zeilen = oo.find(:all, :conditions => {
1034
+ 'TITEL' => 'Brief aus dem Sekretariat',
1035
+ 'VERFASSER' => 'Almassy, Annelene von',
1036
+ }
1037
+ )
1038
+ assert_equal 2, zeilen.size
1039
+ assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
1040
+ "INTERNET"=>nil,
1041
+ "SEITE"=>316.0,
1042
+ "KENNUNG"=>"Aus dem Bibelbund",
1043
+ "OBJEKT"=>"Bibel+Gem",
1044
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
1045
+ "NUMMER"=>"1982-3",
1046
+ "TITEL"=>"Brief aus dem Sekretariat"},
1047
+ {"VERFASSER"=>"Almassy, Annelene von",
1048
+ "INTERNET"=>nil,
1049
+ "SEITE"=>222.0,
1050
+ "KENNUNG"=>"Aus dem Bibelbund",
1051
+ "OBJEKT"=>"Bibel+Gem",
1052
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
1053
+ "NUMMER"=>"1983-2",
1054
+ "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
1055
+
1056
+ # Result as an array
1057
+ zeilen = oo.find(:all,
1058
+ :conditions => {
1059
+ 'TITEL' => 'Brief aus dem Sekretariat',
1060
+ 'VERFASSER' => 'Almassy, Annelene von',
1061
+ }, :array => true)
1062
+ assert_equal 2, zeilen.size
1063
+ assert_equal [
1064
+ [
1065
+ "Brief aus dem Sekretariat",
1066
+ "Almassy, Annelene von",
1067
+ "Bibel+Gem",
1068
+ "1982-3",
1069
+ 316.0,
1070
+ nil,
1071
+ "#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
1072
+ "Aus dem Bibelbund",
1073
+ ],
1074
+ [
1075
+ "Brief aus dem Sekretariat",
1076
+ "Almassy, Annelene von",
1077
+ "Bibel+Gem",
1078
+ "1983-2",
1079
+ 222.0,
1080
+ nil,
1081
+ "#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
1082
+ "Aus dem Bibelbund",
1083
+ ]] , zeilen
1084
+ end # Timeout
1085
+ } # nothing_raised
1086
+ end
1087
+ end
1088
+ end
1089
+
1090
+
1091
+ #TODO: temporaerer Test
1092
+ def test_seiten_als_date
1093
+ if LONG_RUN
1094
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>:excelx) do |oo|
1095
+ assert_equal 'Bericht aus dem Sekretariat', oo.cell(13,1)
1096
+ assert_equal '1981-4', oo.cell(13,'D')
1097
+ assert_equal String, oo.excelx_type(13,'E')[1].class
1098
+ assert_equal [:numeric_or_formula,"General"], oo.excelx_type(13,'E')
1099
+ assert_equal '428', oo.excelx_value(13,'E')
1100
+ assert_equal 428.0, oo.cell(13,'E')
1101
+ end
1102
+ end
1103
+ end
1104
+
1105
+ def test_column
1106
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
1107
+ 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)]
1108
+ assert_equal expected, oo.column(1)
1109
+ assert_equal expected, oo.column('a')
1110
+ end
1111
+ end
1112
+
1113
+ def test_column_huge_document
1114
+ if LONG_RUN
1115
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice,
1116
+ :excel,
1117
+ :excelx]) do |oo|
1118
+ assert_nothing_raised(Timeout::Error) {
1119
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
1120
+ oo.default_sheet = oo.sheets.first
1121
+ assert_equal 3735, oo.column('a').size
1122
+ #assert_equal 499, oo.column('a').size
1123
+ end
1124
+ }
1125
+ end
1126
+ end
1127
+ end
1128
+
1129
+ def test_simple_spreadsheet_find_by_condition
1130
+ with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
1131
+ oo.header_line = 3
1132
+ # oo.date_format = '%m/%d/%Y' if oo.class == Google
1133
+ erg = oo.find(:all, :conditions => {'Comment' => 'Task 1'})
1134
+ assert_equal Date.new(2007,05,07), erg[1]['Date']
1135
+ assert_equal 10.75 , erg[1]['Start time']
1136
+ assert_equal 12.50 , erg[1]['End time']
1137
+ assert_equal 0 , erg[1]['Pause']
1138
+ assert_equal 1.75 , erg[1]['Sum'] unless oo.class == Excel
1139
+ assert_equal "Task 1" , erg[1]['Comment']
1140
+ end
1141
+ end
1142
+
1143
+ # Ruby-spreadsheet now allows us to at least give the current value
1144
+ # from a cell with a formula (no possible with parseexcel)
1145
+ def test_bug_false_borders_with_formulas
1146
+ with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1147
+ assert_equal 1, oo.first_row
1148
+ assert_equal 3, oo.last_row
1149
+ assert_equal 1, oo.first_column
1150
+ assert_equal 4, oo.last_column
1151
+ end
1152
+ end
1153
+
1154
+ # We'ce added minimal formula support so we can now read these
1155
+ # though not sure how the spreadsheet reports older values....
1156
+ def test_fe
1157
+ with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1158
+ assert_equal Date.new(2007,11,1), oo.cell('a',1)
1159
+ #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('a',1)
1160
+ #DOES NOT WORK IN EXCEL FILES: assert_equal '=TODAY()', oo.formula('a',1)
1161
+
1162
+ assert_equal Date.new(2008,2,9), oo.cell('B',1)
1163
+ #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('B',1)
1164
+ #DOES NOT WORK IN EXCEL FILES: assert_equal "=A1+100", oo.formula('B',1)
1165
+
1166
+ assert_kind_of DateTime, oo.cell('C',1)
1167
+ #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('C',1)
1168
+ #DOES NOT WORK IN EXCEL FILES: assert_equal "=C1", oo.formula('C',1)
1169
+
1170
+ assert_equal 'H1', oo.cell('A',2)
1171
+ assert_equal 'H2', oo.cell('B',2)
1172
+ assert_equal 'H3', oo.cell('C',2)
1173
+ assert_equal 'H4', oo.cell('D',2)
1174
+ assert_equal 'R1', oo.cell('A',3)
1175
+ assert_equal 'R2', oo.cell('B',3)
1176
+ assert_equal 'R3', oo.cell('C',3)
1177
+ assert_equal 'R4', oo.cell('D',3)
1178
+ end
1179
+ end
1180
+
1181
+ def test_excel_does_not_support_formulas
1182
+ with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1183
+ assert_raise(RuntimeError) { void = oo.formula('a',1) }
1184
+ assert_raise(RuntimeError) { void = oo.formula?('a',1) }
1185
+ assert_raise(RuntimeError) { void = oo.formulas(oo.sheets.first) }
1186
+ end
1187
+ end
1188
+
1189
+ def get_extension(oo)
1190
+ case oo
1191
+ when Openoffice
1192
+ ".ods"
1193
+ when Excel
1194
+ ".xls"
1195
+ when Excelx
1196
+ ".xlsx"
1197
+ when Google
1198
+ ""
1199
+ end
1200
+ end
1201
+
1202
+ def test_info
1203
+ expected_templ = "File: numbers1%s\n"+
1204
+ "Number of sheets: 5\n"+
1205
+ "Sheets: Tabelle1, Name of Sheet 2, Sheet3, Sheet4, Sheet5\n"+
1206
+ "Sheet 1:\n"+
1207
+ " First row: 1\n"+
1208
+ " Last row: 18\n"+
1209
+ " First column: A\n"+
1210
+ " Last column: G\n"+
1211
+ "Sheet 2:\n"+
1212
+ " First row: 5\n"+
1213
+ " Last row: 14\n"+
1214
+ " First column: B\n"+
1215
+ " Last column: E\n"+
1216
+ "Sheet 3:\n"+
1217
+ " First row: 1\n"+
1218
+ " Last row: 1\n"+
1219
+ " First column: A\n"+
1220
+ " Last column: BA\n"+
1221
+ "Sheet 4:\n"+
1222
+ " First row: 1\n"+
1223
+ " Last row: 1\n"+
1224
+ " First column: A\n"+
1225
+ " Last column: E\n"+
1226
+ "Sheet 5:\n"+
1227
+ " First row: 1\n"+
1228
+ " Last row: 6\n"+
1229
+ " First column: A\n"+
1230
+ " Last column: E"
1231
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
1232
+ ext = get_extension(oo)
1233
+ expected = sprintf(expected_templ,ext)
1234
+ begin
1235
+ if oo.class == Google
1236
+ assert_equal expected.gsub(/numbers1/,key_of("numbers1")), oo.info
1237
+ else
1238
+ assert_equal expected, oo.info
1239
+ end
1240
+ rescue NameError
1241
+ #
1242
+ end
1243
+ end
1244
+ end
1245
+
1246
+ def test_bug_excel_numbers1_sheet5_last_row
1247
+ with_each_spreadsheet(:name=>'numbers1', :format=>:excel) do |oo|
1248
+ oo.default_sheet = "Tabelle1"
1249
+ assert_equal 1, oo.first_row
1250
+ assert_equal 18, oo.last_row
1251
+ assert_equal Openoffice.letter_to_number('A'), oo.first_column
1252
+ assert_equal Openoffice.letter_to_number('G'), oo.last_column
1253
+ oo.default_sheet = "Name of Sheet 2"
1254
+ assert_equal 5, oo.first_row
1255
+ assert_equal 14, oo.last_row
1256
+ assert_equal Openoffice.letter_to_number('B'), oo.first_column
1257
+ assert_equal Openoffice.letter_to_number('E'), oo.last_column
1258
+ oo.default_sheet = "Sheet3"
1259
+ assert_equal 1, oo.first_row
1260
+ assert_equal 1, oo.last_row
1261
+ assert_equal Openoffice.letter_to_number('A'), oo.first_column
1262
+ assert_equal Openoffice.letter_to_number('BA'), oo.last_column
1263
+ oo.default_sheet = "Sheet4"
1264
+ assert_equal 1, oo.first_row
1265
+ assert_equal 1, oo.last_row
1266
+ assert_equal Openoffice.letter_to_number('A'), oo.first_column
1267
+ assert_equal Openoffice.letter_to_number('E'), oo.last_column
1268
+ oo.default_sheet = "Sheet5"
1269
+ assert_equal 1, oo.first_row
1270
+ assert_equal 6, oo.last_row
1271
+ assert_equal Openoffice.letter_to_number('A'), oo.first_column
1272
+ assert_equal Openoffice.letter_to_number('E'), oo.last_column
1273
+ end
1274
+ end
1275
+
1276
+ def test_should_raise_file_not_found_error
1277
+ if OPENOFFICE
1278
+ assert_raise(IOError) {
1279
+ oo = Openoffice.new(File.join('testnichtvorhanden','Bibelbund.ods'))
1280
+ }
1281
+ end
1282
+ if EXCEL
1283
+ assert_raise(IOError) {
1284
+ oo = Excel.new(File.join('testnichtvorhanden','Bibelbund.xls'))
1285
+ }
1286
+ end
1287
+ if EXCELX
1288
+ assert_raise(IOError) {
1289
+ oo = Excelx.new(File.join('testnichtvorhanden','Bibelbund.xlsx'))
1290
+ }
1291
+ end
1292
+ if GOOGLE
1293
+ # assert_raise(Net::HTTPServerException) {
1294
+ # oo = Google.new(key_of('testnichtvorhanden'+'Bibelbund.ods'))
1295
+ # oo = Google.new('testnichtvorhanden')
1296
+ # }
1297
+ end
1298
+ end
1299
+
1300
+ def test_write_google
1301
+ # write.me: http://spreadsheets.google.com/ccc?key=ptu6bbahNZpY0N0RrxQbWdw&hl=en_GB
1302
+ with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1303
+ oo.default_sheet = oo.sheets.first
1304
+ oo.set_value(1,1,"hello from the tests")
1305
+ assert_equal "hello from the tests", oo.cell(1,1)
1306
+ oo.set_value(1,1, 1.0)
1307
+ assert_equal 1.0, oo.cell(1,1)
1308
+ end
1309
+ end
1310
+
1311
+ def test_bug_set_value_with_more_than_one_sheet_google
1312
+ # write.me: http://spreadsheets.google.com/ccc?key=ptu6bbahNZpY0N0RrxQbWdw&hl=en_GB
1313
+ after Date.new(2011,6,19) do
1314
+ with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1315
+ content1 = 'AAA'
1316
+ content2 = 'BBB'
1317
+ oo.default_sheet = oo.sheets.first
1318
+ oo.set_value(1,1,content1)
1319
+ oo.default_sheet = oo.sheets[1]
1320
+ oo.set_value(1,1,content2) # in the second sheet
1321
+ oo.default_sheet = oo.sheets.first
1322
+ assert_equal content1, oo.cell(1,1)
1323
+ oo.default_sheet = oo.sheets[1]
1324
+ assert_equal content2, oo.cell(1,1)
1325
+ end
1326
+ end
1327
+ end
1328
+
1329
+ def test_set_value_with_sheet_argument_google
1330
+ with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1331
+ random_row = rand(10)+1
1332
+ random_column = rand(10)+1
1333
+ content1 = 'ABC'
1334
+ content2 = 'DEF'
1335
+ oo.set_value(random_row,random_column,content1,oo.sheets.first)
1336
+ oo.set_value(random_row,random_column,content2,oo.sheets[1])
1337
+ assert_equal content1, oo.cell(random_row,random_column,oo.sheets.first)
1338
+ assert_equal content2, oo.cell(random_row,random_column,oo.sheets[1])
1339
+ end
1340
+ end
1341
+
1342
+ def test_set_value_for_non_existing_sheet_google
1343
+ with_each_spreadsheet(:name=>'ptu6bbahNZpY0N0RrxQbWdw', :format=>:google) do |oo|
1344
+ assert_raise(RangeError) { oo.set_value(1,1,"dummy","no_sheet") }
1345
+ end
1346
+ end
1347
+
1348
+ def test_bug_bbu
1349
+ with_each_spreadsheet(:name=>'bbu', :format=>[:openoffice, :excelx, :excel]) do |oo|
1350
+ assert_nothing_raised() {
1351
+ assert_equal "File: bbu#{get_extension(oo)}
1352
+ Number of sheets: 3
1353
+ Sheets: 2007_12, Tabelle2, Tabelle3
1354
+ Sheet 1:
1355
+ First row: 1
1356
+ Last row: 4
1357
+ First column: A
1358
+ Last column: F
1359
+ Sheet 2:
1360
+ - empty -
1361
+ Sheet 3:
1362
+ - empty -", oo.info
1363
+ }
1364
+
1365
+ oo.default_sheet = oo.sheets[1] # empty sheet
1366
+ assert_nil oo.first_row
1367
+ assert_nil oo.last_row
1368
+ assert_nil oo.first_column
1369
+ assert_nil oo.last_column
1370
+ end
1371
+ end
1372
+
1373
+
1374
+ def test_bug_time_nil
1375
+ with_each_spreadsheet(:name=>'time-test') do |oo|
1376
+ assert_equal 12*3600+13*60+14, oo.cell('B',1) # 12:13:14 (secs since midnight)
1377
+ assert_equal :time, oo.celltype('B',1)
1378
+ assert_equal 15*3600+16*60, oo.cell('C',1) # 15:16 (secs since midnight)
1379
+ assert_equal :time, oo.celltype('C',1)
1380
+ assert_equal 23*3600, oo.cell('D',1) # 23:00 (secs since midnight)
1381
+ assert_equal :time, oo.celltype('D',1)
1382
+ end
1383
+ end
1384
+
1385
+ def test_date_time_to_csv
1386
+ with_each_spreadsheet(:name=>'time-test') do |oo|
1387
+ begin
1388
+ csv_output = File.join(tempdir,'time_test.csv')
1389
+ assert oo.to_csv(csv_output)
1390
+ assert File.exists?(csv_output)
1391
+ assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}`
1392
+ # --strip-trailing-cr is needed because the test-file use 0A and
1393
+ # the test on an windows box generates 0D 0A as line endings
1394
+ ensure
1395
+ File.delete_if_exist(csv_output)
1396
+ end
1397
+ end
1398
+ end
1399
+
1400
+ def test_date_time_yaml
1401
+ with_each_spreadsheet(:name=>'time-test') do |oo|
1402
+ expected =
1403
+ "--- \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"
1404
+ assert_equal expected, oo.to_yaml
1405
+ end
1406
+ end
1407
+
1408
+ def test_no_remaining_tmp_files_openoffice
1409
+ if OPENOFFICE
1410
+ prev = Dir.glob(TMP_PREFIX)
1411
+ assert_raise(Zip::ZipError) { #TODO: besseres Fehlerkriterium bei
1412
+ # oo = Openoffice.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1413
+ # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1414
+ oo = Openoffice.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1415
+ false,
1416
+ :ignore)
1417
+ }
1418
+ now = Dir.glob(TMP_PREFIX)
1419
+ assert (now-prev).empty?, "temporay directory was not deleted"
1420
+ end
1421
+ end
1422
+
1423
+ def test_no_remaining_tmp_files_excel
1424
+ if EXCEL
1425
+ prev = Dir.glob(TMP_PREFIX)
1426
+ assert_raise(Ole::Storage::FormatError) {
1427
+ oo = Excel.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1428
+ false,
1429
+ :ignore)
1430
+ }
1431
+ now = Dir.glob(TMP_PREFIX)
1432
+ assert (now-prev).empty?, "temporary directory not removed"
1433
+ end
1434
+ end
1435
+
1436
+ def test_no_remaining_tmp_files_excelx
1437
+ if EXCELX
1438
+ prev = Dir.glob(TMP_PREFIX)
1439
+ assert_raise(Zip::ZipError) { #TODO: besseres Fehlerkriterium bei
1440
+
1441
+ # oo = Excelx.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1442
+ # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1443
+ oo = Excelx.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1444
+ false,
1445
+ :ignore)
1446
+
1447
+ }
1448
+ now = Dir.glob(TMP_PREFIX)
1449
+ assert (now-prev).empty?
1450
+ end
1451
+ end
1452
+
1453
+ def test_no_remaining_tmp_files_google
1454
+ # Exception ist irgendwie anders, nochmal ansehen TODO:
1455
+ after Date.new(2011,6,19) do
1456
+ if GOOGLE
1457
+ prev = Dir.glob(TMP_PREFIX)
1458
+ assert_nothing_raised() {
1459
+ oo = Google.new(key_of("no_spreadsheet_file.txt"))
1460
+ }
1461
+ now = Dir.glob(TMP_PREFIX)
1462
+ assert (now-prev).empty?
1463
+ end
1464
+ end
1465
+ end
1466
+
1467
+ # Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
1468
+ # Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
1469
+ # nicht immer in der gleichen Reihenfolge erzeugt.
1470
+ def init_all_cells(oo,sheet)
1471
+ all = []
1472
+ oo.first_row(sheet).upto(oo.last_row(sheet)) do |row|
1473
+ oo.first_column(sheet).upto(oo.last_column(sheet)) do |col|
1474
+ unless oo.empty?(row,col,sheet)
1475
+ all << {:row => row.to_s,
1476
+ :column => col.to_s,
1477
+ :content => oo.cell(row,col,sheet).to_s,
1478
+ :type => oo.celltype(row,col,sheet).to_s,
1479
+ }
1480
+ end
1481
+ end
1482
+ end
1483
+ all
1484
+ end
1485
+
1486
+ def test_to_xml
1487
+ with_each_spreadsheet(:name=>'numbers1', :encoding => 'utf8') do |oo|
1488
+ assert_nothing_raised {oo.to_xml}
1489
+ sheetname = oo.sheets.first
1490
+ doc = Nokogiri::XML(oo.to_xml)
1491
+ sheet_count = 0
1492
+ doc.xpath('//spreadsheet/sheet').each {|tmpelem|
1493
+ sheet_count += 1
1494
+ }
1495
+ assert_equal 5, sheet_count
1496
+ doc.xpath('//spreadsheet/sheet').each { |xml_sheet|
1497
+ all_cells = init_all_cells(oo, sheetname)
1498
+ x = 0
1499
+ assert_equal sheetname, xml_sheet.attributes['name'].value
1500
+ xml_sheet.children.each {|cell|
1501
+ if cell.attributes['name']
1502
+ expected = [all_cells[x][:row],
1503
+ all_cells[x][:column],
1504
+ all_cells[x][:content],
1505
+ all_cells[x][:type],
1506
+ ]
1507
+ result = [
1508
+ cell.attributes['row'],
1509
+ cell.attributes['column'],
1510
+ cell.content,
1511
+ cell.attributes['type'],
1512
+ ]
1513
+ assert_equal expected, result
1514
+ x += 1
1515
+ end # if
1516
+ } # end of sheet
1517
+ sheetname = oo.sheets[oo.sheets.index(sheetname)+1]
1518
+ }
1519
+ end
1520
+ end
1521
+
1522
+ def test_bug_row_column_fixnum_float
1523
+ with_each_spreadsheet(:name=>'bug-row-column-fixnum-float', :format=>:excel) do |oo|
1524
+ assert_equal 42.5, oo.cell('b',2)
1525
+ assert_equal 43 , oo.cell('c',2)
1526
+ assert_equal ['hij',42.5, 43], oo.row(2)
1527
+ assert_equal ['def',42.5, 'nop'], oo.column(2)
1528
+ end
1529
+ end
1530
+
1531
+ def test_file_warning_default
1532
+ after Date.new(2011,7,18) do
1533
+ if OPENOFFICE
1534
+ prev = Dir.glob(TMP_PREFIX)
1535
+ assert_raises(TypeError, "test/numbers1.xls is not an openoffice spreadsheet") { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls")) }
1536
+ assert_raises(TypeError) { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx")) }
1537
+ now = Dir.glob(TMP_PREFIX)
1538
+ assert (now-prev).empty?
1539
+ end
1540
+ if EXCEL
1541
+ prev = Dir.glob(TMP_PREFIX)
1542
+ assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.ods")) }
1543
+ assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx")) }
1544
+ now = Dir.glob(TMP_PREFIX)
1545
+ assert (now-prev).empty?, "temporary directory was not removed"
1546
+ end
1547
+ if EXCELX
1548
+ prev = Dir.glob(TMP_PREFIX)
1549
+ assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.ods")) }
1550
+ assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.xls")) }
1551
+ now = Dir.glob(TMP_PREFIX)
1552
+ assert (now-prev).empty?
1553
+ end
1554
+ end
1555
+ end
1556
+
1557
+ def test_file_warning_error
1558
+ after Date.new(2011,7,22) do
1559
+ if OPENOFFICE
1560
+ prev = Dir.glob(TMP_PREFIX)
1561
+ assert_raises(TypeError) { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false,:error) }
1562
+ assert_raises(TypeError) { oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false,:error) }
1563
+ now = Dir.glob(TMP_PREFIX)
1564
+ assert (now-prev).empty?
1565
+ end
1566
+ if EXCEL
1567
+ prev = Dir.glob(TMP_PREFIX)
1568
+ assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.ods"),false,:error) }
1569
+ assert_raises(TypeError) { oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false,:error) }
1570
+ now = Dir.glob(TMP_PREFIX)
1571
+ assert (now-prev).empty?, "temporary directory was not deleted"
1572
+ end
1573
+ if EXCELX
1574
+ prev = Dir.glob(TMP_PREFIX)
1575
+ assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.ods"),false,:error) }
1576
+ assert_raises(TypeError) { oo = Excelx.new(File.join(TESTDIR,"numbers1.xls"),false,:error) }
1577
+ now = Dir.glob(TMP_PREFIX)
1578
+ assert (now-prev).empty?, "temporary directory was not deleted"
1579
+ end
1580
+ end
1581
+ end
1582
+
1583
+ def test_file_warning_warning
1584
+ after Date.new(2011,7,22) do
1585
+ if OPENOFFICE
1586
+ assert_nothing_raised(TypeError) {
1587
+ assert_raises(Zip::ZipError) {
1588
+ oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false, :warning)
1589
+ }
1590
+ }
1591
+ prev = Dir.glob(TMP_PREFIX)
1592
+ assert_nothing_raised(TypeError) {
1593
+ assert_raises(Errno::ENOENT) {
1594
+ oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false, :warning)
1595
+ }
1596
+ }
1597
+ now = Dir.glob(TMP_PREFIX)
1598
+ assert (now-prev).empty?
1599
+ end
1600
+ if EXCEL
1601
+ prev = Dir.glob(TMP_PREFIX)
1602
+ assert_nothing_raised(TypeError) {
1603
+ assert_raises(Ole::Storage::FormatError) {
1604
+ oo = Excel.new(File.join(TESTDIR,"numbers1.ods"),false, :warning) }
1605
+ }
1606
+ assert_nothing_raised(TypeError) {
1607
+ assert_raises(Ole::Storage::FormatError) {
1608
+ oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false, :warning) }
1609
+ }
1610
+ now = Dir.glob(TMP_PREFIX)
1611
+ assert (now-prev).empty?, "temporary directory was not removed"
1612
+ end
1613
+ if EXCELX
1614
+ prev = Dir.glob(TMP_PREFIX)
1615
+ assert_nothing_raised(TypeError) {
1616
+ assert_raises(Errno::ENOENT) {
1617
+ oo = Excelx.new(File.join(TESTDIR,"numbers1.ods"),false, :warning) }
1618
+ }
1619
+ assert_nothing_raised(TypeError) {
1620
+ assert_raises(Zip::ZipError) {
1621
+ oo = Excelx.new(File.join(TESTDIR,"numbers1.xls"),false, :warning) }
1622
+ }
1623
+ now = Dir.glob(TMP_PREFIX)
1624
+ assert (now-prev).empty?
1625
+ end
1626
+ end
1627
+ end
1628
+
1629
+ def test_file_warning_ignore
1630
+ after Date.new(2011,7,22) do
1631
+ if OPENOFFICE
1632
+ # Files, die eigentlich Openoffice-
1633
+ # Files sind, aber die falsche Endung haben.
1634
+ # Es soll ohne Fehlermeldung oder Warnung
1635
+ # oder Abbruch die Datei geoffnet werden
1636
+
1637
+ # xls
1638
+ assert_nothing_raised() {
1639
+ oo = Openoffice.new(File.join(TESTDIR,"type_openoffice.xls"),false, :ignore)
1640
+ }
1641
+ # xlsx
1642
+ assert_nothing_raised() {
1643
+ oo = Openoffice.new(File.join(TESTDIR,"type_openoffice.xlsx"),false, :ignore)
1644
+ }
1645
+ end
1646
+ if EXCEL
1647
+ assert_nothing_raised() {
1648
+ oo = Excel.new(File.join(TESTDIR,"type_excel.ods"),false, :ignore)
1649
+ }
1650
+ assert_nothing_raised() {
1651
+ oo = Excel.new(File.join(TESTDIR,"type_excel.xlsx"),false, :ignore)
1652
+ }
1653
+ end
1654
+ if EXCELX
1655
+ assert_nothing_raised() {
1656
+ oo = Excelx.new(File.join(TESTDIR,"type_excelx.ods"),false, :ignore)
1657
+ }
1658
+ assert_nothing_raised() {
1659
+ oo = Excelx.new(File.join(TESTDIR,"type_excelx.xls"),false, :ignore)
1660
+ }
1661
+ end
1662
+ =begin
1663
+ if OPENOFFICE
1664
+ assert_nothing_raised() {
1665
+ assert_raises(Zip::ZipError) {
1666
+ oo = Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false, :ignore) }
1667
+ }
1668
+ assert_nothing_raised() {
1669
+ assert_raises(Errno::ENOENT) {
1670
+ oo = Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false, :ignore) }
1671
+ }
1672
+ assert Dir.glob(TMP_PREFIX).empty?
1673
+ end
1674
+ if EXCEL
1675
+ assert_nothing_raised(TypeError) {
1676
+ assert_raises(Ole::Storage::FormatError) {
1677
+ oo = Excel.new(File.join(TESTDIR,"numbers1.ods"),false, :ignore) }
1678
+ }
1679
+ assert_nothing_raised(TypeError) {
1680
+ assert_raises(Ole::Storage::FormatError) {oo = Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false, :ignore) }}
1681
+ assert Dir.glob(TMP_PREFIX).empty?
1682
+ end
1683
+ if EXCELX
1684
+ assert_nothing_raised(TypeError) {
1685
+ assert_raises(Errno::ENOENT) {
1686
+ oo = Excelx.new(File.join(TESTDIR,"numbers1.ods"),false, :ignore)
1687
+ }
1688
+ }
1689
+ assert_nothing_raised(TypeError) {
1690
+ assert_raises(Zip::ZipError) {
1691
+ oo = Excelx.new(File.join(TESTDIR,"numbers1.xls"),false, :ignore)
1692
+ }
1693
+ }
1694
+ assert Dir.glob(TMP_PREFIX).empty?
1695
+ end
1696
+ =end
1697
+ end
1698
+ end
1699
+
1700
+ def test_bug_last_row_excel
1701
+ with_each_spreadsheet(:name=>'time-test', :format=>:excel) do |oo|
1702
+ assert_equal 2, oo.last_row
1703
+ end
1704
+ end
1705
+
1706
+ def test_bug_to_xml_with_empty_sheets
1707
+ with_each_spreadsheet(:name=>'emptysheets', :format=>[:openoffice, :excel]) do |oo|
1708
+ oo.sheets.each { |sheet|
1709
+ assert_equal nil, oo.first_row, "first_row not nil in sheet #{sheet}"
1710
+ assert_equal nil, oo.last_row, "last_row not nil in sheet #{sheet}"
1711
+ assert_equal nil, oo.first_column, "first_column not nil in sheet #{sheet}"
1712
+ assert_equal nil, oo.last_column, "last_column not nil in sheet #{sheet}"
1713
+ assert_equal nil, oo.first_row(sheet), "first_row not nil in sheet #{sheet}"
1714
+ assert_equal nil, oo.last_row(sheet), "last_row not nil in sheet #{sheet}"
1715
+ assert_equal nil, oo.first_column(sheet), "first_column not nil in sheet #{sheet}"
1716
+ assert_equal nil, oo.last_column(sheet), "last_column not nil in sheet #{sheet}"
1717
+ }
1718
+ assert_nothing_raised() { result = oo.to_xml }
1719
+ end
1720
+ end
1721
+
1722
+ def test_bug_simple_spreadsheet_time_bug
1723
+ # really a bug? are cells really of type time?
1724
+ # No! :float must be the correct type
1725
+ with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1726
+ # puts oo.cell('B',5).to_s
1727
+ # assert_equal :time, oo.celltype('B',5)
1728
+ assert_equal :float, oo.celltype('B',5)
1729
+ assert_equal 10.75, oo.cell('B',5)
1730
+ assert_equal 12.50, oo.cell('C',5)
1731
+ assert_equal 0, oo.cell('D',5)
1732
+ assert_equal 1.75, oo.cell('E',5)
1733
+ assert_equal 'Task 1', oo.cell('F',5)
1734
+ assert_equal Date.new(2007,5,7), oo.cell('A',5)
1735
+ end
1736
+ end
1737
+
1738
+ def test_simple2_excelx
1739
+ with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1740
+ assert_equal [:numeric_or_formula, "yyyy\\-mm\\-dd"], oo.excelx_type('A',4)
1741
+ assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('B',4)
1742
+ assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('c',4)
1743
+ assert_equal [:numeric_or_formula, "General"], oo.excelx_type('d',4)
1744
+ assert_equal [:numeric_or_formula, "General"], oo.excelx_type('e',4)
1745
+ assert_equal :string, oo.excelx_type('f',4)
1746
+
1747
+ assert_equal "39209", oo.excelx_value('a',4)
1748
+ assert_equal "yyyy\\-mm\\-dd", oo.excelx_format('a',4)
1749
+ assert_equal "9.25", oo.excelx_value('b',4)
1750
+ assert_equal "10.25", oo.excelx_value('c',4)
1751
+ assert_equal "0", oo.excelx_value('d',4)
1752
+ #... Sum-Spalte
1753
+ # assert_equal "Task 1", oo.excelx_value('f',4)
1754
+ assert_equal "Task 1", oo.cell('f',4)
1755
+ assert_equal Date.new(2007,05,07), oo.cell('a',4)
1756
+ assert_equal "9.25", oo.excelx_value('b',4)
1757
+ assert_equal "#,##0.00", oo.excelx_format('b',4)
1758
+ assert_equal 9.25, oo.cell('b',4)
1759
+ assert_equal :float, oo.celltype('b',4)
1760
+ assert_equal :float, oo.celltype('d',4)
1761
+ assert_equal 0, oo.cell('d',4)
1762
+ assert_equal :formula, oo.celltype('e',4)
1763
+ assert_equal 1, oo.cell('e',4)
1764
+ assert_equal 'C4-B4-D4', oo.formula('e',4)
1765
+ assert_equal :string, oo.celltype('f',4)
1766
+ assert_equal "Task 1", oo.cell('f',4)
1767
+ end
1768
+ end
1769
+
1770
+ def test_datetime
1771
+ with_each_spreadsheet(:name=>'datetime') do |oo|
1772
+ val = oo.cell('c',3)
1773
+ assert_kind_of DateTime, val
1774
+ assert_equal :datetime, oo.celltype('c',3)
1775
+ assert_equal DateTime.new(1961,11,21,12,17,18), val
1776
+ val = oo.cell('a',1)
1777
+ assert_kind_of Date, val
1778
+ assert_equal :date, oo.celltype('a',1)
1779
+ assert_equal Date.new(1961,11,21), val
1780
+ assert_equal Date.new(1961,11,21), oo.cell('a',1)
1781
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',3)
1782
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',3)
1783
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',3)
1784
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',4)
1785
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',4)
1786
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',4)
1787
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',5)
1788
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',5)
1789
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',5)
1790
+ assert_equal Date.new(1961,11,21), oo.cell('a',6)
1791
+ assert_equal Date.new(1961,11,21), oo.cell('b',6)
1792
+ assert_equal Date.new(1961,11,21), oo.cell('c',6)
1793
+ assert_equal Date.new(1961,11,21), oo.cell('a',7)
1794
+ assert_equal Date.new(1961,11,21), oo.cell('b',7)
1795
+ assert_equal Date.new(1961,11,21), oo.cell('c',7)
1796
+ end
1797
+ end
1798
+
1799
+ def test_cell_openoffice_html_escape
1800
+ with_each_spreadsheet(:name=>'html-escape', :format=>:openoffice) do |oo|
1801
+ assert_equal "'", oo.cell(1,1)
1802
+ assert_equal "&", oo.cell(2,1)
1803
+ assert_equal ">", oo.cell(3,1)
1804
+ assert_equal "<", oo.cell(4,1)
1805
+ assert_equal "`", oo.cell(5,1)
1806
+ # test_openoffice_zipped will catch issues with &quot;
1807
+ end
1808
+ end
1809
+
1810
+ def test_cell_boolean
1811
+ with_each_spreadsheet(:name=>'boolean', :format=>[:openoffice, :excel, :excelx]) do |oo|
1812
+ if oo.class == Excelx
1813
+ assert_equal "TRUE", oo.cell(1,1), "failure in "+oo.class.to_s
1814
+ assert_equal "FALSE", oo.cell(2,1), "failure in "+oo.class.to_s
1815
+ else
1816
+ assert_equal "true", oo.cell(1,1), "failure in "+oo.class.to_s
1817
+ assert_equal "false", oo.cell(2,1), "failure in "+oo.class.to_s
1818
+ end
1819
+ end
1820
+ end
1821
+
1822
+ def test_cell_multiline
1823
+ with_each_spreadsheet(:name=>'paragraph', :format=>[:openoffice, :excel, :excelx]) do |oo|
1824
+ assert_equal "This is a test\nof a multiline\nCell", oo.cell(1,1)
1825
+ assert_equal "This is a test\n¶\nof a multiline\n\nCell", oo.cell(1,2)
1826
+ assert_equal "first p\n\nsecond p\n\nlast p", oo.cell(2,1)
1827
+ end
1828
+ end
1829
+
1830
+ def test_cell_styles
1831
+ # styles only valid in excel spreadsheets?
1832
+ # TODO: what todo with other spreadsheet types
1833
+ with_each_spreadsheet(:name=>'style', :format=>[# :openoffice,
1834
+ :excel,
1835
+ # :excelx
1836
+ ]) do |oo|
1837
+ # bold
1838
+ assert_equal true, oo.font(1,1).bold?
1839
+ assert_equal false, oo.font(1,1).italic?
1840
+ assert_equal false, oo.font(1,1).underline?
1841
+
1842
+ # italic
1843
+ assert_equal false, oo.font(2,1).bold?
1844
+ assert_equal true, oo.font(2,1).italic?
1845
+ assert_equal false, oo.font(2,1).underline?
1846
+
1847
+ # normal
1848
+ assert_equal false, oo.font(3,1).bold?
1849
+ assert_equal false, oo.font(3,1).italic?
1850
+ assert_equal false, oo.font(3,1).underline?
1851
+
1852
+ # underline
1853
+ assert_equal false, oo.font(4,1).bold?
1854
+ assert_equal false, oo.font(4,1).italic?
1855
+ assert_equal true, oo.font(4,1).underline?
1856
+
1857
+ # bold italic
1858
+ assert_equal true, oo.font(5,1).bold?
1859
+ assert_equal true, oo.font(5,1).italic?
1860
+ assert_equal false, oo.font(5,1).underline?
1861
+
1862
+ # bold underline
1863
+ assert_equal true, oo.font(6,1).bold?
1864
+ assert_equal false, oo.font(6,1).italic?
1865
+ assert_equal true, oo.font(6,1).underline?
1866
+
1867
+ # italic underline
1868
+ assert_equal false, oo.font(7,1).bold?
1869
+ assert_equal true, oo.font(7,1).italic?
1870
+ assert_equal true, oo.font(7,1).underline?
1871
+
1872
+ # bolded row
1873
+ assert_equal true, oo.font(8,1).bold?
1874
+ assert_equal false, oo.font(8,1).italic?
1875
+ assert_equal false, oo.font(8,1).underline?
1876
+
1877
+ # bolded col
1878
+ assert_equal true, oo.font(9,2).bold?
1879
+ assert_equal false, oo.font(9,2).italic?
1880
+ assert_equal false, oo.font(9,2).underline?
1881
+
1882
+ # bolded row, italic col
1883
+ assert_equal true, oo.font(10,3).bold?
1884
+ assert_equal true, oo.font(10,3).italic?
1885
+ assert_equal false, oo.font(10,3).underline?
1886
+
1887
+ # normal
1888
+ assert_equal false, oo.font(11,4).bold?
1889
+ assert_equal false, oo.font(11,4).italic?
1890
+ assert_equal false, oo.font(11,4).underline?
1891
+ end
1892
+ end
1893
+
1894
+ # If a cell has a date-like string but is preceeded by a '
1895
+ # to force that date to be treated like a string, we were getting an exception.
1896
+ # This test just checks for that exception to make sure it's not raised in this case
1897
+ def test_date_to_float_conversion
1898
+ with_each_spreadsheet(:name=>'datetime_floatconv', :format=>:excel) do |oo|
1899
+ assert_nothing_raised(NoMethodError) do
1900
+ oo.cell('a',1)
1901
+ oo.cell('a',2)
1902
+ end
1903
+ end
1904
+ end
1905
+
1906
+ # Need to extend to other formats
1907
+ def test_row_whitespace
1908
+ # auf dieses Dokument habe ich keinen Zugriff TODO:
1909
+ with_each_spreadsheet(:name=>'whitespace') do |oo|
1910
+ oo.default_sheet = "Sheet1"
1911
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(1)
1912
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(2)
1913
+ assert_equal ["Date", "Start time", "End time", "Pause", "Sum", "Comment"], oo.row(3)
1914
+ assert_equal [Date.new(2007,5,7), 9.25, 10.25, 0.0, 1.0, "Task 1"], oo.row(4)
1915
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(5)
1916
+ assert_equal [Date.new(2007,5,7), 10.75, 10.75, 0.0, 0.0, "Task 1"], oo.row(6)
1917
+ oo.default_sheet = "Sheet2"
1918
+ assert_equal ["Date", nil, "Start time"], oo.row(1)
1919
+ assert_equal [Date.new(2007,5,7), nil, 9.25], oo.row(2)
1920
+ assert_equal [Date.new(2007,5,7), nil, 10.75], oo.row(3)
1921
+ end
1922
+ end
1923
+
1924
+ def test_col_whitespace
1925
+ #TODO:
1926
+ # kein Zugriff auf Dokument whitespace
1927
+ with_each_spreadsheet(:name=>'whitespace') do |oo|
1928
+ oo.default_sheet = "Sheet1"
1929
+ assert_equal ["Date", Date.new(2007,5,7), nil, Date.new(2007,5,7)], oo.column(1)
1930
+ assert_equal ["Start time", 9.25, nil, 10.75], oo.column(2)
1931
+ assert_equal ["End time", 10.25, nil, 10.75], oo.column(3)
1932
+ assert_equal ["Pause", 0.0, nil, 0.0], oo.column(4)
1933
+ assert_equal ["Sum", 1.0, nil, 0.0], oo.column(5)
1934
+ assert_equal ["Comment","Task 1", nil, "Task 1"], oo.column(6)
1935
+ oo.default_sheet = "Sheet2"
1936
+ assert_equal [nil, nil, nil], oo.column(1)
1937
+ assert_equal [nil, nil, nil], oo.column(2)
1938
+ assert_equal ["Date", Date.new(2007,5,7), Date.new(2007,5,7)], oo.column(3)
1939
+ assert_equal [nil, nil, nil], oo.column(4)
1940
+ assert_equal [ "Start time", 9.25, 10.75], oo.column(5)
1941
+ end
1942
+ end
1943
+
1944
+ # Excel has two base date formats one from 1900 and the other from 1904.
1945
+ # There's a MS bug that 1900 base dates include an extra day due to erroneously
1946
+ # including 1900 as a leap yar.
1947
+ def test_base_dates_in_excel
1948
+ with_each_spreadsheet(:name=>'1900_base', :format=>:excel) do |oo|
1949
+ assert_equal Date.new(2009,06,15), oo.cell(1,1)
1950
+ #we don't want to to 'interpret' formulas assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
1951
+ # if we test TODAY() we have also have to calculate
1952
+ # other date calculations
1953
+ #
1954
+ assert_equal :date, oo.celltype(1,1)
1955
+ end
1956
+ with_each_spreadsheet(:name=>'1904_base', :format=>:excel) do |oo|
1957
+ assert_equal Date.new(2009,06,15), oo.cell(1,1)
1958
+ # see comment above
1959
+ # assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
1960
+ assert_equal :date, oo.celltype(1,1)
1961
+ end
1962
+ end
1963
+
1964
+ def test_bad_date
1965
+ with_each_spreadsheet(:name=>'prova', :format=>:excel) do |oo|
1966
+ assert_nothing_raised(ArgumentError) {
1967
+ assert_equal DateTime.new(2006,2,2,10,0,0), oo.cell('a',1)
1968
+ }
1969
+ end # each
1970
+ end
1971
+
1972
+ def test_cell_methods
1973
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
1974
+ assert_equal 10, oo.a4 # cell(4,'A')
1975
+ assert_equal 11, oo.b4 # cell(4,'B')
1976
+ assert_equal 12, oo.c4 # cell(4,'C')
1977
+ assert_equal 13, oo.d4 # cell(4,'D')
1978
+ assert_equal 14, oo.e4 # cell(4,'E')
1979
+ assert_equal 'ABC', oo.c6('Sheet5')
1980
+
1981
+ #assert_raises(ArgumentError) {
1982
+ assert_raises(NoMethodError) {
1983
+ # a42a is not a valid cell name, should raise ArgumentError
1984
+ assert_equal 9999, oo.a42a
1985
+ }
1986
+ end
1987
+ end
1988
+
1989
+
1990
+ # compare large spreadsheets
1991
+ def test_compare_large_spreadsheets
1992
+ # problematisch, weil Formeln in Excel nicht unterstützt werden
1993
+ if LONG_RUN
1994
+ qq = Openoffice.new(File.join('test',"Bibelbund.ods"))
1995
+ with_each_spreadsheet(:name=>'Bibelbund') do |oo|
1996
+ # p "comparing Bibelbund.ods with #{oo.class}"
1997
+ oo.sheets.each do |sh|
1998
+ oo.first_row.upto(oo.last_row) do |row|
1999
+ oo.first_column.upto(oo.last_column) do |col|
2000
+ c1 = qq.cell(row,col,sh)
2001
+ c1.force_encoding("UTF-8") if c1.class == String
2002
+ c2 = oo.cell(row,col,sh)
2003
+ c2.force_encoding("UTF-8") if c2.class == String
2004
+ assert_equal c1, c2, "diff in #{sh}/#{row}/#{col}}"
2005
+ assert_equal qq.celltype(row,col,sh), oo.celltype(row,col,sh)
2006
+ assert_equal qq.formula?(row,col,sh), oo.formula?(row,col,sh) if oo.class != Excel
2007
+ end
2008
+ end
2009
+ end
2010
+ end
2011
+ end # LONG_RUN
2012
+ end
2013
+
2014
+ def test_labeled_cells
2015
+ # to do
2016
+ # "more spreadsheet types"
2017
+ # end
2018
+ with_each_spreadsheet(:name=>'named_cells', :format=>:openoffice) do |oo|
2019
+ oo.default_sheet = oo.sheets.first
2020
+ begin
2021
+ row,col = oo.label('anton')
2022
+ rescue ArgumentError
2023
+ puts "labels error at #{oo.class}"
2024
+ raise
2025
+ end
2026
+ assert_equal 5, row
2027
+ assert_equal 3, col
2028
+
2029
+ row,col = oo.label('anton')
2030
+ assert_equal 'Anton', oo.cell(row,col)
2031
+
2032
+ row,col = oo.label('berta')
2033
+ assert_equal 'Bertha', oo.cell(row,col)
2034
+
2035
+ row,col = oo.label('caesar')
2036
+ assert_equal 'Cäsar', oo.cell(row,col)
2037
+
2038
+ row,col = oo.label('never')
2039
+ assert_nil row
2040
+ assert_nil col
2041
+
2042
+ row,col,sheet = oo.label('anton')
2043
+ assert_equal 5, row
2044
+ assert_equal 3, col
2045
+ assert_equal "Sheet1", sheet
2046
+
2047
+ assert_equal "Anton", oo.anton
2048
+ assert_raises(NoMethodError) {
2049
+ row,col = oo.never
2050
+ }
2051
+ end
2052
+ end
2053
+
2054
+
2055
+ def test_bug_excel_last_row_255
2056
+ if LONG_RUN
2057
+ local_only do
2058
+ after Date.new(2011,6,17) do
2059
+ oo = Excel.new(File.join('..','confidential','ScienceStaff.xls'))
2060
+ oo.default_sheet = oo.sheets.first
2061
+ assert_equal "COMSCI", oo.cell(255,1)
2062
+ assert_equal "lala", oo.cell(256,1)
2063
+ assert_equal 1537, oo.last_row
2064
+ end
2065
+ end
2066
+ end
2067
+ end
2068
+
2069
+ def test_bug_excel_last_row_255_modified
2070
+ local_only do
2071
+ oo = Excel.new(File.join('..','confidential','ScienceStaff_modified.xls'))
2072
+ oo.default_sheet = oo.sheets.first
2073
+ assert_equal 1537, oo.last_row
2074
+ end
2075
+ end
2076
+
2077
+ require 'matrix'
2078
+ def test_matrix
2079
+ with_each_spreadsheet(:name => 'matrix', :format => [:openoffice, :excel, :google]) do |oo|
2080
+ oo.default_sheet = oo.sheets.first
2081
+ assert_equal Matrix[
2082
+ [1.0, 2.0, 3.0],
2083
+ [4.0, 5.0, 6.0],
2084
+ [7.0, 8.0, 9.0] ], oo.to_matrix
2085
+ end
2086
+ end
2087
+
2088
+ def test_matrix_selected_range
2089
+ with_each_spreadsheet(:name => 'matrix', :format=>[:excel,:openoffice,:google]) do |oo|
2090
+ oo.default_sheet = 'Sheet2'
2091
+ assert_equal Matrix[
2092
+ [1.0, 2.0, 3.0],
2093
+ [4.0, 5.0, 6.0],
2094
+ [7.0, 8.0, 9.0] ], oo.to_matrix(3,4,5,6)
2095
+ end
2096
+ end
2097
+
2098
+ def test_matrix_all_nil
2099
+ with_each_spreadsheet(:name => 'matrix', :format=>[:excel,:openoffice,:google]) do |oo|
2100
+ oo.default_sheet = 'Sheet2'
2101
+ assert_equal Matrix[
2102
+ [nil, nil, nil],
2103
+ [nil, nil, nil],
2104
+ [nil, nil, nil] ], oo.to_matrix(10,10,12,12)
2105
+ end
2106
+ end
2107
+
2108
+ def test_matrix_values_and_nil
2109
+ with_each_spreadsheet(:name => 'matrix', :format=>[:excel,:openoffice,:google]) do |oo|
2110
+ oo.default_sheet = 'Sheet3'
2111
+ assert_equal Matrix[
2112
+ [1.0, nil, 3.0],
2113
+ [4.0, 5.0, 6.0],
2114
+ [7.0, 8.0, nil] ], oo.to_matrix(1,1,3,3)
2115
+ end end
2116
+
2117
+ def test_bug_date_mileszs
2118
+ after Date.new(2011,7,17) do
2119
+ # to do
2120
+ # "An richtige Stelle kopieren. Ist das Dokument vertraulich?"
2121
+ # 'ist auf dem Netbook nicht vorhanden'
2122
+ # end
2123
+ oo = Excel.new "/home/tp/Documents/feb-sales-analysis.xls"
2124
+ oo.default_sheet = oo.sheets.first
2125
+ # 2/1/2010 A2-A6 mm/dd/yyyy
2126
+ 2.upto(6) do |i|
2127
+ assert_equal Date.new(2010,2,1), oo.cell('A',i)
2128
+ end
2129
+ end
2130
+ end
2131
+
2132
+ #def test_no_generic_new
2133
+ #oo = GenericSpreadsheet.new
2134
+ #assert_equal GenericSpreadsheet, oo.class
2135
+ #end
2136
+
2137
+ # unter Windows soll es laut Bug-Reports nicht moeglich sein, eine Excel-Datei, die
2138
+ # mit Excel.new geoeffnet wurde nach dem Processing anschliessend zu loeschen.
2139
+ # Anmerkung: Das Spreadsheet-Gem erlaubt kein explizites Close von Spreadsheet-Dateien,
2140
+ # was verhindern koennte, das die Datei geloescht werden kann.
2141
+ def test_bug_cannot_delete_opened_excel_sheet
2142
+ # with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
2143
+ after Date.new(2011,7,17) do
2144
+ # to do
2145
+ # 'kopiere nach temporaere Datei
2146
+ # und versuche diese zu oeffnen und zu loeschen'
2147
+ # end
2148
+ end
2149
+ #end
2150
+ end
2151
+
2152
+ def test_bug_xlsx_reference_cell
2153
+ =begin
2154
+ If cell A contains a string and cell B references cell A. When reading the value of cell B, the result will be
2155
+ "0.0" instead of the value of cell A.
2156
+
2157
+ With the attached test case, I ran the following code:
2158
+ spreadsheet = Excelx.new('formula_string_error.xlsx')
2159
+ spreadsheet.default_sheet = 'sheet1'
2160
+ p "A: #{spreadsheet.cell(1, 1)}"
2161
+ p "B: #{spreadsheet.cell(2, 1)}"
2162
+
2163
+ with the following results
2164
+ "A: TestString"
2165
+ "B: 0.0"
2166
+
2167
+ where the expected result is
2168
+ "A: TestString"
2169
+ "B: TestString"
2170
+ =end
2171
+ xlsx = Excelx.new(File.join('test', "formula_string_error.xlsx"))
2172
+ xlsx.default_sheet = xlsx.sheets.first
2173
+ assert_equal 'Teststring', xlsx.cell('a',1)
2174
+ assert_equal 'Teststring', xlsx.cell('a',2)
2175
+ end
2176
+
2177
+ def test_bug_guest_list_2011_05_05
2178
+ after Date.new(2011,7,17) do
2179
+ oo = Excel.new(File.join("..","confidential","guest_list_addresses.xls"))
2180
+ oo.default_sheet = oo.sheets.first
2181
+ assert_equal "lalala", oo.cell('a',1) # anderer Inhalt im Spreadsheet
2182
+ assert_equal :string, oo.celltype('a',1)
2183
+ end
2184
+ end
2185
+
2186
+ def test_bug_guest_list_2011_05_05_spreadsheet
2187
+ # to do
2188
+ # 'wieder entfernen'
2189
+ # end
2190
+ require 'spreadsheet'
2191
+ book = Spreadsheet.open File.join('..','confidential','guest_list_addresses.xls')
2192
+ sheet1 = book.worksheet 0
2193
+ sheet1.each do |row|
2194
+ p row[0]
2195
+
2196
+ end
2197
+ end
2198
+
2199
+ # don't test it with other spreadsheet types! this was only a problem
2200
+ # with .xlsx files
2201
+ def test_bug_date_not_recognized_2011_05_21
2202
+ #oo = Excelx.new(File.join(TESTDIR,'2011-05-21_sample_date_problem.xlsx'))
2203
+ oo = Excelx.new(File.join('..','confidential','2011-05-21_sample_date_problem.xlsx'))
2204
+ oo.default_sheet = oo.sheets.first
2205
+ assert_equal Date.new(2011,3,24), oo.b4
2206
+ assert_equal Date.new(2011,3,25), oo.b5
2207
+ assert_equal Date.new(2011,5,5), oo.b6
2208
+ assert_equal Date.new(2012,3,23), oo.b7
2209
+ end
2210
+
2211
+ def test_bug_string_as_a_date_2011_05_21_spreadsheet_only
2212
+ after Date.new(2011,6,24) do
2213
+ # to do
2214
+ # 'wieder entfernen'
2215
+ # end
2216
+ require 'spreadsheet'
2217
+ book = Spreadsheet.open File.join('..','confidential','2011-05-21_sample_type_problem.xls')
2218
+ sheet1 = book.worksheet 0
2219
+ sheet1.each_with_index do |row,rownum|
2220
+ # p row[0]
2221
+ if rownum == 2
2222
+ assert_equal 68, row[6]
2223
+ end
2224
+
2225
+ end
2226
+ end
2227
+ end
2228
+
2229
+ def test_bug_string_as_a_date_2011_05_21
2230
+ after Date.new(2011,7,22) do
2231
+ #oo = Excel.new(File.join(TESTDIR,'2011-05-21_sample_type_problem.xls'))
2232
+ oo = Excel.new(File.join('..','confidential','2011-05-21_sample_type_problem.xls'))
2233
+ oo.default_sheet = oo.sheets.first
2234
+ assert_equal 68, oo.g2
2235
+ assert_equal 72, oo.g3
2236
+ assert_equal 75, oo.g4
2237
+ assert_equal 76, oo.g5
2238
+ assert_equal 77, oo.g6
2239
+ assert_equal 78, oo.g7
2240
+ end
2241
+ end
2242
+
2243
+ def test_bug_string_as_a_date_2011_05_21_saved_as_ods
2244
+ #oo = Openoffice.new(File.join(TESTDIR,'2011-05-21_sample_type_problem.ods'))
2245
+ oo = Openoffice.new(File.join('..','confidential','2011-05-21_sample_type_problem.ods'))
2246
+ oo.default_sheet = oo.sheets.first
2247
+ assert_equal 68, oo.g2
2248
+ assert_equal 72, oo.g3
2249
+ assert_equal 75, oo.g4
2250
+ assert_equal 76, oo.g5
2251
+ assert_equal 77, oo.g6
2252
+ assert_equal 78, oo.g7
2253
+ end
2254
+ end # class