roo-xls 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.rubocop.yml +17 -0
  4. data/.simplecov +4 -0
  5. data/.travis.yml +11 -0
  6. data/Gemfile +30 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +41 -0
  9. data/Rakefile +23 -0
  10. data/defaults.reek +11 -0
  11. data/lib/roo-xls.rb +11 -0
  12. data/lib/roo/xls/excel.rb +352 -0
  13. data/lib/roo/xls/excel_2003_xml.rb +294 -0
  14. data/lib/roo/xls/spreadsheet_extensions.rb +24 -0
  15. data/lib/roo/xls/version.rb +5 -0
  16. data/roo-xls.gemspec +27 -0
  17. data/spec/lib/roo/xls/excel2003xml_spec.rb +15 -0
  18. data/spec/lib/roo/xls/excel_spec.rb +17 -0
  19. data/spec/spec_helper.rb +2 -0
  20. data/test/files/1900_base.xls +0 -0
  21. data/test/files/1904_base.xls +0 -0
  22. data/test/files/Bibelbund.csv +3741 -0
  23. data/test/files/Bibelbund.xls +0 -0
  24. data/test/files/Bibelbund.xml +62518 -0
  25. data/test/files/bad_excel_date.xls +0 -0
  26. data/test/files/bbu.xls +0 -0
  27. data/test/files/bbu.xml +152 -0
  28. data/test/files/bode-v1.xls.zip +0 -0
  29. data/test/files/boolean.csv +2 -0
  30. data/test/files/boolean.xls +0 -0
  31. data/test/files/boolean.xml +112 -0
  32. data/test/files/borders.xls +0 -0
  33. data/test/files/borders.xml +144 -0
  34. data/test/files/bug-row-column-fixnum-float.xls +0 -0
  35. data/test/files/bug-row-column-fixnum-float.xml +127 -0
  36. data/test/files/comments.xls +0 -0
  37. data/test/files/datetime.xls +0 -0
  38. data/test/files/datetime.xml +150 -0
  39. data/test/files/datetime_floatconv.xls +0 -0
  40. data/test/files/datetime_floatconv.xml +148 -0
  41. data/test/files/emptysheets.xls +0 -0
  42. data/test/files/emptysheets.xml +105 -0
  43. data/test/files/excel2003.xml +21140 -0
  44. data/test/files/excel2003_namespace.xml +197 -0
  45. data/test/files/false_encoding.xls +0 -0
  46. data/test/files/false_encoding.xml +132 -0
  47. data/test/files/formula.xls +0 -0
  48. data/test/files/formula.xml +134 -0
  49. data/test/files/formula_parse_error.xls +0 -0
  50. data/test/files/formula_parse_error.xml +1833 -0
  51. data/test/files/link.csv +1 -0
  52. data/test/files/link.xls +0 -0
  53. data/test/files/matrix.xls +0 -0
  54. data/test/files/named_cells.xls +0 -0
  55. data/test/files/numbers1.ods +0 -0
  56. data/test/files/numbers1.xls +0 -0
  57. data/test/files/numbers1.xlsx +0 -0
  58. data/test/files/numbers1.xml +312 -0
  59. data/test/files/only_one_sheet.xls +0 -0
  60. data/test/files/only_one_sheet.xml +67 -0
  61. data/test/files/paragraph.xls +0 -0
  62. data/test/files/paragraph.xml +127 -0
  63. data/test/files/prova.xls +0 -0
  64. data/test/files/simple_spreadsheet.xls +0 -0
  65. data/test/files/simple_spreadsheet.xml +225 -0
  66. data/test/files/simple_spreadsheet_from_italo.xls +0 -0
  67. data/test/files/simple_spreadsheet_from_italo.xml +242 -0
  68. data/test/files/so_datetime.csv +8 -0
  69. data/test/files/style.xls +0 -0
  70. data/test/files/style.xml +154 -0
  71. data/test/files/time-test.csv +2 -0
  72. data/test/files/time-test.xls +0 -0
  73. data/test/files/time-test.xml +131 -0
  74. data/test/files/type_excel.ods +0 -0
  75. data/test/files/type_excel.xlsx +0 -0
  76. data/test/files/type_excelx.xls +0 -0
  77. data/test/files/type_openoffice.xls +0 -0
  78. data/test/files/whitespace.xls +0 -0
  79. data/test/files/whitespace.xml +184 -0
  80. data/test/rm_test.rb +7 -0
  81. data/test/test_excel_2003_xml.rb +41 -0
  82. data/test/test_helper.rb +57 -0
  83. data/test/test_roo_excel.rb +1093 -0
  84. metadata +278 -0
