roo 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +17 -0
- data/.travis.yml +2 -4
- data/CHANGELOG.md +11 -0
- data/Gemfile +2 -4
- data/Gemfile_ruby2 +4 -3
- data/README.md +10 -0
- data/lib/roo/base.rb +14 -165
- data/lib/roo/csv.rb +93 -98
- data/lib/roo/excelx.rb +8 -3
- data/lib/roo/excelx/cell/datetime.rb +38 -28
- data/lib/roo/excelx/cell/number.rb +19 -23
- data/lib/roo/excelx/cell/time.rb +13 -13
- data/lib/roo/formatters/base.rb +15 -0
- data/lib/roo/formatters/csv.rb +84 -0
- data/lib/roo/formatters/matrix.rb +23 -0
- data/lib/roo/formatters/xml.rb +31 -0
- data/lib/roo/formatters/yaml.rb +40 -0
- data/lib/roo/open_office.rb +9 -3
- data/lib/roo/tempdir.rb +5 -10
- data/lib/roo/version.rb +1 -1
- data/roo.gemspec +1 -0
- data/spec/spec_helper.rb +2 -6
- data/test/excelx/cell/test_time.rb +3 -3
- data/test/formatters/test_csv.rb +119 -0
- data/test/formatters/test_matrix.rb +76 -0
- data/test/formatters/test_xml.rb +74 -0
- data/test/formatters/test_yaml.rb +20 -0
- data/test/roo/test_csv.rb +52 -0
- data/test/roo/test_excelx.rb +186 -0
- data/test/roo/test_libre_office.rb +9 -0
- data/test/roo/test_open_office.rb +126 -0
- data/test/test_helper.rb +74 -3
- data/test/test_roo.rb +22 -928
- metadata +55 -21
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "test_helper"
|
3
|
+
|
4
|
+
class TestRooOpenOffice < Minitest::Test
|
5
|
+
def test_openoffice_download_uri_and_zipped
|
6
|
+
port = 12_345
|
7
|
+
file = "rata.ods.zip"
|
8
|
+
start_local_server(file, port) do
|
9
|
+
url = "#{local_server(port)}/#{file}"
|
10
|
+
|
11
|
+
oo = roo_class.new(url, packed: :zip)
|
12
|
+
assert_in_delta 0.001, 505.14, oo.cell("c", 33).to_f
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_download_uri_with_invalid_host
|
17
|
+
assert_raises(RuntimeError) do
|
18
|
+
roo_class.new("http://example.com/file.ods")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_download_uri_with_query_string
|
23
|
+
file = filename("simple_spreadsheet")
|
24
|
+
port = 12_346
|
25
|
+
url = "#{local_server(port)}/#{file}?query-param=value"
|
26
|
+
start_local_server(file, port) do
|
27
|
+
spreadsheet = roo_class.new(url)
|
28
|
+
assert_equal "Task 1", spreadsheet.cell("f", 4)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_openoffice_zipped
|
33
|
+
oo = roo_class.new(File.join(TESTDIR, "bode-v1.ods.zip"), packed: :zip)
|
34
|
+
assert oo
|
35
|
+
assert_equal 'ist "e" im Nenner von H(s)', oo.cell("b", 5)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_should_raise_file_not_found_error
|
39
|
+
assert_raises(IOError) do
|
40
|
+
roo_class.new(File.join("testnichtvorhanden", "Bibelbund.ods"))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def test_file_warning_default_is_error
|
46
|
+
expected_message = "test/files/numbers1.xls is not an openoffice spreadsheet"
|
47
|
+
assert_raises(TypeError, expected_message) do
|
48
|
+
roo_class.new(File.join(TESTDIR, "numbers1.xls"))
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_raises(TypeError) do
|
52
|
+
roo_class.new(File.join(TESTDIR, "numbers1.xlsx"))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_file_warning_error
|
57
|
+
assert_raises(TypeError) do
|
58
|
+
roo_class.new(File.join(TESTDIR, "numbers1.xls"),
|
59
|
+
packed: false,
|
60
|
+
file_warning: :error
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
assert_raises(TypeError) do
|
65
|
+
roo_class.new(File.join(TESTDIR, "numbers1.xlsx"),
|
66
|
+
packed: false,
|
67
|
+
file_warning: :error)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_file_warning_warning
|
72
|
+
assert_raises(ArgumentError) do
|
73
|
+
roo_class.new(File.join(TESTDIR, "numbers1.xlsx"),
|
74
|
+
packed: false,
|
75
|
+
file_warning: :warning)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_file_warning_ignore
|
80
|
+
assert roo_class.new(File.join(TESTDIR, "type_openoffice.xlsx"),
|
81
|
+
packed: false,
|
82
|
+
file_warning: :ignore), "Should not throw an error"
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_encrypted_file
|
86
|
+
oo = roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "letmein")
|
87
|
+
assert_equal "Hello World", oo.cell("a", 1)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_encrypted_file_requires_password
|
91
|
+
assert_raises(ArgumentError) { roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods")) }
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_encrypted_file_with_incorrect_password
|
95
|
+
assert_raises(ArgumentError) { roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "badpassword") }
|
96
|
+
end
|
97
|
+
|
98
|
+
# 2011-08-11
|
99
|
+
def test_bug_openoffice_formula_missing_letters
|
100
|
+
# NOTE: This document was created using LibreOffice. The formulas seem
|
101
|
+
# different from a document created using OpenOffice.
|
102
|
+
#
|
103
|
+
# TODO: translate
|
104
|
+
# Bei den OpenOffice-Dateien ist in diesem Feld in der XML-
|
105
|
+
# Datei of: als Prefix enthalten, waehrend in dieser Datei
|
106
|
+
# irgendetwas mit oooc: als Prefix verwendet wird.
|
107
|
+
oo = Roo::OpenOffice.new(File.join(TESTDIR,'dreimalvier.ods'))
|
108
|
+
assert_equal '=SUM([.A1:.D1])', oo.formula('e',1)
|
109
|
+
assert_equal '=SUM([.A2:.D2])', oo.formula('e',2)
|
110
|
+
assert_equal '=SUM([.A3:.D3])', oo.formula('e',3)
|
111
|
+
assert_equal [
|
112
|
+
[1,5,'=SUM([.A1:.D1])'],
|
113
|
+
[2,5,'=SUM([.A2:.D2])'],
|
114
|
+
[3,5,'=SUM([.A3:.D3])'],
|
115
|
+
], oo.formulas
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
def roo_class
|
120
|
+
Roo::OpenOffice
|
121
|
+
end
|
122
|
+
|
123
|
+
def filename(name)
|
124
|
+
"#{name}.ods"
|
125
|
+
end
|
126
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require 'simplecov'
|
2
3
|
# require deps
|
3
4
|
require 'tmpdir'
|
4
5
|
require 'fileutils'
|
5
6
|
require 'minitest/autorun'
|
6
7
|
require 'shoulda'
|
7
|
-
require 'fileutils'
|
8
8
|
require 'timeout'
|
9
9
|
require 'logger'
|
10
10
|
require 'date'
|
11
|
-
require 'webmock/minitest'
|
12
11
|
|
13
12
|
# require gem files
|
14
13
|
require 'roo'
|
14
|
+
require "minitest/reporters"
|
15
|
+
Minitest::Reporters.use!([Minitest::Reporters::DefaultReporter.new,
|
16
|
+
Minitest::Reporters::SpecReporter.new]) unless defined? JRUBY_VERSION
|
15
17
|
|
16
|
-
TESTDIR =
|
18
|
+
TESTDIR = File.join(File.dirname(__FILE__), 'files')
|
17
19
|
|
18
20
|
# very simple diff implementation
|
19
21
|
# output is an empty string if the files are equal
|
@@ -52,3 +54,72 @@ class File
|
|
52
54
|
end
|
53
55
|
end
|
54
56
|
end
|
57
|
+
|
58
|
+
def local_server(port)
|
59
|
+
raise ArgumentError unless port.to_i > 0
|
60
|
+
"http://0.0.0.0:#{port}"
|
61
|
+
end
|
62
|
+
|
63
|
+
def start_local_server(filename, port = nil)
|
64
|
+
require "rack"
|
65
|
+
content_type = filename.split(".").last
|
66
|
+
port ||= TEST_RACK_PORT
|
67
|
+
|
68
|
+
web_server = Proc.new do |env|
|
69
|
+
[
|
70
|
+
"200",
|
71
|
+
{ "Content-Type" => content_type },
|
72
|
+
[File.read("#{TESTDIR}/#{filename}")]
|
73
|
+
]
|
74
|
+
end
|
75
|
+
|
76
|
+
t = Thread.new { Rack::Handler::WEBrick.run web_server, Host: '0.0.0.0', Port: port , Logger: WEBrick::BasicLog.new(nil,1) }
|
77
|
+
# give the app a chance to startup
|
78
|
+
sleep(0.2)
|
79
|
+
|
80
|
+
yield
|
81
|
+
ensure
|
82
|
+
t.kill
|
83
|
+
end
|
84
|
+
|
85
|
+
ROO_FORMATS = [
|
86
|
+
:excelx,
|
87
|
+
:excelxm,
|
88
|
+
:openoffice,
|
89
|
+
:libreoffice
|
90
|
+
]
|
91
|
+
|
92
|
+
# call a block of code for each spreadsheet type
|
93
|
+
# and yield a reference to the roo object
|
94
|
+
def with_each_spreadsheet(options)
|
95
|
+
if options[:format]
|
96
|
+
formats = Array(options[:format])
|
97
|
+
invalid_formats = formats - ROO_FORMATS
|
98
|
+
unless invalid_formats.empty?
|
99
|
+
raise "invalid spreadsheet types: #{invalid_formats.join(', ')}"
|
100
|
+
end
|
101
|
+
else
|
102
|
+
formats = ROO_FORMATS
|
103
|
+
end
|
104
|
+
formats.each do |format|
|
105
|
+
begin
|
106
|
+
yield Roo::Spreadsheet.open(File.join(TESTDIR,
|
107
|
+
fixture_filename(options[:name], format)))
|
108
|
+
rescue => e
|
109
|
+
raise e, "#{e.message} for #{format}", e.backtrace unless options[:ignore_errors]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def fixture_filename(name, format)
|
115
|
+
case format
|
116
|
+
when :excelx
|
117
|
+
"#{name}.xlsx"
|
118
|
+
when :excelxm
|
119
|
+
"#{name}.xlsm"
|
120
|
+
when :openoffice, :libreoffice
|
121
|
+
"#{name}.ods"
|
122
|
+
else
|
123
|
+
raise ArgumentError, "unexpected format #{format}"
|
124
|
+
end
|
125
|
+
end
|
data/test/test_roo.rb
CHANGED
@@ -1,17 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
# damit keine falschen Vermutungen aufkommen: Ich habe religioes rein gar nichts
|
3
|
-
# mit diesem Bibelbund zu tun, aber die hatten eine ziemlich grosse
|
4
|
-
# Spreadsheet-Datei mit ca. 3500 Zeilen oeffentlich im Netz, die sich ganz gut
|
5
|
-
# zum Testen eignete.
|
6
|
-
#
|
7
|
-
#--
|
8
|
-
# these test cases were developed to run under Linux OS, some commands
|
9
|
-
# (like 'diff') must be changed (or commented out ;-)) if you want to run
|
10
|
-
# the tests under another OS
|
11
|
-
#
|
12
|
-
|
13
|
-
#TODO
|
14
|
-
# Look at formulas in excel - does not work with date/time
|
15
2
|
|
16
3
|
# Dump warnings that come from the test to open files
|
17
4
|
# with the wrong spreadsheet class
|
@@ -23,69 +10,7 @@ require 'test_helper'
|
|
23
10
|
require 'stringio'
|
24
11
|
|
25
12
|
class TestRoo < Minitest::Test
|
26
|
-
|
27
|
-
OPENOFFICE = true # do OpenOffice-Spreadsheet Tests? (.ods files)
|
28
|
-
EXCELX = true # do Excelx Tests? (.xlsx files)
|
29
|
-
LIBREOFFICE = true # do LibreOffice tests? (.ods files)
|
30
|
-
CSV = true # do CSV tests? (.csv files)
|
31
|
-
|
32
|
-
FORMATS = [
|
33
|
-
:excelx,
|
34
|
-
:excelxm,
|
35
|
-
:openoffice,
|
36
|
-
:libreoffice
|
37
|
-
]
|
38
|
-
|
39
|
-
ONLINE = false
|
40
|
-
LONG_RUN = false
|
41
|
-
|
42
|
-
def fixture_filename(name, format)
|
43
|
-
case format
|
44
|
-
when :excelx
|
45
|
-
"#{name}.xlsx"
|
46
|
-
when :excelxm
|
47
|
-
"#{name}.xlsm"
|
48
|
-
when :openoffice, :libreoffice
|
49
|
-
"#{name}.ods"
|
50
|
-
else
|
51
|
-
raise ArgumentError, "unexpected format #{format}"
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# call a block of code for each spreadsheet type
|
56
|
-
# and yield a reference to the roo object
|
57
|
-
def with_each_spreadsheet(options)
|
58
|
-
if options[:format]
|
59
|
-
formats = Array(options[:format])
|
60
|
-
invalid_formats = formats - FORMATS
|
61
|
-
unless invalid_formats.empty?
|
62
|
-
raise "invalid spreadsheet types: #{invalid_formats.join(', ')}"
|
63
|
-
end
|
64
|
-
else
|
65
|
-
formats = FORMATS
|
66
|
-
end
|
67
|
-
formats.each do |format|
|
68
|
-
begin
|
69
|
-
yield Roo::Spreadsheet.open(File.join(TESTDIR,
|
70
|
-
fixture_filename(options[:name], format)))
|
71
|
-
rescue => e
|
72
|
-
raise e, "#{e.message} for #{format}", e.backtrace unless options[:ignore_errors]
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_sheets_csv
|
78
|
-
if CSV
|
79
|
-
oo = Roo::CSV.new(File.join(TESTDIR,'numbers1.csv'))
|
80
|
-
assert_equal ["default"], oo.sheets
|
81
|
-
assert_raises(RangeError) { oo.default_sheet = "no_sheet" }
|
82
|
-
assert_raises(TypeError) { oo.default_sheet = [1,2,3] }
|
83
|
-
oo.sheets.each { |sh|
|
84
|
-
oo.default_sheet = sh
|
85
|
-
assert_equal sh, oo.default_sheet
|
86
|
-
}
|
87
|
-
end
|
88
|
-
end
|
13
|
+
LONG_RUN = ENV["LONG_RUN"] ? true : false
|
89
14
|
|
90
15
|
def test_sheets
|
91
16
|
with_each_spreadsheet(:name=>'numbers1') do |oo|
|
@@ -168,14 +93,6 @@ class TestRoo < Minitest::Test
|
|
168
93
|
end
|
169
94
|
end
|
170
95
|
|
171
|
-
def test_libre_office
|
172
|
-
if LIBREOFFICE
|
173
|
-
oo = Roo::LibreOffice.new(File.join(TESTDIR, "numbers1.ods"))
|
174
|
-
oo.default_sheet = oo.sheets.first
|
175
|
-
assert_equal 41, oo.cell('a',12)
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
96
|
def test_sheetname
|
180
97
|
with_each_spreadsheet(:name=>'numbers1') do |oo|
|
181
98
|
oo.default_sheet = "Name of Sheet 2"
|
@@ -379,25 +296,6 @@ class TestRoo < Minitest::Test
|
|
379
296
|
end
|
380
297
|
end
|
381
298
|
|
382
|
-
def test_openoffice_download_uri_and_zipped
|
383
|
-
if OPENOFFICE
|
384
|
-
if ONLINE
|
385
|
-
url = 'http://spazioinwind.libero.it/s2/rata.ods.zip'
|
386
|
-
sheet = Roo::OpenOffice.new(url, packed: :zip)
|
387
|
-
#has been changed: assert_equal 'ist "e" im Nenner von H(s)', sheet.cell('b', 5)
|
388
|
-
assert_in_delta 0.001, 505.14, sheet.cell('c', 33).to_f
|
389
|
-
end
|
390
|
-
end
|
391
|
-
end
|
392
|
-
|
393
|
-
def test_openoffice_zipped
|
394
|
-
if OPENOFFICE
|
395
|
-
oo = Roo::OpenOffice.new(File.join(TESTDIR,"bode-v1.ods.zip"), packed: :zip)
|
396
|
-
assert oo
|
397
|
-
assert_equal 'ist "e" im Nenner von H(s)', oo.cell('b', 5)
|
398
|
-
end
|
399
|
-
end
|
400
|
-
|
401
299
|
def test_bug_ric
|
402
300
|
with_each_spreadsheet(:name=>'ric', :format=>:openoffice) do |oo|
|
403
301
|
assert oo.empty?('A',1)
|
@@ -447,51 +345,6 @@ class TestRoo < Minitest::Test
|
|
447
345
|
with_each_spreadsheet(:name=>'Bibelbund1', :format=>:openoffice) do |oo|
|
448
346
|
assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
|
449
347
|
end
|
450
|
-
#if EXCELX
|
451
|
-
# #Datei gibt es noch nicht
|
452
|
-
# oo = Roo::Excelx.new(File.join(TESTDIR,"Bibelbund1.xlsx"))
|
453
|
-
# oo.default_sheet = oo.sheets.first
|
454
|
-
# assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
|
455
|
-
#end
|
456
|
-
end
|
457
|
-
|
458
|
-
# "/tmp/xxxx" darf man unter Windows nicht verwenden, weil das nicht erkannt
|
459
|
-
# wird.
|
460
|
-
# Besser: Methode um temporaeres Dir. portabel zu bestimmen
|
461
|
-
def test_huge_document_to_csv
|
462
|
-
if LONG_RUN
|
463
|
-
with_each_spreadsheet(:name=>'Bibelbund', :format=>[
|
464
|
-
:openoffice,
|
465
|
-
:excelx
|
466
|
-
# Google hier nicht, weil Google-Spreadsheets nicht so gross werden
|
467
|
-
# duerfen
|
468
|
-
]) do |oo|
|
469
|
-
Dir.mktmpdir do |tempdir|
|
470
|
-
assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", oo.cell(45,'A')
|
471
|
-
assert_equal "Tagebuch des Sekret\303\244rs. Nachrichten aus Chile", oo.cell(46,'A')
|
472
|
-
assert_equal "Tagebuch aus Chile Juli 1977", oo.cell(55,'A')
|
473
|
-
assert oo.to_csv(File.join(tempdir,"Bibelbund.csv"))
|
474
|
-
assert File.exists?(File.join(tempdir,"Bibelbund.csv"))
|
475
|
-
assert_equal "", file_diff(File.join(TESTDIR, "Bibelbund.csv"), File.join(tempdir,"Bibelbund.csv")),
|
476
|
-
"error in class #{oo.class}"
|
477
|
-
#end
|
478
|
-
end
|
479
|
-
end
|
480
|
-
end
|
481
|
-
end
|
482
|
-
|
483
|
-
def test_bug_quotes_excelx
|
484
|
-
if LONG_RUN
|
485
|
-
with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice, :excelx]) do |oo|
|
486
|
-
oo.default_sheet = oo.sheets.first
|
487
|
-
assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
|
488
|
-
oo.cell('a',76)
|
489
|
-
oo.to_csv("csv#{$$}")
|
490
|
-
assert_equal 'Einflüsse der neuen Theologie in "de gereformeerde Kerken van Nederland"',
|
491
|
-
oo.cell('a',78)
|
492
|
-
File.delete_if_exist("csv#{$$}")
|
493
|
-
end
|
494
|
-
end
|
495
348
|
end
|
496
349
|
|
497
350
|
def test_bug_mehrere_datum
|
@@ -573,17 +426,6 @@ class TestRoo < Minitest::Test
|
|
573
426
|
end
|
574
427
|
end
|
575
428
|
|
576
|
-
|
577
|
-
def test_bug_empty_sheet
|
578
|
-
with_each_spreadsheet(:name=>'formula', :format=>[:openoffice, :excelx]) do |oo|
|
579
|
-
oo.default_sheet = 'Sheet3' # is an empty sheet
|
580
|
-
Dir.mktmpdir do |tempdir|
|
581
|
-
oo.to_csv(File.join(tempdir,"emptysheet.csv"))
|
582
|
-
assert_equal "", `cat #{File.join(tempdir,"emptysheet.csv")}`
|
583
|
-
end
|
584
|
-
end
|
585
|
-
end
|
586
|
-
|
587
429
|
def test_find_by_row_huge_document
|
588
430
|
if LONG_RUN
|
589
431
|
with_each_spreadsheet(:name=>'Bibelbund', :format=>[:openoffice, :excelx]) do |oo|
|
@@ -718,7 +560,6 @@ class TestRoo < Minitest::Test
|
|
718
560
|
end
|
719
561
|
end
|
720
562
|
|
721
|
-
|
722
563
|
#TODO: temporaerer Test
|
723
564
|
def test_seiten_als_date
|
724
565
|
if LONG_RUN
|
@@ -827,19 +668,6 @@ class TestRoo < Minitest::Test
|
|
827
668
|
end
|
828
669
|
end
|
829
670
|
|
830
|
-
def test_should_raise_file_not_found_error
|
831
|
-
if OPENOFFICE
|
832
|
-
assert_raises(IOError) {
|
833
|
-
Roo::OpenOffice.new(File.join('testnichtvorhanden','Bibelbund.ods'))
|
834
|
-
}
|
835
|
-
end
|
836
|
-
if EXCELX
|
837
|
-
assert_raises(IOError) {
|
838
|
-
Roo::Excelx.new(File.join('testnichtvorhanden','Bibelbund.xlsx'))
|
839
|
-
}
|
840
|
-
end
|
841
|
-
end
|
842
|
-
|
843
671
|
def test_bug_bbu
|
844
672
|
with_each_spreadsheet(:name=>'bbu', :format=>[:openoffice, :excelx]) do |oo|
|
845
673
|
assert_equal "File: bbu#{get_extension(oo)}
|
@@ -875,200 +703,6 @@ Sheet 3:
|
|
875
703
|
end
|
876
704
|
end
|
877
705
|
|
878
|
-
def test_date_time_to_csv
|
879
|
-
with_each_spreadsheet(:name=>'time-test') do |oo|
|
880
|
-
Dir.mktmpdir do |tempdir|
|
881
|
-
csv_output = File.join(tempdir,'time_test.csv')
|
882
|
-
assert oo.to_csv(csv_output)
|
883
|
-
assert File.exists?(csv_output)
|
884
|
-
assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/time-test.csv #{csv_output}`
|
885
|
-
# --strip-trailing-cr is needed because the test-file use 0A and
|
886
|
-
# the test on an windows box generates 0D 0A as line endings
|
887
|
-
end
|
888
|
-
end
|
889
|
-
end
|
890
|
-
|
891
|
-
def test_boolean_to_csv
|
892
|
-
with_each_spreadsheet(:name=>'boolean') do |oo|
|
893
|
-
Dir.mktmpdir do |tempdir|
|
894
|
-
csv_output = File.join(tempdir,'boolean.csv')
|
895
|
-
assert oo.to_csv(csv_output)
|
896
|
-
assert File.exists?(csv_output)
|
897
|
-
assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/boolean.csv #{csv_output}`
|
898
|
-
# --strip-trailing-cr is needed because the test-file use 0A and
|
899
|
-
# the test on an windows box generates 0D 0A as line endings
|
900
|
-
end
|
901
|
-
end
|
902
|
-
end
|
903
|
-
def test_link_to_csv
|
904
|
-
with_each_spreadsheet(:name=>'link',:format=>:excelx) do |oo|
|
905
|
-
Dir.mktmpdir do |tempdir|
|
906
|
-
csv_output = File.join(tempdir,'link.csv')
|
907
|
-
assert oo.to_csv(csv_output)
|
908
|
-
assert File.exists?(csv_output)
|
909
|
-
assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}`
|
910
|
-
# --strip-trailing-cr is needed because the test-file use 0A and
|
911
|
-
# the test on an windows box generates 0D 0A as line endings
|
912
|
-
end
|
913
|
-
end
|
914
|
-
end
|
915
|
-
def test_date_time_yaml
|
916
|
-
with_each_spreadsheet(:name=>'time-test') do |oo|
|
917
|
-
expected =
|
918
|
-
"--- \ncell_1_1: \n row: 1 \n col: 1 \n celltype: string \n value: Mittags: \ncell_1_2: \n row: 1 \n col: 2 \n celltype: time \n value: 12:13:14 \ncell_1_3: \n row: 1 \n col: 3 \n celltype: time \n value: 15:16:00 \ncell_1_4: \n row: 1 \n col: 4 \n celltype: time \n value: 23:00:00 \ncell_2_1: \n row: 2 \n col: 1 \n celltype: date \n value: 2007-11-21 \n"
|
919
|
-
assert_equal expected, oo.to_yaml
|
920
|
-
end
|
921
|
-
end
|
922
|
-
|
923
|
-
# Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
|
924
|
-
# Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
|
925
|
-
# nicht immer in der gleichen Reihenfolge erzeugt.
|
926
|
-
def init_all_cells(oo,sheet)
|
927
|
-
all = []
|
928
|
-
oo.first_row(sheet).upto(oo.last_row(sheet)) do |row|
|
929
|
-
oo.first_column(sheet).upto(oo.last_column(sheet)) do |col|
|
930
|
-
unless oo.empty?(row,col,sheet)
|
931
|
-
all << {:row => row.to_s,
|
932
|
-
:column => col.to_s,
|
933
|
-
:content => oo.cell(row,col,sheet).to_s,
|
934
|
-
:type => oo.celltype(row,col,sheet).to_s,
|
935
|
-
}
|
936
|
-
end
|
937
|
-
end
|
938
|
-
end
|
939
|
-
all
|
940
|
-
end
|
941
|
-
|
942
|
-
def test_to_xml
|
943
|
-
with_each_spreadsheet(:name=>'numbers1', :encoding => 'utf8') do |oo|
|
944
|
-
skip if defined? JRUBY_VERSION
|
945
|
-
oo.to_xml
|
946
|
-
sheetname = oo.sheets.first
|
947
|
-
doc = Nokogiri::XML(oo.to_xml)
|
948
|
-
sheet_count = 0
|
949
|
-
doc.xpath('//spreadsheet/sheet').each {|tmpelem|
|
950
|
-
sheet_count += 1
|
951
|
-
}
|
952
|
-
assert_equal 5, sheet_count
|
953
|
-
doc.xpath('//spreadsheet/sheet').each { |xml_sheet|
|
954
|
-
all_cells = init_all_cells(oo, sheetname)
|
955
|
-
x = 0
|
956
|
-
assert_equal sheetname, xml_sheet.attributes['name'].value
|
957
|
-
xml_sheet.children.each {|cell|
|
958
|
-
if cell.attributes['name']
|
959
|
-
expected = [all_cells[x][:row],
|
960
|
-
all_cells[x][:column],
|
961
|
-
all_cells[x][:content],
|
962
|
-
all_cells[x][:type],
|
963
|
-
]
|
964
|
-
result = [
|
965
|
-
cell.attributes['row'],
|
966
|
-
cell.attributes['column'],
|
967
|
-
cell.content,
|
968
|
-
cell.attributes['type'],
|
969
|
-
]
|
970
|
-
assert_equal expected, result
|
971
|
-
x += 1
|
972
|
-
end # if
|
973
|
-
} # end of sheet
|
974
|
-
sheetname = oo.sheets[oo.sheets.index(sheetname)+1]
|
975
|
-
}
|
976
|
-
end
|
977
|
-
end
|
978
|
-
|
979
|
-
def test_file_warning_default
|
980
|
-
if OPENOFFICE
|
981
|
-
assert_raises(TypeError, "test/files/numbers1.xls is not an openoffice spreadsheet") {
|
982
|
-
Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xls"))
|
983
|
-
}
|
984
|
-
assert_raises(TypeError) { Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx")) }
|
985
|
-
end
|
986
|
-
if EXCELX
|
987
|
-
assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods")) }
|
988
|
-
assert_raises(TypeError) { Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls")) }
|
989
|
-
end
|
990
|
-
end
|
991
|
-
|
992
|
-
def test_file_warning_error
|
993
|
-
if OPENOFFICE
|
994
|
-
assert_raises(TypeError) {
|
995
|
-
Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xls"),
|
996
|
-
packed: false,
|
997
|
-
file_warning: :error
|
998
|
-
)
|
999
|
-
}
|
1000
|
-
assert_raises(TypeError) {
|
1001
|
-
Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx"),
|
1002
|
-
packed: false,
|
1003
|
-
file_warning: :error)
|
1004
|
-
}
|
1005
|
-
end
|
1006
|
-
if EXCELX
|
1007
|
-
assert_raises(TypeError) {
|
1008
|
-
Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),
|
1009
|
-
packed: false,
|
1010
|
-
file_warning: :error)
|
1011
|
-
}
|
1012
|
-
assert_raises(TypeError) {
|
1013
|
-
Roo::Excelx.new(File.join(TESTDIR,"numbers1.xls"),
|
1014
|
-
packed: false,
|
1015
|
-
file_warning: :error)
|
1016
|
-
}
|
1017
|
-
end
|
1018
|
-
end
|
1019
|
-
|
1020
|
-
def test_file_warning_warning
|
1021
|
-
if OPENOFFICE
|
1022
|
-
assert_raises(ArgumentError) {
|
1023
|
-
Roo::OpenOffice.new(File.join(TESTDIR,"numbers1.xlsx"),
|
1024
|
-
packed: false,
|
1025
|
-
file_warning: :warning)
|
1026
|
-
}
|
1027
|
-
end
|
1028
|
-
if EXCELX
|
1029
|
-
assert_raises(ArgumentError) {
|
1030
|
-
Roo::Excelx.new(File.join(TESTDIR,"numbers1.ods"),
|
1031
|
-
packed: false,
|
1032
|
-
file_warning: :warning)
|
1033
|
-
}
|
1034
|
-
end
|
1035
|
-
end
|
1036
|
-
|
1037
|
-
def test_file_warning_ignore
|
1038
|
-
if OPENOFFICE
|
1039
|
-
# Files, die eigentlich OpenOffice-
|
1040
|
-
# Files sind, aber die falsche Endung haben.
|
1041
|
-
# Es soll ohne Fehlermeldung oder Warnung
|
1042
|
-
# oder Abbruch die Datei geoffnet werden
|
1043
|
-
|
1044
|
-
# xlsx
|
1045
|
-
Roo::OpenOffice.new(File.join(TESTDIR,"type_openoffice.xlsx"),
|
1046
|
-
packed: false,
|
1047
|
-
file_warning: :ignore)
|
1048
|
-
end
|
1049
|
-
if EXCELX
|
1050
|
-
Roo::Excelx.new(File.join(TESTDIR,"type_excelx.ods"),
|
1051
|
-
packed: false,
|
1052
|
-
file_warning: :ignore)
|
1053
|
-
end
|
1054
|
-
end
|
1055
|
-
|
1056
|
-
def test_bug_to_xml_with_empty_sheets
|
1057
|
-
with_each_spreadsheet(:name=>'emptysheets', :format=>[:openoffice, :excelx]) do |oo|
|
1058
|
-
oo.sheets.each { |sheet|
|
1059
|
-
assert_nil oo.first_row, "first_row not nil in sheet #{sheet}"
|
1060
|
-
assert_nil oo.last_row, "last_row not nil in sheet #{sheet}"
|
1061
|
-
assert_nil oo.first_column, "first_column not nil in sheet #{sheet}"
|
1062
|
-
assert_nil oo.last_column, "last_column not nil in sheet #{sheet}"
|
1063
|
-
assert_nil oo.first_row(sheet), "first_row not nil in sheet #{sheet}"
|
1064
|
-
assert_nil oo.last_row(sheet), "last_row not nil in sheet #{sheet}"
|
1065
|
-
assert_nil oo.first_column(sheet), "first_column not nil in sheet #{sheet}"
|
1066
|
-
assert_nil oo.last_column(sheet), "last_column not nil in sheet #{sheet}"
|
1067
|
-
}
|
1068
|
-
oo.to_xml
|
1069
|
-
end
|
1070
|
-
end
|
1071
|
-
|
1072
706
|
def test_bug_simple_spreadsheet_time_bug
|
1073
707
|
# really a bug? are cells really of type time?
|
1074
708
|
# No! :float must be the correct type
|
@@ -1324,7 +958,6 @@ Sheet 3:
|
|
1324
958
|
end
|
1325
959
|
end
|
1326
960
|
|
1327
|
-
|
1328
961
|
# compare large spreadsheets
|
1329
962
|
def test_compare_large_spreadsheets
|
1330
963
|
# problematisch, weil Formeln in Excel nicht unterstützt werden
|
@@ -1446,95 +1079,6 @@ Sheet 3:
|
|
1446
1079
|
end
|
1447
1080
|
end
|
1448
1081
|
|
1449
|
-
require 'matrix'
|
1450
|
-
def test_matrix
|
1451
|
-
with_each_spreadsheet(:name => 'matrix', :format => :openoffice) do |oo|
|
1452
|
-
oo.default_sheet = oo.sheets.first
|
1453
|
-
assert_equal Matrix[
|
1454
|
-
[1.0, 2.0, 3.0],
|
1455
|
-
[4.0, 5.0, 6.0],
|
1456
|
-
[7.0, 8.0, 9.0] ], oo.to_matrix
|
1457
|
-
end
|
1458
|
-
end
|
1459
|
-
|
1460
|
-
def test_matrix_selected_range
|
1461
|
-
with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
|
1462
|
-
oo.default_sheet = 'Sheet2'
|
1463
|
-
assert_equal Matrix[
|
1464
|
-
[1.0, 2.0, 3.0],
|
1465
|
-
[4.0, 5.0, 6.0],
|
1466
|
-
[7.0, 8.0, 9.0] ], oo.to_matrix(3,4,5,6)
|
1467
|
-
end
|
1468
|
-
end
|
1469
|
-
|
1470
|
-
def test_matrix_all_nil
|
1471
|
-
with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
|
1472
|
-
oo.default_sheet = 'Sheet2'
|
1473
|
-
assert_equal Matrix[
|
1474
|
-
[nil, nil, nil],
|
1475
|
-
[nil, nil, nil],
|
1476
|
-
[nil, nil, nil] ], oo.to_matrix(10,10,12,12)
|
1477
|
-
end
|
1478
|
-
end
|
1479
|
-
|
1480
|
-
def test_matrix_values_and_nil
|
1481
|
-
with_each_spreadsheet(:name => 'matrix', :format=>:openoffice) do |oo|
|
1482
|
-
oo.default_sheet = 'Sheet3'
|
1483
|
-
assert_equal Matrix[
|
1484
|
-
[1.0, nil, 3.0],
|
1485
|
-
[4.0, 5.0, 6.0],
|
1486
|
-
[7.0, 8.0, nil] ], oo.to_matrix(1,1,3,3)
|
1487
|
-
end
|
1488
|
-
end
|
1489
|
-
|
1490
|
-
def test_matrix_specifying_sheet
|
1491
|
-
with_each_spreadsheet(:name => 'matrix', :format => :openoffice) do |oo|
|
1492
|
-
oo.default_sheet = oo.sheets.first
|
1493
|
-
assert_equal Matrix[
|
1494
|
-
[1.0, nil, 3.0],
|
1495
|
-
[4.0, 5.0, 6.0],
|
1496
|
-
[7.0, 8.0, nil] ], oo.to_matrix(nil, nil, nil, nil, 'Sheet3')
|
1497
|
-
end
|
1498
|
-
end
|
1499
|
-
|
1500
|
-
# unter Windows soll es laut Bug-Reports nicht moeglich sein, eine Excel-Datei, die
|
1501
|
-
# mit Excel.new geoeffnet wurde nach dem Processing anschliessend zu loeschen.
|
1502
|
-
# Anmerkung: Das Spreadsheet-Gem erlaubt kein explizites Close von Spreadsheet-Dateien,
|
1503
|
-
# was verhindern koennte, das die Datei geloescht werden kann.
|
1504
|
-
# def test_bug_cannot_delete_opened_excel_sheet
|
1505
|
-
# with_each_spreadsheet(:name=>'simple_spreadsheet') do |oo|
|
1506
|
-
# 'kopiere nach temporaere Datei und versuche diese zu oeffnen und zu loeschen'
|
1507
|
-
# end
|
1508
|
-
# end
|
1509
|
-
|
1510
|
-
def test_bug_xlsx_reference_cell
|
1511
|
-
|
1512
|
-
if EXCELX
|
1513
|
-
=begin
|
1514
|
-
If cell A contains a string and cell B references cell A. When reading the value of cell B, the result will be
|
1515
|
-
"0.0" instead of the value of cell A.
|
1516
|
-
|
1517
|
-
With the attached test case, I ran the following code:
|
1518
|
-
spreadsheet = Roo::Excelx.new('formula_string_error.xlsx')
|
1519
|
-
spreadsheet.default_sheet = 'sheet1'
|
1520
|
-
p "A: #{spreadsheet.cell(1, 1)}"
|
1521
|
-
p "B: #{spreadsheet.cell(2, 1)}"
|
1522
|
-
|
1523
|
-
with the following results
|
1524
|
-
"A: TestString"
|
1525
|
-
"B: 0.0"
|
1526
|
-
|
1527
|
-
where the expected result is
|
1528
|
-
"A: TestString"
|
1529
|
-
"B: TestString"
|
1530
|
-
=end
|
1531
|
-
xlsx = Roo::Excelx.new(File.join(TESTDIR, "formula_string_error.xlsx"))
|
1532
|
-
xlsx.default_sheet = xlsx.sheets.first
|
1533
|
-
assert_equal 'Teststring', xlsx.cell('a',1)
|
1534
|
-
assert_equal 'Teststring', xlsx.cell('a',2)
|
1535
|
-
end
|
1536
|
-
end
|
1537
|
-
|
1538
1082
|
# #formulas of an empty sheet should return an empty array and not result in
|
1539
1083
|
# an error message
|
1540
1084
|
# 2011-06-24
|
@@ -1547,116 +1091,6 @@ where the expected result is
|
|
1547
1091
|
end
|
1548
1092
|
end
|
1549
1093
|
|
1550
|
-
# #to_yaml of an empty sheet should return an empty string and not result in
|
1551
|
-
# an error message
|
1552
|
-
# 2011-06-24
|
1553
|
-
def test_bug_to_yaml_empty_sheet
|
1554
|
-
with_each_spreadsheet(:name =>'emptysheets',
|
1555
|
-
:format=>[:openoffice,:excelx]) do |oo|
|
1556
|
-
oo.default_sheet = oo.sheets.first
|
1557
|
-
oo.to_yaml
|
1558
|
-
assert_equal('', oo.to_yaml)
|
1559
|
-
end
|
1560
|
-
end
|
1561
|
-
|
1562
|
-
# #to_matrix of an empty sheet should return an empty matrix and not result in
|
1563
|
-
# an error message
|
1564
|
-
# 2011-06-25
|
1565
|
-
def test_bug_to_matrix_empty_sheet
|
1566
|
-
with_each_spreadsheet(:name =>'emptysheets',
|
1567
|
-
:format=>[:openoffice,:excelx]) do |oo|
|
1568
|
-
oo.default_sheet = oo.sheets.first
|
1569
|
-
oo.to_matrix
|
1570
|
-
assert_equal(Matrix.empty(0,0), oo.to_matrix)
|
1571
|
-
end
|
1572
|
-
end
|
1573
|
-
|
1574
|
-
# 2011-08-03
|
1575
|
-
def test_bug_datetime_to_csv
|
1576
|
-
with_each_spreadsheet(:name=>'datetime') do |oo|
|
1577
|
-
Dir.mktmpdir do |tempdir|
|
1578
|
-
datetime_csv_file = File.join(tempdir,"datetime.csv")
|
1579
|
-
|
1580
|
-
assert oo.to_csv(datetime_csv_file)
|
1581
|
-
assert File.exists?(datetime_csv_file)
|
1582
|
-
assert_equal "", file_diff('test/files/so_datetime.csv', datetime_csv_file)
|
1583
|
-
end
|
1584
|
-
end
|
1585
|
-
end
|
1586
|
-
|
1587
|
-
# 2011-08-11
|
1588
|
-
def test_bug_openoffice_formula_missing_letters
|
1589
|
-
if LIBREOFFICE
|
1590
|
-
# Dieses Dokument wurde mit LibreOffice angelegt.
|
1591
|
-
# Keine Ahnung, ob es damit zusammenhaengt, das diese
|
1592
|
-
# Formeln anders sind, als in der Datei formula.ods, welche
|
1593
|
-
# mit OpenOffice angelegt wurde.
|
1594
|
-
# Bei den OpenOffice-Dateien ist in diesem Feld in der XML-
|
1595
|
-
# Datei of: als Prefix enthalten, waehrend in dieser Datei
|
1596
|
-
# irgendetwas mit oooc: als Prefix verwendet wird.
|
1597
|
-
oo = Roo::OpenOffice.new(File.join(TESTDIR,'dreimalvier.ods'))
|
1598
|
-
oo.default_sheet = oo.sheets.first
|
1599
|
-
assert_equal '=SUM([.A1:.D1])', oo.formula('e',1)
|
1600
|
-
assert_equal '=SUM([.A2:.D2])', oo.formula('e',2)
|
1601
|
-
assert_equal '=SUM([.A3:.D3])', oo.formula('e',3)
|
1602
|
-
assert_equal [
|
1603
|
-
[1,5,'=SUM([.A1:.D1])'],
|
1604
|
-
[2,5,'=SUM([.A2:.D2])'],
|
1605
|
-
[3,5,'=SUM([.A3:.D3])'],
|
1606
|
-
], oo.formulas
|
1607
|
-
|
1608
|
-
end
|
1609
|
-
end
|
1610
|
-
|
1611
|
-
=begin
|
1612
|
-
def test_postprocessing_and_types_in_csv
|
1613
|
-
if CSV
|
1614
|
-
oo = CSV.new(File.join(TESTDIR,'csvtypes.csv'))
|
1615
|
-
oo.default_sheet = oo.sheets.first
|
1616
|
-
assert_equal(1,oo.a1)
|
1617
|
-
assert_equal(:float,oo.celltype('A',1))
|
1618
|
-
assert_equal("2",oo.b1)
|
1619
|
-
assert_equal(:string,oo.celltype('B',1))
|
1620
|
-
assert_equal("Mayer",oo.c1)
|
1621
|
-
assert_equal(:string,oo.celltype('C',1))
|
1622
|
-
end
|
1623
|
-
end
|
1624
|
-
=end
|
1625
|
-
|
1626
|
-
=begin
|
1627
|
-
def test_postprocessing_with_callback_function
|
1628
|
-
if CSV
|
1629
|
-
oo = CSV.new(File.join(TESTDIR,'csvtypes.csv'))
|
1630
|
-
oo.default_sheet = oo.sheets.first
|
1631
|
-
|
1632
|
-
#
|
1633
|
-
assert_equal(1, oo.last_column)
|
1634
|
-
end
|
1635
|
-
end
|
1636
|
-
=end
|
1637
|
-
|
1638
|
-
=begin
|
1639
|
-
def x_123
|
1640
|
-
class ::CSV
|
1641
|
-
def cell_postprocessing(row,col,value)
|
1642
|
-
if row < 3
|
1643
|
-
return nil
|
1644
|
-
end
|
1645
|
-
return value
|
1646
|
-
end
|
1647
|
-
end
|
1648
|
-
end
|
1649
|
-
=end
|
1650
|
-
|
1651
|
-
def test_nil_rows_and_lines_csv
|
1652
|
-
# x_123
|
1653
|
-
if CSV
|
1654
|
-
oo = Roo::CSV.new(File.join(TESTDIR,'Bibelbund.csv'))
|
1655
|
-
oo.default_sheet = oo.sheets.first
|
1656
|
-
assert_equal 1, oo.first_row
|
1657
|
-
end
|
1658
|
-
end
|
1659
|
-
|
1660
1094
|
def test_bug_pfand_from_windows_phone_xlsx
|
1661
1095
|
return if defined? JRUBY_VERSION
|
1662
1096
|
with_each_spreadsheet(:name=>'Pfand_from_windows_phone', :format=>:excelx) do |oo|
|
@@ -1707,50 +1141,6 @@ where the expected result is
|
|
1707
1141
|
end
|
1708
1142
|
end
|
1709
1143
|
|
1710
|
-
## PREVIOUSLY SKIPPED
|
1711
|
-
|
1712
|
-
# don't have these test files so removing. We can easily add in
|
1713
|
-
# by modifying with_each_spreadsheet
|
1714
|
-
GNUMERIC_ODS = false # do gnumeric with ods files Tests?
|
1715
|
-
OPENOFFICEWRITE = false # experimental: write access with OO-Documents
|
1716
|
-
|
1717
|
-
def test_writeopenoffice
|
1718
|
-
if OPENOFFICEWRITE
|
1719
|
-
File.cp(File.join(TESTDIR,"numbers1.ods"),
|
1720
|
-
File.join(TESTDIR,"numbers2.ods"))
|
1721
|
-
File.cp(File.join(TESTDIR,"numbers2.ods"),
|
1722
|
-
File.join(TESTDIR,"bak_numbers2.ods"))
|
1723
|
-
oo = OpenOffice.new(File.join(TESTDIR,"numbers2.ods"))
|
1724
|
-
oo.default_sheet = oo.sheets.first
|
1725
|
-
oo.first_row.upto(oo.last_row) {|y|
|
1726
|
-
oo.first_column.upto(oo.last_column) {|x|
|
1727
|
-
unless oo.empty?(y,x)
|
1728
|
-
# oo.set(y, x, oo.cell(y,x) + 7) if oo.celltype(y,x) == "float"
|
1729
|
-
oo.set(y, x, oo.cell(y,x) + 7) if oo.celltype(y,x) == :float
|
1730
|
-
end
|
1731
|
-
}
|
1732
|
-
}
|
1733
|
-
oo.save
|
1734
|
-
|
1735
|
-
oo1 = Roo::OpenOffice.new(File.join(TESTDIR,"numbers2.ods"))
|
1736
|
-
oo2 = Roo::OpenOffice.new(File.join(TESTDIR,"bak_numbers2.ods"))
|
1737
|
-
#p oo2.to_s
|
1738
|
-
assert_equal 999, oo2.cell('a',1), oo2.cell('a',1)
|
1739
|
-
assert_equal oo2.cell('a',1) + 7, oo1.cell('a',1)
|
1740
|
-
assert_equal oo2.cell('b',1)+7, oo1.cell('b',1)
|
1741
|
-
assert_equal oo2.cell('c',1)+7, oo1.cell('c',1)
|
1742
|
-
assert_equal oo2.cell('d',1)+7, oo1.cell('d',1)
|
1743
|
-
assert_equal oo2.cell('a',2)+7, oo1.cell('a',2)
|
1744
|
-
assert_equal oo2.cell('b',2)+7, oo1.cell('b',2)
|
1745
|
-
assert_equal oo2.cell('c',2)+7, oo1.cell('c',2)
|
1746
|
-
assert_equal oo2.cell('d',2)+7, oo1.cell('d',2)
|
1747
|
-
assert_equal oo2.cell('e',2)+7, oo1.cell('e',2)
|
1748
|
-
|
1749
|
-
File.cp(File.join(TESTDIR,"bak_numbers2.ods"),
|
1750
|
-
File.join(TESTDIR,"numbers2.ods"))
|
1751
|
-
end
|
1752
|
-
end
|
1753
|
-
|
1754
1144
|
def common_possible_bug_snowboard_cells(ss)
|
1755
1145
|
assert_equal "A.", ss.cell(13,'A'), ss.class
|
1756
1146
|
assert_equal 147, ss.cell(13,'f'), ss.class
|
@@ -1766,323 +1156,12 @@ where the expected result is
|
|
1766
1156
|
assert_equal "168W", ss.cell(13,'o'), ss.class
|
1767
1157
|
end
|
1768
1158
|
|
1769
|
-
# def test_false_encoding
|
1770
|
-
# ex = Roo::Excel.new(File.join(TESTDIR,'false_encoding.xls'))
|
1771
|
-
# ex.default_sheet = ex.sheets.first
|
1772
|
-
# assert_equal "Sheet1", ex.sheets.first
|
1773
|
-
# ex.first_row.upto(ex.last_row) do |row|
|
1774
|
-
# ex.first_column.upto(ex.last_column) do |col|
|
1775
|
-
# content = ex.cell(row,col)
|
1776
|
-
# puts "#{row}/#{col}"
|
1777
|
-
# #puts content if ! ex.empty?(row,col) or ex.formula?(row,col)
|
1778
|
-
# if ex.formula?(row,col)
|
1779
|
-
# #! ex.empty?(row,col)
|
1780
|
-
# puts content
|
1781
|
-
# end
|
1782
|
-
# end
|
1783
|
-
# end
|
1784
|
-
# end
|
1785
|
-
|
1786
|
-
def test_download_uri
|
1787
|
-
if ONLINE
|
1788
|
-
if OPENOFFICE
|
1789
|
-
assert_raises(RuntimeError) {
|
1790
|
-
Roo::OpenOffice.new("http://gibbsnichtdomainxxxxx.com/file.ods")
|
1791
|
-
}
|
1792
|
-
end
|
1793
|
-
if EXCELX
|
1794
|
-
assert_raises(RuntimeError) {
|
1795
|
-
Roo::Excelx.new("http://gibbsnichtdomainxxxxx.com/file.xlsx")
|
1796
|
-
}
|
1797
|
-
end
|
1798
|
-
end
|
1799
|
-
end
|
1800
|
-
|
1801
|
-
def test_download_uri_with_query_string
|
1802
|
-
dir = File.expand_path("#{File.dirname __FILE__}/files")
|
1803
|
-
{ xlsx: [EXCELX, Roo::Excelx],
|
1804
|
-
ods: [OPENOFFICE, Roo::OpenOffice]}.each do |extension, (flag, type)|
|
1805
|
-
if flag
|
1806
|
-
file = "#{dir}/simple_spreadsheet.#{extension}"
|
1807
|
-
url = "http://test.example.com/simple_spreadsheet.#{extension}?query-param=value"
|
1808
|
-
stub_request(:any, url).to_return(body: File.read(file))
|
1809
|
-
spreadsheet = type.new(url)
|
1810
|
-
spreadsheet.default_sheet = spreadsheet.sheets.first
|
1811
|
-
assert_equal 'Task 1', spreadsheet.cell('f', 4)
|
1812
|
-
end
|
1813
|
-
end
|
1814
|
-
end
|
1815
|
-
|
1816
|
-
# def test_soap_server
|
1817
|
-
# #threads = []
|
1818
|
-
# #threads << Thread.new("serverthread") do
|
1819
|
-
# fork do
|
1820
|
-
# p "serverthread started"
|
1821
|
-
# puts "in child, pid = #$$"
|
1822
|
-
# puts `/usr/bin/ruby rooserver.rb`
|
1823
|
-
# p "serverthread finished"
|
1824
|
-
# end
|
1825
|
-
# #threads << Thread.new("clientthread") do
|
1826
|
-
# p "clientthread started"
|
1827
|
-
# sleep 10
|
1828
|
-
# proxy = SOAP::RPC::Driver.new("http://localhost:12321","spreadsheetserver")
|
1829
|
-
# proxy.add_method('cell','row','col')
|
1830
|
-
# proxy.add_method('officeversion')
|
1831
|
-
# proxy.add_method('last_row')
|
1832
|
-
# proxy.add_method('last_column')
|
1833
|
-
# proxy.add_method('first_row')
|
1834
|
-
# proxy.add_method('first_column')
|
1835
|
-
# proxy.add_method('sheets')
|
1836
|
-
# proxy.add_method('set_default_sheet','s')
|
1837
|
-
# proxy.add_method('ferien_fuer_region', 'region')
|
1838
|
-
|
1839
|
-
# sheets = proxy.sheets
|
1840
|
-
# p sheets
|
1841
|
-
# proxy.set_default_sheet(sheets.first)
|
1842
|
-
|
1843
|
-
# assert_equal 1, proxy.first_row
|
1844
|
-
# assert_equal 1, proxy.first_column
|
1845
|
-
# assert_equal 187, proxy.last_row
|
1846
|
-
# assert_equal 7, proxy.last_column
|
1847
|
-
# assert_equal 42, proxy.cell('C',8)
|
1848
|
-
# assert_equal 43, proxy.cell('F',12)
|
1849
|
-
# assert_equal "1.0", proxy.officeversion
|
1850
|
-
# p "clientthread finished"
|
1851
|
-
# #end
|
1852
|
-
# #threads.each {|t| t.join }
|
1853
|
-
# puts "fertig"
|
1854
|
-
# Process.kill("INT",pid)
|
1855
|
-
# pid = Process.wait
|
1856
|
-
# puts "child terminated, pid= #{pid}, status= #{$?.exitstatus}"
|
1857
|
-
# end
|
1858
|
-
|
1859
|
-
def split_coord(s)
|
1860
|
-
letter = ""
|
1861
|
-
number = 0
|
1862
|
-
i = 0
|
1863
|
-
while i<s.length and "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".include?(s[i,1])
|
1864
|
-
letter += s[i,1]
|
1865
|
-
i+=1
|
1866
|
-
end
|
1867
|
-
while i<s.length and "01234567890".include?(s[i,1])
|
1868
|
-
number = number*10 + s[i,1].to_i
|
1869
|
-
i+=1
|
1870
|
-
end
|
1871
|
-
if letter=="" or number==0
|
1872
|
-
raise ArgumentError
|
1873
|
-
end
|
1874
|
-
return letter,number
|
1875
|
-
end
|
1876
|
-
|
1877
|
-
#def sum(s,expression)
|
1878
|
-
# arg = expression.split(':')
|
1879
|
-
# b,z = split_coord(arg[0])
|
1880
|
-
# first_row = z
|
1881
|
-
# first_col = OpenOffice.letter_to_number(b)
|
1882
|
-
# b,z = split_coord(arg[1])
|
1883
|
-
# last_row = z
|
1884
|
-
# last_col = OpenOffice.letter_to_number(b)
|
1885
|
-
# result = 0
|
1886
|
-
# first_row.upto(last_row) {|row|
|
1887
|
-
# first_col.upto(last_col) {|col|
|
1888
|
-
# result = result + s.cell(row,col)
|
1889
|
-
# }
|
1890
|
-
# }
|
1891
|
-
# result
|
1892
|
-
#end
|
1893
|
-
|
1894
|
-
#def test_dsl
|
1895
|
-
# s = OpenOffice.new(File.join(TESTDIR,"numbers1.ods"))
|
1896
|
-
# s.default_sheet = s.sheets.first
|
1897
|
-
#
|
1898
|
-
# s.set 'a',1, 5
|
1899
|
-
# s.set 'b',1, 3
|
1900
|
-
# s.set 'c',1, 7
|
1901
|
-
# s.set('a',2, s.cell('a',1)+s.cell('b',1))
|
1902
|
-
# assert_equal 8, s.cell('a',2)
|
1903
|
-
#
|
1904
|
-
# assert_equal 15, sum(s,'A1:C1')
|
1905
|
-
# end
|
1906
|
-
|
1907
|
-
#def test_create_spreadsheet1
|
1908
|
-
# name = File.join(TESTDIR,'createdspreadsheet.ods')
|
1909
|
-
# rm(name) if File.exists?(File.join(TESTDIR,'createdspreadsheet.ods'))
|
1910
|
-
# # anlegen, falls noch nicht existierend
|
1911
|
-
# s = OpenOffice.new(name,true)
|
1912
|
-
# assert File.exists?(name)
|
1913
|
-
#end
|
1914
|
-
|
1915
|
-
#def test_create_spreadsheet2
|
1916
|
-
# # anlegen, falls noch nicht existierend
|
1917
|
-
# s = OpenOffice.new(File.join(TESTDIR,"createdspreadsheet.ods"),true)
|
1918
|
-
# s.set 'a',1,42
|
1919
|
-
# s.set 'b',1,43
|
1920
|
-
# s.set 'c',1,44
|
1921
|
-
# s.save
|
1922
|
-
#
|
1923
|
-
# t = OpenOffice.new(File.join(TESTDIR,"createdspreadsheet.ods"))
|
1924
|
-
# assert_equal 42, t.cell(1,'a')
|
1925
|
-
# assert_equal 43, t.cell('b',1)
|
1926
|
-
# assert_equal 44, t.cell('c',3)
|
1927
|
-
#end
|
1928
|
-
|
1929
|
-
# We don't have the bode-v1.xlsx test file
|
1930
|
-
# #TODO: xlsx-Datei anpassen!
|
1931
|
-
# def test_excelx_download_uri_and_zipped
|
1932
|
-
# #TODO: gezippte xlsx Datei online zum Testen suchen
|
1933
|
-
# if EXCELX
|
1934
|
-
# if ONLINE
|
1935
|
-
# url = 'http://stiny-leonhard.de/bode-v1.xlsx.zip'
|
1936
|
-
# excel = Roo::Excelx.new(url, :zip)
|
1937
|
-
# assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
|
1938
|
-
# end
|
1939
|
-
# end
|
1940
|
-
# end
|
1941
|
-
|
1942
|
-
# def test_excelx_zipped
|
1943
|
-
# # TODO: bode...xls bei Gelegenheit nach .xlsx konverieren lassen und zippen!
|
1944
|
-
# if EXCELX
|
1945
|
-
# # diese Datei gibt es noch nicht gezippt
|
1946
|
-
# excel = Roo::Excelx.new(File.join(TESTDIR,"bode-v1.xlsx.zip"), :zip)
|
1947
|
-
# assert excel
|
1948
|
-
# assert_raises(ArgumentError) {
|
1949
|
-
# assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
|
1950
|
-
# }
|
1951
|
-
# excel.default_sheet = excel.sheets.first
|
1952
|
-
# assert_equal 'ist "e" im Nenner von H(s)', excel.cell('b', 5)
|
1953
|
-
# end
|
1954
|
-
# end
|
1955
|
-
|
1956
|
-
def test_csv_parsing_with_headers
|
1957
|
-
return unless CSV
|
1958
|
-
headers = ["TITEL", "VERFASSER", "OBJEKT", "NUMMER", "SEITE", "INTERNET", "PC", "KENNUNG"]
|
1959
|
-
|
1960
|
-
oo = Roo::Spreadsheet.open(File.join(TESTDIR, 'Bibelbund.csv'))
|
1961
|
-
parsed = oo.parse(:headers => true)
|
1962
|
-
assert_equal headers, parsed[1].keys
|
1963
|
-
end
|
1964
|
-
|
1965
1159
|
def test_bug_numbered_sheet_names
|
1966
1160
|
with_each_spreadsheet(:name=>'bug-numbered-sheet-names', :format=>:excelx) do |oo|
|
1967
1161
|
oo.each_with_pagename { }
|
1968
1162
|
end
|
1969
1163
|
end
|
1970
1164
|
|
1971
|
-
def test_parsing_xslx_from_numbers
|
1972
|
-
return unless EXCELX
|
1973
|
-
xlsx = Roo::Excelx.new(File.join(TESTDIR, "numbers-export.xlsx"))
|
1974
|
-
|
1975
|
-
xlsx.default_sheet = xlsx.sheets.first
|
1976
|
-
assert_equal 'Sheet 1', xlsx.cell('a',1)
|
1977
|
-
|
1978
|
-
# Another buggy behavior of Numbers 3.1: if a warkbook has more than a
|
1979
|
-
# single sheet, all sheets except the first one will have an extra row and
|
1980
|
-
# column added to the beginning. That's why we assert against cell B2 and
|
1981
|
-
# not A1
|
1982
|
-
xlsx.default_sheet = xlsx.sheets.last
|
1983
|
-
assert_equal 'Sheet 2', xlsx.cell('b',2)
|
1984
|
-
end
|
1985
|
-
|
1986
|
-
def test_openoffice_encryption
|
1987
|
-
if OPENOFFICE
|
1988
|
-
assert_raises(ArgumentError) { Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods")) }
|
1989
|
-
assert_raises(ArgumentError) { Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods"), :password => "badpassword") }
|
1990
|
-
oo = Roo::LibreOffice.new(File.join(TESTDIR, "encrypted-letmein.ods"), :password => "letmein")
|
1991
|
-
oo.default_sheet = oo.sheets.first
|
1992
|
-
assert_equal "Hello World", oo.cell('a',1)
|
1993
|
-
end
|
1994
|
-
end
|
1995
|
-
|
1996
|
-
def test_expand_merged_range
|
1997
|
-
return unless EXCELX
|
1998
|
-
xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"), {:expand_merged_ranges => true})
|
1999
|
-
for row in 3..7 do
|
2000
|
-
for col in 'a'..'b'
|
2001
|
-
if row > 3 && row < 7 && col == 'a'
|
2002
|
-
assert_equal 'vertical1', xlsx.cell(col,row)
|
2003
|
-
else
|
2004
|
-
assert_nil xlsx.cell(col,row)
|
2005
|
-
end
|
2006
|
-
end
|
2007
|
-
end
|
2008
|
-
for row in 3..11 do
|
2009
|
-
for col in 'f'..'h'
|
2010
|
-
if row > 3 && row < 11 && col == 'g'
|
2011
|
-
assert_equal 'vertical2', xlsx.cell(col,row)
|
2012
|
-
else
|
2013
|
-
assert_nil xlsx.cell(col,row)
|
2014
|
-
end
|
2015
|
-
end
|
2016
|
-
end
|
2017
|
-
for row in 3..5 do
|
2018
|
-
for col in 'b'..'f'
|
2019
|
-
if row == 4 && col > 'b' && col < 'f'
|
2020
|
-
assert_equal 'horizontal', xlsx.cell(col,row)
|
2021
|
-
else
|
2022
|
-
assert_nil xlsx.cell(col,row)
|
2023
|
-
end
|
2024
|
-
end
|
2025
|
-
end
|
2026
|
-
for row in 8..13 do
|
2027
|
-
for col in 'a'..'e'
|
2028
|
-
if row > 8 && row < 13 && col > 'a' && col < 'e'
|
2029
|
-
assert_equal 'block', xlsx.cell(col,row)
|
2030
|
-
else
|
2031
|
-
assert_nil xlsx.cell(col,row)
|
2032
|
-
end
|
2033
|
-
end
|
2034
|
-
end
|
2035
|
-
end
|
2036
|
-
|
2037
|
-
def test_noexpand_merged_range
|
2038
|
-
return unless EXCELX
|
2039
|
-
xlsx = Roo::Excelx.new(File.join(TESTDIR, "merged_ranges.xlsx"))
|
2040
|
-
for row in 3..7 do
|
2041
|
-
for col in 'a'..'b'
|
2042
|
-
if row == 4 && col == 'a'
|
2043
|
-
assert_equal 'vertical1', xlsx.cell(col,row)
|
2044
|
-
else
|
2045
|
-
assert_nil xlsx.cell(col,row)
|
2046
|
-
end
|
2047
|
-
end
|
2048
|
-
end
|
2049
|
-
for row in 3..11 do
|
2050
|
-
for col in 'f'..'h'
|
2051
|
-
if row == 4 && col == 'g'
|
2052
|
-
assert_equal 'vertical2', xlsx.cell(col,row)
|
2053
|
-
else
|
2054
|
-
assert_nil xlsx.cell(col,row)
|
2055
|
-
end
|
2056
|
-
end
|
2057
|
-
end
|
2058
|
-
for row in 3..5 do
|
2059
|
-
for col in 'b'..'f'
|
2060
|
-
if row == 4 && col == 'c'
|
2061
|
-
assert_equal 'horizontal', xlsx.cell(col,row)
|
2062
|
-
else
|
2063
|
-
assert_nil xlsx.cell(col,row)
|
2064
|
-
end
|
2065
|
-
end
|
2066
|
-
end
|
2067
|
-
for row in 8..13 do
|
2068
|
-
for col in 'a'..'e'
|
2069
|
-
if row == 9 && col == 'b'
|
2070
|
-
assert_equal 'block', xlsx.cell(col,row)
|
2071
|
-
else
|
2072
|
-
assert_nil xlsx.cell(col,row)
|
2073
|
-
end
|
2074
|
-
end
|
2075
|
-
end
|
2076
|
-
end
|
2077
|
-
|
2078
|
-
def test_open_stream
|
2079
|
-
return unless EXCELX
|
2080
|
-
file_contents = File.read File.join(TESTDIR, fixture_filename(:numbers1, :excelx)), encoding: 'BINARY'
|
2081
|
-
stream = StringIO.new(file_contents)
|
2082
|
-
xlsx = Roo::Excelx.new(stream)
|
2083
|
-
assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], xlsx.sheets
|
2084
|
-
end
|
2085
|
-
|
2086
1165
|
def test_close
|
2087
1166
|
with_each_spreadsheet(:name=>'numbers1') do |oo|
|
2088
1167
|
next unless (tempdir = oo.instance_variable_get('@tmpdir'))
|
@@ -2091,16 +1170,31 @@ where the expected result is
|
|
2091
1170
|
end
|
2092
1171
|
end
|
2093
1172
|
|
1173
|
+
# NOTE: Ruby 2.4.0 changed the way GC works. The last Roo object created by
|
1174
|
+
# with_each_spreadsheet wasn't getting GC'd until after the process
|
1175
|
+
# ended.
|
1176
|
+
#
|
1177
|
+
# That behavior change broke this test. In order to fix it, I forked the
|
1178
|
+
# process and passed the temp directories from the forked process in
|
1179
|
+
# order to check if they were removed properly.
|
2094
1180
|
def test_finalize
|
2095
|
-
|
2096
|
-
|
2097
|
-
|
2098
|
-
|
1181
|
+
skip if defined? JRUBY_VERSION
|
1182
|
+
|
1183
|
+
read, write = IO.pipe
|
1184
|
+
pid = Process.fork do
|
1185
|
+
with_each_spreadsheet(name: "numbers1") do |oo|
|
1186
|
+
write.puts oo.instance_variable_get("@tmpdir")
|
2099
1187
|
end
|
2100
|
-
GC.start
|
2101
1188
|
end
|
1189
|
+
|
1190
|
+
Process.wait(pid)
|
1191
|
+
write.close
|
1192
|
+
tempdirs = read.read.split("\n")
|
1193
|
+
read.close
|
1194
|
+
|
1195
|
+
refute tempdirs.empty?
|
2102
1196
|
tempdirs.each do |tempdir|
|
2103
|
-
|
1197
|
+
refute File.exist?(tempdir), "Expected #{tempdir} to be cleaned up, but it still exists"
|
2104
1198
|
end
|
2105
1199
|
end
|
2106
1200
|
|