roo 2.0.1 → 2.7.1

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