jagthedrummer-roo 1.3.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/History.txt +225 -0
  2. data/README.markdown +55 -0
  3. data/examples/roo_soap_client.rb +53 -0
  4. data/examples/roo_soap_server.rb +29 -0
  5. data/examples/write_me.rb +33 -0
  6. data/lib/roo.rb +29 -0
  7. data/lib/roo/excel.rb +469 -0
  8. data/lib/roo/excelx.rb +604 -0
  9. data/lib/roo/generic_spreadsheet.rb +626 -0
  10. data/lib/roo/google.rb +381 -0
  11. data/lib/roo/openoffice.rb +454 -0
  12. data/lib/roo/roo_rails_helper.rb +82 -0
  13. data/lib/roo/version.rb +9 -0
  14. data/test/1900_base.xls +0 -0
  15. data/test/1904_base.xls +0 -0
  16. data/test/Bibelbund.csv +3741 -0
  17. data/test/Bibelbund.ods +0 -0
  18. data/test/Bibelbund.xls +0 -0
  19. data/test/Bibelbund.xlsx +0 -0
  20. data/test/Bibelbund1.ods +0 -0
  21. data/test/bad_excel_date.xls +0 -0
  22. data/test/bbu.ods +0 -0
  23. data/test/bbu.xls +0 -0
  24. data/test/bbu.xlsx +0 -0
  25. data/test/bode-v1.ods.zip +0 -0
  26. data/test/bode-v1.xls.zip +0 -0
  27. data/test/boolean.ods +0 -0
  28. data/test/boolean.xls +0 -0
  29. data/test/boolean.xlsx +0 -0
  30. data/test/borders.ods +0 -0
  31. data/test/borders.xls +0 -0
  32. data/test/borders.xlsx +0 -0
  33. data/test/bug-row-column-fixnum-float.xls +0 -0
  34. data/test/datetime.ods +0 -0
  35. data/test/datetime.xls +0 -0
  36. data/test/datetime.xlsx +0 -0
  37. data/test/datetime_floatconv.xls +0 -0
  38. data/test/emptysheets.ods +0 -0
  39. data/test/emptysheets.xls +0 -0
  40. data/test/false_encoding.xls +0 -0
  41. data/test/formula.ods +0 -0
  42. data/test/formula.xls +0 -0
  43. data/test/formula.xlsx +0 -0
  44. data/test/formula_parse_error.xls +0 -0
  45. data/test/html-escape.ods +0 -0
  46. data/test/no_spreadsheet_file.txt +1 -0
  47. data/test/numbers1.csv +18 -0
  48. data/test/numbers1.ods +0 -0
  49. data/test/numbers1.xls +0 -0
  50. data/test/numbers1.xlsx +0 -0
  51. data/test/only_one_sheet.ods +0 -0
  52. data/test/only_one_sheet.xls +0 -0
  53. data/test/only_one_sheet.xlsx +0 -0
  54. data/test/paragraph.ods +0 -0
  55. data/test/paragraph.xls +0 -0
  56. data/test/paragraph.xlsx +0 -0
  57. data/test/ric.ods +0 -0
  58. data/test/simple_spreadsheet.ods +0 -0
  59. data/test/simple_spreadsheet.xls +0 -0
  60. data/test/simple_spreadsheet.xlsx +0 -0
  61. data/test/simple_spreadsheet_from_italo.ods +0 -0
  62. data/test/simple_spreadsheet_from_italo.xls +0 -0
  63. data/test/skipped_tests.rb +789 -0
  64. data/test/style.ods +0 -0
  65. data/test/style.xls +0 -0
  66. data/test/style.xlsx +0 -0
  67. data/test/test_helper.rb +19 -0
  68. data/test/test_roo.rb +1822 -0
  69. data/test/time-test.csv +2 -0
  70. data/test/time-test.ods +0 -0
  71. data/test/time-test.xls +0 -0
  72. data/test/time-test.xlsx +0 -0
  73. data/test/whitespace.ods +0 -0
  74. data/test/whitespace.xls +0 -0
  75. data/test/whitespace.xlsx +0 -0
  76. metadata +182 -0
