rexcel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ module Excel
2
+ =begin rdoc
3
+ Implements a worksheet.
4
+
5
+ Worksheets are part of a Workbook and contain Row
6
+ =end
7
+ class Worksheet
8
+ def initialize( name = nil, options = {})
9
+ @log = options[:log] || LOGGER
10
+ @name = name || 'Sheet'
11
+ @columns = []
12
+ @rows = []
13
+ options.each{|key,value|
14
+ case key
15
+ when :log
16
+ else
17
+ @log.warn("Excel::Worksheet: undefined option #{option}")
18
+ end
19
+ }
20
+ end
21
+ #Name of the sheet
22
+ attr_reader :name
23
+ #Array with columns sequence. Needed to insert Hashes.
24
+ attr_reader :columns
25
+ #Array with rows
26
+ attr_reader :rows
27
+ =begin rdoc
28
+ Add content to the worksheet.
29
+ * Array: Added as Row to the actual worksheet.
30
+ * Hash: Added as Row to the actual worksheet. Requires the definition of columns
31
+ * Row: Added the actual worksheet.
32
+
33
+ If no actual worksheet is available, a new worksheet i created.
34
+ =end
35
+ def << (insertion)
36
+ case insertion
37
+ when Array
38
+ @rows << row = Row.new()
39
+ row << insertion
40
+ when Hash
41
+ raise ArgumentError, "#{Worksheet}: Hash without heading data" if @columns.empty?
42
+ @rows << row = Row.new()
43
+ @log.fatal("Worksheet: Fixme Columns definition")
44
+ @columns.each{|column|
45
+ row << insertion[column]
46
+ }
47
+ when Row
48
+ @rows << insertion
49
+ else
50
+ raise ArgumentError, "#{Worksheet}: Wrong insertion type #{insertion.inspect}"
51
+ end
52
+ end
53
+ =begin rdoc
54
+ Build the xml for the work sheet.
55
+
56
+ ns must be a method-object to implement the namespace definitions.
57
+ =end
58
+ def to_xml(xmlbuilder, ns)
59
+ raise EmptyError, "Worksheet #{@name} without content" if @rows.empty?
60
+
61
+ @log.debug("Prepare XML for worksheet #{@name}")
62
+ xmlbuilder[ns.call].Worksheet(ns.call('Name') => @name){
63
+ xmlbuilder[ns.call].Table(){
64
+ #~ xmlbuilder[ns.call].Column( ns.call('Width') => 100)
65
+ @rows.each{|row|
66
+ row.to_xml(xmlbuilder, ns)
67
+ } #row
68
+ }
69
+ }
70
+ end #to_xml
71
+ =begin rdoc
72
+ Build the xls for the work sheet.
73
+
74
+ Receives a OLE-Worksheet.
75
+ =end
76
+ def to_xls(ws)
77
+ @log.debug("Prepare Worksheet #{@name}")
78
+ ws.Name = @name
79
+
80
+ #fixme: Add styles
81
+ #~ @log.warn("XLS contains no styles")
82
+
83
+ @rows.each_with_index{|row, rownum|
84
+ row.columns.each_with_index{|col, colnum|
85
+ @log.debug("Prepare cell #{rownum+1},#{colnum+1}")
86
+ col.to_xls( ws.Cells(rownum+1, colnum+1), row )
87
+ }#columns in row
88
+ }#rows
89
+ end #to_xls
90
+
91
+ end #class Worksheet
92
+ end #module Excel
@@ -0,0 +1,34 @@
1
+ <?xml version="1.0"?>
2
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
3
+ <ss:Styles>
4
+ <ss:Style ss:ID="1">
5
+ <ss:Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
6
+ </ss:Style>
7
+ </ss:Styles>
8
+ <ss:Worksheet ss:Name="Sheet">
9
+ <ss:Table>
10
+ <ss:Row>
11
+ <ss:Cell>
12
+ <ss:Data ss:Type="String">Normal text</ss:Data>
13
+ </ss:Cell>
14
+ <ss:Cell ss:StyleID="1">
15
+ <ss:Data ss:Type="String">gray text</ss:Data>
16
+ </ss:Cell>
17
+ </ss:Row>
18
+ <ss:Row ss:StyleID="1">
19
+ <ss:Cell>
20
+ <ss:Data ss:Type="Number">1</ss:Data>
21
+ </ss:Cell>
22
+ <ss:Cell>
23
+ <ss:Data ss:Type="Number">2</ss:Data>
24
+ </ss:Cell>
25
+ <ss:Cell>
26
+ <ss:Data ss:Type="Number">3</ss:Data>
27
+ </ss:Cell>
28
+ <ss:Cell>
29
+ <ss:Data ss:Type="Number">4</ss:Data>
30
+ </ss:Cell>
31
+ </ss:Row>
32
+ </ss:Table>
33
+ </ss:Worksheet>
34
+ </ss:Workbook>
@@ -0,0 +1,34 @@
1
+ <?xml version="1.0"?>
2
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
3
+ <ss:Styles>
4
+ <ss:Style ss:ID="1">
5
+ <ss:Font ss:Bold="1"/>
6
+ </ss:Style>
7
+ </ss:Styles>
8
+ <ss:Worksheet ss:Name="Sheet">
9
+ <ss:Table>
10
+ <ss:Row>
11
+ <ss:Cell>
12
+ <ss:Data ss:Type="String">Normal text</ss:Data>
13
+ </ss:Cell>
14
+ <ss:Cell ss:StyleID="1">
15
+ <ss:Data ss:Type="String">bold text</ss:Data>
16
+ </ss:Cell>
17
+ </ss:Row>
18
+ <ss:Row ss:StyleID="1">
19
+ <ss:Cell>
20
+ <ss:Data ss:Type="Number">1</ss:Data>
21
+ </ss:Cell>
22
+ <ss:Cell>
23
+ <ss:Data ss:Type="Number">2</ss:Data>
24
+ </ss:Cell>
25
+ <ss:Cell>
26
+ <ss:Data ss:Type="Number">3</ss:Data>
27
+ </ss:Cell>
28
+ <ss:Cell>
29
+ <ss:Data ss:Type="Number">4</ss:Data>
30
+ </ss:Cell>
31
+ </ss:Row>
32
+ </ss:Table>
33
+ </ss:Worksheet>
34
+ </ss:Workbook>
@@ -0,0 +1,34 @@
1
+ <?xml version="1.0"?>
2
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
3
+ <ss:Styles>
4
+ <ss:Style ss:ID="1">
5
+ <ss:Font ss:Color="#C0C0C0"/>
6
+ </ss:Style>
7
+ </ss:Styles>
8
+ <ss:Worksheet ss:Name="Sheet">
9
+ <ss:Table>
10
+ <ss:Row>
11
+ <ss:Cell>
12
+ <ss:Data ss:Type="String">Normal text</ss:Data>
13
+ </ss:Cell>
14
+ <ss:Cell ss:StyleID="1">
15
+ <ss:Data ss:Type="String">gray text</ss:Data>
16
+ </ss:Cell>
17
+ </ss:Row>
18
+ <ss:Row ss:StyleID="1">
19
+ <ss:Cell>
20
+ <ss:Data ss:Type="Number">1</ss:Data>
21
+ </ss:Cell>
22
+ <ss:Cell>
23
+ <ss:Data ss:Type="Number">2</ss:Data>
24
+ </ss:Cell>
25
+ <ss:Cell>
26
+ <ss:Data ss:Type="Number">3</ss:Data>
27
+ </ss:Cell>
28
+ <ss:Cell>
29
+ <ss:Data ss:Type="Number">4</ss:Data>
30
+ </ss:Cell>
31
+ </ss:Row>
32
+ </ss:Table>
33
+ </ss:Worksheet>
34
+ </ss:Workbook>
@@ -0,0 +1,34 @@
1
+ <?xml version="1.0"?>
2
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
3
+ <ss:Styles>
4
+ <ss:Style ss:ID="1">
5
+ <ss:Font ss:Italic="1"/>
6
+ </ss:Style>
7
+ </ss:Styles>
8
+ <ss:Worksheet ss:Name="Sheet">
9
+ <ss:Table>
10
+ <ss:Row>
11
+ <ss:Cell>
12
+ <ss:Data ss:Type="String">Normal text</ss:Data>
13
+ </ss:Cell>
14
+ <ss:Cell ss:StyleID="1">
15
+ <ss:Data ss:Type="String">italic text</ss:Data>
16
+ </ss:Cell>
17
+ </ss:Row>
18
+ <ss:Row ss:StyleID="1">
19
+ <ss:Cell>
20
+ <ss:Data ss:Type="Number">1</ss:Data>
21
+ </ss:Cell>
22
+ <ss:Cell>
23
+ <ss:Data ss:Type="Number">2</ss:Data>
24
+ </ss:Cell>
25
+ <ss:Cell>
26
+ <ss:Data ss:Type="Number">3</ss:Data>
27
+ </ss:Cell>
28
+ <ss:Cell>
29
+ <ss:Data ss:Type="Number">4</ss:Data>
30
+ </ss:Cell>
31
+ </ss:Row>
32
+ </ss:Table>
33
+ </ss:Worksheet>
34
+ </ss:Workbook>
@@ -0,0 +1,29 @@
1
+ <?xml version="1.0"?>
2
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
3
+ <ss:Worksheet ss:Name="Sheet0">
4
+ <ss:Table>
5
+ <ss:Row>
6
+ <ss:Cell>
7
+ <ss:Data ss:Type="Number">1</ss:Data>
8
+ </ss:Cell>
9
+ <ss:Cell>
10
+ <ss:Data ss:Type="Number">2</ss:Data>
11
+ </ss:Cell>
12
+ <ss:Cell>
13
+ <ss:Data ss:Type="Number">3</ss:Data>
14
+ </ss:Cell>
15
+ </ss:Row>
16
+ <ss:Row>
17
+ <ss:Cell>
18
+ <ss:Data ss:Type="String">a</ss:Data>
19
+ </ss:Cell>
20
+ <ss:Cell>
21
+ <ss:Data ss:Type="String">b</ss:Data>
22
+ </ss:Cell>
23
+ <ss:Cell>
24
+ <ss:Data ss:Type="String">c</ss:Data>
25
+ </ss:Cell>
26
+ </ss:Row>
27
+ </ss:Table>
28
+ </ss:Worksheet>
29
+ </ss:Workbook>
@@ -0,0 +1,725 @@
1
+ #encoding: UTF-8
2
+ require 'test/unit'
3
+ require "more_unit_test/assert_equal_filecontent.rb" #assert_equal_filecontent
4
+
5
+ =begin
6
+ Offen:
7
+ * Zelltypen
8
+ * Attribute (row/cell)
9
+ =end
10
+
11
+ $:.unshift('../lib') if $0 == __FILE__
12
+ require 'rexcel'
13
+ Excel::LOGGER.level = Log4r::OFF #only really important messages
14
+
15
+ #Encoding from OLE/Excel
16
+ XLS_ENCODING = Encoding.find("CP850")
17
+
18
+ =begin rdoc
19
+ Mock a document.
20
+
21
+ Must defined
22
+ * namespace, including the ns-method
23
+ * xml-frame using and defining the namespace
24
+
25
+ Usage:
26
+ assert_equal(%{},
27
+ @mock.testcase{|builder, ns_proc|
28
+ row.to_xml( builder,ns_proc)
29
+ })
30
+ =end
31
+ class Mock_document
32
+ def initialize()
33
+ @namespace = 'ss'
34
+ end
35
+ def ns(attr=nil)
36
+ attr ? "%s:%s" % [@namespace, attr] : @namespace
37
+ end
38
+ =begin rdoc
39
+ Build relevant xml-part
40
+ =end
41
+ def testcase()
42
+ builder = Nokogiri::XML::Builder.new(){|builder|
43
+ builder.Test( "xmlns:#{@namespace}" =>"urn:schemas-microsoft-com:office:spreadsheet"){|builder|
44
+ yield builder, method(:ns)
45
+ }
46
+ }
47
+
48
+ # builder.to_xml #complete xml
49
+
50
+ #extract content from block
51
+ doc = Nokogiri::XML::Document.parse(builder.to_xml)
52
+ doc.at_css('Test').children.to_xml.strip
53
+ end
54
+ end
55
+ =begin rdoc
56
+ CHeck identic content of xls[x]-files with Beyond Compare.
57
+
58
+ RCs:
59
+ 0 Success
60
+ 1 Binary same
61
+ 2 Rules-based same
62
+ 11 Binary differences
63
+ 12 Similar
64
+ 13 Rules-based differences
65
+ 14 Conflicts detected
66
+ 100 Unknown error
67
+ 101 Conflicts detected, merge output not written
68
+ 102 BComp.exe unable to wait until BCompare.exe finishes
69
+ 103 BComp.exe cannot find BCompare.exe
70
+ 104 Trial period expired
71
+ 105 Error loading script file
72
+ 106 Script syntax error
73
+ 107 Script failed to load folders or files
74
+
75
+ Doesn't recognice type differences (eg. 1 == '1' )
76
+ =end
77
+
78
+ def test_bc(file1,file2)
79
+
80
+ bc_exe = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe"
81
+ if ! File.exist?(bc_exe)
82
+ puts "Beyond compare not found - ignore test"
83
+ return 2
84
+ end
85
+ cmd = [
86
+ '"%s"' % bc_exe,
87
+ file1, file2,
88
+ '/qc'
89
+ ].join(" ")
90
+
91
+ system(cmd)
92
+ $? >> 8 #exit status
93
+ end
94
+
95
+ class Test_excel < Test::Unit::TestCase
96
+ def test_version()
97
+ assert_equal('0.1.0', Excel::VERSION)
98
+ end
99
+ end #class Test_excel < Test::Unit::TestCase
100
+
101
+ class Test_ole < Test::Unit::TestCase
102
+ def test_singleton()
103
+
104
+ assert_instance_of( Excel::Excel, Excel::Excel.instance )
105
+ assert_equal( Excel::Excel.instance.object_id, Excel::Excel.instance.object_id )
106
+ assert_instance_of( WIN32OLE, Excel::Excel.instance.xl )
107
+ #~ Excel::Excel.instance.close
108
+ #~ assert_nil(Excel::Excel.instance.xl )
109
+ #~ Excel::Excel.instance.reconnect
110
+ #~ assert_instance_of( WIN32OLE, Excel::Excel.instance.xl )
111
+ end
112
+
113
+
114
+ end #class Test_excel < Test::Unit::TestCase
115
+
116
+
117
+ class Test_workbook < Test::Unit::TestCase
118
+ FILELIST = %w{
119
+ test_save_xml.xml test_save_xml.xls test_save_xml.xlsx
120
+ test_save.xls test_save.xlsx}
121
+ def setup()
122
+ FILELIST.each{|fn|
123
+ File.delete(fn) if File.exist?(fn)
124
+ }
125
+ end
126
+ def teardown()
127
+ FILELIST.each{|fn|
128
+ File.delete(fn) if File.exist?(fn)
129
+ }
130
+ end
131
+ def test_init()
132
+ assert_nothing_raised{ Excel::Workbook.new() }
133
+ assert_nothing_raised{ Excel::Workbook.new( Excel::LOGGER) }
134
+ assert_raise(ArgumentError){ Excel::Workbook.new( 1 ) }
135
+ end
136
+
137
+ def test_save_xml()
138
+
139
+ assert_false( File.exist?('test_save_xml.xml'))
140
+ assert_false( File.exist?('test_save_xml.xls'))
141
+ assert_false( File.exist?('test_save_xml.xlsx'))
142
+
143
+ wb = Excel::Workbook.new()
144
+ wb << [1,2,3] #add anything, else you get an error
145
+ wb << ['a','b','c'] #add anything, else you get an error
146
+ assert_nothing_raised{ wb.save('test_save_xml.xml') }
147
+ assert_nothing_raised{ wb.save('test_save_xml.xls', 'test_save_xml.xml')}
148
+ assert_nothing_raised{ wb.save('test_save_xml.xlsx', 'test_save_xml.xml')}
149
+
150
+ assert_true( File.exist?('test_save_xml.xml'))
151
+ assert_true( File.exist?('test_save_xml.xls'))
152
+ assert_true( File.exist?('test_save_xml.xlsx'))
153
+
154
+
155
+ #~ assert_equal_filecontent( "expected/%s-%s.xml" %[self.class, __method__],
156
+ #~ wb.build_excel_xml('ss') )
157
+ assert_equal( %{<?xml version="1.0"?>
158
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
159
+ <ss:Worksheet ss:Name="Sheet0">
160
+ <ss:Table>
161
+ <ss:Row>
162
+ <ss:Cell>
163
+ <ss:Data ss:Type="Number">1</ss:Data>
164
+ </ss:Cell>
165
+ <ss:Cell>
166
+ <ss:Data ss:Type="Number">2</ss:Data>
167
+ </ss:Cell>
168
+ <ss:Cell>
169
+ <ss:Data ss:Type="Number">3</ss:Data>
170
+ </ss:Cell>
171
+ </ss:Row>
172
+ <ss:Row>
173
+ <ss:Cell>
174
+ <ss:Data ss:Type="String">a</ss:Data>
175
+ </ss:Cell>
176
+ <ss:Cell>
177
+ <ss:Data ss:Type="String">b</ss:Data>
178
+ </ss:Cell>
179
+ <ss:Cell>
180
+ <ss:Data ss:Type="String">c</ss:Data>
181
+ </ss:Cell>
182
+ </ss:Row>
183
+ </ss:Table>
184
+ </ss:Worksheet>
185
+ </ss:Workbook>},
186
+ wb.build_excel_xml('ss') )
187
+
188
+ assert_equal( 2, test_bc('test_save_xml.xls', 'test_save_xml.xlsx'))
189
+
190
+ end
191
+
192
+ def test_save_xls()
193
+
194
+ assert_false( File.exist?('test_save.xls'))
195
+ assert_false( File.exist?('test_save.xlsx'))
196
+
197
+ wb = Excel::Workbook.new()
198
+ wb << [ 1,2,3,4]
199
+ wb << [ :a, :b, :c ]
200
+ assert_nothing_raised{ wb.save('test_save.xls')}
201
+ assert_nothing_raised{ wb.save('test_save.xlsx')}
202
+
203
+ assert_true( File.exist?('test_save.xls'))
204
+ assert_true( File.exist?('test_save.xlsx'))
205
+
206
+ assert_equal( 2, test_bc('test_save.xls', 'test_save.xlsx'))
207
+
208
+ assert_false( File.exist?('test_save.xml'))
209
+ assert_false( File.exist?('test_save_xml.xls'))
210
+ assert_nothing_raised{ wb.save('test_save_xml.xml')}
211
+ assert_nothing_raised{ wb.save('test_save_xml.xlsx', 'test_save_xml.xml')}
212
+ assert_true( File.exist?('test_save_xml.xml'))
213
+ assert_true( File.exist?('test_save_xml.xlsx'))
214
+ assert_equal( 2, test_bc('test_save.xlsx', 'test_save_xml.xlsx'))
215
+
216
+ end #def test_create_wb()
217
+
218
+ def test_save_other()
219
+ wb = Excel::Workbook.new()
220
+ assert_raise( ArgumentError ){wb.save('test_save_xml.doc')}
221
+ end
222
+ end #class Test_workbook
223
+
224
+ class Test_workbook_insertions < Test::Unit::TestCase
225
+ def test_insert_worksheet()
226
+ wb = Excel::Workbook.new()
227
+ assert_equal(0, wb.worksheets.size)
228
+
229
+ assert_nothing_raised{ wb << Excel::Worksheet.new() }
230
+ assert_equal(1, wb.worksheets.size)
231
+ assert_equal('Sheet', wb.worksheets.last.name)
232
+
233
+ assert_nothing_raised{ wb << Excel::Worksheet.new('Sheet for test') }
234
+ assert_equal(2, wb.worksheets.size)
235
+ assert_equal('Sheet for test', wb.worksheets.last.name)
236
+ end
237
+ def test_insert_array
238
+ wb = Excel::Workbook.new()
239
+ assert_equal(0, wb.worksheets.size)
240
+ assert_nothing_raised{ wb << [:a,:b,:c,:d] }
241
+ assert_equal(1, wb.worksheets.size)
242
+ assert_equal(%{<?xml version="1.0"?>
243
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
244
+ <ss:Worksheet ss:Name="Sheet0">
245
+ <ss:Table>
246
+ <ss:Row>
247
+ <ss:Cell>
248
+ <ss:Data ss:Type="String">a</ss:Data>
249
+ </ss:Cell>
250
+ <ss:Cell>
251
+ <ss:Data ss:Type="String">b</ss:Data>
252
+ </ss:Cell>
253
+ <ss:Cell>
254
+ <ss:Data ss:Type="String">c</ss:Data>
255
+ </ss:Cell>
256
+ <ss:Cell>
257
+ <ss:Data ss:Type="String">d</ss:Data>
258
+ </ss:Cell>
259
+ </ss:Row>
260
+ </ss:Table>
261
+ </ss:Worksheet>
262
+ </ss:Workbook>},
263
+ wb.build_excel_xml( 'ss' )
264
+ )
265
+ assert_nothing_raised{ wb << [:a,:b,:c,:d] }
266
+ end
267
+ def test_insert_row()
268
+ wb = Excel::Workbook.new()
269
+ assert_equal(0, wb.worksheets.size)
270
+ assert_nothing_raised{ wb << Excel::Row.new() }
271
+ assert_equal(1, wb.worksheets.size)
272
+ #Exception for empty row
273
+ assert_raise( Excel::EmptyError ){ wb.build_excel_xml( 'ns' ) }
274
+ end
275
+ def test_insert_hash()
276
+ wb = Excel::Workbook.new()
277
+ assert_equal(0, wb.worksheets.size)
278
+ assert_equal(nil, wb.active_worksheet)
279
+ #Hash requires Heading data
280
+ assert_raise( ArgumentError ){ wb << { 1 => :a, 2 => :b, 3 => :c } }
281
+ assert_equal(1, wb.worksheets.size)
282
+ assert_instance_of(Excel::Worksheet, wb.active_worksheet)
283
+
284
+ ws = wb.active_worksheet
285
+ ws.columns << 1
286
+ ws.columns << 2
287
+
288
+ assert_nothing_raised{ wb << { 1 => :a, 2 => :b, 3 => :c } }
289
+ assert_equal(1, wb.worksheets.size)#no new worksheet created
290
+ assert_equal(%{<?xml version="1.0"?>
291
+ <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
292
+ <ss:Worksheet ss:Name="Sheet0">
293
+ <ss:Table>
294
+ <ss:Row>
295
+ <ss:Cell>
296
+ <ss:Data ss:Type="String">a</ss:Data>
297
+ </ss:Cell>
298
+ <ss:Cell>
299
+ <ss:Data ss:Type="String">b</ss:Data>
300
+ </ss:Cell>
301
+ </ss:Row>
302
+ </ss:Table>
303
+ </ss:Worksheet>
304
+ </ss:Workbook>},
305
+ wb.build_excel_xml( 'ss' )
306
+ )
307
+
308
+ end #def test_insert()
309
+ def test_insert_other()
310
+ wb = Excel::Workbook.new()
311
+ assert_raise(ArgumentError){ wb << 1 }
312
+ end
313
+
314
+ end #class Test_workbook
315
+
316
+ class Test_Worksheet_insertions < Test::Unit::TestCase
317
+ def setup()
318
+ @mock = Mock_document.new
319
+ end
320
+ def test_insert_array
321
+ ws = Excel::Worksheet.new()
322
+ assert_equal(0, ws.rows.size)
323
+ assert_raise(Excel::EmptyError){
324
+ @mock.testcase{|builder, ns_proc|
325
+ ws.to_xml( builder,ns_proc)
326
+ }}
327
+ assert_nothing_raised{ ws << [:a,:b,:c,:d] }
328
+ assert_equal(1, ws.rows.size)
329
+ assert_equal(%{<ss:Worksheet ss:Name="Sheet">
330
+ <ss:Table>
331
+ <ss:Row>
332
+ <ss:Cell>
333
+ <ss:Data ss:Type="String">a</ss:Data>
334
+ </ss:Cell>
335
+ <ss:Cell>
336
+ <ss:Data ss:Type="String">b</ss:Data>
337
+ </ss:Cell>
338
+ <ss:Cell>
339
+ <ss:Data ss:Type="String">c</ss:Data>
340
+ </ss:Cell>
341
+ <ss:Cell>
342
+ <ss:Data ss:Type="String">d</ss:Data>
343
+ </ss:Cell>
344
+ </ss:Row>
345
+ </ss:Table>
346
+ </ss:Worksheet>},
347
+ @mock.testcase{|builder, ns_proc|
348
+ ws.to_xml( builder,ns_proc)
349
+ })
350
+ assert_nothing_raised{ ws << [:a,:b,:c,:d] }
351
+ end
352
+ def test_insert_row()
353
+ ws = Excel::Worksheet.new()
354
+ assert_equal(0, ws.rows.size)
355
+ assert_nothing_raised{ ws << ( Excel::Row.new() << 'a') }
356
+ assert_equal(1, ws.rows.size)
357
+ assert_equal(%{<ss:Worksheet ss:Name="Sheet">
358
+ <ss:Table>
359
+ <ss:Row>
360
+ <ss:Cell>
361
+ <ss:Data ss:Type="String">a</ss:Data>
362
+ </ss:Cell>
363
+ </ss:Row>
364
+ </ss:Table>
365
+ </ss:Worksheet>},
366
+ @mock.testcase{|builder, ns_proc|
367
+ ws.to_xml( builder,ns_proc)
368
+ })
369
+ end
370
+ def test_insert_hash()
371
+ ws = Excel::Worksheet.new()
372
+ assert_equal(0, ws.rows.size)
373
+ #Hash requires Heading data
374
+ assert_raise( ArgumentError ){ ws << { 1 => :a, 2 => :b, 3 => :c } }
375
+ assert_equal(0, ws.rows.size)
376
+
377
+ ws.columns << 1
378
+ ws.columns << 2
379
+
380
+ assert_nothing_raised{ ws << { 1 => :a, 2 => :b, 3 => :c } }
381
+ assert_equal(1, ws.rows.size)
382
+ assert_equal(%{<ss:Worksheet ss:Name="Sheet">
383
+ <ss:Table>
384
+ <ss:Row>
385
+ <ss:Cell>
386
+ <ss:Data ss:Type="String">a</ss:Data>
387
+ </ss:Cell>
388
+ <ss:Cell>
389
+ <ss:Data ss:Type="String">b</ss:Data>
390
+ </ss:Cell>
391
+ </ss:Row>
392
+ </ss:Table>
393
+ </ss:Worksheet>},
394
+ @mock.testcase{|builder, ns_proc|
395
+ ws.to_xml( builder,ns_proc)
396
+ })
397
+
398
+ end #def test_insert()
399
+ def test_insert_other()
400
+ ws = Excel::Worksheet.new()
401
+ assert_raise(ArgumentError){ ws << 1 }
402
+ assert_raise(ArgumentError){ ws << Excel::Worksheet.new() }
403
+ end
404
+
405
+ end #class Test_Worksheet
406
+
407
+ class Test_Row < Test::Unit::TestCase
408
+ def test_style()
409
+ style = Excel::Style.new('style_example')
410
+ assert_equal( nil, Excel::Row.new.style)
411
+ assert_instance_of( Excel::Style, Excel::Row.new(:style => style).style)
412
+ assert_equal( style, Excel::Row.new(:style => style).style)
413
+ end
414
+ end
415
+ class Test_Row_insertions < Test::Unit::TestCase
416
+ def setup()
417
+ @mock = Mock_document.new
418
+ end
419
+ def test_insert_array
420
+ row = Excel::Row.new()
421
+ assert_equal(0, row.columns.size)
422
+ assert_nothing_raised{ row << [:a,:b,:c,:d] }
423
+ assert_equal(%{<ss:Row>
424
+ <ss:Cell>
425
+ <ss:Data ss:Type="String">a</ss:Data>
426
+ </ss:Cell>
427
+ <ss:Cell>
428
+ <ss:Data ss:Type="String">b</ss:Data>
429
+ </ss:Cell>
430
+ <ss:Cell>
431
+ <ss:Data ss:Type="String">c</ss:Data>
432
+ </ss:Cell>
433
+ <ss:Cell>
434
+ <ss:Data ss:Type="String">d</ss:Data>
435
+ </ss:Cell>
436
+ </ss:Row>},
437
+ @mock.testcase{|builder, ns_proc|
438
+ row.to_xml( builder,ns_proc)
439
+ })
440
+ assert_equal(4, row.columns.size)
441
+ assert_nothing_raised{ row << [:a,:b,:c,:d] }
442
+ end
443
+ def test_insert_hash()
444
+ row = Excel::Row.new()
445
+ assert_equal(0, row.columns.size)
446
+ #Hash requires Heading data
447
+ assert_raise( ArgumentError ){ row << { 1 => 1, 2 => 2, 3 => 3 } }
448
+ assert_equal(0, row.columns.size)
449
+ end #def test_insert()
450
+ def test_insert_other()
451
+ row = Excel::Row.new()
452
+ assert_equal(0, row.columns.size)
453
+ assert_raise(Excel::EmptyError){
454
+ @mock.testcase{|builder, ns_proc|row.to_xml( builder,ns_proc)}
455
+ }
456
+ assert_nothing_raised{ row << 'a' }
457
+ assert_equal(1, row.columns.size)
458
+ assert_equal(%{<ss:Row>
459
+ <ss:Cell>
460
+ <ss:Data ss:Type="String">a</ss:Data>
461
+ </ss:Cell>
462
+ </ss:Row>},
463
+ @mock.testcase{|builder, ns_proc|
464
+ row.to_xml( builder,ns_proc)
465
+ })
466
+ assert_nothing_raised{ row << 'x' }
467
+ assert_equal(2, row.columns.size)
468
+ assert_equal(%{<ss:Row>
469
+ <ss:Cell>
470
+ <ss:Data ss:Type="String">a</ss:Data>
471
+ </ss:Cell>
472
+ <ss:Cell>
473
+ <ss:Data ss:Type="String">x</ss:Data>
474
+ </ss:Cell>
475
+ </ss:Row>},
476
+ @mock.testcase{|builder, ns_proc|
477
+ row.to_xml( builder,ns_proc)
478
+ })
479
+ end
480
+ def test_insert_errors()
481
+ row = Excel::Row.new()
482
+ assert_raise(ArgumentError){ row << Excel::Row.new() }
483
+ assert_raise(ArgumentError){ row << Excel::Worksheet.new() }
484
+ assert_raise(ArgumentError){ row << Excel::Workbook.new() }
485
+ end
486
+
487
+ #~ def test_bold()
488
+ #~ pend("#{self.class}##{__method__} output")
489
+ #~ end
490
+ #~ def test_italic()
491
+ #~ pend("#{self.class}##{__method__} output")
492
+ #~ end
493
+ #~ def test_color()
494
+ #~ pend("#{self.class}##{__method__} output")
495
+ #~ end
496
+ #~ def test_backgroundcolor()
497
+ #~ pend("#{self.class}##{__method__} output")
498
+ #~ end
499
+
500
+ end #class Test_Row
501
+
502
+
503
+ class Test_Cell < Test::Unit::TestCase
504
+ def test_create()
505
+ assert_nil( Excel::Cell.new().content)
506
+ assert_equal( 1, Excel::Cell.new(1).content)
507
+ end #def test_create()
508
+ def test_style()
509
+ style = Excel::Style.new('style_example')
510
+ assert_equal( nil, Excel::Cell.new(1).style)
511
+ assert_instance_of( Excel::Style, Excel::Cell.new(1, :style => style).style)
512
+ assert_equal( style, Excel::Cell.new(1, :style => style).style)
513
+ end
514
+
515
+ end #class Test_Cell
516
+
517
+ class Test_Cell_xml < Test::Unit::TestCase
518
+ def setup()
519
+ @mock = Mock_document.new
520
+ end
521
+ def test_content_string
522
+ assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"String\">content</ss:Data>\n </ss:Cell>",
523
+ @mock.testcase{|builder, ns_proc|
524
+ cell = Excel::Cell.new('content')
525
+ cell.to_xml( builder,ns_proc)
526
+ })
527
+ end
528
+ def test_content_number
529
+ assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">1</ss:Data>\n </ss:Cell>",
530
+ @mock.testcase{|builder, ns_proc|
531
+ cell = Excel::Cell.new(1)
532
+ cell.to_xml( builder,ns_proc)
533
+ })
534
+ assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">1.1</ss:Data>\n </ss:Cell>",
535
+ @mock.testcase{|builder, ns_proc|
536
+ cell = Excel::Cell.new(1.1)
537
+ cell.to_xml( builder,ns_proc)
538
+ })
539
+ assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">281474976710656</ss:Data>\n </ss:Cell>",
540
+ @mock.testcase{|builder, ns_proc|
541
+ cell = Excel::Cell.new(281474976710656) #Bignum
542
+ cell.to_xml( builder,ns_proc)
543
+ })
544
+ end
545
+ def test_content_with_type
546
+ assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"String\">1</ss:Data>\n </ss:Cell>",
547
+ @mock.testcase{|builder, ns_proc|
548
+ cell = Excel::Cell.new(1, :string => true)
549
+ cell.to_xml( builder,ns_proc)
550
+ })
551
+ end #def test_content
552
+
553
+ end #class Test_Cell_xlm
554
+
555
+ class Test_Cell_xls < Test::Unit::TestCase
556
+ def setup()
557
+ wb = Excel::Excel.instance.xl.Workbooks.Add #Includes 3 worksheets
558
+ @ws = wb.ActiveSheet
559
+ end
560
+ def test_content_string_xls
561
+ cell = Excel::Cell.new('content')
562
+ cell_ole = cell.to_xls(@ws.Cells(1,1))
563
+ assert_instance_of( WIN32OLE, cell_ole)
564
+ assert_equal(XLS_ENCODING, cell_ole.value.encoding)
565
+ assert_equal('content', cell_ole.value)
566
+ assert_equal('@', cell_ole.NumberFormat)
567
+
568
+ end #def test_content_string_xls
569
+ def test_content_number
570
+ cell_ole = Excel::Cell.new(1).to_xls(@ws.Cells(1,1))
571
+ assert_equal(1, cell_ole.value)
572
+ assert_equal(XLS_ENCODING, cell_ole.NumberFormat.encoding)
573
+ assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
574
+
575
+ cell_ole = Excel::Cell.new(1.1).to_xls(@ws.Cells(1,2))
576
+ assert_equal(1.1, cell_ole.value)
577
+ assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
578
+
579
+ #Bignum
580
+ cell_ole = Excel::Cell.new(281474976710656).to_xls(@ws.Cells(1,3))
581
+ assert_equal(281474976710656, cell_ole.value)
582
+ assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
583
+ end
584
+ def test_content_with_type
585
+ cell_ole = Excel::Cell.new(1, :string => true ).to_xls(@ws.Cells(1,1))
586
+ assert_equal('1', cell_ole.value)
587
+ assert_equal('@', cell_ole.NumberFormat)
588
+ end #def test_content
589
+ end #class Test_Cell_xls
590
+
591
+ class Test_Style < Test::Unit::TestCase
592
+ def setup()
593
+ #~ wb = Excel::Excel.instance.xl.Workbooks.Add #Includes 3 worksheets
594
+ #~ @ws = wb.ActiveSheet
595
+ end
596
+ def test_create()
597
+ style = Excel::Style.new('style1')
598
+ assert_instance_of( Excel::Style, style)
599
+ assert_equal('style1', style.name)
600
+ #Assignment after usage in workbook
601
+ assert_equal(nil, style.style_id)
602
+
603
+ #Detect duplicate styles
604
+ #~ assert_raise(ArgumentError){ Excel::Style.new('style1') }
605
+
606
+ style2 = Excel::Style.new('style2')
607
+ assert_instance_of( Excel::Style, style2)
608
+ assert_equal('style2', style2.name)
609
+ assert_equal(nil, style2.style_id)
610
+ end
611
+ def test_insert()
612
+ wb = Excel::Workbook.new()
613
+ style = Excel::Style.new('style1')
614
+
615
+ assert_equal(0, wb.styles.size)
616
+ assert_nothing_raised{ wb << style }
617
+ assert_equal(1, wb.styles.size)
618
+ assert_equal(1, style.style_id)
619
+ end
620
+
621
+ def test_insert_ws()
622
+ ws = Excel::Worksheet.new
623
+ style = Excel::Style.new('style1')
624
+ #Worksheets don't accept Styles
625
+ assert_raise(ArgumentError){ ws << style }
626
+ end
627
+ end
628
+
629
+
630
+ class Test_Style_xlm < Test::Unit::TestCase
631
+ def setup()
632
+ @wb = Excel::Workbook.new
633
+ @wb << @ws = Excel::Worksheet.new
634
+ @ws << @row = Excel::Row.new
635
+ @row << cell = Excel::Cell.new('Normal text')
636
+ end
637
+ def test_bold()
638
+
639
+ #~ assert_equal_filecontent( "expected/%s-%s.xml" %[self.class, __method__],
640
+ #~ wb.build_excel_xml('ss') )
641
+
642
+ @wb << style = Excel::Style.new('bold', :bold => true )
643
+ assert_true( style.bold )
644
+ assert_nil( style.italic )
645
+ assert_nil( style.color )
646
+ assert_nil( style.backgroundcolor )
647
+
648
+ #add a formated cell
649
+ @row << Excel::Cell.new('bold text', :style => style)
650
+ #And a additional row
651
+ @ws << ( Excel::Row.new(:style => style) << [1,2,3,4] )
652
+ assert_equal_filecontent( "expected/%s-%s-bold.xml" %[self.class, __method__],
653
+ @wb.build_excel_xml('ss') )
654
+ #~ @wb.save("expected/%s-%s-bold.xls" %[self.class, __method__])
655
+ end
656
+ def test_italic()
657
+
658
+ #~ assert_equal_filecontent( "expected/%s-%s.xml" %[self.class, __method__],
659
+ #~ wb.build_excel_xml('ss') )
660
+
661
+ @wb << style = Excel::Style.new('italic', :italic => true )
662
+ assert_nil( style.bold )
663
+ assert_true( style.italic )
664
+ assert_nil( style.color )
665
+ assert_nil( style.backgroundcolor )
666
+
667
+ #add a formated cell
668
+ @row << Excel::Cell.new('italic text', :style => style)
669
+
670
+ #And a additional row
671
+ @ws << ( Excel::Row.new(:style => style) << [1,2,3,4] )
672
+
673
+ assert_equal_filecontent( "expected/%s-%s-italic.xml" %[self.class, __method__],
674
+ @wb.build_excel_xml('ss') )
675
+ end
676
+ def test_color()
677
+ @wb << style = Excel::Style.new('gray', :color => 15 )
678
+ assert_nil( style.bold )
679
+ assert_nil( style.italic )
680
+ assert_equal(15, style.color )
681
+ assert_nil( style.backgroundcolor )
682
+
683
+ @row << Excel::Cell.new('gray text', :style => style)
684
+
685
+ #And a additional row
686
+ @ws << ( Excel::Row.new(:style => style) << [1,2,3,4] )
687
+
688
+ assert_equal_filecontent( "expected/%s-%s-gray.xml" %[self.class, __method__],
689
+ @wb.build_excel_xml('ss') )
690
+
691
+ end
692
+ def test_backgroundcolor()
693
+ @wb << style = Excel::Style.new('gray', :backgroundcolor => 15 )
694
+ assert_nil( style.bold )
695
+ assert_nil( style.italic )
696
+ assert_equal(15, style.backgroundcolor )
697
+ assert_nil( style.color )
698
+
699
+ @row << Excel::Cell.new('gray text', :style => style)
700
+
701
+ #And a additional row
702
+ @ws << ( Excel::Row.new(:style => style) << [1,2,3,4] )
703
+
704
+ assert_equal_filecontent( "expected/%s-%s-gray.xml" %[self.class, __method__],
705
+ @wb.build_excel_xml('ss') )
706
+
707
+ end
708
+ end
709
+
710
+ #~ END{
711
+ #~ Excel::Excel.instance.close
712
+ #~ }
713
+
714
+ __END__
715
+
716
+ #~ build_xml('test_save_xml.xml')
717
+
718
+ #~ mk_excel(File.join(Dir.pwd, "test_xml.xlsx"), File.join(Dir.pwd, 'test_save_xml.xml'))
719
+ xls = ExcelXML.new
720
+ #~ xls.save_xml("test_xml.xml")
721
+ File.delete("test_xml.xml") if File.exist?("test_xml.xml")
722
+ xls.mk_excel("test_xml.xlsx")
723
+ #~ xls.mk_excel("test_xml.xlsx", 'example_excel_xml.xml')
724
+
725
+ #~ mk_excel("./test2.xls", data) #Speichert in Eigene Dateien