ruby-spreadsheet 0.6.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/GUIDE.txt +267 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +20 -0
- data/History.txt +307 -0
- data/LICENSE.txt +619 -0
- data/README.txt +91 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/bin/xlsopcodes +18 -0
- data/lib/parseexcel.rb +27 -0
- data/lib/parseexcel/parseexcel.rb +75 -0
- data/lib/parseexcel/parser.rb +11 -0
- data/lib/spreadsheet.rb +79 -0
- data/lib/spreadsheet/column.rb +71 -0
- data/lib/spreadsheet/compatibility.rb +23 -0
- data/lib/spreadsheet/datatypes.rb +110 -0
- data/lib/spreadsheet/encodings.rb +46 -0
- data/lib/spreadsheet/excel.rb +88 -0
- data/lib/spreadsheet/excel/error.rb +26 -0
- data/lib/spreadsheet/excel/internals.rb +386 -0
- data/lib/spreadsheet/excel/internals/biff5.rb +17 -0
- data/lib/spreadsheet/excel/internals/biff8.rb +19 -0
- data/lib/spreadsheet/excel/offset.rb +41 -0
- data/lib/spreadsheet/excel/reader.rb +1173 -0
- data/lib/spreadsheet/excel/reader/biff5.rb +22 -0
- data/lib/spreadsheet/excel/reader/biff8.rb +193 -0
- data/lib/spreadsheet/excel/row.rb +92 -0
- data/lib/spreadsheet/excel/sst_entry.rb +46 -0
- data/lib/spreadsheet/excel/workbook.rb +80 -0
- data/lib/spreadsheet/excel/worksheet.rb +100 -0
- data/lib/spreadsheet/excel/writer.rb +1 -0
- data/lib/spreadsheet/excel/writer/biff8.rb +75 -0
- data/lib/spreadsheet/excel/writer/format.rb +253 -0
- data/lib/spreadsheet/excel/writer/workbook.rb +652 -0
- data/lib/spreadsheet/excel/writer/worksheet.rb +948 -0
- data/lib/spreadsheet/font.rb +92 -0
- data/lib/spreadsheet/format.rb +177 -0
- data/lib/spreadsheet/formula.rb +9 -0
- data/lib/spreadsheet/helpers.rb +11 -0
- data/lib/spreadsheet/link.rb +43 -0
- data/lib/spreadsheet/row.rb +132 -0
- data/lib/spreadsheet/workbook.rb +120 -0
- data/lib/spreadsheet/worksheet.rb +279 -0
- data/lib/spreadsheet/writer.rb +30 -0
- data/ruby-spreadsheet.gemspec +126 -0
- data/test/data/test_changes.xls +0 -0
- data/test/data/test_copy.xls +0 -0
- data/test/data/test_datetime.xls +0 -0
- data/test/data/test_empty.xls +0 -0
- data/test/data/test_formula.xls +0 -0
- data/test/data/test_missing_row.xls +0 -0
- data/test/data/test_version_excel5.xls +0 -0
- data/test/data/test_version_excel95.xls +0 -0
- data/test/data/test_version_excel97.xls +0 -0
- data/test/excel/row.rb +35 -0
- data/test/excel/writer/worksheet.rb +23 -0
- data/test/font.rb +163 -0
- data/test/integration.rb +1281 -0
- data/test/row.rb +33 -0
- data/test/suite.rb +14 -0
- data/test/workbook.rb +21 -0
- data/test/worksheet.rb +80 -0
- metadata +203 -0
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/test/excel/row.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Excel::TestRow -- Spreadsheet -- 12.10.2008 -- hwyss@ywesee.com
|
3
|
+
|
4
|
+
$: << File.expand_path('../../lib', File.dirname(__FILE__))
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require 'spreadsheet'
|
8
|
+
|
9
|
+
module Spreadsheet
|
10
|
+
module Excel
|
11
|
+
class TestRow < Test::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
@workbook = Excel::Workbook.new
|
14
|
+
@worksheet = Excel::Worksheet.new
|
15
|
+
@workbook.add_worksheet @worksheet
|
16
|
+
end
|
17
|
+
def test_date
|
18
|
+
row = Row.new @worksheet, 0, [nil, 27627.6789]
|
19
|
+
assert_equal Date.new(1975,8,21), row.date(1)
|
20
|
+
end
|
21
|
+
def test_datetime
|
22
|
+
row = Row.new @worksheet, 0, [nil, 27627.765]
|
23
|
+
d1 = DateTime.new(1975,8,21) + 0.765
|
24
|
+
d2 = row.datetime 1
|
25
|
+
assert_equal d1, d2
|
26
|
+
end
|
27
|
+
def test_datetime_overflow
|
28
|
+
row = Row.new @worksheet, 0, [nil, 40010.6666666666]
|
29
|
+
d1 = DateTime.new(2009,07,16,16)
|
30
|
+
d2 = row.datetime 1
|
31
|
+
assert_equal d1, d2
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Excel::Writer::TestWorksheet -- Spreadheet -- 21.11.2007 -- hwyss@ywesee.com
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'spreadsheet/excel/writer/worksheet'
|
6
|
+
|
7
|
+
module Spreadsheet
|
8
|
+
module Excel
|
9
|
+
module Writer
|
10
|
+
class TestWorksheet < Test::Unit::TestCase
|
11
|
+
def test_need_number
|
12
|
+
sheet = Worksheet.new nil, nil
|
13
|
+
assert_equal false, sheet.need_number?(10)
|
14
|
+
assert_equal false, sheet.need_number?(114.55)
|
15
|
+
assert_equal false, sheet.need_number?(0.1)
|
16
|
+
assert_equal false, sheet.need_number?(0.01)
|
17
|
+
assert_equal true, sheet.need_number?(0.001)
|
18
|
+
assert_equal true, sheet.need_number?(10000000.0)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/test/font.rb
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# TestFont -- Spreadsheet -- 09.10.2008 -- hwyss@ywesee.com
|
3
|
+
|
4
|
+
$: << File.expand_path('../lib', File.dirname(__FILE__))
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require 'spreadsheet'
|
8
|
+
|
9
|
+
module Spreadsheet
|
10
|
+
class TestFont < Test::Unit::TestCase
|
11
|
+
def setup
|
12
|
+
@font = Font.new 'Arial'
|
13
|
+
end
|
14
|
+
def test_italic
|
15
|
+
assert_equal false, @font.italic
|
16
|
+
@font.italic!
|
17
|
+
assert_equal true, @font.italic
|
18
|
+
@font.italic = nil
|
19
|
+
assert_equal false, @font.italic
|
20
|
+
@font.italic = 1
|
21
|
+
assert_equal true, @font.italic
|
22
|
+
end
|
23
|
+
def test_encoding
|
24
|
+
assert_equal :default, @font.encoding
|
25
|
+
@font.encoding = :apple_roman
|
26
|
+
assert_equal :apple_roman, @font.encoding
|
27
|
+
@font.encoding = 'Chinese Simplified'
|
28
|
+
assert_equal :chinese_simplified, @font.encoding
|
29
|
+
assert_raises ArgumentError do @font.size = 'ascii' end
|
30
|
+
assert_equal :chinese_simplified, @font.encoding
|
31
|
+
@font.encoding = nil
|
32
|
+
assert_equal :default, @font.encoding
|
33
|
+
end
|
34
|
+
def test_family
|
35
|
+
assert_equal :none, @font.family
|
36
|
+
@font.family = :roman
|
37
|
+
assert_equal :roman, @font.family
|
38
|
+
@font.family = 'Swiss'
|
39
|
+
assert_equal :swiss, @font.family
|
40
|
+
assert_raises ArgumentError do @font.size = :greek end
|
41
|
+
assert_equal :swiss, @font.family
|
42
|
+
@font.family = nil
|
43
|
+
assert_equal :none, @font.family
|
44
|
+
end
|
45
|
+
def test_name
|
46
|
+
assert_equal 'Arial', @font.name
|
47
|
+
@font.name = 'Helvetica'
|
48
|
+
assert_equal 'Helvetica', @font.name
|
49
|
+
end
|
50
|
+
def test_outline
|
51
|
+
assert_equal false, @font.outline
|
52
|
+
@font.outline!
|
53
|
+
assert_equal true, @font.outline
|
54
|
+
@font.outline = nil
|
55
|
+
assert_equal false, @font.outline
|
56
|
+
@font.outline = 1
|
57
|
+
assert_equal true, @font.outline
|
58
|
+
end
|
59
|
+
def test_escapement
|
60
|
+
assert_equal :normal, @font.escapement
|
61
|
+
@font.escapement = :superscript
|
62
|
+
assert_equal :superscript, @font.escapement
|
63
|
+
@font.escapement = 'sub'
|
64
|
+
assert_equal :subscript, @font.escapement
|
65
|
+
assert_raises ArgumentError do @font.size = "upwards" end
|
66
|
+
assert_equal :subscript, @font.escapement
|
67
|
+
@font.escapement = nil
|
68
|
+
assert_equal :normal, @font.escapement
|
69
|
+
end
|
70
|
+
def test_shadow
|
71
|
+
assert_equal false, @font.shadow
|
72
|
+
@font.shadow!
|
73
|
+
assert_equal true, @font.shadow
|
74
|
+
@font.shadow = nil
|
75
|
+
assert_equal false, @font.shadow
|
76
|
+
@font.shadow = 1
|
77
|
+
assert_equal true, @font.shadow
|
78
|
+
end
|
79
|
+
def test_size
|
80
|
+
assert_equal 10, @font.size
|
81
|
+
@font.size = 12
|
82
|
+
assert_equal 12, @font.size
|
83
|
+
@font.size = 11.2
|
84
|
+
assert_equal 11.2, @font.size
|
85
|
+
assert_raises ArgumentError do @font.size = "123" end
|
86
|
+
end
|
87
|
+
def test_strikeout
|
88
|
+
assert_equal false, @font.strikeout
|
89
|
+
@font.strikeout!
|
90
|
+
assert_equal true, @font.strikeout
|
91
|
+
@font.strikeout = nil
|
92
|
+
assert_equal false, @font.strikeout
|
93
|
+
@font.strikeout = 1
|
94
|
+
assert_equal true, @font.strikeout
|
95
|
+
end
|
96
|
+
def test_underline
|
97
|
+
assert_equal :none, @font.underline
|
98
|
+
@font.underline = :single
|
99
|
+
assert_equal :single, @font.underline
|
100
|
+
@font.underline = 'double accounting'
|
101
|
+
assert_equal :double_accounting, @font.underline
|
102
|
+
assert_raises ArgumentError do @font.size = :triple end
|
103
|
+
assert_equal :double_accounting, @font.underline
|
104
|
+
@font.underline = nil
|
105
|
+
assert_equal :none, @font.underline
|
106
|
+
@font.underline = true
|
107
|
+
assert_equal :single, @font.underline
|
108
|
+
end
|
109
|
+
def test_weight
|
110
|
+
assert_equal :normal, @font.weight
|
111
|
+
@font.weight = :bold
|
112
|
+
assert_equal :bold, @font.weight
|
113
|
+
@font.weight = 100
|
114
|
+
assert_equal 100, @font.weight
|
115
|
+
assert_raises ArgumentError do @font.weight = Object.new end
|
116
|
+
assert_equal 100, @font.weight
|
117
|
+
@font.weight = 'bold'
|
118
|
+
assert_equal :bold, @font.weight
|
119
|
+
@font.weight = nil
|
120
|
+
assert_equal :normal, @font.weight
|
121
|
+
end
|
122
|
+
def test_key
|
123
|
+
expected = 'Arial_10_normal_normal_none_text_none_default'
|
124
|
+
assert_equal expected, @font.key
|
125
|
+
@font.name = 'Helvetica'
|
126
|
+
expected = 'Helvetica_10_normal_normal_none_text_none_default'
|
127
|
+
assert_equal expected, @font.key
|
128
|
+
@font.size = 12
|
129
|
+
expected = 'Helvetica_12_normal_normal_none_text_none_default'
|
130
|
+
assert_equal expected, @font.key
|
131
|
+
@font.weight = :bold
|
132
|
+
expected = 'Helvetica_12_bold_normal_none_text_none_default'
|
133
|
+
assert_equal expected, @font.key
|
134
|
+
@font.italic!
|
135
|
+
expected = 'Helvetica_12_bold_italic_normal_none_text_none_default'
|
136
|
+
assert_equal expected, @font.key
|
137
|
+
@font.strikeout!
|
138
|
+
expected = 'Helvetica_12_bold_italic_strikeout_normal_none_text_none_default'
|
139
|
+
assert_equal expected, @font.key
|
140
|
+
@font.outline!
|
141
|
+
expected = 'Helvetica_12_bold_italic_strikeout_outline_normal_none_text_none_default'
|
142
|
+
assert_equal expected, @font.key
|
143
|
+
@font.shadow!
|
144
|
+
expected = 'Helvetica_12_bold_italic_strikeout_outline_shadow_normal_none_text_none_default'
|
145
|
+
assert_equal expected, @font.key
|
146
|
+
@font.escapement = :super
|
147
|
+
expected = 'Helvetica_12_bold_italic_strikeout_outline_shadow_superscript_none_text_none_default'
|
148
|
+
assert_equal expected, @font.key
|
149
|
+
@font.underline = :double
|
150
|
+
expected = 'Helvetica_12_bold_italic_strikeout_outline_shadow_superscript_double_text_none_default'
|
151
|
+
assert_equal expected, @font.key
|
152
|
+
@font.color = :blue
|
153
|
+
expected = 'Helvetica_12_bold_italic_strikeout_outline_shadow_superscript_double_blue_none_default'
|
154
|
+
assert_equal expected, @font.key
|
155
|
+
@font.family = :swiss
|
156
|
+
expected = 'Helvetica_12_bold_italic_strikeout_outline_shadow_superscript_double_blue_swiss_default'
|
157
|
+
assert_equal expected, @font.key
|
158
|
+
@font.encoding = :iso_latin1
|
159
|
+
expected = 'Helvetica_12_bold_italic_strikeout_outline_shadow_superscript_double_blue_swiss_iso_latin1'
|
160
|
+
assert_equal expected, @font.key
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
data/test/integration.rb
ADDED
@@ -0,0 +1,1281 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# TestIntegration -- Spreadheet -- 08.10.2007 -- hwyss@ywesee.com
|
4
|
+
|
5
|
+
$: << File.expand_path('../lib', File.dirname(__FILE__))
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
require 'spreadsheet'
|
9
|
+
require 'fileutils'
|
10
|
+
|
11
|
+
module Spreadsheet
|
12
|
+
class TestIntegration < Test::Unit::TestCase
|
13
|
+
if RUBY_VERSION >= '1.9'
|
14
|
+
class IconvStub
|
15
|
+
def initialize to, from
|
16
|
+
@to, @from = to, from
|
17
|
+
end
|
18
|
+
def iconv str
|
19
|
+
dp = str.dup
|
20
|
+
dp.force_encoding @from
|
21
|
+
dp.encode @to
|
22
|
+
end
|
23
|
+
end
|
24
|
+
@@iconv = IconvStub.new('UTF-16LE', 'UTF-8')
|
25
|
+
@@bytesize = :bytesize
|
26
|
+
else
|
27
|
+
@@iconv = Iconv.new('UTF-16LE', 'UTF-8')
|
28
|
+
@@bytesize = :size
|
29
|
+
end
|
30
|
+
def setup
|
31
|
+
@var = File.expand_path 'var', File.dirname(__FILE__)
|
32
|
+
FileUtils.mkdir_p @var
|
33
|
+
@data = File.expand_path 'data', File.dirname(__FILE__)
|
34
|
+
FileUtils.mkdir_p @data
|
35
|
+
end
|
36
|
+
def teardown
|
37
|
+
Spreadsheet.client_encoding = 'UTF-8'
|
38
|
+
FileUtils.rm_r @var
|
39
|
+
end
|
40
|
+
def test_copy__identical__file_paths
|
41
|
+
path = File.join @data, 'test_copy.xls'
|
42
|
+
copy = File.join @data, 'test_copy1.xls'
|
43
|
+
book = Spreadsheet.open path
|
44
|
+
assert_instance_of Excel::Workbook, book
|
45
|
+
book.write copy
|
46
|
+
assert_equal File.read(path), File.read(copy)
|
47
|
+
ensure
|
48
|
+
File.delete copy if File.exist? copy
|
49
|
+
end
|
50
|
+
def test_empty_workbook
|
51
|
+
path = File.join @data, 'test_empty.xls'
|
52
|
+
book = Spreadsheet.open path
|
53
|
+
assert_instance_of Excel::Workbook, book
|
54
|
+
assert_equal 8, book.biff_version
|
55
|
+
assert_equal 'Microsoft Excel 97/2000/XP', book.version_string
|
56
|
+
enc = 'UTF-16LE'
|
57
|
+
if defined? Encoding
|
58
|
+
enc = Encoding.find enc
|
59
|
+
end
|
60
|
+
assert_equal enc, book.encoding
|
61
|
+
assert_equal 21, book.formats.size
|
62
|
+
assert_equal 4, book.fonts.size
|
63
|
+
assert_equal 0, book.sst.size
|
64
|
+
sheet = book.worksheet 0
|
65
|
+
assert_equal 0, sheet.row_count
|
66
|
+
assert_equal 0, sheet.column_count
|
67
|
+
assert_nothing_raised do sheet.inspect end
|
68
|
+
end
|
69
|
+
def test_version_excel97__ooffice__utf16
|
70
|
+
Spreadsheet.client_encoding = 'UTF-16LE'
|
71
|
+
assert_equal 'UTF-16LE', Spreadsheet.client_encoding
|
72
|
+
path = File.join @data, 'test_version_excel97.xls'
|
73
|
+
book = Spreadsheet.open path
|
74
|
+
assert_instance_of Excel::Workbook, book
|
75
|
+
assert_equal 8, book.biff_version
|
76
|
+
assert_equal @@iconv.iconv('Microsoft Excel 97/2000/XP'),
|
77
|
+
book.version_string
|
78
|
+
enc = 'UTF-16LE'
|
79
|
+
if defined? Encoding
|
80
|
+
enc = Encoding.find enc
|
81
|
+
end
|
82
|
+
assert_equal enc, book.encoding
|
83
|
+
assert_equal 25, book.formats.size
|
84
|
+
assert_equal 5, book.fonts.size
|
85
|
+
str1 = book.shared_string 0
|
86
|
+
other = @@iconv.iconv('Shared String')
|
87
|
+
assert_equal @@iconv.iconv('Shared String'), str1
|
88
|
+
str2 = book.shared_string 1
|
89
|
+
assert_equal @@iconv.iconv('Another Shared String'), str2
|
90
|
+
str3 = book.shared_string 2
|
91
|
+
long = @@iconv.iconv('1234567890 ' * 1000)
|
92
|
+
if str3 != long
|
93
|
+
long.size.times do |idx|
|
94
|
+
len = idx.next
|
95
|
+
if str3[0,len] != long[0,len]
|
96
|
+
assert_equal long[idx - 5, 10], str3[idx - 5, 10], "in position #{idx}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
assert_equal long, str3
|
101
|
+
str4 = book.shared_string 3
|
102
|
+
long = @@iconv.iconv('9876543210 ' * 1000)
|
103
|
+
if str4 != long
|
104
|
+
long.size.times do |idx|
|
105
|
+
len = idx.next
|
106
|
+
if str4[0,len] != long[0,len]
|
107
|
+
assert_equal long[idx - 5, 10], str4[idx - 5, 10], "in position #{idx}"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
assert_equal long, str4
|
112
|
+
sheet = book.worksheet 0
|
113
|
+
assert_equal 11, sheet.row_count
|
114
|
+
assert_equal 12, sheet.column_count
|
115
|
+
useds = [0,0,0,0,0,0,0,1,0,0,11]
|
116
|
+
unuseds = [2,2,1,1,1,2,1,11,1,2,12]
|
117
|
+
sheet.each do |row|
|
118
|
+
assert_equal useds.shift, row.first_used
|
119
|
+
assert_equal unuseds.shift, row.first_unused
|
120
|
+
end
|
121
|
+
assert unuseds.empty?, "not all rows were visited in Spreadsheet#each"
|
122
|
+
row = sheet.row 0
|
123
|
+
assert_equal str1, row[0]
|
124
|
+
assert_equal str1, sheet[0,0]
|
125
|
+
assert_equal str1, sheet.cell(0,0)
|
126
|
+
assert_equal str1, row[1]
|
127
|
+
assert_equal str1, sheet[0,1]
|
128
|
+
assert_equal str1, sheet.cell(0,1)
|
129
|
+
row = sheet.row 1
|
130
|
+
assert_equal str2, row[0]
|
131
|
+
assert_equal str2, sheet[1,0]
|
132
|
+
assert_equal str2, sheet.cell(1,0)
|
133
|
+
assert_equal str2, row[1]
|
134
|
+
assert_equal str2, sheet[1,1]
|
135
|
+
assert_equal str2, sheet.cell(1,1)
|
136
|
+
row = sheet.row 2
|
137
|
+
assert_equal str3, row[0]
|
138
|
+
assert_equal str3, sheet[2,0]
|
139
|
+
assert_equal str3, sheet.cell(2,0)
|
140
|
+
assert_nil row[1]
|
141
|
+
assert_nil sheet[2,1]
|
142
|
+
assert_nil sheet.cell(2,1)
|
143
|
+
row = sheet.row 3
|
144
|
+
assert_equal str4, row[0]
|
145
|
+
assert_equal str4, sheet[3,0]
|
146
|
+
assert_equal str4, sheet.cell(3,0)
|
147
|
+
assert_nil row[1]
|
148
|
+
assert_nil sheet[3,1]
|
149
|
+
assert_nil sheet.cell(3,1)
|
150
|
+
row = sheet.row 4
|
151
|
+
assert_equal 0.25, row[0]
|
152
|
+
assert_equal 0.25, sheet[4,0]
|
153
|
+
assert_equal 0.25, sheet.cell(4,0)
|
154
|
+
row = sheet.row 5
|
155
|
+
assert_equal 0.75, row[0]
|
156
|
+
assert_equal 0.75, sheet[5,0]
|
157
|
+
assert_equal 0.75, sheet.cell(5,0)
|
158
|
+
date = Date.new 1975, 8, 21
|
159
|
+
assert_equal date, row[1]
|
160
|
+
assert_equal date, sheet[5,1]
|
161
|
+
assert_equal date, sheet.cell(5,1)
|
162
|
+
row = sheet.row 6
|
163
|
+
assert_equal 1, row[0]
|
164
|
+
assert_equal 1, sheet[6,0]
|
165
|
+
assert_equal 1, sheet.cell(6,0)
|
166
|
+
row = sheet.row 7
|
167
|
+
assert_nil row[0]
|
168
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
169
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
170
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
171
|
+
row = sheet.row 8
|
172
|
+
assert_equal 0.0001, row[0]
|
173
|
+
row = sheet.row 9
|
174
|
+
assert_equal 0.00009, row[0]
|
175
|
+
assert_equal :green, sheet.row(10).format(11).pattern_fg_color
|
176
|
+
end
|
177
|
+
def test_version_excel97__ooffice
|
178
|
+
path = File.join @data, 'test_version_excel97.xls'
|
179
|
+
book = Spreadsheet.open path
|
180
|
+
assert_instance_of Excel::Workbook, book
|
181
|
+
assert_equal 8, book.biff_version
|
182
|
+
assert_equal 'Microsoft Excel 97/2000/XP', book.version_string
|
183
|
+
enc = 'UTF-16LE'
|
184
|
+
if defined? Encoding
|
185
|
+
enc = Encoding.find enc
|
186
|
+
end
|
187
|
+
assert_equal enc, book.encoding
|
188
|
+
assert_equal 25, book.formats.size
|
189
|
+
assert_equal 5, book.fonts.size
|
190
|
+
str1 = book.shared_string 0
|
191
|
+
assert_equal 'Shared String', str1
|
192
|
+
str2 = book.shared_string 1
|
193
|
+
assert_equal 'Another Shared String', str2
|
194
|
+
str3 = book.shared_string 2
|
195
|
+
long = '1234567890 ' * 1000
|
196
|
+
if str3 != long
|
197
|
+
long.size.times do |idx|
|
198
|
+
len = idx.next
|
199
|
+
if str3[0,len] != long[0,len]
|
200
|
+
assert_equal long[idx - 5, 10], str3[idx - 5, 10], "in position #{idx}"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
assert_equal long, str3
|
205
|
+
str4 = book.shared_string 3
|
206
|
+
long = '9876543210 ' * 1000
|
207
|
+
if str4 != long
|
208
|
+
long.size.times do |idx|
|
209
|
+
len = idx.next
|
210
|
+
if str4[0,len] != long[0,len]
|
211
|
+
assert_equal long[idx - 5, 10], str4[idx - 5, 10], "in position #{idx}"
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
assert_equal long, str4
|
216
|
+
sheet = book.worksheet 0
|
217
|
+
assert_equal 11, sheet.row_count
|
218
|
+
assert_equal 12, sheet.column_count
|
219
|
+
useds = [0,0,0,0,0,0,0,1,0,0,11]
|
220
|
+
unuseds = [2,2,1,1,1,2,1,11,1,2,12]
|
221
|
+
sheet.each do |row|
|
222
|
+
assert_equal useds.shift, row.first_used
|
223
|
+
assert_equal unuseds.shift, row.first_unused
|
224
|
+
end
|
225
|
+
assert unuseds.empty?, "not all rows were visited in Spreadsheet#each"
|
226
|
+
row = sheet.row 0
|
227
|
+
assert_equal str1, row[0]
|
228
|
+
assert_equal str1, sheet[0,0]
|
229
|
+
assert_equal str1, sheet.cell(0,0)
|
230
|
+
assert_equal str1, row[1]
|
231
|
+
assert_equal str1, sheet[0,1]
|
232
|
+
assert_equal str1, sheet.cell(0,1)
|
233
|
+
row = sheet.row 1
|
234
|
+
assert_equal str2, row[0]
|
235
|
+
assert_equal str2, sheet[1,0]
|
236
|
+
assert_equal str2, sheet.cell(1,0)
|
237
|
+
assert_equal str2, row[1]
|
238
|
+
assert_equal str2, sheet[1,1]
|
239
|
+
assert_equal str2, sheet.cell(1,1)
|
240
|
+
row = sheet.row 2
|
241
|
+
assert_equal str3, row[0]
|
242
|
+
assert_equal str3, sheet[2,0]
|
243
|
+
assert_equal str3, sheet.cell(2,0)
|
244
|
+
assert_nil row[1]
|
245
|
+
assert_nil sheet[2,1]
|
246
|
+
assert_nil sheet.cell(2,1)
|
247
|
+
row = sheet.row 3
|
248
|
+
assert_equal str4, row[0]
|
249
|
+
assert_equal str4, sheet[3,0]
|
250
|
+
assert_equal str4, sheet.cell(3,0)
|
251
|
+
assert_nil row[1]
|
252
|
+
assert_nil sheet[3,1]
|
253
|
+
assert_nil sheet.cell(3,1)
|
254
|
+
row = sheet.row 4
|
255
|
+
assert_equal 0.25, row[0]
|
256
|
+
assert_equal 0.25, sheet[4,0]
|
257
|
+
assert_equal 0.25, sheet.cell(4,0)
|
258
|
+
row = sheet.row 5
|
259
|
+
assert_equal 0.75, row[0]
|
260
|
+
assert_equal 0.75, sheet[5,0]
|
261
|
+
assert_equal 0.75, sheet.cell(5,0)
|
262
|
+
date = Date.new 1975, 8, 21
|
263
|
+
assert_equal date, row[1]
|
264
|
+
assert_equal date, sheet[5,1]
|
265
|
+
assert_equal date, sheet.cell(5,1)
|
266
|
+
row = sheet.row 6
|
267
|
+
assert_equal 1, row[0]
|
268
|
+
assert_equal 1, sheet[6,0]
|
269
|
+
assert_equal 1, sheet.cell(6,0)
|
270
|
+
row = sheet.row 7
|
271
|
+
assert_nil row[0]
|
272
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
273
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
274
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
275
|
+
row = sheet.row 8
|
276
|
+
assert_equal 0.0001, row[0]
|
277
|
+
row = sheet.row 9
|
278
|
+
assert_equal 0.00009, row[0]
|
279
|
+
link = row[1]
|
280
|
+
assert_instance_of Link, link
|
281
|
+
assert_equal 'Link-Text', link
|
282
|
+
assert_equal 'http://scm.ywesee.com/spreadsheet', link.url
|
283
|
+
assert_equal 'http://scm.ywesee.com/spreadsheet', link.href
|
284
|
+
end
|
285
|
+
def test_version_excel95__ooffice__utf16
|
286
|
+
Spreadsheet.client_encoding = 'UTF-16LE'
|
287
|
+
path = File.join @data, 'test_version_excel95.xls'
|
288
|
+
book = Spreadsheet.open path
|
289
|
+
assert_instance_of Excel::Workbook, book
|
290
|
+
assert_equal 5, book.biff_version
|
291
|
+
assert_equal @@iconv.iconv('Microsoft Excel 95'), book.version_string
|
292
|
+
enc = 'WINDOWS-1252'
|
293
|
+
if defined? Encoding
|
294
|
+
enc = Encoding.find enc
|
295
|
+
end
|
296
|
+
assert_equal enc, book.encoding
|
297
|
+
str1 = @@iconv.iconv('Shared String')
|
298
|
+
str2 = @@iconv.iconv('Another Shared String')
|
299
|
+
str3 = @@iconv.iconv(('1234567890 ' * 26)[0,255])
|
300
|
+
str4 = @@iconv.iconv(('9876543210 ' * 26)[0,255])
|
301
|
+
sheet = book.worksheet 0
|
302
|
+
assert_equal 8, sheet.row_count
|
303
|
+
assert_equal 11, sheet.column_count
|
304
|
+
useds = [0,0,0,0,0,0,0,1]
|
305
|
+
unuseds = [2,2,1,1,1,1,1,11]
|
306
|
+
sheet.each do |row|
|
307
|
+
assert_equal useds.shift, row.first_used
|
308
|
+
assert_equal unuseds.shift, row.first_unused
|
309
|
+
end
|
310
|
+
assert unuseds.empty?, "not all rows were visited in Spreadsheet#each"
|
311
|
+
row = sheet.row 0
|
312
|
+
assert_equal str1, row[0]
|
313
|
+
assert_equal str1, sheet[0,0]
|
314
|
+
assert_equal str1, sheet.cell(0,0)
|
315
|
+
assert_equal str1, row[1]
|
316
|
+
assert_equal str1, sheet[0,1]
|
317
|
+
assert_equal str1, sheet.cell(0,1)
|
318
|
+
row = sheet.row 1
|
319
|
+
assert_equal str2, row[0]
|
320
|
+
assert_equal str2, sheet[1,0]
|
321
|
+
assert_equal str2, sheet.cell(1,0)
|
322
|
+
assert_equal str2, row[1]
|
323
|
+
assert_equal str2, sheet[1,1]
|
324
|
+
assert_equal str2, sheet.cell(1,1)
|
325
|
+
row = sheet.row 2
|
326
|
+
assert_equal 510, row[0].send(@@bytesize)
|
327
|
+
assert_equal str3, row[0]
|
328
|
+
assert_equal str3, sheet[2,0]
|
329
|
+
assert_equal str3, sheet.cell(2,0)
|
330
|
+
assert_nil row[1]
|
331
|
+
assert_nil sheet[2,1]
|
332
|
+
assert_nil sheet.cell(2,1)
|
333
|
+
row = sheet.row 3
|
334
|
+
assert_equal 510, row[0].send(@@bytesize)
|
335
|
+
assert_equal str4, row[0]
|
336
|
+
assert_equal str4, sheet[3,0]
|
337
|
+
assert_equal str4, sheet.cell(3,0)
|
338
|
+
assert_nil row[1]
|
339
|
+
assert_nil sheet[3,1]
|
340
|
+
assert_nil sheet.cell(3,1)
|
341
|
+
row = sheet.row 4
|
342
|
+
assert_equal 0.25, row[0]
|
343
|
+
assert_equal 0.25, sheet[4,0]
|
344
|
+
assert_equal 0.25, sheet.cell(4,0)
|
345
|
+
row = sheet.row 5
|
346
|
+
assert_equal 0.75, row[0]
|
347
|
+
assert_equal 0.75, sheet[5,0]
|
348
|
+
assert_equal 0.75, sheet.cell(5,0)
|
349
|
+
row = sheet.row 6
|
350
|
+
assert_equal 1, row[0]
|
351
|
+
assert_equal 1, sheet[6,0]
|
352
|
+
assert_equal 1, sheet.cell(6,0)
|
353
|
+
row = sheet.row 7
|
354
|
+
assert_nil row[0]
|
355
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
356
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
357
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
358
|
+
end
|
359
|
+
def test_version_excel95__ooffice
|
360
|
+
path = File.join @data, 'test_version_excel95.xls'
|
361
|
+
book = Spreadsheet.open path
|
362
|
+
assert_instance_of Excel::Workbook, book
|
363
|
+
assert_equal 5, book.biff_version
|
364
|
+
assert_equal 'Microsoft Excel 95', book.version_string
|
365
|
+
enc = 'WINDOWS-1252'
|
366
|
+
if defined? Encoding
|
367
|
+
enc = Encoding.find enc
|
368
|
+
end
|
369
|
+
assert_equal enc, book.encoding
|
370
|
+
str1 = 'Shared String'
|
371
|
+
str2 = 'Another Shared String'
|
372
|
+
str3 = ('1234567890 ' * 26)[0,255]
|
373
|
+
str4 = ('9876543210 ' * 26)[0,255]
|
374
|
+
sheet = book.worksheet 0
|
375
|
+
assert_equal 8, sheet.row_count
|
376
|
+
assert_equal 11, sheet.column_count
|
377
|
+
useds = [0,0,0,0,0,0,0,1]
|
378
|
+
unuseds = [2,2,1,1,1,1,1,11]
|
379
|
+
sheet.each do |row|
|
380
|
+
assert_equal useds.shift, row.first_used
|
381
|
+
assert_equal unuseds.shift, row.first_unused
|
382
|
+
end
|
383
|
+
assert unuseds.empty?, "not all rows were visited in Spreadsheet#each"
|
384
|
+
row = sheet.row 0
|
385
|
+
assert_equal str1, row[0]
|
386
|
+
assert_equal str1, sheet[0,0]
|
387
|
+
assert_equal str1, sheet.cell(0,0)
|
388
|
+
assert_equal str1, row[1]
|
389
|
+
assert_equal str1, sheet[0,1]
|
390
|
+
assert_equal str1, sheet.cell(0,1)
|
391
|
+
row = sheet.row 1
|
392
|
+
assert_equal str2, row[0]
|
393
|
+
assert_equal str2, sheet[1,0]
|
394
|
+
assert_equal str2, sheet.cell(1,0)
|
395
|
+
assert_equal str2, row[1]
|
396
|
+
assert_equal str2, sheet[1,1]
|
397
|
+
assert_equal str2, sheet.cell(1,1)
|
398
|
+
row = sheet.row 2
|
399
|
+
assert_equal 255, row[0].send(@@bytesize)
|
400
|
+
assert_equal str3, row[0]
|
401
|
+
assert_equal str3, sheet[2,0]
|
402
|
+
assert_equal str3, sheet.cell(2,0)
|
403
|
+
assert_nil row[1]
|
404
|
+
assert_nil sheet[2,1]
|
405
|
+
assert_nil sheet.cell(2,1)
|
406
|
+
row = sheet.row 3
|
407
|
+
assert_equal 255, row[0].send(@@bytesize)
|
408
|
+
assert_equal str4, row[0]
|
409
|
+
assert_equal str4, sheet[3,0]
|
410
|
+
assert_equal str4, sheet.cell(3,0)
|
411
|
+
assert_nil row[1]
|
412
|
+
assert_nil sheet[3,1]
|
413
|
+
assert_nil sheet.cell(3,1)
|
414
|
+
row = sheet.row 4
|
415
|
+
assert_equal 0.25, row[0]
|
416
|
+
assert_equal 0.25, sheet[4,0]
|
417
|
+
assert_equal 0.25, sheet.cell(4,0)
|
418
|
+
row = sheet.row 5
|
419
|
+
assert_equal 0.75, row[0]
|
420
|
+
assert_equal 0.75, sheet[5,0]
|
421
|
+
assert_equal 0.75, sheet.cell(5,0)
|
422
|
+
row = sheet.row 6
|
423
|
+
assert_equal 1, row[0]
|
424
|
+
assert_equal 1, sheet[6,0]
|
425
|
+
assert_equal 1, sheet.cell(6,0)
|
426
|
+
row = sheet.row 7
|
427
|
+
assert_nil row[0]
|
428
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
429
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
430
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
431
|
+
end
|
432
|
+
def test_version_excel5__ooffice
|
433
|
+
path = File.join @data, 'test_version_excel5.xls'
|
434
|
+
book = Spreadsheet.open path
|
435
|
+
assert_instance_of Excel::Workbook, book
|
436
|
+
assert_equal 5, book.biff_version
|
437
|
+
assert_equal 'Microsoft Excel 95', book.version_string
|
438
|
+
enc = 'WINDOWS-1252'
|
439
|
+
if defined? Encoding
|
440
|
+
enc = Encoding.find enc
|
441
|
+
end
|
442
|
+
assert_equal enc, book.encoding
|
443
|
+
str1 = 'Shared String'
|
444
|
+
str2 = 'Another Shared String'
|
445
|
+
str3 = ('1234567890 ' * 26)[0,255]
|
446
|
+
str4 = ('9876543210 ' * 26)[0,255]
|
447
|
+
sheet = book.worksheet 0
|
448
|
+
assert_equal 8, sheet.row_count
|
449
|
+
assert_equal 11, sheet.column_count
|
450
|
+
useds = [0,0,0,0,0,0,0,1]
|
451
|
+
unuseds = [2,2,1,1,1,1,1,11]
|
452
|
+
sheet.each do |row|
|
453
|
+
assert_equal useds.shift, row.first_used
|
454
|
+
assert_equal unuseds.shift, row.first_unused
|
455
|
+
end
|
456
|
+
assert unuseds.empty?, "not all rows were visited in Spreadsheet#each"
|
457
|
+
row = sheet.row 0
|
458
|
+
assert_equal str1, row[0]
|
459
|
+
assert_equal str1, sheet[0,0]
|
460
|
+
assert_equal str1, sheet.cell(0,0)
|
461
|
+
assert_equal str1, row[1]
|
462
|
+
assert_equal str1, sheet[0,1]
|
463
|
+
assert_equal str1, sheet.cell(0,1)
|
464
|
+
row = sheet.row 1
|
465
|
+
assert_equal str2, row[0]
|
466
|
+
assert_equal str2, sheet[1,0]
|
467
|
+
assert_equal str2, sheet.cell(1,0)
|
468
|
+
assert_equal str2, row[1]
|
469
|
+
assert_equal str2, sheet[1,1]
|
470
|
+
assert_equal str2, sheet.cell(1,1)
|
471
|
+
row = sheet.row 2
|
472
|
+
assert_equal 255, row[0].send(@@bytesize)
|
473
|
+
assert_equal str3, row[0]
|
474
|
+
assert_equal str3, sheet[2,0]
|
475
|
+
assert_equal str3, sheet.cell(2,0)
|
476
|
+
assert_nil row[1]
|
477
|
+
assert_nil sheet[2,1]
|
478
|
+
assert_nil sheet.cell(2,1)
|
479
|
+
row = sheet.row 3
|
480
|
+
assert_equal 255, row[0].send(@@bytesize)
|
481
|
+
assert_equal str4, row[0]
|
482
|
+
assert_equal str4, sheet[3,0]
|
483
|
+
assert_equal str4, sheet.cell(3,0)
|
484
|
+
assert_nil row[1]
|
485
|
+
assert_nil sheet[3,1]
|
486
|
+
assert_nil sheet.cell(3,1)
|
487
|
+
row = sheet.row 4
|
488
|
+
assert_equal 0.25, row[0]
|
489
|
+
assert_equal 0.25, sheet[4,0]
|
490
|
+
assert_equal 0.25, sheet.cell(4,0)
|
491
|
+
row = sheet.row 5
|
492
|
+
assert_equal 0.75, row[0]
|
493
|
+
assert_equal 0.75, sheet[5,0]
|
494
|
+
assert_equal 0.75, sheet.cell(5,0)
|
495
|
+
row = sheet.row 6
|
496
|
+
assert_equal 1, row[0]
|
497
|
+
assert_equal 1, sheet[6,0]
|
498
|
+
assert_equal 1, sheet.cell(6,0)
|
499
|
+
row = sheet.row 7
|
500
|
+
assert_nil row[0]
|
501
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
502
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
503
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
504
|
+
end
|
505
|
+
def test_worksheets
|
506
|
+
path = File.join @data, 'test_copy.xls'
|
507
|
+
book = Spreadsheet.open path
|
508
|
+
sheets = book.worksheets
|
509
|
+
assert_equal 3, sheets.size
|
510
|
+
sheet = book.worksheet 0
|
511
|
+
assert_instance_of Excel::Worksheet, sheet
|
512
|
+
assert_equal sheet, book.worksheet('Sheet1')
|
513
|
+
end
|
514
|
+
def test_worksheets__utf16
|
515
|
+
Spreadsheet.client_encoding = 'UTF-16LE'
|
516
|
+
path = File.join @data, 'test_copy.xls'
|
517
|
+
book = Spreadsheet.open path
|
518
|
+
sheets = book.worksheets
|
519
|
+
assert_equal 3, sheets.size
|
520
|
+
sheet = book.worksheet 0
|
521
|
+
assert_instance_of Excel::Worksheet, sheet
|
522
|
+
str = "S\000h\000e\000e\000t\0001\000"
|
523
|
+
if RUBY_VERSION >= '1.9'
|
524
|
+
str.force_encoding 'UTF-16LE' if str.respond_to? :force_encoding
|
525
|
+
end
|
526
|
+
assert_equal sheet, book.worksheet(str)
|
527
|
+
end
|
528
|
+
def test_read_datetime
|
529
|
+
path = File.join @data, 'test_datetime.xls'
|
530
|
+
book = Spreadsheet.open path
|
531
|
+
assert_instance_of Excel::Workbook, book
|
532
|
+
sheet = book.worksheet 0
|
533
|
+
time = sheet[0,0]
|
534
|
+
assert_equal 22, time.hour
|
535
|
+
assert_equal 00, time.min
|
536
|
+
assert_equal 00, time.sec
|
537
|
+
time = sheet[1,0]
|
538
|
+
assert_equal 1899, time.year
|
539
|
+
assert_equal 12, time.month
|
540
|
+
assert_equal 30, time.day
|
541
|
+
assert_equal 22, time.hour
|
542
|
+
assert_equal 30, time.min
|
543
|
+
assert_equal 45, time.sec
|
544
|
+
time = sheet[0,1]
|
545
|
+
assert_equal 1899, time.year
|
546
|
+
assert_equal 12, time.month
|
547
|
+
assert_equal 31, time.day
|
548
|
+
assert_equal 4, time.hour
|
549
|
+
assert_equal 30, time.min
|
550
|
+
assert_equal 45, time.sec
|
551
|
+
end
|
552
|
+
def test_change_encoding
|
553
|
+
path = File.join @data, 'test_version_excel95.xls'
|
554
|
+
book = Spreadsheet.open path
|
555
|
+
assert_instance_of Excel::Workbook, book
|
556
|
+
assert_equal 5, book.biff_version
|
557
|
+
assert_equal 'Microsoft Excel 95', book.version_string
|
558
|
+
enc = 'WINDOWS-1252'
|
559
|
+
if defined? Encoding
|
560
|
+
enc = Encoding.find enc
|
561
|
+
end
|
562
|
+
assert_equal enc, book.encoding
|
563
|
+
enc = 'WINDOWS-1256'
|
564
|
+
if defined? Encoding
|
565
|
+
enc = Encoding.find enc
|
566
|
+
end
|
567
|
+
book.encoding = enc
|
568
|
+
path = File.join @var, 'test_change_encoding.xls'
|
569
|
+
book.write path
|
570
|
+
assert_nothing_raised do book = Spreadsheet.open path end
|
571
|
+
assert_equal enc, book.encoding
|
572
|
+
end
|
573
|
+
def test_change_cell
|
574
|
+
path = File.join @data, 'test_version_excel97.xls'
|
575
|
+
book = Spreadsheet.open path
|
576
|
+
assert_instance_of Excel::Workbook, book
|
577
|
+
assert_equal 8, book.biff_version
|
578
|
+
assert_equal 'Microsoft Excel 97/2000/XP', book.version_string
|
579
|
+
path = File.join @var, 'test_change_cell.xls'
|
580
|
+
str1 = book.shared_string 0
|
581
|
+
assert_equal 'Shared String', str1
|
582
|
+
str2 = book.shared_string 1
|
583
|
+
assert_equal 'Another Shared String', str2
|
584
|
+
str3 = book.shared_string 2
|
585
|
+
long = '1234567890 ' * 1000
|
586
|
+
if str3 != long
|
587
|
+
long.size.times do |idx|
|
588
|
+
len = idx.next
|
589
|
+
if str3[0,len] != long[0,len]
|
590
|
+
assert_equal long[idx - 5, 10], str3[idx - 5, 10], "in position #{idx}"
|
591
|
+
end
|
592
|
+
end
|
593
|
+
end
|
594
|
+
assert_equal long, str3
|
595
|
+
str4 = book.shared_string 3
|
596
|
+
long = '9876543210 ' * 1000
|
597
|
+
if str4 != long
|
598
|
+
long.size.times do |idx|
|
599
|
+
len = idx.next
|
600
|
+
if str4[0,len] != long[0,len]
|
601
|
+
assert_equal long[idx - 5, 10], str4[idx - 5, 10], "in position #{idx}"
|
602
|
+
end
|
603
|
+
end
|
604
|
+
end
|
605
|
+
assert_equal long, str4
|
606
|
+
sheet = book.worksheet 0
|
607
|
+
sheet[0,0] = 4
|
608
|
+
row = sheet.row 1
|
609
|
+
row[0] = 3
|
610
|
+
book.write path
|
611
|
+
assert_nothing_raised do book = Spreadsheet.open path end
|
612
|
+
sheet = book.worksheet 0
|
613
|
+
assert_equal 11, sheet.row_count
|
614
|
+
assert_equal 12, sheet.column_count
|
615
|
+
useds = [0,0,0,0,0,0,0,0,0,0,0]
|
616
|
+
unuseds = [2,2,1,1,1,2,1,11,1,2,12]
|
617
|
+
sheet.each do |rw|
|
618
|
+
assert_equal useds.shift, rw.first_used
|
619
|
+
assert_equal unuseds.shift, rw.first_unused
|
620
|
+
end
|
621
|
+
assert unuseds.empty?, "not all rows were visited in Spreadsheet#each"
|
622
|
+
row = sheet.row 0
|
623
|
+
assert_equal 4, row[0]
|
624
|
+
assert_equal 4, sheet[0,0]
|
625
|
+
assert_equal 4, sheet.cell(0,0)
|
626
|
+
assert_equal str1, row[1]
|
627
|
+
assert_equal str1, sheet[0,1]
|
628
|
+
assert_equal str1, sheet.cell(0,1)
|
629
|
+
row = sheet.row 1
|
630
|
+
assert_equal 3, row[0]
|
631
|
+
assert_equal 3, sheet[1,0]
|
632
|
+
assert_equal 3, sheet.cell(1,0)
|
633
|
+
assert_equal str2, row[1]
|
634
|
+
assert_equal str2, sheet[1,1]
|
635
|
+
assert_equal str2, sheet.cell(1,1)
|
636
|
+
row = sheet.row 2
|
637
|
+
assert_equal str3, row[0]
|
638
|
+
assert_equal str3, sheet[2,0]
|
639
|
+
assert_equal str3, sheet.cell(2,0)
|
640
|
+
assert_nil row[1]
|
641
|
+
assert_nil sheet[2,1]
|
642
|
+
assert_nil sheet.cell(2,1)
|
643
|
+
row = sheet.row 3
|
644
|
+
assert_equal str4, row[0]
|
645
|
+
assert_equal str4, sheet[3,0]
|
646
|
+
assert_equal str4, sheet.cell(3,0)
|
647
|
+
assert_nil row[1]
|
648
|
+
assert_nil sheet[3,1]
|
649
|
+
assert_nil sheet.cell(3,1)
|
650
|
+
row = sheet.row 4
|
651
|
+
assert_equal 0.25, row[0]
|
652
|
+
assert_equal 0.25, sheet[4,0]
|
653
|
+
assert_equal 0.25, sheet.cell(4,0)
|
654
|
+
row = sheet.row 5
|
655
|
+
assert_equal 0.75, row[0]
|
656
|
+
assert_equal 0.75, sheet[5,0]
|
657
|
+
assert_equal 0.75, sheet.cell(5,0)
|
658
|
+
date = Date.new 1975, 8, 21
|
659
|
+
assert_equal date, row[1]
|
660
|
+
assert_equal date, sheet[5,1]
|
661
|
+
assert_equal date, sheet.cell(5,1)
|
662
|
+
row = sheet.row 6
|
663
|
+
assert_equal 1, row[0]
|
664
|
+
assert_equal 1, sheet[6,0]
|
665
|
+
assert_equal 1, sheet.cell(6,0)
|
666
|
+
row = sheet.row 7
|
667
|
+
assert_nil row[0]
|
668
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
669
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
670
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
671
|
+
row = sheet.row 8
|
672
|
+
assert_equal 0.0001, row[0]
|
673
|
+
row = sheet.row 9
|
674
|
+
assert_equal 0.00009, row[0]
|
675
|
+
end
|
676
|
+
def test_change_cell__complete_sst_rewrite
|
677
|
+
path = File.join @data, 'test_version_excel97.xls'
|
678
|
+
book = Spreadsheet.open path
|
679
|
+
assert_instance_of Excel::Workbook, book
|
680
|
+
assert_equal 8, book.biff_version
|
681
|
+
assert_equal 'Microsoft Excel 97/2000/XP', book.version_string
|
682
|
+
path = File.join @var, 'test_change_cell.xls'
|
683
|
+
str1 = book.shared_string 0
|
684
|
+
assert_equal 'Shared String', str1
|
685
|
+
str2 = book.shared_string 1
|
686
|
+
assert_equal 'Another Shared String', str2
|
687
|
+
str3 = book.shared_string 2
|
688
|
+
long = '1234567890 ' * 1000
|
689
|
+
if str3 != long
|
690
|
+
long.size.times do |idx|
|
691
|
+
len = idx.next
|
692
|
+
if str3[0,len] != long[0,len]
|
693
|
+
assert_equal long[idx - 5, 10], str3[idx - 5, 10], "in position #{idx}"
|
694
|
+
end
|
695
|
+
end
|
696
|
+
end
|
697
|
+
assert_equal long, str3
|
698
|
+
str4 = book.shared_string 3
|
699
|
+
long = '9876543210 ' * 1000
|
700
|
+
if str4 != long
|
701
|
+
long.size.times do |idx|
|
702
|
+
len = idx.next
|
703
|
+
if str4[0,len] != long[0,len]
|
704
|
+
assert_equal long[idx - 5, 10], str4[idx - 5, 10], "in position #{idx}"
|
705
|
+
end
|
706
|
+
end
|
707
|
+
end
|
708
|
+
assert_equal long, str4
|
709
|
+
sheet = book.worksheet 0
|
710
|
+
sheet[0,0] = 4
|
711
|
+
str5 = 'A completely different String'
|
712
|
+
sheet[0,1] = str5
|
713
|
+
row = sheet.row 1
|
714
|
+
row[0] = 3
|
715
|
+
book.write path
|
716
|
+
assert_nothing_raised do book = Spreadsheet.open path end
|
717
|
+
assert_equal str5, book.shared_string(0)
|
718
|
+
assert_equal str2, book.shared_string(1)
|
719
|
+
assert_equal str3, book.shared_string(2)
|
720
|
+
assert_equal str4, book.shared_string(3)
|
721
|
+
sheet = book.worksheet 0
|
722
|
+
assert_equal 11, sheet.row_count
|
723
|
+
assert_equal 12, sheet.column_count
|
724
|
+
useds = [0,0,0,0,0,0,0,0,0,0,0]
|
725
|
+
unuseds = [2,2,1,1,1,2,1,11,1,2,12]
|
726
|
+
sheet.each do |rw|
|
727
|
+
assert_equal useds.shift, rw.first_used
|
728
|
+
assert_equal unuseds.shift, rw.first_unused
|
729
|
+
end
|
730
|
+
assert unuseds.empty?, "not all rows were visited in Spreadsheet#each"
|
731
|
+
row = sheet.row 0
|
732
|
+
assert_equal 4, row[0]
|
733
|
+
assert_equal 4, sheet[0,0]
|
734
|
+
assert_equal 4, sheet.cell(0,0)
|
735
|
+
assert_equal str5, row[1]
|
736
|
+
assert_equal str5, sheet[0,1]
|
737
|
+
assert_equal str5, sheet.cell(0,1)
|
738
|
+
row = sheet.row 1
|
739
|
+
assert_equal 3, row[0]
|
740
|
+
assert_equal 3, sheet[1,0]
|
741
|
+
assert_equal 3, sheet.cell(1,0)
|
742
|
+
assert_equal str2, row[1]
|
743
|
+
assert_equal str2, sheet[1,1]
|
744
|
+
assert_equal str2, sheet.cell(1,1)
|
745
|
+
row = sheet.row 2
|
746
|
+
assert_equal str3, row[0]
|
747
|
+
assert_equal str3, sheet[2,0]
|
748
|
+
assert_equal str3, sheet.cell(2,0)
|
749
|
+
assert_nil row[1]
|
750
|
+
assert_nil sheet[2,1]
|
751
|
+
assert_nil sheet.cell(2,1)
|
752
|
+
row = sheet.row 3
|
753
|
+
assert_equal str4, row[0]
|
754
|
+
assert_equal str4, sheet[3,0]
|
755
|
+
assert_equal str4, sheet.cell(3,0)
|
756
|
+
assert_nil row[1]
|
757
|
+
assert_nil sheet[3,1]
|
758
|
+
assert_nil sheet.cell(3,1)
|
759
|
+
row = sheet.row 4
|
760
|
+
assert_equal 0.25, row[0]
|
761
|
+
assert_equal 0.25, sheet[4,0]
|
762
|
+
assert_equal 0.25, sheet.cell(4,0)
|
763
|
+
row = sheet.row 5
|
764
|
+
assert_equal 0.75, row[0]
|
765
|
+
assert_equal 0.75, sheet[5,0]
|
766
|
+
assert_equal 0.75, sheet.cell(5,0)
|
767
|
+
date = Date.new 1975, 8, 21
|
768
|
+
assert_equal date, row[1]
|
769
|
+
assert_equal date, sheet[5,1]
|
770
|
+
assert_equal date, sheet.cell(5,1)
|
771
|
+
row = sheet.row 6
|
772
|
+
assert_equal 1, row[0]
|
773
|
+
assert_equal 1, sheet[6,0]
|
774
|
+
assert_equal 1, sheet.cell(6,0)
|
775
|
+
row = sheet.row 7
|
776
|
+
assert_nil row[0]
|
777
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
778
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
779
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
780
|
+
row = sheet.row 8
|
781
|
+
assert_equal 0.0001, row[0]
|
782
|
+
row = sheet.row 9
|
783
|
+
assert_equal 0.00009, row[0]
|
784
|
+
end
|
785
|
+
def test_write_to_stringio
|
786
|
+
book = Spreadsheet::Excel::Workbook.new
|
787
|
+
sheet = book.create_worksheet :name => 'My Worksheet'
|
788
|
+
sheet[0,0] = 'my cell'
|
789
|
+
data = StringIO.new ''
|
790
|
+
assert_nothing_raised do
|
791
|
+
book.write data
|
792
|
+
end
|
793
|
+
assert_nothing_raised do
|
794
|
+
book = Spreadsheet.open data
|
795
|
+
end
|
796
|
+
assert_instance_of Spreadsheet::Excel::Workbook, book
|
797
|
+
assert_equal 1, book.worksheets.size
|
798
|
+
sheet = book.worksheet 0
|
799
|
+
assert_equal 'My Worksheet', sheet.name
|
800
|
+
assert_equal 'my cell', sheet[0,0]
|
801
|
+
end
|
802
|
+
def test_write_new_workbook
|
803
|
+
book = Spreadsheet::Workbook.new
|
804
|
+
path = File.join @var, 'test_write_workbook.xls'
|
805
|
+
sheet1 = book.create_worksheet
|
806
|
+
str1 = 'My Shared String'
|
807
|
+
str2 = 'Another Shared String'
|
808
|
+
assert_equal 1, (str1.size + str2.size) % 2,
|
809
|
+
"str3 should start at an odd offset to test splitting of wide strings"
|
810
|
+
str3 = '–––––––––– ' * 1000
|
811
|
+
str4 = '1234567890 ' * 1000
|
812
|
+
fmt1 = Format.new :italic => true, :color => :blue
|
813
|
+
sheet1.format_column 1, fmt1, :width => 20
|
814
|
+
fmt2 = Format.new(:weight => :bold, :color => :yellow)
|
815
|
+
sheet1.format_column 2, fmt2
|
816
|
+
sheet1.format_column 3, Format.new(:weight => :bold, :color => :red)
|
817
|
+
sheet1.format_column 6..9, fmt1
|
818
|
+
sheet1.format_column [4,5,7], fmt2
|
819
|
+
sheet1.row(0).height = 20
|
820
|
+
sheet1[0,0] = str1
|
821
|
+
sheet1.row(0).push str1
|
822
|
+
sheet1.row(1).concat [str2, str2]
|
823
|
+
sheet1[2,0] = str3
|
824
|
+
sheet1[3,0] = str4
|
825
|
+
fmt = Format.new :color => 'red'
|
826
|
+
sheet1[4,0] = 0.25
|
827
|
+
sheet1.row(4).set_format 0, fmt
|
828
|
+
fmt = Format.new :color => 'aqua'
|
829
|
+
sheet1[5,0] = 0.75
|
830
|
+
sheet1.row(5).set_format 0, fmt
|
831
|
+
link = Link.new 'http://scm.ywesee.com/?p=spreadsheet;a=summary',
|
832
|
+
'The Spreadsheet GitWeb', 'top'
|
833
|
+
sheet1[5,1] = link
|
834
|
+
sheet1[6,0] = 1
|
835
|
+
fmt = Format.new :color => 'green'
|
836
|
+
sheet1.row(6).set_format 0, fmt
|
837
|
+
sheet1[6,1] = Date.new 2008, 10, 10
|
838
|
+
sheet1[6,2] = Date.new 2008, 10, 12
|
839
|
+
fmt = Format.new :number_format => 'D.M.YY'
|
840
|
+
sheet1.row(6).set_format 1, fmt
|
841
|
+
sheet1.update_row 7, nil, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
|
842
|
+
sheet1[8,0] = 0.0005
|
843
|
+
sheet1[8,1] = 0.005
|
844
|
+
sheet1[8,2] = 0.05
|
845
|
+
sheet1[8,3] = 10.5
|
846
|
+
sheet1[8,4] = 1.05
|
847
|
+
sheet1[8,5] = 100.5
|
848
|
+
sheet1[8,6] = 10.05
|
849
|
+
sheet1[8,7] = 1.005
|
850
|
+
sheet1[9,0] = 100.5
|
851
|
+
sheet1[9,1] = 10.05
|
852
|
+
sheet1[9,2] = 1.005
|
853
|
+
sheet1[9,3] = 1000.5
|
854
|
+
sheet1[9,4] = 100.05
|
855
|
+
sheet1[9,5] = 10.005
|
856
|
+
sheet1[9,6] = 1.0005
|
857
|
+
sheet1[10,0] = 10000.5
|
858
|
+
sheet1[10,1] = 1000.05
|
859
|
+
sheet1[10,2] = 100.005
|
860
|
+
sheet1[10,3] = 10.0005
|
861
|
+
sheet1[10,4] = 1.00005
|
862
|
+
sheet1.insert_row 9, ['a', 'b', 'c']
|
863
|
+
assert_equal 'a', sheet1[9,0]
|
864
|
+
assert_equal 'b', sheet1[9,1]
|
865
|
+
assert_equal 'c', sheet1[9,2]
|
866
|
+
sheet1.delete_row 9
|
867
|
+
row = sheet1.row(11)
|
868
|
+
row.height = 40
|
869
|
+
row.push 'x'
|
870
|
+
row.pop
|
871
|
+
sheet2 = book.create_worksheet :name => 'my name'
|
872
|
+
book.write path
|
873
|
+
Spreadsheet.client_encoding = 'UTF-16LE'
|
874
|
+
str1 = @@iconv.iconv str1
|
875
|
+
str2 = @@iconv.iconv str2
|
876
|
+
str3 = @@iconv.iconv str3
|
877
|
+
str4 = @@iconv.iconv str4
|
878
|
+
assert_nothing_raised do book = Spreadsheet.open path end
|
879
|
+
if RUBY_VERSION >= '1.9'
|
880
|
+
assert_equal 'UTF-16LE', book.encoding.name
|
881
|
+
else
|
882
|
+
assert_equal 'UTF-16LE', book.encoding
|
883
|
+
end
|
884
|
+
assert_equal str1, book.shared_string(0)
|
885
|
+
assert_equal str2, book.shared_string(1)
|
886
|
+
test = nil
|
887
|
+
assert_nothing_raised "I've probably split a two-byte-character" do
|
888
|
+
test = book.shared_string 2
|
889
|
+
end
|
890
|
+
if test != str3
|
891
|
+
str3.size.times do |idx|
|
892
|
+
len = idx.next
|
893
|
+
if test[0,len] != str3[0,len]
|
894
|
+
assert_equal str3[idx - 5, 10], test[idx - 5, 10], "in position #{idx}"
|
895
|
+
end
|
896
|
+
end
|
897
|
+
end
|
898
|
+
assert_equal str3, test
|
899
|
+
test = book.shared_string 3
|
900
|
+
if test != str4
|
901
|
+
str4.size.times do |idx|
|
902
|
+
len = idx.next
|
903
|
+
if test[0,len] != str4[0,len]
|
904
|
+
assert_equal str4[idx - 5, 10], test[idx - 5, 10], "in position #{idx}"
|
905
|
+
end
|
906
|
+
end
|
907
|
+
end
|
908
|
+
assert_equal str4, test
|
909
|
+
assert_equal 2, book.worksheets.size
|
910
|
+
sheet = book.worksheets.first
|
911
|
+
assert_instance_of Spreadsheet::Excel::Worksheet, sheet
|
912
|
+
name = "W\000o\000r\000k\000s\000h\000e\000e\000t\0001\000"
|
913
|
+
name.force_encoding 'UTF-16LE' if name.respond_to?(:force_encoding)
|
914
|
+
assert_equal name, sheet.name
|
915
|
+
assert_not_nil sheet.offset
|
916
|
+
assert_not_nil col = sheet.column(1)
|
917
|
+
assert_equal true, col.default_format.font.italic?
|
918
|
+
assert_equal :blue, col.default_format.font.color
|
919
|
+
assert_equal 20, col.width
|
920
|
+
row = sheet.row 0
|
921
|
+
assert_equal col.default_format, row.format(1)
|
922
|
+
assert_equal 20, row.height
|
923
|
+
assert_equal str1, row[0]
|
924
|
+
assert_equal str1, sheet[0,0]
|
925
|
+
assert_equal str1, sheet.cell(0,0)
|
926
|
+
assert_equal str1, row[1]
|
927
|
+
assert_equal str1, sheet[0,1]
|
928
|
+
assert_equal str1, sheet.cell(0,1)
|
929
|
+
row = sheet.row 1
|
930
|
+
assert_equal str2, row[0]
|
931
|
+
assert_equal str2, sheet[1,0]
|
932
|
+
assert_equal str2, sheet.cell(1,0)
|
933
|
+
assert_equal str2, row[1]
|
934
|
+
assert_equal str2, sheet[1,1]
|
935
|
+
assert_equal str2, sheet.cell(1,1)
|
936
|
+
row = sheet.row 2
|
937
|
+
assert_equal str3, row[0]
|
938
|
+
assert_equal str3, sheet[2,0]
|
939
|
+
assert_equal str3, sheet.cell(2,0)
|
940
|
+
assert_nil row[1]
|
941
|
+
assert_nil sheet[2,1]
|
942
|
+
assert_nil sheet.cell(2,1)
|
943
|
+
row = sheet.row 3
|
944
|
+
assert_equal str4, row[0]
|
945
|
+
assert_equal str4, sheet[3,0]
|
946
|
+
assert_equal str4, sheet.cell(3,0)
|
947
|
+
assert_nil row[1]
|
948
|
+
assert_nil sheet[3,1]
|
949
|
+
assert_nil sheet.cell(3,1)
|
950
|
+
row = sheet.row 4
|
951
|
+
assert_equal :red, row.format(0).font.color
|
952
|
+
assert_equal 0.25, row[0]
|
953
|
+
assert_equal 0.25, sheet[4,0]
|
954
|
+
assert_equal 0.25, sheet.cell(4,0)
|
955
|
+
row = sheet.row 5
|
956
|
+
assert_equal :cyan, row.format(0).font.color
|
957
|
+
assert_equal 0.75, row[0]
|
958
|
+
assert_equal 0.75, sheet[5,0]
|
959
|
+
assert_equal 0.75, sheet.cell(5,0)
|
960
|
+
link = row[1]
|
961
|
+
assert_instance_of Link, link
|
962
|
+
url = @@iconv.iconv 'http://scm.ywesee.com/?p=spreadsheet;a=summary'
|
963
|
+
assert_equal @@iconv.iconv('The Spreadsheet GitWeb'), link
|
964
|
+
assert_equal url, link.url
|
965
|
+
assert_equal @@iconv.iconv('top'), link.fragment
|
966
|
+
row = sheet.row 6
|
967
|
+
assert_equal :green, row.format(0).font.color
|
968
|
+
assert_equal 1, row[0]
|
969
|
+
assert_equal 1, sheet[6,0]
|
970
|
+
assert_equal 1, sheet.cell(6,0)
|
971
|
+
assert_equal @@iconv.iconv('D.M.YY'), row.format(1).number_format
|
972
|
+
date = Date.new 2008, 10, 10
|
973
|
+
assert_equal date, row[1]
|
974
|
+
assert_equal date, sheet[6,1]
|
975
|
+
assert_equal date, sheet.cell(6,1)
|
976
|
+
assert_equal @@iconv.iconv('DD.MM.YYYY'), row.format(2).number_format
|
977
|
+
date = Date.new 2008, 10, 12
|
978
|
+
assert_equal date, row[2]
|
979
|
+
assert_equal date, sheet[6,2]
|
980
|
+
assert_equal date, sheet.cell(6,2)
|
981
|
+
row = sheet.row 7
|
982
|
+
assert_nil row[0]
|
983
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
984
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
985
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
986
|
+
assert_equal 0.0005, sheet1[8,0]
|
987
|
+
assert_equal 0.005, sheet1[8,1]
|
988
|
+
assert_equal 0.05, sheet1[8,2]
|
989
|
+
assert_equal 10.5, sheet1[8,3]
|
990
|
+
assert_equal 1.05, sheet1[8,4]
|
991
|
+
assert_equal 100.5, sheet1[8,5]
|
992
|
+
assert_equal 10.05, sheet1[8,6]
|
993
|
+
assert_equal 1.005, sheet1[8,7]
|
994
|
+
assert_equal 100.5, sheet1[9,0]
|
995
|
+
assert_equal 10.05, sheet1[9,1]
|
996
|
+
assert_equal 1.005, sheet1[9,2]
|
997
|
+
assert_equal 1000.5, sheet1[9,3]
|
998
|
+
assert_equal 100.05, sheet1[9,4]
|
999
|
+
assert_equal 10.005, sheet1[9,5]
|
1000
|
+
assert_equal 1.0005, sheet1[9,6]
|
1001
|
+
assert_equal 10000.5, sheet1[10,0]
|
1002
|
+
assert_equal 1000.05, sheet1[10,1]
|
1003
|
+
assert_equal 100.005, sheet1[10,2]
|
1004
|
+
assert_equal 10.0005, sheet1[10,3]
|
1005
|
+
assert_equal 1.00005, sheet1[10,4]
|
1006
|
+
assert_equal 40, sheet1.row(11).height
|
1007
|
+
assert_instance_of Spreadsheet::Excel::Worksheet, sheet
|
1008
|
+
sheet = book.worksheets.last
|
1009
|
+
name = "m\000y\000 \000n\000a\000m\000e\000"
|
1010
|
+
name.force_encoding 'UTF-16LE' if name.respond_to?(:force_encoding)
|
1011
|
+
assert_equal name, sheet.name
|
1012
|
+
assert_not_nil sheet.offset
|
1013
|
+
end
|
1014
|
+
def test_write_new_workbook__utf16
|
1015
|
+
Spreadsheet.client_encoding = 'UTF-16LE'
|
1016
|
+
book = Spreadsheet::Workbook.new
|
1017
|
+
path = File.join @var, 'test_write_workbook.xls'
|
1018
|
+
sheet1 = book.create_worksheet
|
1019
|
+
str1 = @@iconv.iconv 'Shared String'
|
1020
|
+
str2 = @@iconv.iconv 'Another Shared String'
|
1021
|
+
str3 = @@iconv.iconv('1234567890 ' * 1000)
|
1022
|
+
str4 = @@iconv.iconv('9876543210 ' * 1000)
|
1023
|
+
fmt = Format.new :italic => true, :color => :blue
|
1024
|
+
sheet1.format_column 1, fmt, :width => 20
|
1025
|
+
sheet1[0,0] = str1
|
1026
|
+
sheet1.row(0).push str1
|
1027
|
+
sheet1.row(1).concat [str2, str2]
|
1028
|
+
sheet1[2,0] = str3
|
1029
|
+
sheet1[3,0] = str4
|
1030
|
+
fmt = Format.new :color => 'red'
|
1031
|
+
sheet1[4,0] = 0.25
|
1032
|
+
sheet1.row(4).set_format 0, fmt
|
1033
|
+
fmt = Format.new :color => 'aqua'
|
1034
|
+
sheet1[5,0] = 0.75
|
1035
|
+
sheet1.row(5).set_format 0, fmt
|
1036
|
+
sheet1[6,0] = 1
|
1037
|
+
fmt = Format.new :color => 'green'
|
1038
|
+
sheet1.row(6).set_format 0, fmt
|
1039
|
+
sheet1[6,1] = Date.new 2008, 10, 10
|
1040
|
+
sheet1[6,2] = Date.new 2008, 10, 12
|
1041
|
+
fmt = Format.new :number_format => @@iconv.iconv("DD.MM.YYYY")
|
1042
|
+
sheet1.row(6).set_format 1, fmt
|
1043
|
+
sheet1.update_row 7, nil, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
|
1044
|
+
sheet1.row(8).default_format = fmt
|
1045
|
+
sheet1[8,0] = @@iconv.iconv 'formatted when empty'
|
1046
|
+
sheet2 = book.create_worksheet :name => @@iconv.iconv("my name")
|
1047
|
+
book.write path
|
1048
|
+
Spreadsheet.client_encoding = 'UTF-8'
|
1049
|
+
str1 = 'Shared String'
|
1050
|
+
str2 = 'Another Shared String'
|
1051
|
+
str3 = '1234567890 ' * 1000
|
1052
|
+
str4 = '9876543210 ' * 1000
|
1053
|
+
assert_nothing_raised do book = Spreadsheet.open path end
|
1054
|
+
if RUBY_VERSION >= '1.9'
|
1055
|
+
assert_equal 'UTF-16LE', book.encoding.name
|
1056
|
+
else
|
1057
|
+
assert_equal 'UTF-16LE', book.encoding
|
1058
|
+
end
|
1059
|
+
assert_equal str1, book.shared_string(0)
|
1060
|
+
assert_equal str2, book.shared_string(1)
|
1061
|
+
test = book.shared_string 2
|
1062
|
+
if test != str3
|
1063
|
+
str3.size.times do |idx|
|
1064
|
+
len = idx.next
|
1065
|
+
if test[0,len] != str3[0,len]
|
1066
|
+
assert_equal str3[idx - 5, 10], test[idx - 5, 10], "in position #{idx}"
|
1067
|
+
end
|
1068
|
+
end
|
1069
|
+
end
|
1070
|
+
assert_equal str3, test
|
1071
|
+
test = book.shared_string 3
|
1072
|
+
if test != str4
|
1073
|
+
str4.size.times do |idx|
|
1074
|
+
len = idx.next
|
1075
|
+
if test[0,len] != str4[0,len]
|
1076
|
+
assert_equal str4[idx - 5, 10], test[idx - 5, 10], "in position #{idx}"
|
1077
|
+
end
|
1078
|
+
end
|
1079
|
+
end
|
1080
|
+
assert_equal str4, test
|
1081
|
+
assert_equal 2, book.worksheets.size
|
1082
|
+
sheet = book.worksheets.first
|
1083
|
+
assert_instance_of Spreadsheet::Excel::Worksheet, sheet
|
1084
|
+
assert_equal "Worksheet1", sheet.name
|
1085
|
+
assert_not_nil sheet.offset
|
1086
|
+
assert_not_nil col = sheet.column(1)
|
1087
|
+
assert_equal true, col.default_format.font.italic?
|
1088
|
+
assert_equal :blue, col.default_format.font.color
|
1089
|
+
row = sheet.row 0
|
1090
|
+
assert_equal col.default_format, row.format(1)
|
1091
|
+
assert_equal str1, row[0]
|
1092
|
+
assert_equal str1, sheet[0,0]
|
1093
|
+
assert_equal str1, sheet.cell(0,0)
|
1094
|
+
assert_equal str1, row[1]
|
1095
|
+
assert_equal str1, sheet[0,1]
|
1096
|
+
assert_equal str1, sheet.cell(0,1)
|
1097
|
+
row = sheet.row 1
|
1098
|
+
assert_equal str2, row[0]
|
1099
|
+
assert_equal str2, sheet[1,0]
|
1100
|
+
assert_equal str2, sheet.cell(1,0)
|
1101
|
+
assert_equal str2, row[1]
|
1102
|
+
assert_equal str2, sheet[1,1]
|
1103
|
+
assert_equal str2, sheet.cell(1,1)
|
1104
|
+
row = sheet.row 2
|
1105
|
+
assert_equal str3, row[0]
|
1106
|
+
assert_equal str3, sheet[2,0]
|
1107
|
+
assert_equal str3, sheet.cell(2,0)
|
1108
|
+
assert_nil row[1]
|
1109
|
+
assert_nil sheet[2,1]
|
1110
|
+
assert_nil sheet.cell(2,1)
|
1111
|
+
row = sheet.row 3
|
1112
|
+
assert_equal str4, row[0]
|
1113
|
+
assert_equal str4, sheet[3,0]
|
1114
|
+
assert_equal str4, sheet.cell(3,0)
|
1115
|
+
assert_nil row[1]
|
1116
|
+
assert_nil sheet[3,1]
|
1117
|
+
assert_nil sheet.cell(3,1)
|
1118
|
+
row = sheet.row 4
|
1119
|
+
assert_equal :red, row.format(0).font.color
|
1120
|
+
assert_equal 0.25, row[0]
|
1121
|
+
assert_equal 0.25, sheet[4,0]
|
1122
|
+
assert_equal 0.25, sheet.cell(4,0)
|
1123
|
+
row = sheet.row 5
|
1124
|
+
assert_equal :cyan, row.format(0).font.color
|
1125
|
+
assert_equal 0.75, row[0]
|
1126
|
+
assert_equal 0.75, sheet[5,0]
|
1127
|
+
assert_equal 0.75, sheet.cell(5,0)
|
1128
|
+
row = sheet.row 6
|
1129
|
+
assert_equal :green, row.format(0).font.color
|
1130
|
+
assert_equal 1, row[0]
|
1131
|
+
assert_equal 1, sheet[6,0]
|
1132
|
+
assert_equal 1, sheet.cell(6,0)
|
1133
|
+
assert_equal 'DD.MM.YYYY', row.format(1).number_format
|
1134
|
+
date = Date.new 2008, 10, 10
|
1135
|
+
assert_equal date, row[1]
|
1136
|
+
assert_equal date, sheet[6,1]
|
1137
|
+
assert_equal date, sheet.cell(6,1)
|
1138
|
+
assert_equal 'DD.MM.YYYY', row.format(2).number_format
|
1139
|
+
date = Date.new 2008, 10, 12
|
1140
|
+
assert_equal date, row[2]
|
1141
|
+
assert_equal date, sheet[6,2]
|
1142
|
+
assert_equal date, sheet.cell(6,2)
|
1143
|
+
row = sheet.row 7
|
1144
|
+
assert_nil row[0]
|
1145
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], row[1,10]
|
1146
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet[7,1..10]
|
1147
|
+
assert_equal [1,2,3,4,5,6,7,8,9,0], sheet.cell(7,1..10)
|
1148
|
+
row = sheet.row 8
|
1149
|
+
assert_equal 'formatted when empty', row[0]
|
1150
|
+
assert_not_nil row.default_format
|
1151
|
+
assert_instance_of Spreadsheet::Excel::Worksheet, sheet
|
1152
|
+
sheet = book.worksheets.last
|
1153
|
+
assert_equal "my name",
|
1154
|
+
sheet.name
|
1155
|
+
assert_not_nil sheet.offset
|
1156
|
+
end
|
1157
|
+
def test_template
|
1158
|
+
template = File.join @data, 'test_copy.xls'
|
1159
|
+
output = File.join @var, 'test_template.xls'
|
1160
|
+
book = Spreadsheet.open template
|
1161
|
+
sheet1 = book.worksheet 0
|
1162
|
+
sheet1.row(4).replace [ 'Daniel J. Berger', 'U.S.A.',
|
1163
|
+
'Author of original code for Spreadsheet::Excel' ]
|
1164
|
+
book.write output
|
1165
|
+
assert_nothing_raised do
|
1166
|
+
book = Spreadsheet.open output
|
1167
|
+
end
|
1168
|
+
sheet = book.worksheet 0
|
1169
|
+
row = sheet.row(4)
|
1170
|
+
assert_equal 'Daniel J. Berger', row[0]
|
1171
|
+
end
|
1172
|
+
def test_bignum
|
1173
|
+
smallnum = 0x1fffffff
|
1174
|
+
bignum = smallnum + 1
|
1175
|
+
book = Spreadsheet::Workbook.new
|
1176
|
+
sheet = book.create_worksheet
|
1177
|
+
sheet[0,0] = bignum
|
1178
|
+
sheet[1,0] = -bignum
|
1179
|
+
sheet[0,1] = smallnum
|
1180
|
+
sheet[1,1] = -smallnum
|
1181
|
+
sheet[0,2] = bignum - 0.1
|
1182
|
+
sheet[1,2] = -bignum - 0.1
|
1183
|
+
sheet[0,3] = smallnum - 0.1
|
1184
|
+
sheet[1,3] = -smallnum - 0.1
|
1185
|
+
path = File.join @var, 'test_big-number.xls'
|
1186
|
+
book.write path
|
1187
|
+
assert_nothing_raised do
|
1188
|
+
book = Spreadsheet.open path
|
1189
|
+
end
|
1190
|
+
assert_equal bignum, book.worksheet(0)[0,0]
|
1191
|
+
assert_equal(-bignum, book.worksheet(0)[1,0])
|
1192
|
+
assert_equal smallnum, book.worksheet(0)[0,1]
|
1193
|
+
assert_equal(-smallnum, book.worksheet(0)[1,1])
|
1194
|
+
assert_equal bignum - 0.1, book.worksheet(0)[0,2]
|
1195
|
+
assert_equal(-bignum - 0.1, book.worksheet(0)[1,2])
|
1196
|
+
assert_equal smallnum - 0.1, book.worksheet(0)[0,3]
|
1197
|
+
assert_equal(-smallnum - 0.1, book.worksheet(0)[1,3])
|
1198
|
+
end
|
1199
|
+
def test_bigfloat
|
1200
|
+
# reported in http://rubyforge.org/tracker/index.php?func=detail&aid=24119&group_id=678&atid=2677
|
1201
|
+
bigfloat = 10000000.0
|
1202
|
+
book = Spreadsheet::Workbook.new
|
1203
|
+
sheet = book.create_worksheet
|
1204
|
+
sheet[0,0] = bigfloat
|
1205
|
+
sheet[0,1] = bigfloat + 0.1
|
1206
|
+
sheet[0,2] = bigfloat - 0.1
|
1207
|
+
sheet[1,0] = -bigfloat
|
1208
|
+
sheet[1,1] = -bigfloat + 0.1
|
1209
|
+
sheet[1,2] = -bigfloat - 0.1
|
1210
|
+
path = File.join @var, 'test_big-float.xls'
|
1211
|
+
book.write path
|
1212
|
+
assert_nothing_raised do
|
1213
|
+
book = Spreadsheet.open path
|
1214
|
+
end
|
1215
|
+
sheet = book.worksheet(0)
|
1216
|
+
assert_equal bigfloat, sheet[0,0]
|
1217
|
+
assert_equal bigfloat + 0.1, sheet[0,1]
|
1218
|
+
assert_equal bigfloat - 0.1, sheet[0,2]
|
1219
|
+
assert_equal(-bigfloat, sheet[1,0])
|
1220
|
+
assert_equal(-bigfloat + 0.1, sheet[1,1])
|
1221
|
+
assert_equal(-bigfloat - 0.1, sheet[1,2])
|
1222
|
+
end
|
1223
|
+
def test_datetime__off_by_one
|
1224
|
+
# reported in http://rubyforge.org/tracker/index.php?func=detail&aid=24414&group_id=678&atid=2677
|
1225
|
+
datetime1 = DateTime.new(2008)
|
1226
|
+
datetime2 = DateTime.new(2008, 1, 1, 1, 0, 1)
|
1227
|
+
date1 = Date.new(2008)
|
1228
|
+
date2 = Date.new(2009)
|
1229
|
+
book = Spreadsheet::Workbook.new
|
1230
|
+
sheet = book.create_worksheet
|
1231
|
+
sheet[0,0] = datetime1
|
1232
|
+
sheet[0,1] = datetime2
|
1233
|
+
sheet[1,0] = date1
|
1234
|
+
sheet[1,1] = date2
|
1235
|
+
path = File.join @var, 'test_datetime.xls'
|
1236
|
+
book.write path
|
1237
|
+
assert_nothing_raised do
|
1238
|
+
book = Spreadsheet.open path
|
1239
|
+
end
|
1240
|
+
sheet = book.worksheet(0)
|
1241
|
+
assert_equal datetime1, sheet[0,0]
|
1242
|
+
assert_equal datetime2, sheet[0,1]
|
1243
|
+
assert_equal date1, sheet[1,0]
|
1244
|
+
assert_equal date2, sheet[1,1]
|
1245
|
+
assert_equal date1, sheet.row(0).date(0)
|
1246
|
+
assert_equal datetime1, sheet.row(1).datetime(0)
|
1247
|
+
end
|
1248
|
+
def test_sharedfmla
|
1249
|
+
path = File.join @data, 'test_formula.xls'
|
1250
|
+
book = Spreadsheet.open path
|
1251
|
+
assert_instance_of Excel::Workbook, book
|
1252
|
+
sheet = book.worksheet 0
|
1253
|
+
64.times do |idx|
|
1254
|
+
assert_equal '5026', sheet[idx.next, 2].value
|
1255
|
+
end
|
1256
|
+
end
|
1257
|
+
def test_missing_row_op
|
1258
|
+
path = File.join @data, 'test_missing_row.xls'
|
1259
|
+
book = Spreadsheet.open path
|
1260
|
+
assert_instance_of Excel::Workbook, book
|
1261
|
+
sheet = book.worksheet 0
|
1262
|
+
assert_not_nil sheet[1,0]
|
1263
|
+
assert_not_nil sheet[2,1]
|
1264
|
+
end
|
1265
|
+
def test_changes
|
1266
|
+
path = File.join @data, 'test_changes.xls'
|
1267
|
+
book = Spreadsheet.open path
|
1268
|
+
assert_instance_of Excel::Workbook, book
|
1269
|
+
sheet = book.worksheet 1
|
1270
|
+
sheet[20,0] = 'Ciao Mundo!'
|
1271
|
+
target = File.join @var, 'test_changes.xls'
|
1272
|
+
assert_nothing_raised do book.write target end
|
1273
|
+
end
|
1274
|
+
def untest_read_baltic
|
1275
|
+
path = File.join @data, 'test_baltic.xls'
|
1276
|
+
assert_nothing_raised do
|
1277
|
+
Spreadsheet.open path
|
1278
|
+
end
|
1279
|
+
end
|
1280
|
+
end
|
1281
|
+
end
|