rexcel 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/example.rb +28 -6
- data/examples/example_format.rb +1 -0
- data/examples/example_sequel.rb +52 -0
- data/lib/rexcel.rb +13 -63
- data/lib/rexcel/cell.rb +34 -1
- data/lib/rexcel/row.rb +77 -55
- data/lib/rexcel/workbook.rb +141 -43
- data/lib/rexcel/worksheet.rb +120 -76
- data/readme.rdoc +66 -0
- data/unittest/expected/Test_Workbook_to_csv-test_filename.csv +4 -0
- data/unittest/expected/Test_Workbook_to_csv-test_filename_encoding.csv +4 -0
- data/unittest/expected/Test_Worksheet_to_csv-test_filename.csv +4 -0
- data/unittest/expected/Test_Worksheet_to_csv-test_filename_encoding.csv +4 -0
- data/unittest/rexcel_test_helper.rb +91 -0
- data/unittest/test_rexcel.rb +12 -582
- data/unittest/test_rexcel_cell.rb +126 -0
- data/unittest/test_rexcel_row.rb +103 -0
- data/unittest/test_rexcel_workbook.rb +293 -0
- data/unittest/test_rexcel_worksheet.rb +154 -0
- metadata +66 -79
@@ -0,0 +1,126 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
#~ gem 'test-unit', ">= 2.1.2" #tested with this version
|
3
|
+
require_relative 'rexcel_test_helper'
|
4
|
+
|
5
|
+
class Test_Cell < Test::Unit::TestCase
|
6
|
+
def test_create()
|
7
|
+
assert_nil( Excel::Cell.new().content)
|
8
|
+
assert_equal( 1, Excel::Cell.new(1).content)
|
9
|
+
end #def test_create()
|
10
|
+
def test_style()
|
11
|
+
style = Excel::Style.new('style_example')
|
12
|
+
assert_equal( nil, Excel::Cell.new(1).style)
|
13
|
+
assert_instance_of( Excel::Style, Excel::Cell.new(1, :style => style).style)
|
14
|
+
assert_equal( style, Excel::Cell.new(1, :style => style).style)
|
15
|
+
end
|
16
|
+
|
17
|
+
end #class Test_Cell
|
18
|
+
|
19
|
+
class Test_Cell_xml < Test::Unit::TestCase
|
20
|
+
def setup()
|
21
|
+
@mock = Mock_document.new
|
22
|
+
end
|
23
|
+
def test_content_string
|
24
|
+
assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"String\">content</ss:Data>\n </ss:Cell>",
|
25
|
+
@mock.testcase{|builder, ns_proc|
|
26
|
+
cell = Excel::Cell.new('content')
|
27
|
+
cell.to_xml( builder,ns_proc)
|
28
|
+
})
|
29
|
+
end
|
30
|
+
def test_content_number
|
31
|
+
assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">1</ss:Data>\n </ss:Cell>",
|
32
|
+
@mock.testcase{|builder, ns_proc|
|
33
|
+
cell = Excel::Cell.new(1)
|
34
|
+
cell.to_xml( builder,ns_proc)
|
35
|
+
})
|
36
|
+
assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">1.1</ss:Data>\n </ss:Cell>",
|
37
|
+
@mock.testcase{|builder, ns_proc|
|
38
|
+
cell = Excel::Cell.new(1.1)
|
39
|
+
cell.to_xml( builder,ns_proc)
|
40
|
+
})
|
41
|
+
assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">281474976710656</ss:Data>\n </ss:Cell>",
|
42
|
+
@mock.testcase{|builder, ns_proc|
|
43
|
+
cell = Excel::Cell.new(281474976710656) #Bignum
|
44
|
+
cell.to_xml( builder,ns_proc)
|
45
|
+
})
|
46
|
+
end
|
47
|
+
def test_content_with_type
|
48
|
+
assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"String\">1</ss:Data>\n </ss:Cell>",
|
49
|
+
@mock.testcase{|builder, ns_proc|
|
50
|
+
cell = Excel::Cell.new(1, :string => true)
|
51
|
+
cell.to_xml( builder,ns_proc)
|
52
|
+
})
|
53
|
+
end #def test_content
|
54
|
+
|
55
|
+
end #class Test_Cell_xlm
|
56
|
+
|
57
|
+
class Test_Cell_xls < Test::Unit::TestCase
|
58
|
+
def setup()
|
59
|
+
wb = Excel::Excel.instance.xl.Workbooks.Add #Includes 3 worksheets
|
60
|
+
@ws = wb.ActiveSheet
|
61
|
+
end
|
62
|
+
def test_content_string_xls
|
63
|
+
cell = Excel::Cell.new('content')
|
64
|
+
cell_ole = cell.to_xls(@ws.Cells(1,1))
|
65
|
+
assert_instance_of( WIN32OLE, cell_ole)
|
66
|
+
assert_equal(XLS_ENCODING, cell_ole.value.encoding)
|
67
|
+
assert_equal('content', cell_ole.value)
|
68
|
+
assert_equal('@', cell_ole.NumberFormat)
|
69
|
+
|
70
|
+
end #def test_content_string_xls
|
71
|
+
def test_content_number
|
72
|
+
cell_ole = Excel::Cell.new(1).to_xls(@ws.Cells(1,1))
|
73
|
+
assert_equal(1, cell_ole.value)
|
74
|
+
assert_equal(XLS_ENCODING, cell_ole.NumberFormat.encoding)
|
75
|
+
assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
|
76
|
+
|
77
|
+
cell_ole = Excel::Cell.new(1.1).to_xls(@ws.Cells(1,2))
|
78
|
+
assert_equal(1.1, cell_ole.value)
|
79
|
+
assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
|
80
|
+
|
81
|
+
#Bignum
|
82
|
+
cell_ole = Excel::Cell.new(281474976710656).to_xls(@ws.Cells(1,3))
|
83
|
+
assert_equal(281474976710656, cell_ole.value)
|
84
|
+
assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
|
85
|
+
end
|
86
|
+
def test_content_with_type
|
87
|
+
cell_ole = Excel::Cell.new(1, :string => true ).to_xls(@ws.Cells(1,1))
|
88
|
+
assert_equal('1', cell_ole.value)
|
89
|
+
assert_equal('@', cell_ole.NumberFormat)
|
90
|
+
end #def test_content
|
91
|
+
end #class Test_Cell_xls
|
92
|
+
|
93
|
+
class Test_Cell_csv < Test::Unit::TestCase
|
94
|
+
def setup()
|
95
|
+
#~ wb = Excel::Excel.instance.xl.Workbooks.Add #Includes 3 worksheets
|
96
|
+
#~ @ws = wb.ActiveSheet
|
97
|
+
end
|
98
|
+
def test_content_string_csv
|
99
|
+
cell = Excel::Cell.new('content')
|
100
|
+
assert_equal('String', cell.type)
|
101
|
+
assert_equal('content', cell.to_csv())
|
102
|
+
|
103
|
+
cell = Excel::Cell.new('1')
|
104
|
+
assert_equal('String', cell.type)
|
105
|
+
assert_equal('"1"', cell.to_csv())
|
106
|
+
end #def test_content_string_csv
|
107
|
+
|
108
|
+
def test_content_number
|
109
|
+
cell = Excel::Cell.new(1)
|
110
|
+
assert_equal('Number', cell.type)
|
111
|
+
assert_equal(1, cell.to_csv())
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_content_float
|
115
|
+
cell = Excel::Cell.new(1.1)
|
116
|
+
assert_equal('Number', cell.type)
|
117
|
+
assert_equal('="1.1"', cell.to_csv())
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_content_with_type
|
121
|
+
cell = Excel::Cell.new(1, :string => true )
|
122
|
+
assert_equal('String', cell.type)
|
123
|
+
assert_equal('"1"', cell.to_csv)
|
124
|
+
end #def test_content
|
125
|
+
|
126
|
+
end #class Test_Cell_csv
|
@@ -0,0 +1,103 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
require_relative 'rexcel_test_helper'
|
3
|
+
|
4
|
+
|
5
|
+
class Test_Row < Test::Unit::TestCase
|
6
|
+
def test_style()
|
7
|
+
style = Excel::Style.new('style_example')
|
8
|
+
assert_equal( nil, Excel::Row.new.style)
|
9
|
+
assert_instance_of( Excel::Style, Excel::Row.new(:style => style).style)
|
10
|
+
assert_equal( style, Excel::Row.new(:style => style).style)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Test_Row_insertions < Test::Unit::TestCase
|
15
|
+
def setup()
|
16
|
+
@mock = Mock_document.new
|
17
|
+
end
|
18
|
+
def test_insert_array
|
19
|
+
row = Excel::Row.new()
|
20
|
+
assert_equal(0, row.columns.size)
|
21
|
+
assert_nothing_raised{ row << [:a,:b,:c,:d] }
|
22
|
+
assert_equal(%{<ss:Row>
|
23
|
+
<ss:Cell>
|
24
|
+
<ss:Data ss:Type="String">a</ss:Data>
|
25
|
+
</ss:Cell>
|
26
|
+
<ss:Cell>
|
27
|
+
<ss:Data ss:Type="String">b</ss:Data>
|
28
|
+
</ss:Cell>
|
29
|
+
<ss:Cell>
|
30
|
+
<ss:Data ss:Type="String">c</ss:Data>
|
31
|
+
</ss:Cell>
|
32
|
+
<ss:Cell>
|
33
|
+
<ss:Data ss:Type="String">d</ss:Data>
|
34
|
+
</ss:Cell>
|
35
|
+
</ss:Row>},
|
36
|
+
@mock.testcase{|builder, ns_proc|
|
37
|
+
row.to_xml( builder,ns_proc)
|
38
|
+
})
|
39
|
+
assert_equal(4, row.columns.size)
|
40
|
+
assert_equal("a;b;c;d", row.to_csv())
|
41
|
+
assert_nothing_raised{ row << [:a,:b,:c,:d] }
|
42
|
+
end
|
43
|
+
def test_insert_hash()
|
44
|
+
row = Excel::Row.new()
|
45
|
+
assert_equal(0, row.columns.size)
|
46
|
+
#Hash requires Heading data
|
47
|
+
assert_raise( ArgumentError ){ row << { 1 => 1, 2 => 2, 3 => 3 } }
|
48
|
+
assert_equal(0, row.columns.size)
|
49
|
+
end #def test_insert()
|
50
|
+
def test_insert_other()
|
51
|
+
row = Excel::Row.new()
|
52
|
+
assert_equal(0, row.columns.size)
|
53
|
+
assert_raise(Excel::EmptyError){
|
54
|
+
@mock.testcase{|builder, ns_proc|row.to_xml( builder,ns_proc)}
|
55
|
+
}
|
56
|
+
assert_nothing_raised{ row << 'a' }
|
57
|
+
assert_equal(1, row.columns.size)
|
58
|
+
assert_equal(%{<ss:Row>
|
59
|
+
<ss:Cell>
|
60
|
+
<ss:Data ss:Type="String">a</ss:Data>
|
61
|
+
</ss:Cell>
|
62
|
+
</ss:Row>},
|
63
|
+
@mock.testcase{|builder, ns_proc|
|
64
|
+
row.to_xml( builder,ns_proc)
|
65
|
+
})
|
66
|
+
assert_equal("a", row.to_csv())
|
67
|
+
assert_nothing_raised{ row << 'x' }
|
68
|
+
assert_equal(2, row.columns.size)
|
69
|
+
assert_equal(%{<ss:Row>
|
70
|
+
<ss:Cell>
|
71
|
+
<ss:Data ss:Type="String">a</ss:Data>
|
72
|
+
</ss:Cell>
|
73
|
+
<ss:Cell>
|
74
|
+
<ss:Data ss:Type="String">x</ss:Data>
|
75
|
+
</ss:Cell>
|
76
|
+
</ss:Row>},
|
77
|
+
@mock.testcase{|builder, ns_proc|
|
78
|
+
row.to_xml( builder,ns_proc)
|
79
|
+
})
|
80
|
+
assert_equal("a;x", row.to_csv())
|
81
|
+
end
|
82
|
+
def test_insert_errors()
|
83
|
+
row = Excel::Row.new()
|
84
|
+
assert_raise(ArgumentError){ row << Excel::Row.new() }
|
85
|
+
assert_raise(ArgumentError){ row << Excel::Worksheet.new() }
|
86
|
+
assert_raise(ArgumentError){ row << Excel::Workbook.new() }
|
87
|
+
end
|
88
|
+
|
89
|
+
#~ def test_bold()
|
90
|
+
#~ pend("#{self.class}##{__method__} output")
|
91
|
+
#~ end
|
92
|
+
#~ def test_italic()
|
93
|
+
#~ pend("#{self.class}##{__method__} output")
|
94
|
+
#~ end
|
95
|
+
#~ def test_color()
|
96
|
+
#~ pend("#{self.class}##{__method__} output")
|
97
|
+
#~ end
|
98
|
+
#~ def test_backgroundcolor()
|
99
|
+
#~ pend("#{self.class}##{__method__} output")
|
100
|
+
#~ end
|
101
|
+
|
102
|
+
end #class Test_Row
|
103
|
+
|
@@ -0,0 +1,293 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
require_relative 'rexcel_test_helper'
|
3
|
+
|
4
|
+
class Test_Workbook < Test::Unit::TestCase
|
5
|
+
#List of temporary generated files
|
6
|
+
FILELIST = %w{
|
7
|
+
test_save_xml.xml test_save_xml.xls test_save_xml.xlsx test_save_xml_2.xls
|
8
|
+
test_save_csv.csv test_save_csv.xls test_save_csv.xlsx test_save_csv_2.xls
|
9
|
+
test_save.xls test_save.xlsx}
|
10
|
+
|
11
|
+
def setup()
|
12
|
+
FILELIST.each{|fn|
|
13
|
+
File.delete(fn) if File.exist?(fn)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
def teardown()
|
17
|
+
FILELIST.each{|fn|
|
18
|
+
File.delete(fn) if File.exist?(fn)
|
19
|
+
}
|
20
|
+
end
|
21
|
+
def test_init()
|
22
|
+
assert_nothing_raised{ Excel::Workbook.new() }
|
23
|
+
assert_nothing_raised{ Excel::Workbook.new( Excel::LOGGER) }
|
24
|
+
assert_raise(ArgumentError){ Excel::Workbook.new( 1 ) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_save_xml()
|
28
|
+
|
29
|
+
assert_false( File.exist?('test_save_xml.xml'), 'Old testdata found')
|
30
|
+
assert_false( File.exist?('test_save_xml.xls'), 'Old testdata found')
|
31
|
+
assert_false( File.exist?('test_save_xml.xlsx'), 'Old testdata found')
|
32
|
+
|
33
|
+
wb = Excel::Workbook.new()
|
34
|
+
wb << [1,2,3] #add anything, else you get an error
|
35
|
+
wb << ['a','b','c'] #add anything, else you get an error
|
36
|
+
assert_nothing_raised{ wb.save('test_save_xml.xml') }
|
37
|
+
assert_nothing_raised{ wb.save('test_save_xml.xls', 'test_save_xml.xml')}
|
38
|
+
assert_nothing_raised{ wb.save('test_save_xml.xlsx', 'test_save_xml.xml')}
|
39
|
+
assert_nothing_raised{ wb.save('test_save_xml_2.xls')}
|
40
|
+
|
41
|
+
assert_true( File.exist?('test_save_xml.xml'))
|
42
|
+
assert_true( File.exist?('test_save_xml.xls'))
|
43
|
+
assert_true( File.exist?('test_save_xml.xlsx'))
|
44
|
+
|
45
|
+
|
46
|
+
#~ assert_equal_filecontent( "expected/%s-%s.xml" %[self.class, __method__],
|
47
|
+
#~ wb.build_excel_xml('ss') )
|
48
|
+
assert_equal( %{<?xml version="1.0"?>
|
49
|
+
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
|
50
|
+
<ss:Worksheet ss:Name="Sheet0">
|
51
|
+
<ss:Table>
|
52
|
+
<ss:Row>
|
53
|
+
<ss:Cell>
|
54
|
+
<ss:Data ss:Type="Number">1</ss:Data>
|
55
|
+
</ss:Cell>
|
56
|
+
<ss:Cell>
|
57
|
+
<ss:Data ss:Type="Number">2</ss:Data>
|
58
|
+
</ss:Cell>
|
59
|
+
<ss:Cell>
|
60
|
+
<ss:Data ss:Type="Number">3</ss:Data>
|
61
|
+
</ss:Cell>
|
62
|
+
</ss:Row>
|
63
|
+
<ss:Row>
|
64
|
+
<ss:Cell>
|
65
|
+
<ss:Data ss:Type="String">a</ss:Data>
|
66
|
+
</ss:Cell>
|
67
|
+
<ss:Cell>
|
68
|
+
<ss:Data ss:Type="String">b</ss:Data>
|
69
|
+
</ss:Cell>
|
70
|
+
<ss:Cell>
|
71
|
+
<ss:Data ss:Type="String">c</ss:Data>
|
72
|
+
</ss:Cell>
|
73
|
+
</ss:Row>
|
74
|
+
</ss:Table>
|
75
|
+
</ss:Worksheet>
|
76
|
+
</ss:Workbook>},
|
77
|
+
wb.build_excel_xml('ss') )
|
78
|
+
|
79
|
+
assert_equal( 2, compare_with_bc3('test_save_xml.xls', 'test_save_xml.xlsx'))
|
80
|
+
assert_equal( 2, compare_with_bc3('test_save_xml.xls', 'test_save_xml_2.xls'),
|
81
|
+
"Save directly xls is different to save via x")
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_save_csv()
|
86
|
+
|
87
|
+
assert_false( File.exist?('test_save_csv.csv'), 'Old testdata found')
|
88
|
+
assert_false( File.exist?('test_save_csv.xls'), 'Old testdata found')
|
89
|
+
assert_false( File.exist?('test_save_csv.xlsx'), 'Old testdata found')
|
90
|
+
|
91
|
+
wb = Excel::Workbook.new()
|
92
|
+
wb << [1,2,3] #add anything, else you get an error
|
93
|
+
wb << ['a','b','c'] #add anything, else you get an error
|
94
|
+
assert_nothing_raised{ wb.save('test_save_csv.csv') }
|
95
|
+
assert_nothing_raised{ wb.save('test_save_csv.xls', 'test_save_csv.csv')}
|
96
|
+
assert_nothing_raised{ wb.save('test_save_csv.xlsx', 'test_save_csv.csv')}
|
97
|
+
assert_nothing_raised{ wb.save('test_save_csv_2.xls') }
|
98
|
+
|
99
|
+
assert_true( File.exist?('test_save_csv.csv'))
|
100
|
+
assert_true( File.exist?('test_save_csv.xls'))
|
101
|
+
assert_true( File.exist?('test_save_csv.xlsx'))
|
102
|
+
assert_true( File.exist?('test_save_csv_2.xls'))
|
103
|
+
|
104
|
+
assert_equal("1;2;3\na;b;c", wb.build_excel_csv())
|
105
|
+
assert_equal( 2, compare_with_bc3('test_save_csv.xls', 'test_save_csv.xlsx'))
|
106
|
+
|
107
|
+
assert_equal( 2, compare_with_bc3('test_save_csv.xls', 'test_save_csv_2.xls'),
|
108
|
+
"Save directly xls is different to save via csv")
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
def test_save_xls()
|
114
|
+
|
115
|
+
assert_false( File.exist?('test_save.xls'), 'Old testdata found')
|
116
|
+
assert_false( File.exist?('test_save.xlsx'), 'Old testdata found')
|
117
|
+
|
118
|
+
wb = Excel::Workbook.new()
|
119
|
+
wb << [ 1,2,3,4]
|
120
|
+
wb << [ :a, :b, :c ]
|
121
|
+
assert_nothing_raised{ wb.save('test_save.xls')}
|
122
|
+
assert_nothing_raised{ wb.save('test_save.xlsx')}
|
123
|
+
|
124
|
+
assert_true( File.exist?('test_save.xls'))
|
125
|
+
assert_true( File.exist?('test_save.xlsx'))
|
126
|
+
|
127
|
+
assert_false( File.exist?('test_save.xml'))
|
128
|
+
assert_false( File.exist?('test_save_xml.xls'))
|
129
|
+
assert_nothing_raised{ wb.save('test_save_xml.xml')}
|
130
|
+
assert_nothing_raised{ wb.save('test_save_xml.xlsx', 'test_save_xml.xml')}
|
131
|
+
assert_true( File.exist?('test_save_xml.xml'))
|
132
|
+
assert_true( File.exist?('test_save_xml.xlsx'))
|
133
|
+
|
134
|
+
assert_equal( 2, compare_with_bc3('test_save.xls', 'test_save.xlsx'))
|
135
|
+
|
136
|
+
end #def test_create_wb()
|
137
|
+
|
138
|
+
def test_save_other()
|
139
|
+
wb = Excel::Workbook.new()
|
140
|
+
assert_raise( ArgumentError ){wb.save('test_save_xml.doc')}
|
141
|
+
end
|
142
|
+
end #class Test_workbook
|
143
|
+
|
144
|
+
class Test_workbook_insertions < Test::Unit::TestCase
|
145
|
+
def test_insert_worksheet()
|
146
|
+
wb = Excel::Workbook.new()
|
147
|
+
assert_equal(0, wb.worksheets.size)
|
148
|
+
|
149
|
+
assert_nothing_raised{ wb << Excel::Worksheet.new() }
|
150
|
+
assert_equal(1, wb.worksheets.size)
|
151
|
+
assert_equal('Sheet', wb.worksheets.last.name)
|
152
|
+
|
153
|
+
assert_nothing_raised{ wb << Excel::Worksheet.new('Sheet for test') }
|
154
|
+
assert_equal(2, wb.worksheets.size)
|
155
|
+
assert_equal('Sheet for test', wb.worksheets.last.name)
|
156
|
+
end
|
157
|
+
def test_insert_array
|
158
|
+
wb = Excel::Workbook.new()
|
159
|
+
assert_equal(0, wb.worksheets.size)
|
160
|
+
assert_nothing_raised{ wb << [:a,:b,:c,:d] }
|
161
|
+
assert_equal(1, wb.worksheets.size)
|
162
|
+
assert_equal(%{<?xml version="1.0"?>
|
163
|
+
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
|
164
|
+
<ss:Worksheet ss:Name="Sheet0">
|
165
|
+
<ss:Table>
|
166
|
+
<ss:Row>
|
167
|
+
<ss:Cell>
|
168
|
+
<ss:Data ss:Type="String">a</ss:Data>
|
169
|
+
</ss:Cell>
|
170
|
+
<ss:Cell>
|
171
|
+
<ss:Data ss:Type="String">b</ss:Data>
|
172
|
+
</ss:Cell>
|
173
|
+
<ss:Cell>
|
174
|
+
<ss:Data ss:Type="String">c</ss:Data>
|
175
|
+
</ss:Cell>
|
176
|
+
<ss:Cell>
|
177
|
+
<ss:Data ss:Type="String">d</ss:Data>
|
178
|
+
</ss:Cell>
|
179
|
+
</ss:Row>
|
180
|
+
</ss:Table>
|
181
|
+
</ss:Worksheet>
|
182
|
+
</ss:Workbook>},
|
183
|
+
wb.build_excel_xml( 'ss' )
|
184
|
+
)
|
185
|
+
assert_equal("a;b;c;d", wb.build_excel_csv())
|
186
|
+
assert_nothing_raised{ wb << [:a,:b,:c,:d] }
|
187
|
+
end
|
188
|
+
def test_insert_row()
|
189
|
+
wb = Excel::Workbook.new()
|
190
|
+
assert_equal(0, wb.worksheets.size)
|
191
|
+
assert_nothing_raised{ wb << Excel::Row.new() }
|
192
|
+
assert_equal(1, wb.worksheets.size)
|
193
|
+
#Exception for empty row
|
194
|
+
assert_raise( Excel::EmptyError ){ wb.build_excel_xml( 'ns' ) }
|
195
|
+
end
|
196
|
+
def test_insert_hash()
|
197
|
+
wb = Excel::Workbook.new()
|
198
|
+
assert_equal(0, wb.worksheets.size)
|
199
|
+
assert_equal(nil, wb.active_worksheet)
|
200
|
+
#Hash requires Heading data
|
201
|
+
assert_raise( ArgumentError ){ wb << { 1 => :a, 2 => :b, 3 => :c } }
|
202
|
+
assert_equal(1, wb.worksheets.size)
|
203
|
+
assert_instance_of(Excel::Worksheet, wb.active_worksheet)
|
204
|
+
|
205
|
+
ws = wb.active_worksheet
|
206
|
+
ws << Excel::Column.new( 1 )
|
207
|
+
ws << Excel::Column.new( 2 )
|
208
|
+
|
209
|
+
assert_nothing_raised{ wb << { 1 => :a, 2 => :b, 3 => :c } }
|
210
|
+
assert_equal(1, wb.worksheets.size)#no new worksheet created
|
211
|
+
assert_equal(%{<?xml version="1.0"?>
|
212
|
+
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
|
213
|
+
<ss:Worksheet ss:Name="Sheet0">
|
214
|
+
<ss:Table>
|
215
|
+
<ss:Column/>
|
216
|
+
<ss:Column/>
|
217
|
+
<ss:Row>
|
218
|
+
<ss:Cell>
|
219
|
+
<ss:Data ss:Type="String">a</ss:Data>
|
220
|
+
</ss:Cell>
|
221
|
+
<ss:Cell>
|
222
|
+
<ss:Data ss:Type="String">b</ss:Data>
|
223
|
+
</ss:Cell>
|
224
|
+
</ss:Row>
|
225
|
+
</ss:Table>
|
226
|
+
</ss:Worksheet>
|
227
|
+
</ss:Workbook>},
|
228
|
+
wb.build_excel_xml( 'ss' )
|
229
|
+
)
|
230
|
+
assert_equal("a;b", wb.build_excel_csv())
|
231
|
+
|
232
|
+
end #def test_insert()
|
233
|
+
def test_insert_other()
|
234
|
+
wb = Excel::Workbook.new()
|
235
|
+
assert_raise(ArgumentError){ wb << 1 }
|
236
|
+
end
|
237
|
+
|
238
|
+
end #class Test_workbook_insertion
|
239
|
+
|
240
|
+
|
241
|
+
class Test_Workbook_to_csv < Test::Unit::TestCase
|
242
|
+
def setup()
|
243
|
+
@wb = Excel::Workbook.new()
|
244
|
+
@wb << ws = Excel::Worksheet.new()
|
245
|
+
ws << [:a,:b,:c,:d]
|
246
|
+
ws << %w{a b c d}
|
247
|
+
ws << [1,2,3,4]
|
248
|
+
ws << [1.1,2.2,3.3,4.4]
|
249
|
+
#~ ws << %w{1,1 2,2 3,3 4,4}
|
250
|
+
end
|
251
|
+
def teardown()
|
252
|
+
end
|
253
|
+
def test_to_csv
|
254
|
+
assert_equal(%{a;b;c;d\na;b;c;d\n1;2;3;4\n="1.1";="2.2";="3.3";="4.4"}, @wb.build_excel_csv())
|
255
|
+
end
|
256
|
+
def test_to_csv_sep
|
257
|
+
assert_equal(%{a\tb\tc\td\na\tb\tc\td\n1\t2\t3\t4\n="1.1"\t="2.2"\t="3.3"\t="4.4"}, @wb.build_excel_csv(:sep => "\t"))
|
258
|
+
assert_equal(%{a,b,c,d\na,b,c,d\n1,2,3,4\n="1.1",="2.2",="3.3",="4.4"}, @wb.build_excel_csv(:sep => ','))
|
259
|
+
|
260
|
+
assert_raise(ArgumentError){@wb.build_excel_csv(:sep => 'a')}
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_filename()
|
264
|
+
filename = "#{self.class.name}-#{__method__}.csv"
|
265
|
+
File.delete(filename) if File.exist?(filename)
|
266
|
+
assert_false(File.exist?(filename))
|
267
|
+
@wb.build_excel_csv(:filename => filename)
|
268
|
+
assert_true(File.exist?(filename))
|
269
|
+
assert_equal_filecontent(File.join('expected', filename), @wb.build_excel_csv)
|
270
|
+
#~ assert_equal_filecontent(File.join('expected', filename), File.read(filename, :encoding => 'UTF-16LE'))
|
271
|
+
File.delete(filename)
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_filename_encoding()
|
275
|
+
filename = "#{self.class.name}-#{__method__}.csv"
|
276
|
+
File.delete(filename) if File.exist?(filename)
|
277
|
+
assert_false(File.exist?(filename))
|
278
|
+
@wb.build_excel_csv(:filename => filename, :encoding => 'utf-8')
|
279
|
+
assert_true(File.exist?(filename))
|
280
|
+
assert_equal_filecontent(File.join('expected', filename), File.read(filename, :encoding => 'UTF-8'))
|
281
|
+
File.delete(filename)
|
282
|
+
end
|
283
|
+
|
284
|
+
def test_multiple_worksheets()
|
285
|
+
@wb << ws = Excel::Worksheet.new()
|
286
|
+
ws << [:a,:b,:c,:d]
|
287
|
+
assert_raise(ArgumentError, 'csv not possible for multiple worksheets'){ @wb.build_excel_csv }
|
288
|
+
end
|
289
|
+
def test_multiple_worksheets_empty()
|
290
|
+
@wb << ws = Excel::Worksheet.new()
|
291
|
+
assert_nothing_raised(ArgumentError, 'csv possible for multiple worksheets, when only one filled'){ @wb.build_excel_csv }
|
292
|
+
end
|
293
|
+
end #class Test_Workbook_to_csv
|