Binary file
Binary file
Binary file
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/roo'
3
+
4
+ # helper method
5
+ def after(d)
6
+ yield if DateTime.now > d
7
+ end
8
+
9
+ # helper method
10
+ def before(d)
11
+ yield if DateTime.now <= d
12
+ end
13
+
14
+ # helper method
15
+ def local_only
16
+ if ENV["roo_local"] == "thomas-p"
17
+ yield
18
+ end
19
+ end
@@ -0,0 +1,1822 @@
1
+ #damit keine falschen Vermutungen aufkommen: Ich habe religioes rein gar nichts
2
+ # mit diesem Bibelbund zu tun, aber die hatten eine ziemlich grosse
3
+ # Spreadsheet-Datei mit ca. 3500 Zeilen oeffentlich im Netz, die sich ganz gut
4
+ # zum Testen eignete.
5
+ #
6
+ #--
7
+ # these test cases were developed to run under Linux OS, some commands
8
+ # (like 'diff') must be changed (or commented out ;-)) if you want to run
9
+ # the tests under another OS
10
+ #
11
+
12
+
13
+ #TODO
14
+ # Look at formulas in excel - does not work with date/time
15
+
16
+ #puts "running...."
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
+ "write.me" => 'r6m7HFlUOwst0RTUTuhQ0Ow',
60
+ 'numbers1' => "rYraCzjxTtkxw1NxHJgDU8Q",
61
+ 'borders' => "r_nLYMft6uWg_PT9Rc2urXw",
62
+ 'simple_spreadsheet' => "r3aMMCBCA153TmU_wyIaxfw",
63
+ 'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
64
+ "only_one_sheet" => "rqRtkcPJ97nhQ0m9ksDw2rA",
65
+ 'time-test' => 'r2XfDBJMrLPjmuLrPQQrEYw',
66
+ 'datetime' => "r2kQpXWr6xOSUpw9MyXavYg",
67
+ 'whitespace' => "rZyQaoFebVGeHKzjG6e9gRQ"
68
+ }[spreadsheetname]
69
+ # 'numbers1' => "o10837434939102457526.4784396906364855777",
70
+ # 'borders' => "o10837434939102457526.664868920231926255",
71
+ # 'simple_spreadsheet' => "ptu6bbahNZpYe-L1vEBmgGA",
72
+ # 'testnichtvorhandenBibelbund.ods' => "invalidkeyforanyspreadsheet", # !!! intentionally false key
73
+ # "only_one_sheet" => "o10837434939102457526.762705759906130135",
74
+ # "write.me" => 'ptu6bbahNZpY0N0RrxQbWdw&hl',
75
+ # 'formula' => 'o10837434939102457526.3022866619437760118',
76
+ # 'time-test' => 'ptu6bbahNZpYBMhk01UfXSg',
77
+ # 'datetime' => "ptu6bbahNZpYQEtZwzL_dZQ",
78
+ rescue
79
+ raise "unknown spreadsheetname: #{spreadsheetname}"
80
+ end
81
+ end
82
+
83
+ if DB_LOG
84
+ if ! (defined?(@connected) and @connected)
85
+ activerecord_connect
86
+ else
87
+ @connected = true
88
+ end
89
+ end
90
+ alias unlogged_run run
91
+ def run(result, &block)
92
+ t1 = Time.now
93
+ #RAILS_DEFAULT_LOGGER.debug "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
94
+ if DISPLAY_LOG
95
+ print "RUNNING #{self.class} #{@method_name} \t#{Time.now.to_s}"
96
+ STDOUT.flush
97
+ end
98
+ unlogged_run result, &block
99
+ t2 = Time.now
100
+ if DISPLAY_LOG
101
+ puts "\t#{t2-t1} seconds"
102
+ end
103
+ if DB_LOG
104
+ domain = Testrun.create(
105
+ :class_name => self.class.to_s,
106
+ :test_name => @method_name,
107
+ :start => t1,
108
+ :duration => t2-t1
109
+ )
110
+ end
111
+ end
112
+ end
113
+
114
+ class File
115
+ def File.delete_if_exist(filename)
116
+ if File.exist?(filename)
117
+ File.delete(filename)
118
+ end
119
+ end
120
+ end
121
+
122
+ # :nodoc
123
+ class Fixnum
124
+ def minutes
125
+ self * 60
126
+ end
127
+ end
128
+
129
+ class TestRoo < Test::Unit::TestCase
130
+
131
+ OPENOFFICE = true # do Openoffice-Spreadsheet Tests?
132
+ EXCEL = true # do Excel Tests?
133
+ GOOGLE = false # do Google-Spreadsheet Tests?
134
+ EXCELX = true # do Excel-X Tests? (.xlsx-files)
135
+
136
+ ONLINE = true
137
+ LONG_RUN = false
138
+ GLOBAL_TIMEOUT = 48.minutes #*60 # 2*12*60 # seconds
139
+
140
+ def setup
141
+ #if DISPLAY_LOG
142
+ # puts " GLOBAL_TIMEOUT = #{GLOBAL_TIMEOUT}"
143
+ #end
144
+ end
145
+
146
+ def test_internal_minutes
147
+ assert_equal 42*60, 42.minutes
148
+ end
149
+
150
+ # call a block of code for each spreadsheet type
151
+ # and yield a reference to the roo object
152
+ def with_each_spreadsheet(options)
153
+ options[:format] ||= [:excel, :excelx, :openoffice, :google]
154
+ options[:format] = [options[:format]] if options[:format].class == Symbol
155
+ yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xls')) if EXCEL && options[:format].include?(:excel)
156
+ yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.xlsx')) if EXCELX && options[:format].include?(:excelx)
157
+ yield Roo::Spreadsheet.open(File.join(TESTDIR, options[:name] + '.ods')) if OPENOFFICE && options[:format].include?(:openoffice)
158
+ yield Roo::Spreadsheet.open(key_of(options[:name]) || options[:name]) if GOOGLE && options[:format].include?(:google)
159
+ end
160
+
161
+ def with_public_google_spreadsheet(&block)
162
+ user = ENV['GOOGLE_MAIL']
163
+ pass = ENV['GOOGLE_PASSWORD']
164
+ ENV['GOOGLE_MAIL'] = ''
165
+ ENV['GOOGLE_PASSWORD'] = ''
166
+ block.call
167
+ ENV['GOOGLE_MAIL'] = user
168
+ ENV['GOOGLE_PASSWORD'] = pass
169
+ end
170
+
171
+ # Using Date.strptime so check that it's using the method
172
+ # with the value set in date_format
173
+ def test_date
174
+ with_each_spreadsheet(:name=>'numbers1', :format=>:google) do |oo|
175
+ # should default to DDMMYYYY
176
+ assert oo.date?("21/11/1962") == true
177
+ assert oo.date?("11/21/1962") == false
178
+ oo.date_format = '%m/%d/%Y'
179
+ assert oo.date?("21/11/1962") == false
180
+ assert oo.date?("11/21/1962") == true
181
+ oo.date_format = '%Y-%m-%d'
182
+ assert oo.date?("1962-11-21") == true
183
+ assert oo.date?("1962-21-11") == false
184
+ end
185
+ end
186
+
187
+ def test_classes
188
+ if OPENOFFICE
189
+ oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.ods"))
190
+ assert_kind_of Roo::Openoffice, oo
191
+ end
192
+ if EXCEL
193
+ oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.xls"))
194
+ assert_kind_of Roo::Excel, oo
195
+ end
196
+ if GOOGLE
197
+ oo = Roo::Google.new(key_of("numbers1"))
198
+ assert_kind_of Roo::Google, oo
199
+ end
200
+ if EXCELX
201
+ oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.xlsx"))
202
+ assert_kind_of Roo::Excelx, oo
203
+ end
204
+ end
205
+
206
+ def test_letters
207
+ assert_equal 1, GenericSpreadsheet.letter_to_number('A')
208
+ assert_equal 1, GenericSpreadsheet.letter_to_number('a')
209
+ assert_equal 2, GenericSpreadsheet.letter_to_number('B')
210
+ assert_equal 26, GenericSpreadsheet.letter_to_number('Z')
211
+ assert_equal 27, GenericSpreadsheet.letter_to_number('AA')
212
+ assert_equal 27, GenericSpreadsheet.letter_to_number('aA')
213
+ assert_equal 27, GenericSpreadsheet.letter_to_number('Aa')
214
+ assert_equal 27, GenericSpreadsheet.letter_to_number('aa')
215
+ end
216
+
217
+ def test_sheets
218
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
219
+ assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], oo.sheets
220
+ assert_raise(RangeError) { oo.default_sheet = "no_sheet" }
221
+ assert_raise(TypeError) { oo.default_sheet = [1,2,3] }
222
+ oo.sheets.each { |sh|
223
+ oo.default_sheet = sh
224
+ assert_equal sh, oo.default_sheet
225
+ }
226
+ end
227
+ end
228
+
229
+ def test_cells
230
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
231
+ assert_equal 1, oo.cell(1,1)
232
+ assert_equal 2, oo.cell(1,2)
233
+ assert_equal 3, oo.cell(1,3)
234
+ assert_equal 4, oo.cell(1,4)
235
+ assert_equal 5, oo.cell(2,1)
236
+ assert_equal 6, oo.cell(2,2)
237
+ assert_equal 7, oo.cell(2,3)
238
+ assert_equal 8, oo.cell(2,4)
239
+ assert_equal 9, oo.cell(2,5)
240
+ assert_equal "test", oo.cell(2,6)
241
+ assert_equal :string, oo.celltype(2,6)
242
+ assert_equal 11, oo.cell(2,7)
243
+ assert_equal :float, oo.celltype(2,7)
244
+ assert_equal 10, oo.cell(4,1)
245
+ assert_equal 11, oo.cell(4,2)
246
+ assert_equal 12, oo.cell(4,3)
247
+ assert_equal 13, oo.cell(4,4)
248
+ assert_equal 14, oo.cell(4,5)
249
+ assert_equal 10, oo.cell(4,'A')
250
+ assert_equal 11, oo.cell(4,'B')
251
+ assert_equal 12, oo.cell(4,'C')
252
+ assert_equal 13, oo.cell(4,'D')
253
+ assert_equal 14, oo.cell(4,'E')
254
+ assert_equal :date, oo.celltype(5,1)
255
+ assert_equal Date.new(1961,11,21), oo.cell(5,1)
256
+ assert_equal "1961-11-21", oo.cell(5,1).to_s
257
+ end
258
+ end
259
+
260
+ def test_celltype
261
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
262
+ assert_equal :string, oo.celltype(2,6)
263
+ end
264
+ end
265
+
266
+ def test_cell_address
267
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
268
+ assert_equal "tata", oo.cell(6,1)
269
+ assert_equal "tata", oo.cell(6,'A')
270
+ assert_equal "tata", oo.cell('A',6)
271
+ assert_equal "tata", oo.cell(6,'a')
272
+ assert_equal "tata", oo.cell('a',6)
273
+ assert_raise(ArgumentError) { assert_equal "tata", oo.cell('a','f') }
274
+ assert_raise(ArgumentError) { assert_equal "tata", oo.cell('f','a') }
275
+ assert_equal "thisisc8", oo.cell(8,3)
276
+ assert_equal "thisisc8", oo.cell(8,'C')
277
+ assert_equal "thisisc8", oo.cell('C',8)
278
+ assert_equal "thisisc8", oo.cell(8,'c')
279
+ assert_equal "thisisc8", oo.cell('c',8)
280
+ assert_equal "thisisd9", oo.cell('d',9)
281
+ assert_equal "thisisa11", oo.cell('a',11)
282
+ end
283
+ end
284
+
285
+ def test_office_version
286
+ with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
287
+ assert_equal "1.0", oo.officeversion
288
+ end
289
+ end
290
+
291
+ #TODO: inkonsequente Lieferung Fixnum/Float
292
+ def test_rows
293
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
294
+ assert_equal 41, oo.cell('a',12)
295
+ assert_equal 42, oo.cell('b',12)
296
+ assert_equal 43, oo.cell('c',12)
297
+ assert_equal 44, oo.cell('d',12)
298
+ assert_equal 45, oo.cell('e',12)
299
+ assert_equal [41.0,42.0,43.0,44.0,45.0, nil, nil], oo.row(12)
300
+ assert_equal "einundvierzig", oo.cell('a',16)
301
+ assert_equal "zweiundvierzig", oo.cell('b',16)
302
+ assert_equal "dreiundvierzig", oo.cell('c',16)
303
+ assert_equal "vierundvierzig", oo.cell('d',16)
304
+ assert_equal "fuenfundvierzig", oo.cell('e',16)
305
+ assert_equal ["einundvierzig", "zweiundvierzig", "dreiundvierzig", "vierundvierzig", "fuenfundvierzig", nil, nil], oo.row(16)
306
+ end
307
+ end
308
+
309
+ def test_last_row
310
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
311
+ assert_equal 18, oo.last_row
312
+ end
313
+ end
314
+
315
+ def test_last_column
316
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
317
+ assert_equal 7, oo.last_column
318
+ end
319
+ end
320
+
321
+ def test_last_column_as_letter
322
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
323
+ assert_equal 'G', oo.last_column_as_letter
324
+ end
325
+ end
326
+
327
+ def test_first_row
328
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
329
+ assert_equal 1, oo.first_row
330
+ end
331
+ end
332
+
333
+ def test_first_column
334
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
335
+ assert_equal 1, oo.first_column
336
+ end
337
+ end
338
+
339
+ def test_first_column_as_letter
340
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
341
+ assert_equal 'A', oo.first_column_as_letter
342
+ end
343
+ end
344
+
345
+ def test_sheetname
346
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
347
+ oo.default_sheet = "Name of Sheet 2"
348
+ assert_equal 'I am sheet 2', oo.cell('C',5)
349
+ assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
350
+ assert_raise(RangeError) { oo.default_sheet = "non existing sheet name" }
351
+ assert_raise(RangeError) { dummy = oo.cell('C',5,"non existing sheet name")}
352
+ assert_raise(RangeError) { dummy = oo.celltype('C',5,"non existing sheet name")}
353
+ assert_raise(RangeError) { dummy = oo.empty?('C',5,"non existing sheet name")}
354
+ if oo.class == Roo::Excel
355
+ assert_raise(RuntimeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
356
+ assert_raise(RuntimeError) { dummy = oo.formula('C',5,"non existing sheet name")}
357
+ else
358
+ assert_raise(RangeError) { dummy = oo.formula?('C',5,"non existing sheet name")}
359
+ assert_raise(RangeError) { dummy = oo.formula('C',5,"non existing sheet name")}
360
+ assert_raise(RangeError) { dummy = oo.set('C',5,42,"non existing sheet name")} unless oo.class == Roo::Google
361
+ assert_raise(RangeError) { dummy = oo.formulas("non existing sheet name")}
362
+ end
363
+ assert_raise(RangeError) { dummy = oo.to_yaml({},1,1,1,1,"non existing sheet name")}
364
+ end
365
+ end
366
+
367
+ def test_boundaries
368
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
369
+ oo.default_sheet = "Name of Sheet 2"
370
+ assert_equal 2, oo.first_column
371
+ assert_equal 'B', oo.first_column_as_letter
372
+ assert_equal 5, oo.first_row
373
+ assert_equal 'E', oo.last_column_as_letter
374
+ assert_equal 14, oo.last_row
375
+ end
376
+ end
377
+
378
+ def test_multiple_letters
379
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
380
+ oo.default_sheet = "Sheet3"
381
+ assert_equal "i am AA", oo.cell('AA',1)
382
+ assert_equal "i am AB", oo.cell('AB',1)
383
+ assert_equal "i am BA", oo.cell('BA',1)
384
+ assert_equal 'BA', oo.last_column_as_letter
385
+ assert_equal "i am BA", oo.cell(1,'BA')
386
+ end
387
+ end
388
+
389
+ def test_argument_error
390
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
391
+ assert_nothing_raised(ArgumentError) { oo.default_sheet = "Tabelle1" }
392
+ end
393
+ end
394
+
395
+ def test_empty_eh
396
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
397
+ assert oo.empty?('a',14)
398
+ assert !oo.empty?('a',15)
399
+ assert oo.empty?('a',20)
400
+ end
401
+ end
402
+
403
+ def test_reload
404
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
405
+ assert_equal 1, oo.cell(1,1)
406
+ oo.reload
407
+ assert_equal 1, oo.cell(1,1)
408
+ end
409
+ end
410
+
411
+ def test_bug_contiguous_cells
412
+ with_each_spreadsheet(:name=>'numbers1', :format=>:openoffice) do |oo|
413
+ oo.default_sheet = "Sheet4"
414
+ assert_equal Date.new(2007,06,16), oo.cell('a',1)
415
+ assert_equal 10, oo.cell('b',1)
416
+ assert_equal 10, oo.cell('c',1)
417
+ assert_equal 10, oo.cell('d',1)
418
+ assert_equal 10, oo.cell('e',1)
419
+ end
420
+ end
421
+
422
+ def test_bug_italo_ve
423
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
424
+ oo.default_sheet = "Sheet5"
425
+ assert_equal 1, oo.cell('A',1)
426
+ assert_equal 5, oo.cell('b',1)
427
+ assert_equal 5, oo.cell('c',1)
428
+ assert_equal 2, oo.cell('a',2)
429
+ assert_equal 3, oo.cell('a',3)
430
+ end
431
+ end
432
+
433
+ def test_italo_table
434
+ with_each_spreadsheet(:name=>'simple_spreadsheet_from_italo', :format=>[:openoffice, :excel]) do |oo|
435
+ assert_equal '1', oo.cell('A',1)
436
+ assert_equal '1', oo.cell('B',1)
437
+ assert_equal '1', oo.cell('C',1)
438
+ assert_equal 1, oo.cell('A',2).to_i
439
+ assert_equal 2, oo.cell('B',2).to_i
440
+ assert_equal 1, oo.cell('C',2).to_i
441
+ assert_equal 1, oo.cell('A',3)
442
+ assert_equal 3, oo.cell('B',3)
443
+ assert_equal 1, oo.cell('C',3)
444
+ assert_equal 'A', oo.cell('A',4)
445
+ assert_equal 'A', oo.cell('B',4)
446
+ assert_equal 'A', oo.cell('C',4)
447
+ assert_equal 0.01, oo.cell('A',5)
448
+ assert_equal 0.01, oo.cell('B',5)
449
+ assert_equal 0.01, oo.cell('C',5)
450
+ assert_equal 0.03, oo.cell('a',5)+oo.cell('b',5)+oo.cell('c',5)
451
+
452
+ # Cells values in row 1:
453
+ assert_equal "1:string", oo.cell(1, 1)+":"+oo.celltype(1, 1).to_s
454
+ assert_equal "1:string",oo.cell(1, 2)+":"+oo.celltype(1, 2).to_s
455
+ assert_equal "1:string",oo.cell(1, 3)+":"+oo.celltype(1, 3).to_s
456
+
457
+ # Cells values in row 2:
458
+ assert_equal "1:string",oo.cell(2, 1)+":"+oo.celltype(2, 1).to_s
459
+ assert_equal "2:string",oo.cell(2, 2)+":"+oo.celltype(2, 2).to_s
460
+ assert_equal "1:string",oo.cell(2, 3)+":"+oo.celltype(2, 3).to_s
461
+
462
+ # Cells values in row 3:
463
+ assert_equal "1.0:float",oo.cell(3, 1).to_s+":"+oo.celltype(3, 1).to_s
464
+ assert_equal "3.0:float",oo.cell(3, 2).to_s+":"+oo.celltype(3, 2).to_s
465
+ assert_equal "1.0:float",oo.cell(3, 3).to_s+":"+oo.celltype(3, 3).to_s
466
+
467
+ # Cells values in row 4:
468
+ assert_equal "A:string",oo.cell(4, 1)+":"+oo.celltype(4, 1).to_s
469
+ assert_equal "A:string",oo.cell(4, 2)+":"+oo.celltype(4, 2).to_s
470
+ assert_equal "A:string",oo.cell(4, 3)+":"+oo.celltype(4, 3).to_s
471
+
472
+ # Cells values in row 5:
473
+ if oo.class == Roo::Openoffice
474
+ assert_equal "0.01:percentage",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
475
+ assert_equal "0.01:percentage",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
476
+ assert_equal "0.01:percentage",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
477
+ else
478
+ assert_equal "0.01:float",oo.cell(5, 1).to_s+":"+oo.celltype(5, 1).to_s
479
+ assert_equal "0.01:float",oo.cell(5, 2).to_s+":"+oo.celltype(5, 2).to_s
480
+ assert_equal "0.01:float",oo.cell(5, 3).to_s+":"+oo.celltype(5, 3).to_s
481
+ end
482
+ end
483
+ end
484
+
485
+ def test_formula_openoffice
486
+ with_each_spreadsheet(:name=>'formula', :format=>:openoffice) do |oo|
487
+ assert_equal 1, oo.cell('A',1)
488
+ assert_equal 2, oo.cell('A',2)
489
+ assert_equal 3, oo.cell('A',3)
490
+ assert_equal 4, oo.cell('A',4)
491
+ assert_equal 5, oo.cell('A',5)
492
+ assert_equal 6, oo.cell('A',6)
493
+ assert_equal 21, oo.cell('A',7)
494
+ assert_equal :formula, oo.celltype('A',7)
495
+ assert_equal "=[Sheet2.A1]", oo.formula('C',7)
496
+ assert_nil oo.formula('A',6)
497
+ assert_equal [[7, 1, "=SUM([.A1:.A6])"],
498
+ [7, 2, "=SUM([.$A$1:.B6])"],
499
+ [7, 3, "=[Sheet2.A1]"],
500
+ [8, 2, "=SUM([.$A$1:.B7])"],
501
+ ], oo.formulas(oo.sheets.first)
502
+
503
+ # setting a cell
504
+ oo.set('A',15, 41)
505
+ assert_equal 41, oo.cell('A',15)
506
+ oo.set('A',16, "41")
507
+ assert_equal "41", oo.cell('A',16)
508
+ oo.set('A',17, 42.5)
509
+ assert_equal 42.5, oo.cell('A',17)
510
+ end
511
+ end
512
+
513
+ def test_formula_google
514
+ with_each_spreadsheet(:name=>'formula', :format=>:google) do |oo|
515
+ assert_equal 1, oo.cell('A',1)
516
+ assert_equal 2, oo.cell('A',2)
517
+ assert_equal 3, oo.cell('A',3)
518
+ assert_equal 4, oo.cell('A',4)
519
+ assert_equal 5, oo.cell('A',5)
520
+ assert_equal 6, oo.cell('A',6)
521
+ # assert_equal 21, oo.cell('A',7)
522
+ assert_equal 21.0, oo.cell('A',7) #TODO: better solution Fixnum/Float
523
+ assert_equal :formula, oo.celltype('A',7)
524
+ # assert_equal "=[Sheet2.A1]", oo.formula('C',7)
525
+ # !!! different from formulas in Openoffice
526
+ #was: assert_equal "=sheet2!R[-6]C[-2]", oo.formula('C',7)
527
+ # has Google changed their format of formulas/references to other sheets?
528
+ assert_equal "=Sheet2!R[-6]C[-2]", oo.formula('C',7)
529
+ assert_nil oo.formula('A',6)
530
+ # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
531
+ # [7, 2, "=SUM([.$A$1:.B6])"],
532
+ # [7, 3, "=[Sheet2.A1]"],
533
+ # [8, 2, "=SUM([.$A$1:.B7])"],
534
+ # ], oo.formulas(oo.sheets.first)
535
+ # different format than in openoffice spreadsheets:
536
+ #was:
537
+ # assert_equal [[7, 1, "=SUM(R[-6]C[0]:R[-1]C[0])"],
538
+ # [7, 2, "=SUM(R1C1:R[-1]C[0])"],
539
+ # [7, 3, "=sheet2!R[-6]C[-2]"],
540
+ # [8, 2, "=SUM(R1C1:R[-1]C[0])"]],
541
+ # oo.formulas(oo.sheets.first)
542
+ assert_equal [[7, 1, "=SUM(R[-6]C:R[-1]C)"],
543
+ [7, 2, "=SUM(R1C1:R[-1]C)"],
544
+ [7, 3, "=Sheet2!R[-6]C[-2]"],
545
+ [8, 2, "=SUM(R1C1:R[-1]C)"]],
546
+ oo.formulas(oo.sheets.first)
547
+ end
548
+ end
549
+
550
+ def test_formula_excelx
551
+ with_each_spreadsheet(:name=>'formula', :format=>:excelx) do |oo|
552
+ assert_equal 1, oo.cell('A',1)
553
+ assert_equal 2, oo.cell('A',2)
554
+ assert_equal 3, oo.cell('A',3)
555
+ assert_equal 4, oo.cell('A',4)
556
+ assert_equal 5, oo.cell('A',5)
557
+ assert_equal 6, oo.cell('A',6)
558
+ assert_equal 21, oo.cell('A',7)
559
+ assert_equal :formula, oo.celltype('A',7)
560
+ #steht nicht in Datei, oder?
561
+ #nein, diesen Bezug habe ich nur in der Openoffice-Datei
562
+ #assert_equal "=[Sheet2.A1]", oo.formula('C',7)
563
+ assert_nil oo.formula('A',6)
564
+ # assert_equal [[7, 1, "=SUM([.A1:.A6])"],
565
+ # [7, 2, "=SUM([.$A$1:.B6])"],
566
+ #[7, 3, "=[Sheet2.A1]"],
567
+ #[8, 2, "=SUM([.$A$1:.B7])"],
568
+ #], oo.formulas(oo.sheets.first)
569
+ assert_equal [[7, 1, 'SUM(A1:A6)'],
570
+ [7, 2, 'SUM($A$1:B6)'],
571
+ # [7, 3, "=[Sheet2.A1]"],
572
+ # [8, 2, "=SUM([.$A$1:.B7])"],
573
+ ], oo.formulas(oo.sheets.first)
574
+
575
+ # setting a cell
576
+ oo.set('A',15, 41)
577
+ assert_equal 41, oo.cell('A',15)
578
+ oo.set('A',16, "41")
579
+ assert_equal "41", oo.cell('A',16)
580
+ oo.set('A',17, 42.5)
581
+ assert_equal 42.5, oo.cell('A',17)
582
+ end
583
+ end
584
+
585
+ # Excel can only read the cell's value
586
+ def test_formula_excel
587
+ with_each_spreadsheet(:name=>'formula', :format=>:excel) do |oo|
588
+ assert_equal 21, oo.cell('A',7)
589
+ assert_equal 21, oo.cell('B',7)
590
+ end
591
+ end
592
+
593
+
594
+ def test_borders_sheets
595
+ with_each_spreadsheet(:name=>'borders') do |oo|
596
+ oo.default_sheet = oo.sheets[1]
597
+ assert_equal 6, oo.first_row
598
+ assert_equal 11, oo.last_row
599
+ assert_equal 4, oo.first_column
600
+ assert_equal 8, oo.last_column
601
+
602
+ oo.default_sheet = oo.sheets.first
603
+ assert_equal 5, oo.first_row
604
+ assert_equal 10, oo.last_row
605
+ assert_equal 3, oo.first_column
606
+ assert_equal 7, oo.last_column
607
+
608
+ oo.default_sheet = oo.sheets[2]
609
+ assert_equal 7, oo.first_row
610
+ assert_equal 12, oo.last_row
611
+ assert_equal 5, oo.first_column
612
+ assert_equal 9, oo.last_column
613
+ end
614
+ end
615
+
616
+ def yaml_entry(row,col,type,value)
617
+ "cell_#{row}_#{col}: \n row: #{row} \n col: #{col} \n celltype: #{type} \n value: #{value} \n"
618
+ end
619
+
620
+ def test_to_yaml
621
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
622
+ assert_equal "--- \n"+yaml_entry(5,1,"date","1961-11-21"), oo.to_yaml({}, 5,1,5,1)
623
+ assert_equal "--- \n"+yaml_entry(8,3,"string","thisisc8"), oo.to_yaml({}, 8,3,8,3)
624
+ assert_equal "--- \n"+yaml_entry(12,3,"float",43.0), oo.to_yaml({}, 12,3,12,3)
625
+ assert_equal \
626
+ "--- \n"+yaml_entry(12,3,"float",43.0) +
627
+ yaml_entry(12,4,"float",44.0) +
628
+ yaml_entry(12,5,"float",45.0), oo.to_yaml({}, 12,3,12)
629
+ assert_equal \
630
+ "--- \n"+yaml_entry(12,3,"float",43.0)+
631
+ yaml_entry(12,4,"float",44.0)+
632
+ yaml_entry(12,5,"float",45.0)+
633
+ yaml_entry(15,3,"float",43.0)+
634
+ yaml_entry(15,4,"float",44.0)+
635
+ yaml_entry(15,5,"float",45.0)+
636
+ yaml_entry(16,3,"string","dreiundvierzig")+
637
+ yaml_entry(16,4,"string","vierundvierzig")+
638
+ yaml_entry(16,5,"string","fuenfundvierzig"), oo.to_yaml({}, 12,3)
639
+ end
640
+ end
641
+
642
+ def test_only_one_sheet
643
+ with_each_spreadsheet(:name=>'only_one_sheet') do |oo|
644
+ assert_equal 42, oo.cell('B',4)
645
+ assert_equal 43, oo.cell('C',4)
646
+ assert_equal 44, oo.cell('D',4)
647
+ oo.default_sheet = oo.sheets.first
648
+ assert_equal 42, oo.cell('B',4)
649
+ assert_equal 43, oo.cell('C',4)
650
+ assert_equal 44, oo.cell('D',4)
651
+ end
652
+ end
653
+
654
+ def test_excel_open_from_uri_and_zipped
655
+ if EXCEL
656
+ if ONLINE
657
+ begin
658
+ url = 'http://stiny-leonhard.de/bode-v1.xls.zip'
659
+ excel = Roo::Excel.new(url, :zip)
660
+ excel.default_sheet = excel.sheets.first
661
+ assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
662
+ ensure
663
+ excel.remove_tmp
664
+ end
665
+ end
666
+ end
667
+ end
668
+
669
+ def test_openoffice_open_from_uri_and_zipped
670
+ if OPENOFFICE
671
+ if ONLINE
672
+ begin
673
+ url = 'http://spazioinwind.libero.it/s2/rata.ods.zip'
674
+ sheet = Roo::Openoffice.new(url, :zip)
675
+ #has been changed: assert_equal 'ist "e" im Nenner von H(s)', sheet.cell('b', 5)
676
+ assert_in_delta 0.001, 505.14, sheet.cell('c', 33).to_f
677
+ ensure
678
+ sheet.remove_tmp
679
+ end
680
+ end
681
+ end
682
+ end
683
+
684
+ def test_excel_zipped
685
+ if EXCEL
686
+ begin
687
+ oo = Roo::Excel.new(File.join(TESTDIR,"bode-v1.xls.zip"), :zip)
688
+ assert oo
689
+ assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
690
+ ensure
691
+ oo.remove_tmp
692
+ end
693
+ end
694
+ end
695
+
696
+ def test_openoffice_zipped
697
+ if OPENOFFICE
698
+ begin
699
+ oo = Roo::Openoffice.new(File.join(TESTDIR,"bode-v1.ods.zip"), :zip)
700
+ assert oo
701
+ assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
702
+ ensure
703
+ oo.remove_tmp
704
+ end
705
+ end
706
+ end
707
+
708
+ def test_bug_ric
709
+ with_each_spreadsheet(:name=>'ric', :format=>:openoffice) do |oo|
710
+ assert oo.empty?('A',1)
711
+ assert oo.empty?('B',1)
712
+ assert oo.empty?('C',1)
713
+ assert oo.empty?('D',1)
714
+ expected = 1
715
+ letter = 'e'
716
+ while letter <= 'u'
717
+ assert_equal expected, oo.cell(letter,1)
718
+ letter.succ!
719
+ expected += 1
720
+ end
721
+ assert_equal 'J', oo.cell('v',1)
722
+ assert_equal 'P', oo.cell('w',1)
723
+ assert_equal 'B', oo.cell('x',1)
724
+ assert_equal 'All', oo.cell('y',1)
725
+ assert_equal 0, oo.cell('a',2)
726
+ assert oo.empty?('b',2)
727
+ assert oo.empty?('c',2)
728
+ assert oo.empty?('d',2)
729
+ assert_equal 'B', oo.cell('e',2)
730
+ assert_equal 'B', oo.cell('f',2)
731
+ assert_equal 'B', oo.cell('g',2)
732
+ assert_equal 'B', oo.cell('h',2)
733
+ assert_equal 'B', oo.cell('i',2)
734
+ assert_equal 'B', oo.cell('j',2)
735
+ assert_equal 'B', oo.cell('k',2)
736
+ assert_equal 'B', oo.cell('l',2)
737
+ assert_equal 'B', oo.cell('m',2)
738
+ assert_equal 'B', oo.cell('n',2)
739
+ assert_equal 'B', oo.cell('o',2)
740
+ assert_equal 'B', oo.cell('p',2)
741
+ assert_equal 'B', oo.cell('q',2)
742
+ assert_equal 'B', oo.cell('r',2)
743
+ assert_equal 'B', oo.cell('s',2)
744
+ assert oo.empty?('t',2)
745
+ assert oo.empty?('u',2)
746
+ assert_equal 0 , oo.cell('v',2)
747
+ assert_equal 0 , oo.cell('w',2)
748
+ assert_equal 15 , oo.cell('x',2)
749
+ assert_equal 15 , oo.cell('y',2)
750
+ end
751
+ end
752
+
753
+ def test_mehrteilig
754
+ with_each_spreadsheet(:name=>'Bibelbund1', :format=>:openoffice) do |oo|
755
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
756
+ end
757
+ #if EXCELX
758
+ # #Datei gibt es noch nicht
759
+ # oo = Roo::Excelx.new(File.join(TESTDIR,"Bibelbund1.xlsx"))
760
+ # oo.default_sheet = oo.sheets.first
761
+ # assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
762
+ #end
763
+ end
764
+
765
+ def test_huge_document_to_csv
766
+ if LONG_RUN
767
+ with_each_spreadsheet(:name=>'Bibelbund') do |oo|
768
+ assert_nothing_raised(Timeout::Error) {
769
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
770
+ File.delete_if_exist("/tmp/Bibelbund.csv")
771
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
772
+ assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46,'A')
773
+ assert_equal "Tagebuch aus Chile Juli 1977", oo.cell(55,'A')
774
+ assert oo.to_csv("/tmp/Bibelbund.csv")
775
+ assert File.exists?("/tmp/Bibelbund.csv")
776
+ assert_equal "", `diff test/Bibelbund.csv /tmp/Bibelbund.csv`
777
+ end
778
+ }
779
+ end
780
+ end
781
+ end
782
+
783
+ def test_to_csv
784
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
785
+ master = "#{TESTDIR}/numbers1.csv"
786
+ File.delete_if_exist("/tmp/numbers1.csv")
787
+ assert oo.to_csv("/tmp/numbers1.csv",oo.sheets.first)
788
+ assert File.exists?("/tmp/numbers1.csv")
789
+ assert_equal "", `diff #{master} /tmp/numbers1.csv`
790
+ assert oo.to_csv("/tmp/numbers1.csv")
791
+ assert File.exists?("/tmp/numbers1.csv")
792
+ assert_equal "", `diff #{master} /tmp/numbers1.csv`
793
+ end
794
+ end
795
+
796
+ def test_bug_mehrere_datum
797
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
798
+ oo.default_sheet = 'Sheet5'
799
+ assert_equal :date, oo.celltype('A',4)
800
+ assert_equal :date, oo.celltype('B',4)
801
+ assert_equal :date, oo.celltype('C',4)
802
+ assert_equal :date, oo.celltype('D',4)
803
+ assert_equal :date, oo.celltype('E',4)
804
+ assert_equal Date.new(2007,11,21), oo.cell('A',4)
805
+ assert_equal Date.new(2007,11,21), oo.cell('B',4)
806
+ assert_equal Date.new(2007,11,21), oo.cell('C',4)
807
+ assert_equal Date.new(2007,11,21), oo.cell('D',4)
808
+ assert_equal Date.new(2007,11,21), oo.cell('E',4)
809
+ assert_equal :float, oo.celltype('A',5)
810
+ assert_equal :float, oo.celltype('B',5)
811
+ assert_equal :float, oo.celltype('C',5)
812
+ assert_equal :float, oo.celltype('D',5)
813
+ assert_equal :float, oo.celltype('E',5)
814
+ assert_equal 42, oo.cell('A',5)
815
+ assert_equal 42, oo.cell('B',5)
816
+ assert_equal 42, oo.cell('C',5)
817
+ assert_equal 42, oo.cell('D',5)
818
+ assert_equal 42, oo.cell('E',5)
819
+ assert_equal :string, oo.celltype('A',6)
820
+ assert_equal :string, oo.celltype('B',6)
821
+ assert_equal :string, oo.celltype('C',6)
822
+ assert_equal :string, oo.celltype('D',6)
823
+ assert_equal :string, oo.celltype('E',6)
824
+ assert_equal "ABC", oo.cell('A',6)
825
+ assert_equal "ABC", oo.cell('B',6)
826
+ assert_equal "ABC", oo.cell('C',6)
827
+ assert_equal "ABC", oo.cell('D',6)
828
+ assert_equal "ABC", oo.cell('E',6)
829
+ end
830
+ end
831
+
832
+ def test_multiple_sheets
833
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
834
+ 2.times do
835
+ oo.default_sheet = "Tabelle1"
836
+ assert_equal 1, oo.cell(1,1)
837
+ assert_equal 1, oo.cell(1,1,"Tabelle1")
838
+ assert_equal "I am sheet 2", oo.cell('C',5,"Name of Sheet 2")
839
+ sheetname = 'Sheet5'
840
+ assert_equal :date, oo.celltype('A',4,sheetname)
841
+ assert_equal :date, oo.celltype('B',4,sheetname)
842
+ assert_equal :date, oo.celltype('C',4,sheetname)
843
+ assert_equal :date, oo.celltype('D',4,sheetname)
844
+ assert_equal :date, oo.celltype('E',4,sheetname)
845
+ assert_equal Date.new(2007,11,21), oo.cell('A',4,sheetname)
846
+ assert_equal Date.new(2007,11,21), oo.cell('B',4,sheetname)
847
+ assert_equal Date.new(2007,11,21), oo.cell('C',4,sheetname)
848
+ assert_equal Date.new(2007,11,21), oo.cell('D',4,sheetname)
849
+ assert_equal Date.new(2007,11,21), oo.cell('E',4,sheetname)
850
+ assert_equal :float, oo.celltype('A',5,sheetname)
851
+ assert_equal :float, oo.celltype('B',5,sheetname)
852
+ assert_equal :float, oo.celltype('C',5,sheetname)
853
+ assert_equal :float, oo.celltype('D',5,sheetname)
854
+ assert_equal :float, oo.celltype('E',5,sheetname)
855
+ assert_equal 42, oo.cell('A',5,sheetname)
856
+ assert_equal 42, oo.cell('B',5,sheetname)
857
+ assert_equal 42, oo.cell('C',5,sheetname)
858
+ assert_equal 42, oo.cell('D',5,sheetname)
859
+ assert_equal 42, oo.cell('E',5,sheetname)
860
+ assert_equal :string, oo.celltype('A',6,sheetname)
861
+ assert_equal :string, oo.celltype('B',6,sheetname)
862
+ assert_equal :string, oo.celltype('C',6,sheetname)
863
+ assert_equal :string, oo.celltype('D',6,sheetname)
864
+ assert_equal :string, oo.celltype('E',6,sheetname)
865
+ assert_equal "ABC", oo.cell('A',6,sheetname)
866
+ assert_equal "ABC", oo.cell('B',6,sheetname)
867
+ assert_equal "ABC", oo.cell('C',6,sheetname)
868
+ assert_equal "ABC", oo.cell('D',6,sheetname)
869
+ assert_equal "ABC", oo.cell('E',6,sheetname)
870
+ oo.reload
871
+ end
872
+ end
873
+ end
874
+
875
+
876
+ def test_bug_empty_sheet
877
+ with_each_spreadsheet(:name=>'formula', :format=>[:openoffice, :excelx]) do |oo|
878
+ oo.default_sheet = 'Sheet3' # is an empty sheet
879
+ assert_nothing_raised(NoMethodError) { oo.to_csv(File.join("/","tmp","emptysheet.csv")) }
880
+ assert_equal "", `cat /tmp/emptysheet.csv`
881
+ end
882
+ end
883
+
884
+ def test_find_by_row_huge_document
885
+ if LONG_RUN
886
+ with_each_spreadsheet(:name=>'Bibelbund') do |oo|
887
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
888
+ oo.default_sheet = oo.sheets.first
889
+ rec = oo.find 20
890
+ assert rec
891
+ # assert_equal "Brief aus dem Sekretariat", rec[0]
892
+ #p rec
893
+ assert_equal "Brief aus dem Sekretariat", rec[0]['TITEL']
894
+ rec = oo.find 22
895
+ assert rec
896
+ # assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]
897
+ assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]['TITEL']
898
+ end
899
+ end
900
+ end
901
+ end
902
+
903
+ def test_find_by_row
904
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
905
+ oo.header_line = nil
906
+ rec = oo.find 16
907
+ assert rec
908
+ assert_nil oo.header_line
909
+ # keine Headerlines in diesem Beispiel definiert
910
+ assert_equal "einundvierzig", rec[0]
911
+ #assert_equal false, rec
912
+ rec = oo.find 15
913
+ assert rec
914
+ assert_equal 41,rec[0]
915
+ end
916
+ end
917
+
918
+ def test_find_by_conditions
919
+ if LONG_RUN
920
+ with_each_spreadsheet(:name=>'Bibelbund') do |oo|
921
+ assert_nothing_raised(Timeout::Error) {
922
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
923
+ #-----------------------------------------------------------------
924
+ zeilen = oo.find(:all, :conditions => {
925
+ 'TITEL' => 'Brief aus dem Sekretariat'
926
+ }
927
+ )
928
+ assert_equal 2, zeilen.size
929
+ assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
930
+ "INTERNET"=>nil,
931
+ "SEITE"=>316.0,
932
+ "KENNUNG"=>"Aus dem Bibelbund",
933
+ "OBJEKT"=>"Bibel+Gem",
934
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
935
+ "NUMMER"=>"1982-3",
936
+ "TITEL"=>"Brief aus dem Sekretariat"},
937
+ {"VERFASSER"=>"Almassy, Annelene von",
938
+ "INTERNET"=>nil,
939
+ "SEITE"=>222.0,
940
+ "KENNUNG"=>"Aus dem Bibelbund",
941
+ "OBJEKT"=>"Bibel+Gem",
942
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
943
+ "NUMMER"=>"1983-2",
944
+ "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
945
+
946
+ #----------------------------------------------------------
947
+ zeilen = oo.find(:all,
948
+ :conditions => { 'VERFASSER' => 'Almassy, Annelene von' }
949
+ )
950
+ assert_equal 13, zeilen.size
951
+ #----------------------------------------------------------
952
+ zeilen = oo.find(:all, :conditions => {
953
+ 'TITEL' => 'Brief aus dem Sekretariat',
954
+ 'VERFASSER' => 'Almassy, Annelene von',
955
+ }
956
+ )
957
+ assert_equal 2, zeilen.size
958
+ assert_equal [{"VERFASSER"=>"Almassy, Annelene von",
959
+ "INTERNET"=>nil,
960
+ "SEITE"=>316.0,
961
+ "KENNUNG"=>"Aus dem Bibelbund",
962
+ "OBJEKT"=>"Bibel+Gem",
963
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
964
+ "NUMMER"=>"1982-3",
965
+ "TITEL"=>"Brief aus dem Sekretariat"},
966
+ {"VERFASSER"=>"Almassy, Annelene von",
967
+ "INTERNET"=>nil,
968
+ "SEITE"=>222.0,
969
+ "KENNUNG"=>"Aus dem Bibelbund",
970
+ "OBJEKT"=>"Bibel+Gem",
971
+ "PC"=>"#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
972
+ "NUMMER"=>"1983-2",
973
+ "TITEL"=>"Brief aus dem Sekretariat"}] , zeilen
974
+
975
+ # Result as an array
976
+ zeilen = oo.find(:all,
977
+ :conditions => {
978
+ 'TITEL' => 'Brief aus dem Sekretariat',
979
+ 'VERFASSER' => 'Almassy, Annelene von',
980
+ }, :array => true)
981
+ assert_equal 2, zeilen.size
982
+ assert_equal [
983
+ [
984
+ "Brief aus dem Sekretariat",
985
+ "Almassy, Annelene von",
986
+ "Bibel+Gem",
987
+ "1982-3",
988
+ 316.0,
989
+ nil,
990
+ "#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#",
991
+ "Aus dem Bibelbund",
992
+ ],
993
+ [
994
+ "Brief aus dem Sekretariat",
995
+ "Almassy, Annelene von",
996
+ "Bibel+Gem",
997
+ "1983-2",
998
+ 222.0,
999
+ nil,
1000
+ "#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#",
1001
+ "Aus dem Bibelbund",
1002
+ ]] , zeilen
1003
+ end # Timeout
1004
+ } # nothing_raised
1005
+ end
1006
+ end
1007
+ end
1008
+
1009
+
1010
+ #TODO: temporaerer Test
1011
+ def test_seiten_als_date
1012
+ with_each_spreadsheet(:name=>'Bibelbund', :format=>:excelx) do |oo|
1013
+ assert_equal 'Bericht aus dem Sekretariat', oo.cell(13,1)
1014
+ assert_equal '1981-4', oo.cell(13,'D')
1015
+ assert_equal [:numeric_or_formula,"General"], oo.excelx_type(13,'E')
1016
+ assert_equal '428', oo.excelx_value(13,'E')
1017
+ assert_equal 428.0, oo.cell(13,'E')
1018
+ end
1019
+ end
1020
+
1021
+ def test_column
1022
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
1023
+ 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)]
1024
+ assert_equal expected, oo.column(1)
1025
+ assert_equal expected, oo.column('a')
1026
+ end
1027
+ end
1028
+
1029
+ def test_column_huge_document
1030
+ if LONG_RUN
1031
+ with_each_spreadsheet(:name=>'Bibelbund') do |oo|
1032
+ assert_nothing_raised(Timeout::Error) {
1033
+ Timeout::timeout(GLOBAL_TIMEOUT) do |timeout_length|
1034
+ oo.default_sheet = oo.sheets.first
1035
+ assert_equal 3735, oo.column('a').size
1036
+ #assert_equal 499, oo.column('a').size
1037
+ end
1038
+ }
1039
+ end
1040
+ end
1041
+ end
1042
+
1043
+ def test_simple_spreadsheet_find_by_condition
1044
+ with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
1045
+ oo.header_line = 3
1046
+ oo.date_format = '%m/%d/%Y' if oo.class == Roo::Google
1047
+ erg = oo.find(:all, :conditions => {'Comment' => 'Task 1'})
1048
+ assert_equal Date.new(2007,05,07), erg[1]['Date']
1049
+ assert_equal 10.75 , erg[1]['Start time']
1050
+ assert_equal 12.50 , erg[1]['End time']
1051
+ assert_equal 0 , erg[1]['Pause']
1052
+ assert_equal 1.75 , erg[1]['Sum'] unless oo.class == Roo::Excel
1053
+ assert_equal "Task 1" , erg[1]['Comment']
1054
+ end
1055
+ end
1056
+
1057
+ # Ruby-spreadsheet now allows us to at least give the current value
1058
+ # from a cell with a formula (no possible with parseexcel)
1059
+ def test_bug_false_borders_with_formulas
1060
+ with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1061
+ assert_equal 1, oo.first_row
1062
+ assert_equal 3, oo.last_row
1063
+ assert_equal 1, oo.first_column
1064
+ assert_equal 4, oo.last_column
1065
+ end
1066
+ end
1067
+
1068
+ # We'ce added minimal formula support so we can now read these
1069
+ # though not sure how the spreadsheet reports older values....
1070
+ def test_fe
1071
+ with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1072
+ assert_equal Date.new(2007,11,1), oo.cell('a',1)
1073
+ #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('a',1)
1074
+ #DOES NOT WORK IN EXCEL FILES: assert_equal '=TODAY()', oo.formula('a',1)
1075
+
1076
+ assert_equal Date.new(2008,2,9), oo.cell('B',1)
1077
+ #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('B',1)
1078
+ #DOES NOT WORK IN EXCEL FILES: assert_equal "=A1+100", oo.formula('B',1)
1079
+
1080
+ assert_kind_of DateTime, oo.cell('C',1)
1081
+ #DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('C',1)
1082
+ #DOES NOT WORK IN EXCEL FILES: assert_equal "=C1", oo.formula('C',1)
1083
+
1084
+ assert_equal 'H1', oo.cell('A',2)
1085
+ assert_equal 'H2', oo.cell('B',2)
1086
+ assert_equal 'H3', oo.cell('C',2)
1087
+ assert_equal 'H4', oo.cell('D',2)
1088
+ assert_equal 'R1', oo.cell('A',3)
1089
+ assert_equal 'R2', oo.cell('B',3)
1090
+ assert_equal 'R3', oo.cell('C',3)
1091
+ assert_equal 'R4', oo.cell('D',3)
1092
+ end
1093
+ end
1094
+
1095
+ def test_excel_does_not_support_formulas
1096
+ with_each_spreadsheet(:name=>'false_encoding', :format=>:excel) do |oo|
1097
+ assert_raise(RuntimeError) { void = oo.formula('a',1) }
1098
+ assert_raise(RuntimeError) { void = oo.formula?('a',1) }
1099
+ assert_raise(RuntimeError) { void = oo.formulas(oo.sheets.first) }
1100
+ end
1101
+ end
1102
+
1103
+ def get_extension(oo)
1104
+ case oo
1105
+ when Roo::Openoffice
1106
+ ".ods"
1107
+ when Roo::Excel
1108
+ ".xls"
1109
+ when Roo::Excelx
1110
+ ".xlsx"
1111
+ when Roo::Google
1112
+ ""
1113
+ end
1114
+ end
1115
+
1116
+ def test_info
1117
+ expected_templ = "File: numbers1%s\n"+
1118
+ "Number of sheets: 5\n"+
1119
+ "Sheets: Tabelle1, Name of Sheet 2, Sheet3, Sheet4, Sheet5\n"+
1120
+ "Sheet 1:\n"+
1121
+ " First row: 1\n"+
1122
+ " Last row: 18\n"+
1123
+ " First column: A\n"+
1124
+ " Last column: G\n"+
1125
+ "Sheet 2:\n"+
1126
+ " First row: 5\n"+
1127
+ " Last row: 14\n"+
1128
+ " First column: B\n"+
1129
+ " Last column: E\n"+
1130
+ "Sheet 3:\n"+
1131
+ " First row: 1\n"+
1132
+ " Last row: 1\n"+
1133
+ " First column: A\n"+
1134
+ " Last column: BA\n"+
1135
+ "Sheet 4:\n"+
1136
+ " First row: 1\n"+
1137
+ " Last row: 1\n"+
1138
+ " First column: A\n"+
1139
+ " Last column: E\n"+
1140
+ "Sheet 5:\n"+
1141
+ " First row: 1\n"+
1142
+ " Last row: 6\n"+
1143
+ " First column: A\n"+
1144
+ " Last column: E"
1145
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
1146
+ ext = get_extension(oo)
1147
+ expected = sprintf(expected_templ,ext)
1148
+ if oo.class == Roo::Google
1149
+ assert_equal expected.gsub(/numbers1/,key_of("numbers1")), oo.info
1150
+ else
1151
+ assert_equal expected, oo.info
1152
+ end
1153
+ end
1154
+ end
1155
+
1156
+ def test_bug_excel_numbers1_sheet5_last_row
1157
+ with_each_spreadsheet(:name=>'numbers1', :format=>:excel) do |oo|
1158
+ oo.default_sheet = "Tabelle1"
1159
+ assert_equal 1, oo.first_row
1160
+ assert_equal 18, oo.last_row
1161
+ assert_equal Roo::Openoffice.letter_to_number('A'), oo.first_column
1162
+ assert_equal Roo::Openoffice.letter_to_number('G'), oo.last_column
1163
+ oo.default_sheet = "Name of Sheet 2"
1164
+ assert_equal 5, oo.first_row
1165
+ assert_equal 14, oo.last_row
1166
+ assert_equal Roo::Openoffice.letter_to_number('B'), oo.first_column
1167
+ assert_equal Roo::Openoffice.letter_to_number('E'), oo.last_column
1168
+ oo.default_sheet = "Sheet3"
1169
+ assert_equal 1, oo.first_row
1170
+ assert_equal 1, oo.last_row
1171
+ assert_equal Roo::Openoffice.letter_to_number('A'), oo.first_column
1172
+ assert_equal Roo::Openoffice.letter_to_number('BA'), oo.last_column
1173
+ oo.default_sheet = "Sheet4"
1174
+ assert_equal 1, oo.first_row
1175
+ assert_equal 1, oo.last_row
1176
+ assert_equal Roo::Openoffice.letter_to_number('A'), oo.first_column
1177
+ assert_equal Roo::Openoffice.letter_to_number('E'), oo.last_column
1178
+ oo.default_sheet = "Sheet5"
1179
+ assert_equal 1, oo.first_row
1180
+ assert_equal 6, oo.last_row
1181
+ assert_equal Roo::Openoffice.letter_to_number('A'), oo.first_column
1182
+ assert_equal Roo::Openoffice.letter_to_number('E'), oo.last_column
1183
+ end
1184
+ end
1185
+
1186
+ def test_should_raise_file_not_found_error
1187
+ if OPENOFFICE
1188
+ assert_raise(IOError) {
1189
+ oo = Roo::Openoffice.new(File.join('testnichtvorhanden','Bibelbund.ods'))
1190
+ }
1191
+ end
1192
+ if EXCEL
1193
+ assert_raise(IOError) {
1194
+ oo = Roo::Excel.new(File.join('testnichtvorhanden','Bibelbund.xls'))
1195
+ }
1196
+ end
1197
+ if EXCELX
1198
+ assert_raise(IOError) {
1199
+ oo = Roo::Excelx.new(File.join('testnichtvorhanden','Bibelbund.xlsx'))
1200
+ }
1201
+ end
1202
+ if GOOGLE
1203
+ # assert_raise(Net::HTTPServerException) {
1204
+ # oo = Google.new(key_of('testnichtvorhanden'+'Bibelbund.ods'))
1205
+ # oo = Google.new('testnichtvorhanden')
1206
+ # }
1207
+ end
1208
+ end
1209
+
1210
+ def test_write_google
1211
+ # write.me: http://spreadsheets.google.com/ccc?key=ptu6bbahNZpY0N0RrxQbWdw&hl=en_GB
1212
+
1213
+ with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1214
+ oo.set_value(1,1,"hello from the tests")
1215
+ assert_equal "hello from the tests", oo.cell(1,1)
1216
+ oo.set_value(1,1, 1.0)
1217
+ assert_equal 1.0, oo.cell(1,1)
1218
+ end
1219
+ end
1220
+
1221
+ def test_bug_set_value_with_more_than_one_sheet_google
1222
+ # write.me: http://spreadsheets.google.com/ccc?key=ptu6bbahNZpY0N0RrxQbWdw&hl=en_GB
1223
+ with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1224
+ content1 = 'AAA'
1225
+ content2 = 'BBB'
1226
+ oo.default_sheet = oo.sheets.first
1227
+ oo.set_value(1,1,content1)
1228
+ oo.default_sheet = oo.sheets[1]
1229
+ oo.set_value(1,1,content2) # in the second sheet
1230
+ oo.default_sheet = oo.sheets.first
1231
+ assert_equal content1, oo.cell(1,1)
1232
+ oo.default_sheet = oo.sheets[1]
1233
+ assert_equal content2, oo.cell(1,1)
1234
+ end
1235
+ end
1236
+
1237
+ def test_set_value_with_sheet_argument_google
1238
+ with_each_spreadsheet(:name=>'write.me', :format=>:google) do |oo|
1239
+ random_row = rand(10)+1
1240
+ random_column = rand(10)+1
1241
+ content1 = 'ABC'
1242
+ content2 = 'DEF'
1243
+ oo.set_value(random_row,random_column,content1,oo.sheets.first)
1244
+ oo.set_value(random_row,random_column,content2,oo.sheets[1])
1245
+ assert_equal content1, oo.cell(random_row,random_column,oo.sheets.first)
1246
+ assert_equal content2, oo.cell(random_row,random_column,oo.sheets[1])
1247
+ end
1248
+ end
1249
+
1250
+ def test_set_value_for_non_existing_sheet_google
1251
+ with_each_spreadsheet(:name=>'ptu6bbahNZpY0N0RrxQbWdw', :format=>:google) do |oo|
1252
+ assert_raise(RangeError) { oo.set_value(1,1,"dummy","no_sheet") }
1253
+ end
1254
+ end
1255
+
1256
+ def test_bug_bbu
1257
+ with_each_spreadsheet(:name=>'bbu', :format=>[:openoffice, :excelx, :excel]) do |oo|
1258
+ assert_nothing_raised() {
1259
+ assert_equal "File: bbu#{get_extension(oo)}
1260
+ Number of sheets: 3
1261
+ Sheets: 2007_12, Tabelle2, Tabelle3
1262
+ Sheet 1:
1263
+ First row: 1
1264
+ Last row: 4
1265
+ First column: A
1266
+ Last column: F
1267
+ Sheet 2:
1268
+ - empty -
1269
+ Sheet 3:
1270
+ - empty -", oo.info
1271
+ }
1272
+
1273
+ oo.default_sheet = oo.sheets[1] # empty sheet
1274
+ assert_nil oo.first_row
1275
+ assert_nil oo.last_row
1276
+ assert_nil oo.first_column
1277
+ assert_nil oo.last_column
1278
+ end
1279
+ end
1280
+
1281
+
1282
+ def test_bug_time_nil
1283
+ with_each_spreadsheet(:name=>'time-test') do |oo|
1284
+ assert_equal 12*3600+13*60+14, oo.cell('B',1) # 12:13:14 (secs since midnight)
1285
+ assert_equal :time, oo.celltype('B',1)
1286
+ assert_equal 15*3600+16*60, oo.cell('C',1) # 15:16 (secs since midnight)
1287
+ assert_equal :time, oo.celltype('C',1)
1288
+ assert_equal 23*3600, oo.cell('D',1) # 23:00 (secs since midnight)
1289
+ assert_equal :time, oo.celltype('D',1)
1290
+ end
1291
+ end
1292
+
1293
+ def test_date_time_to_csv
1294
+ with_each_spreadsheet(:name=>'time-test') do |oo|
1295
+ begin
1296
+ assert oo.to_csv("/tmp/time-test.csv")
1297
+ assert File.exists?("/tmp/time-test.csv")
1298
+ assert_equal "", `diff #{TESTDIR}/time-test.csv /tmp/time-test.csv`
1299
+ ensure
1300
+ File.delete_if_exist("/tmp/time-test.csv")
1301
+ end
1302
+ end
1303
+ end
1304
+
1305
+ def test_date_time_yaml
1306
+ with_each_spreadsheet(:name=>'time-test') do |oo|
1307
+ expected =
1308
+ "--- \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"
1309
+ assert_equal expected, oo.to_yaml
1310
+ end
1311
+ end
1312
+
1313
+ def test_no_remaining_tmp_files_openoffice
1314
+ if OPENOFFICE
1315
+ assert_raise(Zip::ZipError) { #TODO: besseres Fehlerkriterium bei
1316
+ # oo = Roo::Openoffice.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1317
+ # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1318
+ oo = Roo::Openoffice.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1319
+ false,
1320
+ :ignore)
1321
+ }
1322
+ a=Dir.glob("oo_*")
1323
+ assert_equal [], a
1324
+ end
1325
+ end
1326
+
1327
+ def test_no_remaining_tmp_files_excel
1328
+ if EXCEL
1329
+ assert_raise(Ole::Storage::FormatError) {
1330
+ # oo = Roo::Excel.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1331
+ # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1332
+ oo = Roo::Excel.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1333
+ false,
1334
+ :ignore)
1335
+ }
1336
+ a=Dir.glob("oo_*")
1337
+ assert_equal [], a
1338
+ end
1339
+ end
1340
+
1341
+ def test_no_remaining_tmp_files_excelx
1342
+ if EXCELX
1343
+ assert_raise(Zip::ZipError) { #TODO: besseres Fehlerkriterium bei
1344
+
1345
+ # oo = Roo::Excelx.new(File.join(TESTDIR,"no_spreadsheet_file.txt"))
1346
+ # es soll absichtlich ein Abbruch provoziert werden, deshalb :ignore
1347
+ oo = Roo::Excelx.new(File.join(TESTDIR,"no_spreadsheet_file.txt"),
1348
+ false,
1349
+ :ignore)
1350
+
1351
+ }
1352
+ a=Dir.glob("oo_*")
1353
+ assert_equal [], a
1354
+ end
1355
+ end
1356
+
1357
+ def test_no_remaining_tmp_files_google
1358
+ if GOOGLE
1359
+ assert_raise(GoogleReadError) {
1360
+ oo = Roo::Google.new(key_of("no_spreadsheet_file.txt"))
1361
+ }
1362
+ a=Dir.glob("oo_*")
1363
+ assert_equal [], a
1364
+ end
1365
+ end
1366
+
1367
+ # Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
1368
+ # Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
1369
+ # nicht immer in der gleichen Reihenfolge erzeugt.
1370
+ def init_all_cells(oo,sheet)
1371
+ all = []
1372
+ oo.first_row(sheet).upto(oo.last_row(sheet)) do |row|
1373
+ oo.first_column(sheet).upto(oo.last_column(sheet)) do |col|
1374
+ unless oo.empty?(row,col,sheet)
1375
+ all << {:row => row.to_s,
1376
+ :column => col.to_s,
1377
+ :content => oo.cell(row,col,sheet).to_s,
1378
+ :type => oo.celltype(row,col,sheet).to_s,
1379
+ }
1380
+ end
1381
+ end
1382
+ end
1383
+ all
1384
+ end
1385
+
1386
+ def test_to_xml
1387
+ with_each_spreadsheet(:name=>'numbers1') do |oo|
1388
+ assert_nothing_raised {oo.to_xml}
1389
+ sheetname = oo.sheets.first
1390
+ doc = XML::Parser.string(oo.to_xml).parse
1391
+ doc.root.each_element {|xml_sheet|
1392
+ all_cells = init_all_cells(oo, sheetname)
1393
+ x = 0
1394
+ assert_equal sheetname, xml_sheet.attributes['name']
1395
+ xml_sheet.each_element {|cell|
1396
+ expected = [all_cells[x][:row],
1397
+ all_cells[x][:column],
1398
+ all_cells[x][:content],
1399
+ all_cells[x][:type],
1400
+ ]
1401
+ result = [
1402
+ cell.attributes['row'],
1403
+ cell.attributes['column'],
1404
+ cell.content,
1405
+ cell.attributes['type'],
1406
+ ]
1407
+ assert_equal expected, result
1408
+ x += 1
1409
+ } # end of sheet
1410
+ sheetname = oo.sheets[oo.sheets.index(sheetname)+1]
1411
+ }
1412
+ end
1413
+ end
1414
+
1415
+ def test_bug_row_column_fixnum_float
1416
+ with_each_spreadsheet(:name=>'bug-row-column-fixnum-float', :format=>:excel) do |oo|
1417
+ assert_equal 42.5, oo.cell('b',2)
1418
+ assert_equal 43 , oo.cell('c',2)
1419
+ assert_equal ['hij',42.5, 43], oo.row(2)
1420
+ assert_equal ['def',42.5, 'nop'], oo.column(2)
1421
+ end
1422
+ end
1423
+
1424
+ def test_file_warning_default
1425
+ if OPENOFFICE
1426
+ assert_raises(TypeError) { oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xls")) }
1427
+ assert_raises(TypeError) { oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xlsx")) }
1428
+ assert_equal [], Dir.glob("oo_*")
1429
+ end
1430
+ if EXCEL
1431
+ assert_raises(TypeError) { oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.ods")) }
1432
+ assert_raises(TypeError) { oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.xlsx")) }
1433
+ assert_equal [], Dir.glob("oo_*")
1434
+ end
1435
+ if EXCELX
1436
+ assert_raises(TypeError) { oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods")) }
1437
+ assert_raises(TypeError) { oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls")) }
1438
+ assert_equal [], Dir.glob("oo_*")
1439
+ end
1440
+ end
1441
+
1442
+ def test_file_warning_error
1443
+ if OPENOFFICE
1444
+ assert_raises(TypeError) { oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false,:error) }
1445
+ assert_raises(TypeError) { oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false,:error) }
1446
+ assert_equal [], Dir.glob("oo_*")
1447
+ end
1448
+ if EXCEL
1449
+ assert_raises(TypeError) { oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.ods"),false,:error) }
1450
+ assert_raises(TypeError) { oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false,:error) }
1451
+ assert_equal [], Dir.glob("oo_*")
1452
+ end
1453
+ if EXCELX
1454
+ assert_raises(TypeError) { oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),false,:error) }
1455
+ assert_raises(TypeError) { oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls"),false,:error) }
1456
+ assert_equal [], Dir.glob("oo_*")
1457
+ end
1458
+ end
1459
+
1460
+ def test_file_warning_warning
1461
+ if OPENOFFICE
1462
+ assert_nothing_raised(TypeError) {
1463
+ assert_raises(Zip::ZipError) {
1464
+ oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false, :warning)
1465
+ }
1466
+ }
1467
+ assert_nothing_raised(TypeError) {
1468
+ assert_raises(Errno::ENOENT) {
1469
+ oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false, :warning)
1470
+ }
1471
+ }
1472
+ assert_equal [], Dir.glob("oo_*")
1473
+ end
1474
+ if EXCEL
1475
+ assert_nothing_raised(TypeError) {
1476
+ assert_raises(Ole::Storage::FormatError) {
1477
+ oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.ods"),false, :warning) }
1478
+ }
1479
+ assert_nothing_raised(TypeError) {
1480
+ assert_raises(Ole::Storage::FormatError) {
1481
+ oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false, :warning) }
1482
+ }
1483
+ assert_equal [], Dir.glob("oo_*")
1484
+ end
1485
+ if EXCELX
1486
+ assert_nothing_raised(TypeError) {
1487
+ assert_raises(Errno::ENOENT) {
1488
+ oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),false, :warning) }
1489
+ }
1490
+ assert_nothing_raised(TypeError) {
1491
+ assert_raises(Zip::ZipError) {
1492
+ oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls"),false, :warning) }
1493
+ }
1494
+ assert_equal [], Dir.glob("oo_*")
1495
+ end
1496
+ end
1497
+
1498
+ def test_file_warning_ignore
1499
+ if OPENOFFICE
1500
+ assert_nothing_raised(TypeError) {
1501
+ assert_raises(Zip::ZipError) {
1502
+ oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xls"),false, :ignore) }
1503
+ }
1504
+ assert_nothing_raised(TypeError) {
1505
+ assert_raises(Errno::ENOENT) {
1506
+ oo = Roo::Openoffice.new(File.join(TESTDIR,"numbers1.xlsx"),false, :ignore) }
1507
+ }
1508
+ assert_equal [], Dir.glob("oo_*")
1509
+ end
1510
+ if EXCEL
1511
+ assert_nothing_raised(TypeError) {
1512
+ assert_raises(Ole::Storage::FormatError) {
1513
+ oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.ods"),false, :ignore) }
1514
+ }
1515
+ assert_nothing_raised(TypeError) {
1516
+ assert_raises(Ole::Storage::FormatError) {oo = Roo::Excel.new(File.join(TESTDIR,"numbers1.xlsx"),false, :ignore) }}
1517
+ assert_equal [], Dir.glob("oo_*")
1518
+ end
1519
+ if EXCELX
1520
+ assert_nothing_raised(TypeError) {
1521
+ assert_raises(Errno::ENOENT) {
1522
+ oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),false, :ignore)
1523
+ }
1524
+ }
1525
+ assert_nothing_raised(TypeError) {
1526
+ assert_raises(Zip::ZipError) {
1527
+ oo = Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls"),false, :ignore)
1528
+ }
1529
+ }
1530
+ assert_equal [], Dir.glob("oo_*")
1531
+ end
1532
+ end
1533
+
1534
+ def test_bug_last_row_excel
1535
+ with_each_spreadsheet(:name=>'time-test', :format=>:excel) do |oo|
1536
+ assert_equal 2, oo.last_row
1537
+ end
1538
+ end
1539
+
1540
+ def test_bug_to_xml_with_empty_sheets
1541
+ with_each_spreadsheet(:name=>'emptysheets', :format=>[:openoffice, :excel]) do |oo|
1542
+ oo.sheets.each { |sheet|
1543
+ assert_equal nil, oo.first_row, "first_row not nil in sheet #{sheet}"
1544
+ assert_equal nil, oo.last_row, "last_row not nil in sheet #{sheet}"
1545
+ assert_equal nil, oo.first_column, "first_column not nil in sheet #{sheet}"
1546
+ assert_equal nil, oo.last_column, "last_column not nil in sheet #{sheet}"
1547
+ assert_equal nil, oo.first_row(sheet), "first_row not nil in sheet #{sheet}"
1548
+ assert_equal nil, oo.last_row(sheet), "last_row not nil in sheet #{sheet}"
1549
+ assert_equal nil, oo.first_column(sheet), "first_column not nil in sheet #{sheet}"
1550
+ assert_equal nil, oo.last_column(sheet), "last_column not nil in sheet #{sheet}"
1551
+ }
1552
+ assert_nothing_raised() { result = oo.to_xml }
1553
+ end
1554
+ end
1555
+
1556
+ def test_bug_simple_spreadsheet_time_bug
1557
+ # really a bug? are cells really of type time?
1558
+ # No! :float must be the correct type
1559
+ with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1560
+ # puts oo.cell('B',5).to_s
1561
+ # assert_equal :time, oo.celltype('B',5)
1562
+ assert_equal :float, oo.celltype('B',5)
1563
+ assert_equal 10.75, oo.cell('B',5)
1564
+ assert_equal 12.50, oo.cell('C',5)
1565
+ assert_equal 0, oo.cell('D',5)
1566
+ assert_equal 1.75, oo.cell('E',5)
1567
+ assert_equal 'Task 1', oo.cell('F',5)
1568
+ assert_equal Date.new(2007,5,7), oo.cell('A',5)
1569
+ end
1570
+ end
1571
+
1572
+ def test_simple2_excelx
1573
+ with_each_spreadsheet(:name=>'simple_spreadsheet', :format=>:excelx) do |oo|
1574
+ assert_equal [:numeric_or_formula, "yyyy\\-mm\\-dd"], oo.excelx_type('A',4)
1575
+ assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('B',4)
1576
+ assert_equal [:numeric_or_formula, "#,##0.00"], oo.excelx_type('c',4)
1577
+ assert_equal [:numeric_or_formula, "General"], oo.excelx_type('d',4)
1578
+ assert_equal [:numeric_or_formula, "General"], oo.excelx_type('e',4)
1579
+ assert_equal :string, oo.excelx_type('f',4)
1580
+
1581
+ assert_equal "39209", oo.excelx_value('a',4)
1582
+ assert_equal "yyyy\\-mm\\-dd", oo.excelx_format('a',4)
1583
+ assert_equal "9.25", oo.excelx_value('b',4)
1584
+ assert_equal "10.25", oo.excelx_value('c',4)
1585
+ assert_equal "0", oo.excelx_value('d',4)
1586
+ #... Sum-Spalte
1587
+ # assert_equal "Task 1", oo.excelx_value('f',4)
1588
+ assert_equal "Task 1", oo.cell('f',4)
1589
+ assert_equal Date.new(2007,05,07), oo.cell('a',4)
1590
+ assert_equal "9.25", oo.excelx_value('b',4)
1591
+ assert_equal "#,##0.00", oo.excelx_format('b',4)
1592
+ assert_equal 9.25, oo.cell('b',4)
1593
+ assert_equal :float, oo.celltype('b',4)
1594
+ assert_equal :float, oo.celltype('d',4)
1595
+ assert_equal 0, oo.cell('d',4)
1596
+ assert_equal :formula, oo.celltype('e',4)
1597
+ assert_equal 1, oo.cell('e',4)
1598
+ assert_equal 'C4-B4-D4', oo.formula('e',4)
1599
+ assert_equal :string, oo.celltype('f',4)
1600
+ assert_equal "Task 1", oo.cell('f',4)
1601
+ end
1602
+ end
1603
+
1604
+ def test_datetime
1605
+ with_each_spreadsheet(:name=>'datetime') do |oo|
1606
+ val = oo.cell('c',3)
1607
+ assert_kind_of DateTime, val
1608
+ assert_equal :datetime, oo.celltype('c',3)
1609
+ assert_equal DateTime.new(1961,11,21,12,17,18), val
1610
+ val = oo.cell('a',1)
1611
+ assert_kind_of Date, val
1612
+ assert_equal :date, oo.celltype('a',1)
1613
+ assert_equal Date.new(1961,11,21), val
1614
+ assert_equal Date.new(1961,11,21), oo.cell('a',1)
1615
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',3)
1616
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',3)
1617
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',3)
1618
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',4)
1619
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',4)
1620
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',4)
1621
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('a',5)
1622
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('b',5)
1623
+ assert_equal DateTime.new(1961,11,21,12,17,18), oo.cell('c',5)
1624
+ assert_equal Date.new(1961,11,21), oo.cell('a',6)
1625
+ assert_equal Date.new(1961,11,21), oo.cell('b',6)
1626
+ assert_equal Date.new(1961,11,21), oo.cell('c',6)
1627
+ assert_equal Date.new(1961,11,21), oo.cell('a',7)
1628
+ assert_equal Date.new(1961,11,21), oo.cell('b',7)
1629
+ assert_equal Date.new(1961,11,21), oo.cell('c',7)
1630
+ end
1631
+ end
1632
+
1633
+ def test_cell_openoffice_html_escape
1634
+ with_each_spreadsheet(:name=>'html-escape', :format=>:openoffice) do |oo|
1635
+ assert_equal "'", oo.cell(1,1)
1636
+ assert_equal "&", oo.cell(2,1)
1637
+ assert_equal ">", oo.cell(3,1)
1638
+ assert_equal "<", oo.cell(4,1)
1639
+ assert_equal "`", oo.cell(5,1)
1640
+ # test_openoffice_zipped will catch issues with &quot;
1641
+ end
1642
+ end
1643
+
1644
+ def test_cell_boolean
1645
+ with_each_spreadsheet(:name=>'boolean', :format=>[:openoffice, :excel, :excelx]) do |oo|
1646
+ if oo.class == Roo::Excelx
1647
+ assert_equal "TRUE", oo.cell(1,1)
1648
+ assert_equal "FALSE", oo.cell(2,1)
1649
+ else
1650
+ assert_equal "true", oo.cell(1,1)
1651
+ assert_equal "false", oo.cell(2,1)
1652
+ end
1653
+ end
1654
+ end
1655
+
1656
+ def test_cell_multiline
1657
+ with_each_spreadsheet(:name=>'paragraph', :format=>[:openoffice, :excel, :excelx]) do |oo|
1658
+ assert_equal "This is a test\nof a multiline\nCell", oo.cell(1,1)
1659
+ assert_equal "This is a test\n¶\nof a multiline\n\nCell", oo.cell(1,2)
1660
+ assert_equal "first p\n\nsecond p\n\nlast p", oo.cell(2,1)
1661
+ end
1662
+ end
1663
+
1664
+ def test_cell_styles
1665
+ with_each_spreadsheet(:name=>'style', :format=>[:openoffice, :excel, :excelx]) do |oo|
1666
+ # bold
1667
+ assert_equal true, oo.font(1,1).bold?
1668
+ assert_equal false, oo.font(1,1).italic?
1669
+ assert_equal false, oo.font(1,1).underline?
1670
+
1671
+ # italic
1672
+ assert_equal false, oo.font(2,1).bold?
1673
+ assert_equal true, oo.font(2,1).italic?
1674
+ assert_equal false, oo.font(2,1).underline?
1675
+
1676
+ # normal
1677
+ assert_equal false, oo.font(3,1).bold?
1678
+ assert_equal false, oo.font(3,1).italic?
1679
+ assert_equal false, oo.font(3,1).underline?
1680
+
1681
+ # underline
1682
+ assert_equal false, oo.font(4,1).bold?
1683
+ assert_equal false, oo.font(4,1).italic?
1684
+ assert_equal true, oo.font(4,1).underline?
1685
+
1686
+ # bold italic
1687
+ assert_equal true, oo.font(5,1).bold?
1688
+ assert_equal true, oo.font(5,1).italic?
1689
+ assert_equal false, oo.font(5,1).underline?
1690
+
1691
+ # bold underline
1692
+ assert_equal true, oo.font(6,1).bold?
1693
+ assert_equal false, oo.font(6,1).italic?
1694
+ assert_equal true, oo.font(6,1).underline?
1695
+
1696
+ # italic underline
1697
+ assert_equal false, oo.font(7,1).bold?
1698
+ assert_equal true, oo.font(7,1).italic?
1699
+ assert_equal true, oo.font(7,1).underline?
1700
+
1701
+ # bolded row
1702
+ assert_equal true, oo.font(8,1).bold?
1703
+ assert_equal false, oo.font(8,1).italic?
1704
+ assert_equal false, oo.font(8,1).underline?
1705
+
1706
+ # bolded col
1707
+ assert_equal true, oo.font(9,2).bold?
1708
+ assert_equal false, oo.font(9,2).italic?
1709
+ assert_equal false, oo.font(9,2).underline?
1710
+
1711
+ # bolded row, italic col
1712
+ assert_equal true, oo.font(10,3).bold?
1713
+ assert_equal true, oo.font(10,3).italic?
1714
+ assert_equal false, oo.font(10,3).underline?
1715
+
1716
+ # normal
1717
+ assert_equal false, oo.font(11,4).bold?
1718
+ assert_equal false, oo.font(11,4).italic?
1719
+ assert_equal false, oo.font(11,4).underline?
1720
+ end
1721
+ end
1722
+
1723
+ # If a cell has a date-like string but is preceeded by a '
1724
+ # to force that date to be treated like a string, we were getting an exception.
1725
+ # This test just checks for that exception to make sure it's not raised in this case
1726
+ def test_date_to_float_conversion
1727
+ with_each_spreadsheet(:name=>'datetime_floatconv', :format=>:excel) do |oo|
1728
+ assert_nothing_raised(NoMethodError) do
1729
+ oo.cell('a',1)
1730
+ oo.cell('a',2)
1731
+ end
1732
+ end
1733
+ end
1734
+
1735
+ # Need to extend to other formats
1736
+ def test_row_whitespace
1737
+ with_each_spreadsheet(:name=>'whitespace') do |oo|
1738
+ oo.default_sheet = "Sheet1"
1739
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(1)
1740
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(2)
1741
+ assert_equal ["Date", "Start time", "End time", "Pause", "Sum", "Comment"], oo.row(3)
1742
+ assert_equal [Date.new(2007,5,7), 9.25, 10.25, 0.0, 1.0, "Task 1"], oo.row(4)
1743
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(5)
1744
+ assert_equal [Date.new(2007,5,7), 10.75, 10.75, 0.0, 0.0, "Task 1"], oo.row(6)
1745
+ oo.default_sheet = "Sheet2"
1746
+ assert_equal ["Date", nil, "Start time"], oo.row(1)
1747
+ assert_equal [Date.new(2007,5,7), nil, 9.25], oo.row(2)
1748
+ assert_equal [Date.new(2007,5,7), nil, 10.75], oo.row(3)
1749
+ end
1750
+ end
1751
+
1752
+ def test_col_whitespace
1753
+ with_each_spreadsheet(:name=>'whitespace') do |oo|
1754
+ oo.default_sheet = "Sheet1"
1755
+ assert_equal ["Date", Date.new(2007,5,7), nil, Date.new(2007,5,7)], oo.column(1)
1756
+ assert_equal ["Start time", 9.25, nil, 10.75], oo.column(2)
1757
+ assert_equal ["End time", 10.25, nil, 10.75], oo.column(3)
1758
+ assert_equal ["Pause", 0.0, nil, 0.0], oo.column(4)
1759
+ assert_equal ["Sum", 1.0, nil, 0.0], oo.column(5)
1760
+ assert_equal ["Comment","Task 1", nil, "Task 1"], oo.column(6)
1761
+ oo.default_sheet = "Sheet2"
1762
+ assert_equal [nil, nil, nil], oo.column(1)
1763
+ assert_equal [nil, nil, nil], oo.column(2)
1764
+ assert_equal ["Date", Date.new(2007,5,7), Date.new(2007,5,7)], oo.column(3)
1765
+ assert_equal [nil, nil, nil], oo.column(4)
1766
+ assert_equal [ "Start time", 9.25, 10.75], oo.column(5)
1767
+ end
1768
+ end
1769
+
1770
+
1771
+ def test_ruby_spreadsheet_formula_bug
1772
+ with_each_spreadsheet(:name=>'formula_parse_error', :format=>:excel) do |oo|
1773
+ assert_equal '5026', oo.cell(2,3)
1774
+ assert_equal '5026', oo.cell(3,3)
1775
+ end
1776
+ end
1777
+
1778
+
1779
+ # Excel has two base date formats one from 1900 and the other from 1904.
1780
+ # There's a MS bug that 1900 base dates include an extra day due to erroneously
1781
+ # including 1900 as a leap yar.
1782
+ def test_base_dates_in_excel
1783
+ with_each_spreadsheet(:name=>'1900_base', :format=>:excel) do |oo|
1784
+ assert_equal Date.new(2009,06,15), oo.cell(1,1)
1785
+ assert_equal Date.new(2009,06,28), oo.cell(2,1) #formula for TODAY(), last calculated on 06.28
1786
+ assert_equal :date, oo.celltype(1,1)
1787
+ end
1788
+ with_each_spreadsheet(:name=>'1904_base', :format=>:excel) do |oo|
1789
+ assert_equal Date.new(2009,06,15), oo.cell(1,1)
1790
+ assert_equal Date.new(2009,06,28), oo.cell(2,1) #formula for TODAY(), last calculated on 06.28
1791
+ assert_equal :date, oo.celltype(1,1)
1792
+ end
1793
+ end
1794
+
1795
+
1796
+ def test_bad_excel_date
1797
+ with_each_spreadsheet(:name=>'bad_excel_date', :format=>:excel) do |oo|
1798
+ assert_nothing_raised(ArgumentError) {
1799
+ assert_equal DateTime.new(2006,2,2,10,0,0), oo.cell('a',1)
1800
+ }
1801
+ end
1802
+ end
1803
+
1804
+ def test_public_google_doc
1805
+ with_public_google_spreadsheet do
1806
+ assert_raise(GoogleHTTPError) { Roo::Google.new("foo") }
1807
+ assert_raise(GoogleReadError) { Roo::Google.new(key_of('numbers1'))}
1808
+ assert_nothing_raised { Roo::Google.new("0AncOJVyN5MMMcjZtN0hGbFVPd3N0MFJUVVR1aFEwT3c") } # use spreadsheet key (private)
1809
+ assert_nothing_raised { Roo::Google.new(key_of('write.me')) } # use spreadsheet key (public)
1810
+ end
1811
+ end
1812
+
1813
+ def test_public_google_doc_write
1814
+ with_public_google_spreadsheet do
1815
+ assert_raise(GoogleWriteError) {
1816
+ oo = Roo::Google.new(key_of('write.me'))
1817
+ oo.set_value(1,1,'test')
1818
+ }
1819
+ end
1820
+ end
1821
+
1822
+ end # class