rexcel 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,28 +1,29 @@
1
+ #encoding: utf-8
1
2
  module Excel
2
3
  =begin rdoc
3
4
  Implements a worksheet.
4
5
 
5
6
  Worksheets are part of a Workbook and contain Row
6
7
  =end
7
- class Worksheet
8
- def initialize( name = nil, options = {})
9
- @log = options[:log] || LOGGER
10
- @name = name || 'Sheet'
11
- #Array with columns sequence. Needed to insert Hashes.
12
- @columns = {}
13
- @rows = []
14
- options.each{|key,value|
15
- case key
16
- when :log
17
- else
18
- @log.warn("Excel::Worksheet: undefined option #{option}")
19
- end
20
- }
21
- end
22
- #Name of the sheet
23
- attr_reader :name
24
- #Array with rows
25
- attr_reader :rows
8
+ class Worksheet
9
+ def initialize( name = nil, options = {})
10
+ @log = options[:log] || LOGGER
11
+ @name = name || 'Sheet'
12
+ #Array with columns sequence. Needed to insert Hashes.
13
+ @columns = {}
14
+ @rows = []
15
+ options.each{|key,value|
16
+ case key
17
+ when :log
18
+ else
19
+ @log.warn("Excel::Worksheet: undefined option #{option}")
20
+ end
21
+ }
22
+ end
23
+ #Name of the sheet
24
+ attr_reader :name
25
+ #Array with rows
26
+ attr_reader :rows
26
27
  =begin rdoc
27
28
  Add content to the worksheet.
28
29
  * Array: Added as Row to the actual worksheet.
@@ -31,80 +32,123 @@ Add content to the worksheet.
31
32
 
32
33
  If no actual worksheet is available, a new worksheet i created.
33
34
  =end
34
- def << (insertion)
35
- case insertion
36
- when Column
37
- @columns[insertion.id] = insertion
38
- when Array
39
- @rows << row = Row.new()
40
- row << insertion
41
- when Hash
42
- raise ArgumentError, "#{Worksheet}: Hash without heading data" if @columns.empty?
43
- @rows << row = Row.new()
44
- #Hashes keep the order of entry. -> no internal order necessary
45
- @columns.each{|id, column|
46
- row << insertion[id]
47
- }
48
- when Row
49
- @rows << insertion
50
- else
51
- raise ArgumentError, "#{Worksheet}: Wrong insertion type #{insertion.inspect}"
35
+ def << (insertion)
36
+ case insertion
37
+ when Column
38
+ @columns[insertion.id] = insertion
39
+ when Array
40
+ @rows << row = Row.new()
41
+ row << insertion
42
+ when Hash
43
+ raise ArgumentError, "#{Worksheet}: Hash without heading data" if @columns.empty?
44
+ @rows << row = Row.new()
45
+ #Hashes keep the order of entry. -> no internal order necessary
46
+ @columns.each{|id, column|
47
+ row << insertion[id]
48
+ }
49
+ when Row
50
+ @rows << insertion
51
+ else
52
+ raise ArgumentError, "#{Worksheet}: Wrong insertion type #{insertion.inspect}"
53
+ end
52
54
  end
53
- end
54
55
  =begin rdoc
55
56
  Add a title line from columns data.
56
57
 
57
58
 
58
59
  =end
59
- def add_title_row(columns = @columns, style = Style.new("Title_row#{object_id}", :bold => true))
60
- #~ raise ArgumentError, "#{Worksheet}##{__method__}: No columns list found" unless columns.respond_to?(:each)
61
- raise ArgumentError, "#{Worksheet}##{__method__}: No columns list found" unless columns.is_a?(Hash)
62
- raise ArgumentError, "#{Worksheet}##{__method__}: Columns list empty" if columns.empty?
63
-
64
- @rows << row = Row.new(:style => style )
65
- #Hashes keep the order of entry. -> no internal order necessary
66
- columns.each{|id, column|
67
- row << Cell.new(column.title)
68
- }
69
- row
70
- end
60
+ def add_title_row(columns = @columns, style = Style.new("Title_row#{object_id}", :bold => true))
61
+ #~ raise ArgumentError, "#{Worksheet}##{__method__}: No columns list found" unless columns.respond_to?(:each)
62
+ raise ArgumentError, "#{Worksheet}##{__method__}: No columns list found" unless columns.is_a?(Hash)
63
+ raise ArgumentError, "#{Worksheet}##{__method__}: Columns list empty" if columns.empty?
64
+
65
+ @rows << row = Row.new(:style => style )
66
+ #Hashes keep the order of entry. -> no internal order necessary
67
+ columns.each{|id, column|
68
+ row << Cell.new(column.title)
69
+ }
70
+ row
71
+ end
71
72
  =begin rdoc
