roo 2.6.0 → 2.8.3

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