data/test/rm_test.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'spreadsheet'
2
+
3
+ book = Spreadsheet.open 'tmp.xls'
4
+ sheet = book.worksheet 0
5
+ sheet.each { |row| puts row[0] }
6
+
7
+ FileUtils.rm('tmp.xls')
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class TestRooExcel < MiniTest::Test
4
+ def with_xml_spreadsheet(name)
5
+ yield ::Roo::Excel2003XML.new(File.join(TESTDIR, "#{name}.xml"))
6
+ end
7
+
8
+ def test_xml_namespace_ss
9
+ with_xml_spreadsheet('excel2003') do |oo|
10
+ oo.default_sheet = oo.sheets.first
11
+ assert_equal 'BST Variables', oo.cell(1, 1)
12
+ end
13
+ end
14
+
15
+ def test_xml_namespace_non_ss
16
+ with_xml_spreadsheet('excel2003_namespace') do |oo|
17
+ oo.default_sheet = oo.sheets.first
18
+ assert_equal 'DYS393', oo.cell(1, 1)
19
+ assert_equal '13', oo.cell(2, 1)
20
+ end
21
+ end
22
+
23
+ # If a cell has a date-like string but is preceeded by a '
24
+ # to force that date to be treated like a string, we were getting an exception.
25
+ # This test just checks for that exception to make sure it's not raised in this case
26
+ def test_date_to_float_conversion
27
+ with_xml_spreadsheet('datetime_floatconv') do |oo|
28
+ assert_nothing_raised(NoMethodError) do
29
+ oo.cell('a', 1)
30
+ oo.cell('a', 2)
31
+ end
32
+ end
33
+ end
34
+
35
+ def test_ruby_spreadsheet_formula_bug
36
+ with_xml_spreadsheet('formula_parse_error') do |oo|
37
+ assert_equal '5026', oo.cell(2, 3)
38
+ assert_equal '5026', oo.cell(3, 3)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,57 @@
1
+ require 'simplecov'
2
+ # require deps
3
+ require 'tmpdir'
4
+ require 'fileutils'
5
+ require 'minitest/autorun'
6
+ require 'shoulda'
7
+ require 'fileutils'
8
+ require 'timeout'
9
+ require 'logger'
10
+ require 'date'
11
+ require 'webmock/minitest'
12
+
13
+ # require gem files
14
+ require File.dirname(__FILE__) + '/../lib/roo-xls'
15
+
16
+ TESTDIR = File.join(File.dirname(__FILE__), 'files')
17
+
18
+ ONLINE = false
19
+ LONG_RUN = false
20
+
21
+ # very simple diff implementation
22
+ # output is an empty string if the files are equal
23
+ # otherwise differences a printen (not compatible to
24
+ # the diff command)
25
+ def file_diff(fn1, fn2)
26
+ result = ''
27
+ File.open(fn1) do |f1|
28
+ File.open(fn2) do |f2|
29
+ while f1.eof? == false && f2.eof? == false
30
+ line1 = f1.gets.chomp
31
+ line2 = f2.gets.chomp
32
+ result << "<#{line1}\n>#{line2}\n" if line1 != line2
33
+ end
34
+ if f1.eof? == false
35
+ while f1.eof? == false
36
+ line1 = f1.gets
37
+ result << "<#{line1}\n"
38
+ end
39
+ end
40
+ if f2.eof? == false
41
+ while f2.eof? == false
42
+ line2 = f2.gets
43
+ result << ">#{line2}\n"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ result
49
+ end
50
+
51
+ class File
52
+ def self.delete_if_exist(filename)
53
+ if File.exist?(filename)
54
+ File.delete(filename)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,1093 @@
1
+ # -- encoding : utf-8 --
2
+ require 'test_helper'
3
+
4
+ class TestRooExcel < MiniTest::Test
5
+ def with_spreadsheet(name)
6
+ yield ::Roo::Spreadsheet.open(File.join(TESTDIR, "#{name}.xls"))
7
+ end
8
+
9
+ # Excel can only read the cell's value
10
+ def test_formula_excel
11
+ with_spreadsheet('formula') do |oo|
12
+ assert_equal 21, oo.cell('A', 7)
13
+ assert_equal 21, oo.cell('B', 7)
14
+ end
15
+ end
16
+
17
+ # Ruby-spreadsheet now allows us to at least give the current value
18
+ # from a cell with a formula (no possible with parseexcel)
19
+ def test_bug_false_borders_with_formulas
20
+ with_spreadsheet('false_encoding') do |oo|
21
+ assert_equal 1, oo.first_row
22
+ assert_equal 3, oo.last_row
23
+ assert_equal 1, oo.first_column
24
+ assert_equal 4, oo.last_column
25
+ end
26
+ end
27
+
28
+ # We'ce added minimal formula support so we can now read these
29
+ # though not sure how the spreadsheet reports older values....
30
+ def test_fe
31
+ with_spreadsheet('false_encoding') do |oo|
32
+ assert_equal Date.new(2007, 11, 1), oo.cell('a', 1)
33
+ # DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('a',1)
34
+ # DOES NOT WORK IN EXCEL FILES: assert_equal '=TODAY()', oo.formula('a',1)
35
+
36
+ assert_equal Date.new(2008, 2, 9), oo.cell('B', 1)
37
+ # DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('B',1)
38
+ # DOES NOT WORK IN EXCEL FILES: assert_equal "=A1+100", oo.formula('B',1)
39
+
40
+ assert_kind_of DateTime, oo.cell('C', 1)
41
+ # DOES NOT WORK IN EXCEL FILES: assert_equal true, oo.formula?('C',1)
42
+ # DOES NOT WORK IN EXCEL FILES: assert_equal "=C1", oo.formula('C',1)
43
+
44
+ assert_equal 'H1', oo.cell('A', 2)
45
+ assert_equal 'H2', oo.cell('B', 2)
46
+ assert_equal 'H3', oo.cell('C', 2)
47
+ assert_equal 'H4', oo.cell('D', 2)
48
+ assert_equal 'R1', oo.cell('A', 3)
49
+ assert_equal 'R2', oo.cell('B', 3)
50
+ assert_equal 'R3', oo.cell('C', 3)
51
+ assert_equal 'R4', oo.cell('D', 3)
52
+ end
53
+ end
54
+
55
+ def test_excel_does_not_support_formulas
56
+ with_spreadsheet('false_encoding') do |oo|
57
+ assert_raises(NotImplementedError) { oo.formula('a', 1) }
58
+ assert_raises(NotImplementedError) { oo.formula?('a', 1) }
59
+ assert_raises(NotImplementedError) { oo.formulas(oo.sheets.first) }
60
+ end
61
+ end
62
+
63
+ def test_bug_excel_numbers1_sheet5_last_row
64
+ with_spreadsheet('numbers1') do |oo|
65
+ oo.default_sheet = 'Tabelle1'
66
+ assert_equal 1, oo.first_row
67
+ assert_equal 18, oo.last_row
68
+ assert_equal ::Roo::Utils.letter_to_number('A'), oo.first_column
69
+ assert_equal ::Roo::Utils.letter_to_number('G'), oo.last_column
70
+ oo.default_sheet = 'Name of Sheet 2'
71
+ assert_equal 5, oo.first_row
72
+ assert_equal 14, oo.last_row
73
+ assert_equal ::Roo::Utils.letter_to_number('B'), oo.first_column
74
+ assert_equal ::Roo::Utils.letter_to_number('E'), oo.last_column
75
+ oo.default_sheet = 'Sheet3'
76
+ assert_equal 1, oo.first_row
77
+ assert_equal 1, oo.last_row
78
+ assert_equal ::Roo::Utils.letter_to_number('A'), oo.first_column
79
+ assert_equal ::Roo::Utils.letter_to_number('BA'), oo.last_column
80
+ oo.default_sheet = 'Sheet4'
81
+ assert_equal 1, oo.first_row
82
+ assert_equal 1, oo.last_row
83
+ assert_equal ::Roo::Utils.letter_to_number('A'), oo.first_column
84
+ assert_equal ::Roo::Utils.letter_to_number('E'), oo.last_column
85
+ oo.default_sheet = 'Sheet5'
86
+ assert_equal 1, oo.first_row
87
+ assert_equal 6, oo.last_row
88
+ assert_equal ::Roo::Utils.letter_to_number('A'), oo.first_column
89
+ assert_equal ::Roo::Utils.letter_to_number('E'), oo.last_column
90
+ end
91
+ end
92
+
93
+ def test_bug_row_column_fixnum_float
94
+ with_spreadsheet('bug-row-column-fixnum-float') do |oo|
95
+ assert_equal 42.5, oo.cell('b', 2)
96
+ assert_equal 43, oo.cell('c', 2)
97
+ assert_equal ['hij', 42.5, 43], oo.row(2)
98
+ assert_equal ['def', 42.5, 'nop'], oo.column(2)
99
+ end
100
+ end
101
+
102
+ def test_file_warning_ignore
103
+ Roo::Excel.new(File.join(TESTDIR, 'type_excel.ods'),
104
+ packed: false,
105
+ file_warning: :ignore)
106
+ Roo::Excel.new(File.join(TESTDIR, 'type_excel.xlsx'),
107
+ packed: false,
108
+ file_warning: :ignore)
109
+ end
110
+
111
+ def test_bug_last_row_excel
112
+ with_spreadsheet('time-test') do |oo|
113
+ assert_equal 2, oo.last_row
114
+ end
115
+ end
116
+
117
+ def test_excel_download_uri_and_zipped
118
+ if ONLINE
119
+ url = 'http://stiny-leonhard.de/bode-v1.xls.zip'
120
+ excel = Roo::Excel.new(url, packed: :zip)
121
+ excel.default_sheet = excel.sheets.first
122
+ assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
123
+ end
124
+ end
125
+
126
+ def test_excel_zipped
127
+ oo = Roo::Excel.new(File.join(TESTDIR, 'bode-v1.xls.zip'), packed: :zip)
128
+ assert oo
129
+ assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
130
+ end
131
+
132
+ def test_should_raise_file_not_found_error
133
+ assert_raises(IOError) do
134
+ Roo::Excel.new(File.join('testnichtvorhanden', 'Bibelbund.xls'))
135
+ end
136
+ end
137
+
138
+ def test_file_warning_default
139
+ assert_raises(TypeError) { Roo::Excel.new(File.join(TESTDIR, 'numbers1.ods')) }
140
+ assert_raises(TypeError) { Roo::Excel.new(File.join(TESTDIR, 'numbers1.xlsx')) }
141
+ end
142
+
143
+ def test_file_warning_error
144
+ assert_raises(TypeError) do
145
+ Roo::Excel.new(File.join(TESTDIR, 'numbers1.ods'),
146
+ packed: false,
147
+ file_warning: :error)
148
+ end
149
+ assert_raises(TypeError) do
150
+ Roo::Excel.new(File.join(TESTDIR, 'numbers1.xlsx'),
151
+ packed: false,
152
+ file_warning: :error)
153
+ end
154
+ end
155
+
156
+ def test_file_warning_warning
157
+ assert_raises(Ole::Storage::FormatError) do
158
+ Roo::Excel.new(File.join(TESTDIR, 'numbers1.ods'),
159
+ packed: false,
160
+ file_warning: :warning)
161
+ end
162
+ assert_raises(Ole::Storage::FormatError) do
163
+ Roo::Excel.new(File.join(TESTDIR, 'numbers1.xlsx'),
164
+ packed: false,
165
+ file_warning: :warning)
166
+ end
167
+ end
168
+
169
+ def test_download_uri
170
+ if ONLINE
171
+ assert_raises(RuntimeError) do
172
+ Roo::Excel.new('http://gibbsnichtdomainxxxxx.com/file.xls')
173
+ end
174
+ end
175
+ end
176
+
177
+ def test_download_uri_with_query_string
178
+ dir = File.expand_path("#{File.dirname __FILE__}/files")
179
+ file = "#{dir}/simple_spreadsheet.xls"
180
+ url = 'http://test.example.com/simple_spreadsheet.xls?query-param=value'
181
+ stub_request(:any, url).to_return(body: File.read(file))
182
+ spreadsheet = Roo::Excel.new(url)
183
+ spreadsheet.default_sheet = spreadsheet.sheets.first
184
+ assert_equal 'Task 1', spreadsheet.cell('f', 4)
185
+ end
186
+
187
+ def test_italo_table
188
+ with_spreadsheet('simple_spreadsheet_from_italo') do |oo|
189
+ assert_equal '1', oo.cell('A', 1)
190
+ assert_equal '1', oo.cell('B', 1)
191
+ assert_equal '1', oo.cell('C', 1)
192
+ assert_equal 1, oo.cell('A', 2).to_i
193
+ assert_equal 2, oo.cell('B', 2).to_i
194
+ assert_equal 1, oo.cell('C', 2).to_i
195
+ assert_equal 1, oo.cell('A', 3)
196
+ assert_equal 3, oo.cell('B', 3)
197
+ assert_equal 1, oo.cell('C', 3)
198
+ assert_equal 'A', oo.cell('A', 4)
199
+ assert_equal 'A', oo.cell('B', 4)
200
+ assert_equal 'A', oo.cell('C', 4)
201
+ assert_equal 0.01, oo.cell('A', 5)
202
+ assert_equal 0.01, oo.cell('B', 5)
203
+ assert_equal 0.01, oo.cell('C', 5)
204
+ assert_equal 0.03, oo.cell('a', 5) + oo.cell('b', 5) + oo.cell('c', 5)
205
+
206
+ # Cells values in row 1:
207
+ assert_equal '1:string', oo.cell(1, 1) + ':' + oo.celltype(1, 1).to_s
208
+ assert_equal '1:string', oo.cell(1, 2) + ':' + oo.celltype(1, 2).to_s
209
+ assert_equal '1:string', oo.cell(1, 3) + ':' + oo.celltype(1, 3).to_s
210
+
211
+ # Cells values in row 2:
212
+ assert_equal '1:string', oo.cell(2, 1) + ':' + oo.celltype(2, 1).to_s
213
+ assert_equal '2:string', oo.cell(2, 2) + ':' + oo.celltype(2, 2).to_s
214
+ assert_equal '1:string', oo.cell(2, 3) + ':' + oo.celltype(2, 3).to_s
215
+
216
+ # Cells values in row 3:
217
+ assert_equal '1.0:float', oo.cell(3, 1).to_s + ':' + oo.celltype(3, 1).to_s
218
+ assert_equal '3.0:float', oo.cell(3, 2).to_s + ':' + oo.celltype(3, 2).to_s
219
+ assert_equal '1.0:float', oo.cell(3, 3).to_s + ':' + oo.celltype(3, 3).to_s
220
+
221
+ # Cells values in row 4:
222
+ assert_equal 'A:string', oo.cell(4, 1) + ':' + oo.celltype(4, 1).to_s
223
+ assert_equal 'A:string', oo.cell(4, 2) + ':' + oo.celltype(4, 2).to_s
224
+ assert_equal 'A:string', oo.cell(4, 3) + ':' + oo.celltype(4, 3).to_s
225
+
226
+ # Cells values in row 5:
227
+ assert_equal '0.01:float', oo.cell(5, 1).to_s + ':' + oo.celltype(5, 1).to_s
228
+ assert_equal '0.01:float', oo.cell(5, 2).to_s + ':' + oo.celltype(5, 2).to_s
229
+ assert_equal '0.01:float', oo.cell(5, 3).to_s + ':' + oo.celltype(5, 3).to_s
230
+ end
231
+ end
232
+
233
+ # "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt
234
+ # wird.
235
+ # Besser: Methode um temporaeres Dir. portabel zu bestimmen
236
+ def test_huge_document_to_csv
237
+ if LONG_RUN
238
+ with_spreadsheet('Bibelbund') do |oo|
239
+ Dir.mktmpdir do |tempdir|
240
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45, 'A')
241
+ assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46, 'A')
242
+ assert_equal 'Tagebuch aus Chile Juli 1977', oo.cell(55, 'A')
243
+ assert oo.to_csv(File.join(tempdir, 'Bibelbund.csv'))
244
+ assert File.exist?(File.join(tempdir, 'Bibelbund.csv'))
245
+ assert_equal '', file_diff(File.join(TESTDIR, 'Bibelbund.csv'), File.join(tempdir, 'Bibelbund.csv'))
246
+ end
247
+ end
248
+ end
249
+ end
250
+
251
+ def test_bug_quotes_excelx
252
+ if LONG_RUN
253
+ with_spreadsheet('Bibelbund') do |oo|
254
+ oo.default_sheet = oo.sheets.first
255
+ assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
256
+ oo.cell('a', 76)
257
+ oo.to_csv("csv#{$PROCESS_ID}")
258
+ assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
259
+ oo.cell('a', 78)
260
+ File.delete_if_exist("csv#{$PROCESS_ID}")
261
+ end
262
+ end
263
+ end
264
+
265
+ def test_find_by_row_huge_document
266
+ if LONG_RUN
267
+ with_spreadsheet('Bibelbund') do |oo|
268
+ oo.default_sheet = oo.sheets.first
269
+ rec = oo.find 20
270
+ assert rec
271
+ # assert_equal "Brief aus dem Sekretariat", rec[0]
272
+ # p rec
273
+ assert_equal 'Brief aus dem Sekretariat', rec[0]['TITEL']
274
+ rec = oo.find 22
275
+ assert rec
276
+ # assert_equal "Brief aus dem Skretariat. Tagung in Amberg/Opf.",rec[0]
277
+ assert_equal 'Brief aus dem Skretariat. Tagung in Amberg/Opf.', rec[0]['TITEL']
278
+ end
279
+ end
280
+ end
281
+
282
+ def test_find_by_row
283
+ with_spreadsheet('numbers1') do |oo|
284
+ oo.header_line = nil
285
+ rec = oo.find 16
286
+ assert rec
287
+ assert_nil oo.header_line
288
+ # keine Headerlines in diesem Beispiel definiert
289
+ assert_equal 'einundvierzig', rec[0]
290
+ # assert_equal false, rec
291
+ rec = oo.find 15
292
+ assert rec
293
+ assert_equal 41, rec[0]
294
+ end
295
+ end
296
+
297
+ def test_find_by_row_if_header_line_is_not_nil
298
+ with_spreadsheet('numbers1') do |oo|
299
+ oo.header_line = 2
300
+ refute_nil oo.header_line
301
+ rec = oo.find 1
302
+ assert rec
303
+ assert_equal 5, rec[0]
304
+ assert_equal 6, rec[1]
305
+ rec = oo.find 15
306
+ assert rec
307
+ assert_equal 'einundvierzig', rec[0]
308
+ end
309
+ end
310
+
311
+ def test_find_by_conditions
312
+ if LONG_RUN
313
+ with_spreadsheet('Bibelbund') do |oo|
314
+ #-----------------------------------------------------------------
315
+ zeilen = oo.find(:all, conditions: {
316
+ 'TITEL' => 'Brief aus dem Sekretariat'
317
+ }
318
+ )
319
+ assert_equal 2, zeilen.size
320
+ assert_equal [{ 'VERFASSER' => 'Almassy, Annelene von',
321
+ 'INTERNET' => nil,
322
+ 'SEITE' => 316.0,
323
+ 'KENNUNG' => 'Aus dem Bibelbund',
324
+ 'OBJEKT' => 'Bibel+Gem',
325
+ 'PC' => '#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#',
326
+ 'NUMMER' => '1982-3',
327
+ 'TITEL' => 'Brief aus dem Sekretariat' },
328
+ { 'VERFASSER' => 'Almassy, Annelene von',
329
+ 'INTERNET' => nil,
330
+ 'SEITE' => 222.0,
331
+ 'KENNUNG' => 'Aus dem Bibelbund',
332
+ 'OBJEKT' => 'Bibel+Gem',
333
+ 'PC' => '#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#',
334
+ 'NUMMER' => '1983-2',
335
+ 'TITEL' => 'Brief aus dem Sekretariat' }], zeilen
336
+
337
+ #----------------------------------------------------------
338
+ zeilen = oo.find(:all,
339
+ conditions: { 'VERFASSER' => 'Almassy, Annelene von' }
340
+ )
341
+ assert_equal 13, zeilen.size
342
+ #----------------------------------------------------------
343
+ zeilen = oo.find(:all, conditions: {
344
+ 'TITEL' => 'Brief aus dem Sekretariat',
345
+ 'VERFASSER' => 'Almassy, Annelene von'
346
+ }
347
+ )
348
+ assert_equal 2, zeilen.size
349
+ assert_equal [{ 'VERFASSER' => 'Almassy, Annelene von',
350
+ 'INTERNET' => nil,
351
+ 'SEITE' => 316.0,
352
+ 'KENNUNG' => 'Aus dem Bibelbund',
353
+ 'OBJEKT' => 'Bibel+Gem',
354
+ 'PC' => '#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#',
355
+ 'NUMMER' => '1982-3',
356
+ 'TITEL' => 'Brief aus dem Sekretariat' },
357
+ { 'VERFASSER' => 'Almassy, Annelene von',
358
+ 'INTERNET' => nil,
359
+ 'SEITE' => 222.0,
360
+ 'KENNUNG' => 'Aus dem Bibelbund',
361
+ 'OBJEKT' => 'Bibel+Gem',
362
+ 'PC' => '#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#',
363
+ 'NUMMER' => '1983-2',
364
+ 'TITEL' => 'Brief aus dem Sekretariat' }], zeilen
365
+
366
+ # Result as an array
367
+ zeilen = oo.find(:all,
368
+ conditions: {
369
+ 'TITEL' => 'Brief aus dem Sekretariat',
370
+ 'VERFASSER' => 'Almassy, Annelene von'
371
+ }, array: true)
372
+ assert_equal 2, zeilen.size
373
+ assert_equal [
374
+ [
375
+ 'Brief aus dem Sekretariat',
376
+ 'Almassy, Annelene von',
377
+ 'Bibel+Gem',
378
+ '1982-3',
379
+ 316.0,
380
+ nil,
381
+ '#C:\\Bibelbund\\reprint\\BuG1982-3.pdf#',
382
+ 'Aus dem Bibelbund'
383
+ ],
384
+ [
385
+ 'Brief aus dem Sekretariat',
386
+ 'Almassy, Annelene von',
387
+ 'Bibel+Gem',
388
+ '1983-2',
389
+ 222.0,
390
+ nil,
391
+ '#C:\\Bibelbund\\reprint\\BuG1983-2.pdf#',
392
+ 'Aus dem Bibelbund'
393
+ ]], zeilen
394
+ end
395
+ end
396
+ end
397
+
398
+ # TODO: temporaerer Test
399
+ def test_seiten_als_date
400
+ if LONG_RUN
401
+ with_spreadsheet('Bibelbund', format: :excelx) do |oo|
402
+ assert_equal 'Bericht aus dem Sekretariat', oo.cell(13, 1)
403
+ assert_equal '1981-4', oo.cell(13, 'D')
404
+ assert_equal String, oo.excelx_type(13, 'E')[1].class
405
+ assert_equal [:numeric_or_formula, 'General'], oo.excelx_type(13, 'E')
406
+ assert_equal '428', oo.excelx_value(13, 'E')
407
+ assert_equal 428.0, oo.cell(13, 'E')
408
+ end
409
+ end
410
+ end
411
+
412
+ def test_column
413
+ with_spreadsheet('numbers1') do |oo|
414
+ 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)]
415
+ assert_equal expected, oo.column(1)
416
+ assert_equal expected, oo.column('a')
417
+ end
418
+ end
419
+
420
+ def test_column_huge_document
421
+ if LONG_RUN
422
+ with_spreadsheet('Bibelbund') do |oo|
423
+ oo.default_sheet = oo.sheets.first
424
+ assert_equal 3735, oo.column('a').size
425
+ # assert_equal 499, oo.column('a').size
426
+ end
427
+ end
428
+ end
429
+
430
+ def test_simple_spreadsheet_find_by_condition
431
+ with_spreadsheet('simple_spreadsheet') do |oo|
432
+ oo.header_line = 3
433
+ # oo.date_format = '%m/%d/%Y' if oo.class == Google
434
+ erg = oo.find(:all, conditions: { 'Comment' => 'Task 1' })
435
+ assert_equal Date.new(2007, 05, 07), erg[1]['Date']
436
+ assert_equal 10.75, erg[1]['Start time']
437
+ assert_equal 12.50, erg[1]['End time']
438
+ assert_equal 0, erg[1]['Pause']
439
+ assert_equal 1.75, erg[1]['Sum']
440
+ assert_equal 'Task 1', erg[1]['Comment']
441
+ end
442
+ end
443
+
444
+ def test_info
445
+ expected_templ = "File: numbers1%s\n"\
446
+ "Number of sheets: 5\n"\
447
+ "Sheets: Tabelle1, Name of Sheet 2, Sheet3, Sheet4, Sheet5\n"\
448
+ "Sheet 1:\n"\
449
+ " First row: 1\n"\
450
+ " Last row: 18\n"\
451
+ " First column: A\n"\
452
+ " Last column: G\n"\
453
+ "Sheet 2:\n"\
454
+ " First row: 5\n"\
455
+ " Last row: 14\n"\
456
+ " First column: B\n"\
457
+ " Last column: E\n"\
458
+ "Sheet 3:\n"\
459
+ " First row: 1\n"\
460
+ " Last row: 1\n"\
461
+ " First column: A\n"\
462
+ " Last column: BA\n"\
463
+ "Sheet 4:\n"\
464
+ " First row: 1\n"\
465
+ " Last row: 1\n"\
466
+ " First column: A\n"\
467
+ " Last column: E\n"\
468
+ "Sheet 5:\n"\
469
+ " First row: 1\n"\
470
+ " Last row: 6\n"\
471
+ " First column: A\n"\
472
+ ' Last column: E'
473
+ with_spreadsheet('numbers1') do |oo|
474
+ expected = sprintf(expected_templ, '.xls')
475
+ begin
476
+ assert_equal expected, oo.info
477
+ rescue NameError
478
+ #
479
+ end
480
+ end
481
+ end
482
+
483
+ def test_info_doesnt_set_default_sheet
484
+ with_spreadsheet('numbers1') do |oo|
485
+ oo.default_sheet = 'Sheet3'
486
+ oo.info
487
+ assert_equal 'Sheet3', oo.default_sheet
488
+ end
489
+ end
490
+
491
+ def test_bug_bbu
492
+ with_spreadsheet('bbu') do |oo|
493
+ assert_equal "File: bbu.xls
494
+ Number of sheets: 3
495
+ Sheets: 2007_12, Tabelle2, Tabelle3
496
+ Sheet 1:
497
+ First row: 1
498
+ Last row: 4
499
+ First column: A
500
+ Last column: F
501
+ Sheet 2:
502
+ - empty -
503
+ Sheet 3:
504
+ - empty -", oo.info
505
+
506
+ oo.default_sheet = oo.sheets[1] # empty sheet
507
+ assert_nil oo.first_row
508
+ assert_nil oo.last_row
509
+ assert_nil oo.first_column
510
+ assert_nil oo.last_column
511
+ end
512
+ end
513
+
514
+ def test_bug_time_nil
515
+ with_spreadsheet('time-test') do |oo|
516
+ assert_equal 12 * 3600 + 13 * 60 + 14, oo.cell('B', 1) # 12:13:14 (secs since midnight)
517
+ assert_equal :time, oo.celltype('B', 1)
518
+ assert_equal 15 * 3600 + 16 * 60, oo.cell('C', 1) # 15:16 (secs since midnight)
519
+ assert_equal :time, oo.celltype('C', 1)
520
+ assert_equal 23 * 3600, oo.cell('D', 1) # 23:00 (secs since midnight)
521
+ assert_equal :time, oo.celltype('D', 1)
522
+ end
523
+ end
524
+
525
+ def test_date_time_to_csv
526
+ with_spreadsheet('time-test') do |oo|
527
+ Dir.mktmpdir do |tempdir|
528
+ csv_output = File.join(tempdir, 'time_test.csv')
529
+ assert oo.to_csv(csv_output)
530
+ assert File.exist?(csv_output)
531
+ assert_equal '', `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}`
532
+ # --strip-trailing-cr is needed because the test-file use 0A and
533
+ # the test on an windows box generates 0D 0A as line endings
534
+ end
535
+ end
536
+ end
537
+
538
+ def test_boolean_to_csv
539
+ with_spreadsheet('boolean') do |oo|
540
+ Dir.mktmpdir do |tempdir|
541
+ csv_output = File.join(tempdir, 'boolean.csv')
542
+ assert oo.to_csv(csv_output)
543
+ assert File.exist?(csv_output)
544
+ assert_equal '', `diff --strip-trailing-cr #{TESTDIR}/boolean.csv #{csv_output}`
545
+ # --strip-trailing-cr is needed because the test-file use 0A and
546
+ # the test on an windows box generates 0D 0A as line endings
547
+ end
548
+ end
549
+ end
550
+
551
+ def test_link_to_csv
552
+ with_spreadsheet('link') do |oo|
553
+ Dir.mktmpdir do |tempdir|
554
+ csv_output = File.join(tempdir, 'link.csv')
555
+ assert oo.to_csv(csv_output)
556
+ assert File.exist?(csv_output)
557
+ assert_equal '', `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}`
558
+ # --strip-trailing-cr is needed because the test-file use 0A and
559
+ # the test on an windows box generates 0D 0A as line endings
560
+ end
561
+ end
562
+ end
563
+
564
+ def test_date_time_yaml
565
+ with_spreadsheet('time-test') do |oo|
566
+ expected =
567
+ "--- \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"
568
+ assert_equal expected, oo.to_yaml
569
+ end
570
+ end
571
+
572
+ # Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
573
+ # Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
574
+ # nicht immer in der gleichen Reihenfolge erzeugt.
575
+ def init_all_cells(oo, sheet)
576
+ all = []
577
+ oo.first_row(sheet).upto(oo.last_row(sheet)) do |row|
578
+ oo.first_column(sheet).upto(oo.last_column(sheet)) do |col|
579
+ unless oo.empty?(row, col, sheet)
580
+ all << { row: row.to_s,
581
+ column: col.to_s,
582
+ content: oo.cell(row, col, sheet).to_s,
583
+ type: oo.celltype(row, col, sheet).to_s
584
+ }
585
+ end
586
+ end
587
+ end
588
+ all
589
+ end
590
+
591
+ def test_to_xml
592
+ with_spreadsheet('numbers1') do |oo|
593
+ oo.to_xml
594
+ sheetname = oo.sheets.first
595
+ doc = Nokogiri::XML(oo.to_xml)
596
+ sheet_count = 0
597
+ doc.xpath('//spreadsheet/sheet').each do|_tmpelem|
598
+ sheet_count += 1
599
+ end
600
+ assert_equal 5, sheet_count
601
+ doc.xpath('//spreadsheet/sheet').each do |xml_sheet|
602
+ all_cells = init_all_cells(oo, sheetname)
603
+ x = 0
604
+ assert_equal sheetname, xml_sheet.attributes['name'].value
605
+ xml_sheet.children.each do|cell|
606
+ if cell.attributes['name']
607
+ expected = [all_cells[x][:row],
608
+ all_cells[x][:column],
609
+ all_cells[x][:content],
610
+ all_cells[x][:type]
611
+ ]
612
+ result = [
613
+ cell.attributes['row'],
614
+ cell.attributes['column'],
615
+ cell.content,
616
+ cell.attributes['type']
617
+ ]
618
+ assert_equal expected, result
619
+ x += 1
620
+ end # if
621
+ end # end of sheet
622
+ sheetname = oo.sheets[oo.sheets.index(sheetname) + 1]
623
+ end
624
+ end
625
+ end
626
+
627
+ def test_bug_to_xml_with_empty_sheets
628
+ with_spreadsheet('emptysheets') do |oo|
629
+ oo.sheets.each do |sheet|
630
+ assert_equal nil, oo.first_row, "first_row not nil in sheet #{sheet}"
631
+ assert_equal nil, oo.last_row, "last_row not nil in sheet #{sheet}"
632
+ assert_equal nil, oo.first_column, "first_column not nil in sheet #{sheet}"
633
+ assert_equal nil, oo.last_column, "last_column not nil in sheet #{sheet}"
634
+ assert_equal nil, oo.first_row(sheet), "first_row not nil in sheet #{sheet}"
635
+ assert_equal nil, oo.last_row(sheet), "last_row not nil in sheet #{sheet}"
636
+ assert_equal nil, oo.first_column(sheet), "first_column not nil in sheet #{sheet}"
637
+ assert_equal nil, oo.last_column(sheet), "last_column not nil in sheet #{sheet}"
638
+ end
639
+ oo.to_xml
640
+ end
641
+ end
642
+
643
+ def test_datetime
644
+ with_spreadsheet('datetime') do |oo|
645
+ val = oo.cell('c', 3)
646
+ assert_kind_of DateTime, val
647
+ assert_equal :datetime, oo.celltype('c', 3)
648
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), val
649
+ val = oo.cell('a', 1)
650
+ assert_kind_of Date, val
651
+ assert_equal :date, oo.celltype('a', 1)
652
+ assert_equal Date.new(1961, 11, 21), val
653
+ assert_equal Date.new(1961, 11, 21), oo.cell('a', 1)
654
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('a', 3)
655
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('b', 3)
656
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('c', 3)
657
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('a', 4)
658
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('b', 4)
659
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('c', 4)
660
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('a', 5)
661
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('b', 5)
662
+ assert_equal DateTime.new(1961, 11, 21, 12, 17, 18), oo.cell('c', 5)
663
+ assert_equal Date.new(1961, 11, 21), oo.cell('a', 6)
664
+ assert_equal Date.new(1961, 11, 21), oo.cell('b', 6)
665
+ assert_equal Date.new(1961, 11, 21), oo.cell('c', 6)
666
+ assert_equal Date.new(1961, 11, 21), oo.cell('a', 7)
667
+ assert_equal Date.new(1961, 11, 21), oo.cell('b', 7)
668
+ assert_equal Date.new(1961, 11, 21), oo.cell('c', 7)
669
+ assert_equal DateTime.new(2013, 11, 5, 11, 45, 00), oo.cell('a', 8)
670
+ assert_equal DateTime.new(2013, 11, 5, 11, 45, 00), oo.cell('b', 8)
671
+ assert_equal DateTime.new(2013, 11, 5, 11, 45, 00), oo.cell('c', 8)
672
+ end
673
+ end
674
+
675
+ def test_cell_boolean
676
+ with_spreadsheet('boolean') do |oo|
677
+ assert_equal 'true', oo.cell(1, 1)
678
+ assert_equal 'false', oo.cell(2, 1)
679
+ end
680
+ end
681
+
682
+ def test_cell_multiline
683
+ with_spreadsheet('paragraph') do |oo|
684
+ assert_equal "This is a test\nof a multiline\nCell", oo.cell(1, 1)
685
+ assert_equal "This is a test\n¶\nof a multiline\n\nCell", oo.cell(1, 2)
686
+ assert_equal "first p\n\nsecond p\n\nlast p", oo.cell(2, 1)
687
+ end
688
+ end
689
+
690
+ def test_cell_styles
691
+ # styles only valid in excel spreadsheets?
692
+ # TODO: what todo with other spreadsheet types
693
+ with_spreadsheet('style') do |oo|
694
+ # bold
695
+ assert_equal true, oo.font(1, 1).bold?
696
+ assert_equal false, oo.font(1, 1).italic?
697
+ assert_equal false, oo.font(1, 1).underline?
698
+
699
+ # italic
700
+ assert_equal false, oo.font(2, 1).bold?
701
+ assert_equal true, oo.font(2, 1).italic?
702
+ assert_equal false, oo.font(2, 1).underline?
703
+
704
+ # normal
705
+ assert_equal false, oo.font(3, 1).bold?
706
+ assert_equal false, oo.font(3, 1).italic?
707
+ assert_equal false, oo.font(3, 1).underline?
708
+
709
+ # underline
710
+ assert_equal false, oo.font(4, 1).bold?
711
+ assert_equal false, oo.font(4, 1).italic?
712
+ assert_equal true, oo.font(4, 1).underline?
713
+
714
+ # bold italic
715
+ assert_equal true, oo.font(5, 1).bold?
716
+ assert_equal true, oo.font(5, 1).italic?
717
+ assert_equal false, oo.font(5, 1).underline?
718
+
719
+ # bold underline
720
+ assert_equal true, oo.font(6, 1).bold?
721
+ assert_equal false, oo.font(6, 1).italic?
722
+ assert_equal true, oo.font(6, 1).underline?
723
+
724
+ # italic underline
725
+ assert_equal false, oo.font(7, 1).bold?
726
+ assert_equal true, oo.font(7, 1).italic?
727
+ assert_equal true, oo.font(7, 1).underline?
728
+
729
+ # bolded row
730
+ assert_equal true, oo.font(8, 1).bold?
731
+ assert_equal false, oo.font(8, 1).italic?
732
+ assert_equal false, oo.font(8, 1).underline?
733
+
734
+ # bolded col
735
+ assert_equal true, oo.font(9, 2).bold?
736
+ assert_equal false, oo.font(9, 2).italic?
737
+ assert_equal false, oo.font(9, 2).underline?
738
+
739
+ # bolded row, italic col
740
+ assert_equal true, oo.font(10, 3).bold?
741
+ assert_equal true, oo.font(10, 3).italic?
742
+ assert_equal false, oo.font(10, 3).underline?
743
+
744
+ # normal
745
+ assert_equal false, oo.font(11, 4).bold?
746
+ assert_equal false, oo.font(11, 4).italic?
747
+ assert_equal false, oo.font(11, 4).underline?
748
+ end
749
+ end
750
+
751
+ # If a cell has a date-like string but is preceeded by a '
752
+ # to force that date to be treated like a string, we were getting an exception.
753
+ # This test just checks for that exception to make sure it's not raised in this case
754
+ def test_date_to_float_conversion
755
+ with_spreadsheet('datetime_floatconv') do |oo|
756
+ oo.cell('a', 1)
757
+ oo.cell('a', 2)
758
+ end
759
+ end
760
+
761
+ # Need to extend to other formats
762
+ def test_row_whitespace
763
+ # auf dieses Dokument habe ich keinen Zugriff TODO:
764
+ with_spreadsheet('whitespace') do |oo|
765
+ oo.default_sheet = 'Sheet1'
766
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(1)
767
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(2)
768
+ assert_equal ['Date', 'Start time', 'End time', 'Pause', 'Sum', 'Comment'], oo.row(3)
769
+ assert_equal [Date.new(2007, 5, 7), 9.25, 10.25, 0.0, 1.0, 'Task 1'], oo.row(4)
770
+ assert_equal [nil, nil, nil, nil, nil, nil], oo.row(5)
771
+ assert_equal [Date.new(2007, 5, 7), 10.75, 10.75, 0.0, 0.0, 'Task 1'], oo.row(6)
772
+ oo.default_sheet = 'Sheet2'
773
+ assert_equal ['Date', nil, 'Start time'], oo.row(1)
774
+ assert_equal [Date.new(2007, 5, 7), nil, 9.25], oo.row(2)
775
+ assert_equal [Date.new(2007, 5, 7), nil, 10.75], oo.row(3)
776
+ end
777
+ end
778
+
779
+ def test_col_whitespace
780
+ # TODO:
781
+ # kein Zugriff auf Dokument whitespace
782
+ with_spreadsheet('whitespace') do |oo|
783
+ oo.default_sheet = 'Sheet1'
784
+ assert_equal ['Date', Date.new(2007, 5, 7), nil, Date.new(2007, 5, 7)], oo.column(1)
785
+ assert_equal ['Start time', 9.25, nil, 10.75], oo.column(2)
786
+ assert_equal ['End time', 10.25, nil, 10.75], oo.column(3)
787
+ assert_equal ['Pause', 0.0, nil, 0.0], oo.column(4)
788
+ assert_equal ['Sum', 1.0, nil, 0.0], oo.column(5)
789
+ assert_equal ['Comment', 'Task 1', nil, 'Task 1'], oo.column(6)
790
+ oo.default_sheet = 'Sheet2'
791
+ assert_equal [nil, nil, nil], oo.column(1)
792
+ assert_equal [nil, nil, nil], oo.column(2)
793
+ assert_equal ['Date', Date.new(2007, 5, 7), Date.new(2007, 5, 7)], oo.column(3)
794
+ assert_equal [nil, nil, nil], oo.column(4)
795
+ assert_equal ['Start time', 9.25, 10.75], oo.column(5)
796
+ end
797
+ end
798
+
799
+ def test_ruby_spreadsheet_formula_bug
800
+ with_spreadsheet('formula_parse_error') do |oo|
801
+ assert_equal '5026', oo.cell(2, 3)
802
+ assert_equal '5026', oo.cell(3, 3)
803
+ end
804
+ end
805
+
806
+ def test_excel_links
807
+ with_spreadsheet('link') do |oo|
808
+ assert_equal 'Google', oo.cell(1, 1)
809
+ assert_equal 'http://www.google.com', oo.cell(1, 1).url
810
+ end
811
+ end
812
+
813
+ # Excel has two base date formats one from 1900 and the other from 1904.
814
+ # There's a MS bug that 1900 base dates include an extra day due to erroneously
815
+ # including 1900 as a leap yar.
816
+ def test_base_dates
817
+ with_spreadsheet('1900_base') do |oo|
818
+ assert_equal Date.new(2009, 06, 15), oo.cell(1, 1)
819
+ # we don't want to to 'interpret' formulas assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
820
+ # if we test TODAY() we have also have to calculate
821
+ # other date calculations
822
+ #
823
+ assert_equal :date, oo.celltype(1, 1)
824
+ end
825
+ with_spreadsheet('1904_base') do |oo|
826
+ assert_equal Date.new(2009, 06, 15), oo.cell(1, 1)
827
+ # see comment above
828
+ # assert_equal Date.new(Time.now.year,Time.now.month,Time.now.day), oo.cell(2,1) #formula for TODAY()
829
+ assert_equal :date, oo.celltype(1, 1)
830
+ end
831
+ end
832
+
833
+ def test_bad_date
834
+ with_spreadsheet('prova') do |oo|
835
+ assert_equal DateTime.new(2006, 2, 2, 10, 0, 0), oo.cell('a', 1)
836
+ end
837
+ end
838
+
839
+ def test_bad_excel_date
840
+ with_spreadsheet('bad_excel_date') do |oo|
841
+ assert_equal DateTime.new(2006, 2, 2, 10, 0, 0), oo.cell('a', 1)
842
+ end
843
+ end
844
+
845
+ def test_cell_methods
846
+ with_spreadsheet('numbers1') do |oo|
847
+ assert_equal 10, oo.a4 # cell(4,'A')
848
+ assert_equal 11, oo.b4 # cell(4,'B')
849
+ assert_equal 12, oo.c4 # cell(4,'C')
850
+ assert_equal 13, oo.d4 # cell(4,'D')
851
+ assert_equal 14, oo.e4 # cell(4,'E')
852
+ assert_equal 'ABC', oo.c6('Sheet5')
853
+
854
+ # assert_raises(ArgumentError) {
855
+ assert_raises(NoMethodError) do
856
+ # a42a is not a valid cell name, should raise ArgumentError
857
+ assert_equal 9999, oo.a42a
858
+ end
859
+ end
860
+ end
861
+
862
+ # compare large spreadsheets
863
+ def test_compare_large_spreadsheets
864
+ # problematisch, weil Formeln in Excel nicht unterstützt werden
865
+ if LONG_RUN
866
+ qq = Roo::OpenOffice.new(File.join('test', 'Bibelbund.ods'))
867
+ with_spreadsheet('Bibelbund') do |oo|
868
+ # p "comparing Bibelbund.ods with #{oo.class}"
869
+ oo.sheets.each do |sh|
870
+ oo.first_row.upto(oo.last_row) do |row|
871
+ oo.first_column.upto(oo.last_column) do |col|
872
+ c1 = qq.cell(row, col, sh)
873
+ c1.force_encoding('UTF-8') if c1.class == String
874
+ c2 = oo.cell(row, col, sh)
875
+ c2.force_encoding('UTF-8') if c2.class == String
876
+ assert_equal c1, c2, "diff in #{sh}/#{row}/#{col}}"
877
+ assert_equal qq.celltype(row, col, sh), oo.celltype(row, col, sh)
878
+ end
879
+ end
880
+ end
881
+ end
882
+ end # LONG_RUN
883
+ end
884
+
885
+ require 'matrix'
886
+ def test_matrix
887
+ with_spreadsheet('matrix') do |oo|
888
+ oo.default_sheet = oo.sheets.first
889
+ assert_equal Matrix[
890
+ [1.0, 2.0, 3.0],
891
+ [4.0, 5.0, 6.0],
892
+ [7.0, 8.0, 9.0]], oo.to_matrix
893
+ end
894
+ end
895
+
896
+ def test_matrix_selected_range
897
+ with_spreadsheet('matrix') do |oo|
898
+ oo.default_sheet = 'Sheet2'
899
+ assert_equal Matrix[
900
+ [1.0, 2.0, 3.0],
901
+ [4.0, 5.0, 6.0],
902
+ [7.0, 8.0, 9.0]], oo.to_matrix(3, 4, 5, 6)
903
+ end
904
+ end
905
+
906
+ def test_matrix_all_nil
907
+ with_spreadsheet('matrix') do |oo|
908
+ oo.default_sheet = 'Sheet2'
909
+ assert_equal Matrix[
910
+ [nil, nil, nil],
911
+ [nil, nil, nil],
912
+ [nil, nil, nil]], oo.to_matrix(10, 10, 12, 12)
913
+ end
914
+ end
915
+
916
+ def test_matrix_values_and_nil
917
+ with_spreadsheet('matrix') do |oo|
918
+ oo.default_sheet = 'Sheet3'
919
+ assert_equal Matrix[
920
+ [1.0, nil, 3.0],
921
+ [4.0, 5.0, 6.0],
922
+ [7.0, 8.0, nil]], oo.to_matrix(1, 1, 3, 3)
923
+ end
924
+ end
925
+
926
+ def test_matrix_specifying_sheet
927
+ with_spreadsheet('matrix') do |oo|
928
+ oo.default_sheet = oo.sheets.first
929
+ assert_equal Matrix[
930
+ [1.0, nil, 3.0],
931
+ [4.0, 5.0, 6.0],
932
+ [7.0, 8.0, nil]], oo.to_matrix(nil, nil, nil, nil, 'Sheet3')
933
+ end
934
+ end
935
+
936
+ # 2011-08-03
937
+ def test_bug_datetime_to_csv
938
+ with_spreadsheet('datetime') do |oo|
939
+ Dir.mktmpdir do |tempdir|
940
+ datetime_csv_file = File.join(tempdir, 'datetime.csv')
941
+
942
+ assert oo.to_csv(datetime_csv_file)
943
+ assert File.exist?(datetime_csv_file)
944
+ assert_equal '', file_diff('test/files/so_datetime.csv', datetime_csv_file)
945
+ end
946
+ end
947
+ end
948
+
949
+ def common_possible_bug_snowboard_cells(ss)
950
+ assert_equal 'A.', ss.cell(13, 'A'), ss.class
951
+ assert_equal 147, ss.cell(13, 'f'), ss.class
952
+ assert_equal 152, ss.cell(13, 'g'), ss.class
953
+ assert_equal 156, ss.cell(13, 'h'), ss.class
954
+ assert_equal 158, ss.cell(13, 'i'), ss.class
955
+ assert_equal 160, ss.cell(13, 'j'), ss.class
956
+ assert_equal 164, ss.cell(13, 'k'), ss.class
957
+ assert_equal 168, ss.cell(13, 'l'), ss.class
958
+ assert_equal :string, ss.celltype(13, 'm'), ss.class
959
+ assert_equal '159W', ss.cell(13, 'm'), ss.class
960
+ assert_equal '164W', ss.cell(13, 'n'), ss.class
961
+ assert_equal '168W', ss.cell(13, 'o'), ss.class
962
+ end
963
+
964
+ # def test_false_encoding
965
+ # ex = Roo::Excel.new(File.join(TESTDIR,'false_encoding.xls'))
966
+ # ex.default_sheet = ex.sheets.first
967
+ # assert_equal "Sheet1", ex.sheets.first
968
+ # ex.first_row.upto(ex.last_row) do |row|
969
+ # ex.first_column.upto(ex.last_column) do |col|
970
+ # content = ex.cell(row,col)
971
+ # puts "#{row}/#{col}"
972
+ # #puts content if ! ex.empty?(row,col) or ex.formula?(row,col)
973
+ # if ex.formula?(row,col)
974
+ # #! ex.empty?(row,col)
975
+ # puts content
976
+ # end
977
+ # end
978
+ # end
979
+ # end
980
+
981
+ # def test_soap_server
982
+ # #threads = []
983
+ # #threads << Thread.new("serverthread") do
984
+ # fork do
985
+ # p "serverthread started"
986
+ # puts "in child, pid = #$$"
987
+ # puts `/usr/bin/ruby rooserver.rb`
988
+ # p "serverthread finished"
989
+ # end
990
+ # #threads << Thread.new("clientthread") do
991
+ # p "clientthread started"
992
+ # sleep 10
993
+ # proxy = SOAP::RPC::Driver.new("http://localhost:12321","spreadsheetserver")
994
+ # proxy.add_method('cell','row','col')
995
+ # proxy.add_method('officeversion')
996
+ # proxy.add_method('last_row')
997
+ # proxy.add_method('last_column')
998
+ # proxy.add_method('first_row')
999
+ # proxy.add_method('first_column')
1000
+ # proxy.add_method('sheets')
1001
+ # proxy.add_method('set_default_sheet','s')
1002
+ # proxy.add_method('ferien_fuer_region', 'region')
1003
+
1004
+ # sheets = proxy.sheets
1005
+ # p sheets
1006
+ # proxy.set_default_sheet(sheets.first)
1007
+
1008
+ # assert_equal 1, proxy.first_row
1009
+ # assert_equal 1, proxy.first_column
1010
+ # assert_equal 187, proxy.last_row
1011
+ # assert_equal 7, proxy.last_column
1012
+ # assert_equal 42, proxy.cell('C',8)
1013
+ # assert_equal 43, proxy.cell('F',12)
1014
+ # assert_equal "1.0", proxy.officeversion
1015
+ # p "clientthread finished"
1016
+ # #end
1017
+ # #threads.each {|t| t.join }
1018
+ # puts "fertig"
1019
+ # Process.kill("INT",pid)
1020
+ # pid = Process.wait
1021
+ # puts "child terminated, pid= #{pid}, status= #{$?.exitstatus}"
1022
+ # end
1023
+
1024
+ def split_coord(s)
1025
+ letter = ''
1026
+ number = 0
1027
+ i = 0
1028
+ while i < s.length && 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.include?(s[i, 1])
1029
+ letter += s[i, 1]
1030
+ i += 1
1031
+ end
1032
+ while i < s.length && '01234567890'.include?(s[i, 1])
1033
+ number = number * 10 + s[i, 1].to_i
1034
+ i += 1
1035
+ end
1036
+ if letter == '' || number == 0
1037
+ fail ArgumentError
1038
+ end
1039
+ [letter, number]
1040
+ end
1041
+
1042
+ # def sum(s,expression)
1043
+ # arg = expression.split(':')
1044
+ # b,z = split_coord(arg[0])
1045
+ # first_row = z
1046
+ # first_col = OpenOffice.letter_to_number(b)
1047
+ # b,z = split_coord(arg[1])
1048
+ # last_row = z
1049
+ # last_col = OpenOffice.letter_to_number(b)
1050
+ # result = 0
1051
+ # first_row.upto(last_row) {|row|
1052
+ # first_col.upto(last_col) {|col|
1053
+ # result = result + s.cell(row,col)
1054
+ # }
1055
+ # }
1056
+ # result
1057
+ # end
1058
+
1059
+ # def test_create_spreadsheet1
1060
+ # name = File.join(TESTDIR,'createdspreadsheet.ods')
1061
+ # rm(name) if File.exists?(File.join(TESTDIR,'createdspreadsheet.ods'))
1062
+ # # anlegen, falls noch nicht existierend
1063
+ # s = OpenOffice.new(name,true)
1064
+ # assert File.exists?(name)
1065
+ # end
1066
+
1067
+ # We don't have the bode-v1.xlsx test file
1068
+ # #TODO: xlsx-Datei anpassen!
1069
+ # def test_excelx_download_uri_and_zipped
1070
+ # #TODO: gezippte xlsx Datei online zum Testen suchen
1071
+ # if EXCELX
1072
+ # if ONLINE
1073
+ # url = 'http://stiny-leonhard.de/bode-v1.xlsx.zip'
1074
+ # excel = Roo::Excelx.new(url, :zip)
1075
+ # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1076
+ # end
1077
+ # end
1078
+ # end
1079
+
1080
+ # def test_excelx_zipped
1081
+ # # TODO: bode...xls bei Gelegenheit nach .xlsx konverieren lassen und zippen!
1082
+ # if EXCELX
1083
+ # # diese Datei gibt es noch nicht gezippt
1084
+ # excel = Roo::Excelx.new(File.join(TESTDIR,"bode-v1.xlsx.zip"), :zip)
1085
+ # assert excel
1086
+ # assert_raises(ArgumentError) {
1087
+ # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1088
+ # }
1089
+ # excel.default_sheet = excel.sheets.first
1090
+ # assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
1091
+ # end
1092
+ # end
1093
+ end