72
73
  Build the xml for the work sheet.
73
74
 
74
75
  ns must be a method-object to implement the namespace definitions.
75
76
  =end
76
- def to_xml(xmlbuilder, ns)
77
- raise EmptyError, "Worksheet #{@name} without content" if @rows.empty?
77
+ def to_xml(xmlbuilder, ns)
78
+ raise EmptyError, "Worksheet #{@name} without content" if @rows.empty?
78
79
 
79
- @log.debug("Prepare XML for worksheet #{@name}")
80
- xmlbuilder[ns.call].Worksheet(ns.call('Name') => @name){
81
- xmlbuilder[ns.call].Table(){
82
- @columns.each{|id,column| column.to_xml(xmlbuilder, ns) }
83
- @rows.each{|row|
84
- row.to_xml(xmlbuilder, ns)
85
- } #row
80
+ @log.debug("Prepare XML for worksheet #{@name}")
81
+ xmlbuilder[ns.call].Worksheet(ns.call('Name') => @name){
82
+ xmlbuilder[ns.call].Table(){
83
+ @columns.each{|id,column| column.to_xml(xmlbuilder, ns) }
84
+ @rows.each{|row|
85
+ row.to_xml(xmlbuilder, ns)
86
+ } #row
87
+ }
86
88
  }
87
- }
88
- end #to_xml
89
+ end #to_xml
89
90
  =begin rdoc
90
91
  Build the xls for the work sheet.
91
92
 
92
93
  Receives a OLE-Worksheet.
93
94
  =end
94
- def to_xls(ws)
95
- @log.debug("Prepare Worksheet #{@name}")
96
- ws.Name = @name
97
-
98
- #fixme: Add styles
99
- #~ @log.warn("XLS contains no styles")
95
+ def to_xls(ws)
96
+ @log.debug("Prepare Worksheet #{@name}")
97
+ ws.Name = @name
98
+
99
+ #fixme: Add styles
100
+ #~ @log.warn("XLS contains no styles")
101
+
102
+ @rows.each_with_index{|row, rownum|
103
+ row.columns.each_with_index{|col, colnum|
104
+ @log.debug("Prepare cell #{rownum+1},#{colnum+1}")
105
+ col.to_xls( ws.Cells(rownum+1, colnum+1), row )
106
+ }#columns in row
107
+ }#rows
108
+ end #to_xls
109
+ =begin rdoc
110
+ Build a csv for the work sheet.
111
+
112
+ The csv is optimized to be used by Excel.
113
+ The field content are converted for this target.
114
+
115
+ Probably the csv expects a German Excel.
116
+ Details see Rexcel::Cell#to_csv.
117
+
118
+
119
+ Options:
120
+ * :sep cell separator. default: ; (as expected from Excel)
121
+ * :filename: Store the data to a file (optional).
122
+ * :encoding: Used for storage in filename. Default: UTF-16LE
123
+
124
+ fixme::
125
+ * :filehandle
126
+ =end
127
+ def to_csv( options = {} )
128
+ @log.debug("Prepare Worksheet #{@name} for csv-output")
129
+ (options.keys - [:sep, :filename, :encoding] ).each{|option|
130
+ @log.warn("Undefined option #{option} in #{self.class}##{__method__}")
131
+ }
132
+ #Set options defaults
133
+ options[:encoding] ||= "UTF-16LE"
134
+ #~ options[:filename] ||= 'test.csv' #no default
100
135
 
101
- @rows.each_with_index{|row, rownum|
102
- row.columns.each_with_index{|col, colnum|
103
- @log.debug("Prepare cell #{rownum+1},#{colnum+1}")
104
- col.to_xls( ws.Cells(rownum+1, colnum+1), row )
105
- }#columns in row
106
- }#rows
107
- end #to_xls
108
-
109
- end #class Worksheet
136
+ csv = []
137
+
138
+ @rows.each_with_index{|row, rownum|
139
+ @log.debug("Prepare row #{rownum} for csv-output") if @log.debug?
140
+ csv << row.to_csv(options)
141
+ }#rows
142
+
143
+ #Save to file if requested
144
+ if options[:filename]
145
+ File.open(options[:filename], 'w', :encoding => options[:encoding] ){|csv_file|
146
+ csv_file << csv.join("\n")
147
+ }
148
+ end #if options[:filename]
149
+
150
+ csv.join("\n")
151
+ end #to_csv
152
+
153
+ end #class Worksheet
110
154
  end #module Excel
@@ -0,0 +1,66 @@
1
+ :title:Create Excel-documents with ruby
2
+ Build Excel documents via win32ole.
3
+
4
+
5
+ This gem was tested with
6
+ * Windows XP + MS Excel 2007 (Version 12)
7
+ * Windows 7 + MS Excel 2007 (Version 12)
8
+
9
+ ==Example 1 (Simple Spreadsheet):
10
+ require 'excel'
11
+
12
+ wb = Excel::Workbook.new()
13
+
14
+ wb << [1,2,3,4,5,6,7]
15
+ wb << [:a, :b, :c, :d, :e, :f]
16
+
17
+ #Save as excel spreadsheets
18
+ wb.save('test.xls') #excel97_2003_format
19
+ wb.save('test.xlsx') #Excel 2007
20
+
21
+ wb.save('test.xml') #Microsoft Office Word 2003 XML Format
22
+ wb.save('test.xls', 'test.xml')#Build xls from xlm
23
+
24
+ ==Example 2 (Access to Worksheets, Rows and Cells):
25
+ require 'excel'
26
+
27
+ wb = Excel::Workbook.new()
28
+
29
+ wb << ws = Excel::Worksheet.new('My Spreadsheet')
30
+ ws << row = Excel::Row.new()
31
+ row << Excel::Cell.new(1)
32
+ row << Excel::Cell.new(2)
33
+ row << Excel::Cell.new(4)
34
+
35
+ #Save as excel spreadsheets
36
+ wb.save('test.xls') #excel97_2003_format
37
+ wb.save('test.xlsx') #Excel 2007
38
+
39
+ wb.save('test.xml') #Microsoft Office Word 2003 XML Format
40
+ wb.save('test.xls', 'test.xml')#Build xls from xlm
41
+
42
+ ==Example 3 (With Format options):
43
+ require 'excel'
44
+
45
+ wb = Excel::Workbook.new()
46
+ wb << style = Excel::Style.new('fett', :bold => true)
47
+
48
+ wb << ws = Excel::Worksheet.new('My Spreadsheet')
49
+ ws << row = Excel::Row.new()
50
+ row << Excel::Cell.new(1)
51
+ row << Excel::Cell.new(2)
52
+ row << Excel::Cell.new(999, :style => style)
53
+
54
+ #Save as excel spreadsheets
55
+ wb.save('test.xls') #excel97_2003_format
56
+ wb.save('test.xlsx') #Excel 2007
57
+
58
+ wb.save('test.xml') #Microsoft Office Word 2003 XML Format
59
+ wb.save('test.xls', 'test.xml')#Build xls from xlm
60
+
61
+
62
+ 'Big' spreadsheets needs a long time to be build.
63
+ (20000 lines need 2 hours)
64
+
65
+ When you build the xml, and then the xls from xlm, the wor is done in seconds.
66
+
@@ -0,0 +1,4 @@
1
+ a;b;c;d
2
+ a;b;c;d
3
+ 1;2;3;4
4
+ ="1.1";="2.2";="3.3";="4.4"
@@ -0,0 +1,4 @@
1
+ a;b;c;d
2
+ a;b;c;d
3
+ 1;2;3;4
4
+ ="1.1";="2.2";="3.3";="4.4"
@@ -0,0 +1,4 @@
1
+ a;b;c;d
2
+ a;b;c;d
3
+ 1;2;3;4
4
+ ="1.1";="2.2";="3.3";="4.4"
@@ -0,0 +1,4 @@
1
+ a;b;c;d
2
+ a;b;c;d
3
+ 1;2;3;4
4
+ ="1.1";="2.2";="3.3";="4.4"
@@ -0,0 +1,91 @@
1
+ #~ gem 'test-unit', ">= 2.1.2" #tested with this version
2
+ gem 'test-unit', ">= 2.1"
3
+ require 'test/unit'
4
+ require "more_unit_test/assert_equal_filecontent.rb" #assert_equal_filecontent
5
+
6
+ $:.unshift('../lib') #if $0 == __FILE__
7
+ require 'rexcel'
8
+ Excel::LOGGER.level = Log4r::OFF #only really important messages
9
+
10
+ #Encoding from OLE/Excel
11
+ XLS_ENCODING = Encoding.find("CP850")
12
+
13
+
14
+ =begin rdoc
15
+ Mock a document.
16
+
17
+ Must defined
18
+ * namespace, including the ns-method
19
+ * xml-frame using and defining the namespace
20
+
21
+ Usage:
22
+ assert_equal(%{},
23
+ @mock.testcase{|builder, ns_proc|
24
+ row.to_xml( builder,ns_proc)
25
+ })
26
+ =end
27
+ class Mock_document
28
+ def initialize()
29
+ @namespace = 'ss'
30
+ end
31
+ def ns(attr=nil)
32
+ attr ? "%s:%s" % [@namespace, attr] : @namespace
33
+ end
34
+ =begin rdoc
35
+ Build relevant xml-part
36
+ =end
37
+ def testcase()
38
+ builder = Nokogiri::XML::Builder.new(){|builder|
39
+ builder.Test( "xmlns:#{@namespace}" =>"urn:schemas-microsoft-com:office:spreadsheet"){|builder|
40
+ yield builder, method(:ns)
41
+ }
42
+ }
43
+
44
+ # builder.to_xml #complete xml
45
+
46
+ #extract content from block
47
+ doc = Nokogiri::XML::Document.parse(builder.to_xml)
48
+ doc.at_css('Test').children.to_xml.strip
49
+ end
50
+ end
51
+
52
+
53
+ =begin rdoc
54
+ Check identic content of xls[x]-files with Beyond Compare
55
+ and returns the returncode
56
+
57
+ RCs:
58
+ 0 Success
59
+ 1 Binary same
60
+ 2 Rules-based same
61
+ 11 Binary differences
62
+ 12 Similar
63
+ 13 Rules-based differences
64
+ 14 Conflicts detected
65
+ 100 Unknown error
66
+ 101 Conflicts detected, merge output not written
67
+ 102 BComp.exe unable to wait until BCompare.exe finishes
68
+ 103 BComp.exe cannot find BCompare.exe
69
+ 104 Trial period expired
70
+ 105 Error loading script file
71
+ 106 Script syntax error
72
+ 107 Script failed to load folders or files
73
+
74
+ Doesn't recognize type differences (eg. 1 == '1' )
75
+ =end
76
+ def compare_with_bc3(file1,file2)
77
+
78
+ bc_exe = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe"
79
+ if ! File.exist?(bc_exe)
80
+ omit "Beyond compare not found - ignore test"
81
+ #Alternative: return 2 #report ok
82
+ end
83
+ cmd = [
84
+ '"%s"' % bc_exe,
85
+ file1, file2,
86
+ '/qc'
87
+ ].join(" ")
88
+
89
+ system(cmd)
90
+ $? >> 8 #exit status
91
+ end
@@ -1,6 +1,5 @@
1
1
  #encoding: UTF-8
2
- require 'test/unit'
3
- require "more_unit_test/assert_equal_filecontent.rb" #assert_equal_filecontent
2
+ require_relative 'rexcel_test_helper'
4
3
 
5
4
  =begin
6
5
  Offen:
@@ -8,93 +7,11 @@ Offen:
8
7
  * Attribute (row/cell)
9
8
  =end
10
9
 
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
10
 
95
11
  class Test_excel < Test::Unit::TestCase
96
12
  def test_version()
97
- assert_equal('0.1.1', Excel::VERSION)
13
+ assert_equal('0.1.2', Excel::VERSION)
14
+ #~ pend "Check version"
98
15
  end
99
16
  end #class Test_excel < Test::Unit::TestCase
100
17
 
@@ -109,211 +26,21 @@ class Test_ole < Test::Unit::TestCase
109
26
  #~ Excel::Excel.instance.reconnect
110
27
  #~ assert_instance_of( WIN32OLE, Excel::Excel.instance.xl )
111
28
  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
29
 
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
-
30
+ def test_system()
31
+ #sinnvoll??
32
+ assert_equal( 'Microsoft Excel', Excel::Excel.instance.xl.Name)
33
+ #Both ok.
34
+ #~ assert_equal( 'Windows (32-bit) NT 5.01', Excel::Excel.instance.xl.OperatingSystem )
35
+ #~ assert_equal( 'Windows (32-bit) NT 6.01', Excel::Excel.instance.xl.OperatingSystem )
36
+ #~ assert_equal( '12', Excel::Excel.instance.xl.version ) #Build number 12 (Office 2007)
37
+ #~ assert_equal( '12.0', Excel::Excel.instance.xl.version ) #Build number 12 (Office 2007)
190
38
  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
39
 
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
40
 
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 << Excel::Column.new( 1 )
286
- ws << Excel::Column.new( 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:Column/>
295
- <ss:Column/>
296
- <ss:Row>
297
- <ss:Cell>
298
- <ss:Data ss:Type="String">a</ss:Data>
299
- </ss:Cell>
300
- <ss:Cell>
301
- <ss:Data ss:Type="String">b</ss:Data>
302
- </ss:Cell>
303
- </ss:Row>
304
- </ss:Table>
305
- </ss:Worksheet>
306
- </ss:Workbook>},
307
- wb.build_excel_xml( 'ss' )
308
- )
41
+ end #class Test_excel < Test::Unit::TestCase
309
42
 
310
- end #def test_insert()
311
- def test_insert_other()
312
- wb = Excel::Workbook.new()
313
- assert_raise(ArgumentError){ wb << 1 }
314
- end
315
43
 
316
- end #class Test_workbook
317
44
 
318
45
  class Test_Columns < Test::Unit::TestCase
319
46
  def test_init
@@ -361,303 +88,6 @@ class Test_Columns_xml < Test::Unit::TestCase
361
88
 
362
89
  end #class Test_Columns_xml < Test::Unit::TestCase
363
90
 
364
- class Test_Worksheet < Test::Unit::TestCase
365
- def test_init
366
- ws = Excel::Worksheet.new()
367
- assert_equal('Sheet', ws.name)
368
- assert_equal([], ws.rows)
369
- #~ assert_equal({}, ws.columns) #only internal.
370
- end #def test_init
371
- def test_add_title_row
372
- ws = Excel::Worksheet.new()
373
- assert_raise(ArgumentError){ ws.add_title_row(:xx) } #no #each
374
- assert_raise(ArgumentError){ ws.add_title_row([]) } #wrong type
375
- assert_raise(ArgumentError){ ws.add_title_row({}) } #empty
376
-
377
- assert_raise(ArgumentError){ ws.add_title_row() }#no columns defined
378
-
379
- title_row = ws.add_title_row({ 1 => Excel::Column.new(1), 2=> Excel::Column.new(2)})
380
- assert_instance_of(Excel::Row, title_row)
381
- assert_equal(2, title_row.columns.size)
382
-
383
- end
384
- end #class Test_Worksheet
385
- class Test_Worksheet_insertions < Test::Unit::TestCase
386
- def setup()
387
- @mock = Mock_document.new
388
- end
389
- def test_insert_array
390
- ws = Excel::Worksheet.new()
391
- assert_equal(0, ws.rows.size)
392
- assert_raise(Excel::EmptyError){
393
- @mock.testcase{|builder, ns_proc|
394
- ws.to_xml( builder,ns_proc)
395
- }}
396
- assert_nothing_raised{ ws << [:a,:b,:c,:d] }
397
- assert_equal(1, ws.rows.size)
398
- assert_equal(%{<ss:Worksheet ss:Name="Sheet">
399
- <ss:Table>
400
- <ss:Row>
401
- <ss:Cell>
402
- <ss:Data ss:Type="String">a</ss:Data>
403
- </ss:Cell>
404
- <ss:Cell>
405
- <ss:Data ss:Type="String">b</ss:Data>
406
- </ss:Cell>
407
- <ss:Cell>
408
- <ss:Data ss:Type="String">c</ss:Data>
409
- </ss:Cell>
410
- <ss:Cell>
411
- <ss:Data ss:Type="String">d</ss:Data>
412
- </ss:Cell>
413
- </ss:Row>
414
- </ss:Table>
415
- </ss:Worksheet>},
416
- @mock.testcase{|builder, ns_proc|
417
- ws.to_xml( builder,ns_proc)
418
- })
419
- assert_nothing_raised{ ws << [:a,:b,:c,:d] }
420
- end
421
- def test_insert_row()
422
- ws = Excel::Worksheet.new()
423
- assert_equal(0, ws.rows.size)
424
- assert_nothing_raised{ ws << ( Excel::Row.new() << 'a') }
425
- assert_equal(1, ws.rows.size)
426
- assert_equal(%{<ss:Worksheet ss:Name="Sheet">
427
- <ss:Table>
428
- <ss:Row>
429
- <ss:Cell>
430
- <ss:Data ss:Type="String">a</ss:Data>
431
- </ss:Cell>
432
- </ss:Row>
433
- </ss:Table>
434
- </ss:Worksheet>},
435
- @mock.testcase{|builder, ns_proc|
436
- ws.to_xml( builder,ns_proc)
437
- })
438
- end
439
- def test_insert_hash()
440
- ws = Excel::Worksheet.new()
441
- assert_equal(0, ws.rows.size)
442
- #Hash requires Heading data
443
- assert_raise( ArgumentError ){ ws << { 1 => :a, 2 => :b, 3 => :c } }
444
- assert_equal(0, ws.rows.size)
445
-
446
- ws << Excel::Column.new(1)
447
- ws << Excel::Column.new(2)
448
-
449
- assert_nothing_raised{ ws << { 1 => :a, 2 => :b, 3 => :c } }
450
- assert_equal(1, ws.rows.size)
451
- assert_equal(%{<ss:Worksheet ss:Name="Sheet">
452
- <ss:Table>
453
- <ss:Column/>
454
- <ss:Column/>
455
- <ss:Row>
456
- <ss:Cell>
457
- <ss:Data ss:Type="String">a</ss:Data>
458
- </ss:Cell>
459
- <ss:Cell>
460
- <ss:Data ss:Type="String">b</ss:Data>
461
- </ss:Cell>
462
- </ss:Row>
463
- </ss:Table>
464
- </ss:Worksheet>},
465
- @mock.testcase{|builder, ns_proc|
466
- ws.to_xml( builder,ns_proc)
467
- })
468
-
469
- end #def test_insert()
470
- def test_insert_other()
471
- ws = Excel::Worksheet.new()
472
- assert_raise(ArgumentError){ ws << 1 }
473
- assert_raise(ArgumentError){ ws << Excel::Worksheet.new() }
474
- end
475
-
476
- end #class Test_Worksheet
477
-
478
- class Test_Row < Test::Unit::TestCase
479
- def test_style()
480
- style = Excel::Style.new('style_example')
481
- assert_equal( nil, Excel::Row.new.style)
482
- assert_instance_of( Excel::Style, Excel::Row.new(:style => style).style)
483
- assert_equal( style, Excel::Row.new(:style => style).style)
484
- end
485
- end
486
- class Test_Row_insertions < Test::Unit::TestCase
487
- def setup()
488
- @mock = Mock_document.new
489
- end
490
- def test_insert_array
491
- row = Excel::Row.new()
492
- assert_equal(0, row.columns.size)
493
- assert_nothing_raised{ row << [:a,:b,:c,:d] }
494
- assert_equal(%{<ss:Row>
495
- <ss:Cell>
496
- <ss:Data ss:Type="String">a</ss:Data>
497
- </ss:Cell>
498
- <ss:Cell>
499
- <ss:Data ss:Type="String">b</ss:Data>
500
- </ss:Cell>
501
- <ss:Cell>
502
- <ss:Data ss:Type="String">c</ss:Data>
503
- </ss:Cell>
504
- <ss:Cell>
505
- <ss:Data ss:Type="String">d</ss:Data>
506
- </ss:Cell>
507
- </ss:Row>},
508
- @mock.testcase{|builder, ns_proc|
509
- row.to_xml( builder,ns_proc)
510
- })
511
- assert_equal(4, row.columns.size)
512
- assert_nothing_raised{ row << [:a,:b,:c,:d] }
513
- end
514
- def test_insert_hash()
515
- row = Excel::Row.new()
516
- assert_equal(0, row.columns.size)
517
- #Hash requires Heading data
518
- assert_raise( ArgumentError ){ row << { 1 => 1, 2 => 2, 3 => 3 } }
519
- assert_equal(0, row.columns.size)
520
- end #def test_insert()
521
- def test_insert_other()
522
- row = Excel::Row.new()
523
- assert_equal(0, row.columns.size)
524
- assert_raise(Excel::EmptyError){
525
- @mock.testcase{|builder, ns_proc|row.to_xml( builder,ns_proc)}
526
- }
527
- assert_nothing_raised{ row << 'a' }
528
- assert_equal(1, row.columns.size)
529
- assert_equal(%{<ss:Row>
530
- <ss:Cell>
531
- <ss:Data ss:Type="String">a</ss:Data>
532
- </ss:Cell>
533
- </ss:Row>},
534
- @mock.testcase{|builder, ns_proc|
535
- row.to_xml( builder,ns_proc)
536
- })
537
- assert_nothing_raised{ row << 'x' }
538
- assert_equal(2, row.columns.size)
539
- assert_equal(%{<ss:Row>
540
- <ss:Cell>
541
- <ss:Data ss:Type="String">a</ss:Data>
542
- </ss:Cell>
543
- <ss:Cell>
544
- <ss:Data ss:Type="String">x</ss:Data>
545
- </ss:Cell>
546
- </ss:Row>},
547
- @mock.testcase{|builder, ns_proc|
548
- row.to_xml( builder,ns_proc)
549
- })
550
- end
551
- def test_insert_errors()
552
- row = Excel::Row.new()
553
- assert_raise(ArgumentError){ row << Excel::Row.new() }
554
- assert_raise(ArgumentError){ row << Excel::Worksheet.new() }
555
- assert_raise(ArgumentError){ row << Excel::Workbook.new() }
556
- end
557
-
558
- #~ def test_bold()
559
- #~ pend("#{self.class}##{__method__} output")
560
- #~ end
561
- #~ def test_italic()
562
- #~ pend("#{self.class}##{__method__} output")
563
- #~ end
564
- #~ def test_color()
565
- #~ pend("#{self.class}##{__method__} output")
566
- #~ end
567
- #~ def test_backgroundcolor()
568
- #~ pend("#{self.class}##{__method__} output")
569
- #~ end
570
-
571
- end #class Test_Row
572
-
573
-
574
- class Test_Cell < Test::Unit::TestCase
575
- def test_create()
576
- assert_nil( Excel::Cell.new().content)
577
- assert_equal( 1, Excel::Cell.new(1).content)
578
- end #def test_create()
579
- def test_style()
580
- style = Excel::Style.new('style_example')
581
- assert_equal( nil, Excel::Cell.new(1).style)
582
- assert_instance_of( Excel::Style, Excel::Cell.new(1, :style => style).style)
583
- assert_equal( style, Excel::Cell.new(1, :style => style).style)
584
- end
585
-
586
- end #class Test_Cell
587
-
588
- class Test_Cell_xml < Test::Unit::TestCase
589
- def setup()
590
- @mock = Mock_document.new
591
- end
592
- def test_content_string
593
- assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"String\">content</ss:Data>\n </ss:Cell>",
594
- @mock.testcase{|builder, ns_proc|
595
- cell = Excel::Cell.new('content')
596
- cell.to_xml( builder,ns_proc)
597
- })
598
- end
599
- def test_content_number
600
- assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">1</ss:Data>\n </ss:Cell>",
601
- @mock.testcase{|builder, ns_proc|
602
- cell = Excel::Cell.new(1)
603
- cell.to_xml( builder,ns_proc)
604
- })
605
- assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">1.1</ss:Data>\n </ss:Cell>",
606
- @mock.testcase{|builder, ns_proc|
607
- cell = Excel::Cell.new(1.1)
608
- cell.to_xml( builder,ns_proc)
609
- })
610
- assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"Number\">281474976710656</ss:Data>\n </ss:Cell>",
611
- @mock.testcase{|builder, ns_proc|
612
- cell = Excel::Cell.new(281474976710656) #Bignum
613
- cell.to_xml( builder,ns_proc)
614
- })
615
- end
616
- def test_content_with_type
617
- assert_equal("<ss:Cell>\n <ss:Data ss:Type=\"String\">1</ss:Data>\n </ss:Cell>",
618
- @mock.testcase{|builder, ns_proc|
619
- cell = Excel::Cell.new(1, :string => true)
620
- cell.to_xml( builder,ns_proc)
621
- })
622
- end #def test_content
623
-
624
- end #class Test_Cell_xlm
625
-
626
- class Test_Cell_xls < Test::Unit::TestCase
627
- def setup()
628
- wb = Excel::Excel.instance.xl.Workbooks.Add #Includes 3 worksheets
629
- @ws = wb.ActiveSheet
630
- end
631
- def test_content_string_xls
632
- cell = Excel::Cell.new('content')
633
- cell_ole = cell.to_xls(@ws.Cells(1,1))
634
- assert_instance_of( WIN32OLE, cell_ole)
635
- assert_equal(XLS_ENCODING, cell_ole.value.encoding)
636
- assert_equal('content', cell_ole.value)
637
- assert_equal('@', cell_ole.NumberFormat)
638
-
639
- end #def test_content_string_xls
640
- def test_content_number
641
- cell_ole = Excel::Cell.new(1).to_xls(@ws.Cells(1,1))
642
- assert_equal(1, cell_ole.value)
643
- assert_equal(XLS_ENCODING, cell_ole.NumberFormat.encoding)
644
- assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
645
-
646
- cell_ole = Excel::Cell.new(1.1).to_xls(@ws.Cells(1,2))
647
- assert_equal(1.1, cell_ole.value)
648
- assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
649
-
650
- #Bignum
651
- cell_ole = Excel::Cell.new(281474976710656).to_xls(@ws.Cells(1,3))
652
- assert_equal(281474976710656, cell_ole.value)
653
- assert_equal("Standard".encode(XLS_ENCODING), cell_ole.NumberFormat)
654
- end
655
- def test_content_with_type
656
- cell_ole = Excel::Cell.new(1, :string => true ).to_xls(@ws.Cells(1,1))
657
- assert_equal('1', cell_ole.value)
658
- assert_equal('@', cell_ole.NumberFormat)
659
- end #def test_content
660
- end #class Test_Cell_xls
661
91
 
662
92
  class Test_Style < Test::Unit::TestCase
663
93
  def setup()