WriteExcel 0.2.0

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.
Files changed (80) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +17 -0
  5. data/Rakefile +47 -0
  6. data/VERSION +1 -0
  7. data/examples/a_simple.rb +42 -0
  8. data/examples/autofilters.rb +266 -0
  9. data/examples/bigfile.rb +30 -0
  10. data/examples/copyformat.rb +51 -0
  11. data/examples/data_validate.rb +278 -0
  12. data/examples/date_time.rb +86 -0
  13. data/examples/demo.rb +118 -0
  14. data/examples/diag_border.rb +35 -0
  15. data/examples/formats.rb +489 -0
  16. data/examples/header.rb +136 -0
  17. data/examples/hidden.rb +28 -0
  18. data/examples/hyperlink.rb +42 -0
  19. data/examples/images.rb +52 -0
  20. data/examples/merge1.rb +39 -0
  21. data/examples/merge2.rb +44 -0
  22. data/examples/merge3.rb +65 -0
  23. data/examples/merge4.rb +82 -0
  24. data/examples/merge5.rb +79 -0
  25. data/examples/protection.rb +46 -0
  26. data/examples/regions.rb +52 -0
  27. data/examples/repeat.rb +42 -0
  28. data/examples/stats.rb +75 -0
  29. data/examples/stocks.rb +80 -0
  30. data/examples/tab_colors.rb +30 -0
  31. data/lib/WriteExcel.rb +30 -0
  32. data/lib/WriteExcel/biffwriter.rb +259 -0
  33. data/lib/WriteExcel/chart.rb +217 -0
  34. data/lib/WriteExcel/excelformula.y +138 -0
  35. data/lib/WriteExcel/excelformulaparser.rb +573 -0
  36. data/lib/WriteExcel/format.rb +1108 -0
  37. data/lib/WriteExcel/formula.rb +986 -0
  38. data/lib/WriteExcel/olewriter.rb +322 -0
  39. data/lib/WriteExcel/properties.rb +250 -0
  40. data/lib/WriteExcel/storage_lite.rb +590 -0
  41. data/lib/WriteExcel/workbook.rb +2602 -0
  42. data/lib/WriteExcel/worksheet.rb +6378 -0
  43. data/spec/WriteExcel_spec.rb +7 -0
  44. data/spec/spec.opts +1 -0
  45. data/spec/spec_helper.rb +9 -0
  46. data/test/tc_all.rb +31 -0
  47. data/test/tc_biff.rb +104 -0
  48. data/test/tc_chart.rb +22 -0
  49. data/test/tc_example_match.rb +1280 -0
  50. data/test/tc_format.rb +1264 -0
  51. data/test/tc_formula.rb +63 -0
  52. data/test/tc_ole.rb +110 -0
  53. data/test/tc_storage_lite.rb +102 -0
  54. data/test/tc_workbook.rb +115 -0
  55. data/test/tc_worksheet.rb +115 -0
  56. data/test/test_00_IEEE_double.rb +14 -0
  57. data/test/test_01_add_worksheet.rb +12 -0
  58. data/test/test_02_merge_formats.rb +58 -0
  59. data/test/test_04_dimensions.rb +397 -0
  60. data/test/test_05_rows.rb +182 -0
  61. data/test/test_06_extsst.rb +80 -0
  62. data/test/test_11_date_time.rb +484 -0
  63. data/test/test_12_date_only.rb +506 -0
  64. data/test/test_13_date_seconds.rb +486 -0
  65. data/test/test_21_escher.rb +629 -0
  66. data/test/test_22_mso_drawing_group.rb +739 -0
  67. data/test/test_23_note.rb +78 -0
  68. data/test/test_24_txo.rb +80 -0
  69. data/test/test_26_autofilter.rb +327 -0
  70. data/test/test_27_autofilter.rb +144 -0
  71. data/test/test_28_autofilter.rb +174 -0
  72. data/test/test_29_process_jpg.rb +131 -0
  73. data/test/test_30_validation_dval.rb +82 -0
  74. data/test/test_31_validation_dv_strings.rb +131 -0
  75. data/test/test_32_validation_dv_formula.rb +211 -0
  76. data/test/test_40_property_types.rb +191 -0
  77. data/test/test_41_properties.rb +238 -0
  78. data/test/test_42_set_properties.rb +430 -0
  79. data/test/ts_all.rb +34 -0
  80. metadata +154 -0
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Writeexcel" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'WriteExcel'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,31 @@
1
+ require "tc_biff"
2
+ require "tc_ole"
3
+ require "tc_workbook"
4
+ require "tc_worksheet"
5
+ require "tc_format"
6
+ require "tc_formula"
7
+ require 'tc_chart'
8
+ require "test_00_IEEE_double"
9
+ require 'test_01_add_worksheet'
10
+ require 'test_02_merge_formats'
11
+ require 'test_04_dimensions'
12
+ require 'test_05_rows'
13
+ require 'test_06_extsst'
14
+ require 'test_11_date_time'
15
+ require 'test_12_date_only'
16
+ require 'test_13_date_seconds'
17
+ require 'test_21_escher'
18
+ require 'test_22_mso_drawing_group'
19
+ require 'test_23_note'
20
+ require 'test_24_txo'
21
+ require 'test_26_autofilter'
22
+ require 'test_27_autofilter'
23
+ require 'test_28_autofilter'
24
+ require 'test_29_process_jpg'
25
+ require 'test_30_validation_dval'
26
+ require 'test_31_validation_dv_strings'
27
+ require 'test_32_validation_dv_formula'
28
+ require 'test_40_property_types'
29
+ require 'test_41_properties'
30
+ require 'test_42_set_properties'
31
+ require 'tc_example_match'
@@ -0,0 +1,104 @@
1
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require "test/unit"
4
+ require "WriteExcel"
5
+
6
+ class TC_BIFFWriter < Test::Unit::TestCase
7
+
8
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
9
+ PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
10
+
11
+ def setup
12
+ t = Time.now.strftime("%Y%m%d")
13
+ path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
14
+ @test_file = File.join(Dir.tmpdir, path)
15
+ @biff = BIFFWriter.new
16
+ @ruby_file = @test_file
17
+ end
18
+
19
+ def test_append_no_error
20
+ assert_nothing_raised{ @biff.append("World") }
21
+ end
22
+
23
+ def test_prepend_no_error
24
+ assert_nothing_raised{ @biff.prepend("Hello") }
25
+ end
26
+
27
+ def test_data_added
28
+ assert_nothing_raised{ @biff.append("Hello", "World") }
29
+ data = ''
30
+ while d = @biff.get_data
31
+ data = data + d
32
+ end
33
+ assert_equal("HelloWorld", data, "Bad data contents")
34
+ assert_equal(10, @biff.datasize, "Bad data size")
35
+ end
36
+
37
+ def test_data_prepended
38
+
39
+ assert_nothing_raised{ @biff.append("Hello") }
40
+ assert_nothing_raised{ @biff.prepend("World") }
41
+ data = ''
42
+ while d = @biff.get_data
43
+ data = data + d
44
+ end
45
+ assert_equal("WorldHello", data, "Bad data contents")
46
+ assert_equal(10, @biff.datasize, "Bad data size")
47
+ end
48
+
49
+ def test_store_bof_length
50
+ assert_nothing_raised{ @biff.store_bof }
51
+ assert_equal(20, @biff.datasize, "Bad data size after store_bof call")
52
+ end
53
+
54
+ def test_store_eof_length
55
+ assert_nothing_raised{ @biff.store_eof }
56
+ assert_equal(4, @biff.datasize, "Bad data size after store_eof call")
57
+ end
58
+
59
+ def test_datasize_mixed
60
+ assert_nothing_raised{ @biff.append("Hello") }
61
+ assert_nothing_raised{ @biff.prepend("World") }
62
+ assert_nothing_raised{ @biff.store_bof }
63
+ assert_nothing_raised{ @biff.store_eof }
64
+ assert_equal(34, @biff.datasize, "Bad data size for mixed data")
65
+ end
66
+
67
+ def test_add_continue
68
+ perl_file = "#{PERL_OUTDIR}/biff_add_continue_testdata"
69
+ size = File.size(perl_file)
70
+ @fh = File.new(@ruby_file,"w+")
71
+ @fh.print(@biff.add_continue('testdata'))
72
+ @fh.close
73
+ rsize = File.size(@ruby_file)
74
+ assert_equal(size,rsize,"File sizes not the same")
75
+ compare_file(perl_file, @ruby_file)
76
+ end
77
+
78
+ def teardown
79
+ @biff = nil
80
+ File.delete(@ruby_file) if File.exist?(@ruby_file)
81
+ end
82
+
83
+ def compare_file(expected, target)
84
+ fh_e = File.open(expected, "r")
85
+ fh_t = File.open(target, "r")
86
+ while true do
87
+ e1 = fh_e.read(1)
88
+ t1 = fh_t.read(1)
89
+ if e1.nil?
90
+ assert( t1.nil?, "#{expexted} is EOF but #{target} is NOT EOF.")
91
+ break
92
+ elsif t1.nil?
93
+ assert( e1.nil?, '#{target} is EOF but #{expected} is NOT EOF.')
94
+ break
95
+ end
96
+ assert_equal(e1, t1, sprintf(" #{expected} = '%s' but #{target} = '%s'", e1, t1))
97
+ break
98
+ end
99
+ fh_e.close
100
+ fh_t.close
101
+ end
102
+
103
+
104
+ end
@@ -0,0 +1,22 @@
1
+ #####################################################
2
+ # tc_chart.rb
3
+ #
4
+ # Test suite for the Chart class (chart.rb)
5
+ #####################################################
6
+ base = File.basename(Dir.pwd)
7
+ if base == "test" || base =~ /spreadsheet/i
8
+ Dir.chdir("..") if base == "test"
9
+ $LOAD_PATH.unshift(Dir.pwd + "/lib/spreadsheet")
10
+ Dir.chdir("test") rescue nil
11
+ end
12
+
13
+ require "test/unit"
14
+ require "WriteExcel"
15
+
16
+ class TC_Chart < Test::Unit::TestCase
17
+
18
+ def test_
19
+ assert(true)
20
+ end
21
+
22
+ end
@@ -0,0 +1,1280 @@
1
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require "test/unit"
4
+ require 'WriteExcel'
5
+
6
+ class TC_example_match < Test::Unit::TestCase
7
+
8
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
9
+ PERL_OUTDIR = File.join(TEST_DIR, 'perl_output')
10
+
11
+ def setup
12
+ t = Time.now.strftime("%Y%m%d")
13
+ path = "temp#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
14
+ @test_file = File.join(Dir.tmpdir, path)
15
+ @filename = @test_file
16
+ @filename2 = @test_file + "2"
17
+ end
18
+
19
+ def teardown
20
+ File.delete(@filename) if File.exist?(@filename)
21
+ File.delete(@filename2) if File.exist?(@filename2)
22
+ end
23
+
24
+ def test_a_simple
25
+ workbook = Spreadsheet::WriteExcel.new(@filename);
26
+ worksheet = workbook.add_worksheet
27
+
28
+ # The general syntax is write(row, column, token). Note that row and
29
+ # column are zero indexed
30
+ #
31
+
32
+ # Write some text
33
+ worksheet.write(0, 0, "Hi Excel!")
34
+
35
+
36
+ # Write some numbers
37
+ worksheet.write(2, 0, 3) # Writes 3
38
+ worksheet.write(3, 0, 3.00000) # Writes 3
39
+ worksheet.write(4, 0, 3.00001) # Writes 3.00001
40
+ worksheet.write(5, 0, 3.14159) # TeX revision no.?
41
+
42
+
43
+ # Write some formulas
44
+ worksheet.write(7, 0, '=A3 + A6')
45
+ worksheet.write(8, 0, '=IF(A5>3,"Yes", "No")')
46
+
47
+
48
+ # Write a hyperlink
49
+ worksheet.write(10, 0, 'http://www.perl.com/')
50
+
51
+ # File save
52
+ workbook.close
53
+
54
+ # do assertion
55
+ compare_file("#{PERL_OUTDIR}/a_simple.xls", @filename)
56
+ end
57
+
58
+ def test_regions
59
+ workbook = Spreadsheet::WriteExcel.new(@filename)
60
+
61
+ # Add some worksheets
62
+ north = workbook.add_worksheet("North")
63
+ south = workbook.add_worksheet("South")
64
+ east = workbook.add_worksheet("East")
65
+ west = workbook.add_worksheet("West")
66
+
67
+ # Add a Format
68
+ format = workbook.add_format()
69
+ format.set_bold()
70
+ format.set_color('blue')
71
+
72
+ # Add a caption to each worksheet
73
+ workbook.sheets.each do |worksheet|
74
+ worksheet.write(0, 0, "Sales", format)
75
+ end
76
+
77
+ # Write some data
78
+ north.write(0, 1, 200000)
79
+ south.write(0, 1, 100000)
80
+ east.write(0, 1, 150000)
81
+ west.write(0, 1, 100000)
82
+
83
+ # Set the active worksheet
84
+ bp=1
85
+ south.activate()
86
+
87
+ # Set the width of the first column
88
+ south.set_column(0, 0, 20)
89
+
90
+ # Set the active cell
91
+ south.set_selection(0, 1)
92
+
93
+ workbook.close
94
+
95
+ # do assertion
96
+ compare_file("#{PERL_OUTDIR}/regions.xls", @filename)
97
+ end
98
+
99
+ def test_stats
100
+ workbook = Spreadsheet::WriteExcel.new(@filename)
101
+ worksheet = workbook.add_worksheet('Test data')
102
+
103
+ # Set the column width for columns 1
104
+ worksheet.set_column(0, 0, 20)
105
+
106
+ # Create a format for the headings
107
+ format = workbook.add_format
108
+ format.set_bold
109
+
110
+ # Write the sample data
111
+ worksheet.write(0, 0, 'Sample', format)
112
+ worksheet.write(0, 1, 1)
113
+ worksheet.write(0, 2, 2)
114
+ worksheet.write(0, 3, 3)
115
+ worksheet.write(0, 4, 4)
116
+ worksheet.write(0, 5, 5)
117
+ worksheet.write(0, 6, 6)
118
+ worksheet.write(0, 7, 7)
119
+ worksheet.write(0, 8, 8)
120
+
121
+ worksheet.write(1, 0, 'Length', format)
122
+ worksheet.write(1, 1, 25.4)
123
+ worksheet.write(1, 2, 25.4)
124
+ worksheet.write(1, 3, 24.8)
125
+ worksheet.write(1, 4, 25.0)
126
+ worksheet.write(1, 5, 25.3)
127
+ worksheet.write(1, 6, 24.9)
128
+ worksheet.write(1, 7, 25.2)
129
+ worksheet.write(1, 8, 24.8)
130
+
131
+ # Write some statistical functions
132
+ worksheet.write(4, 0, 'Count', format)
133
+ worksheet.write(4, 1, '=COUNT(B1:I1)')
134
+
135
+ worksheet.write(5, 0, 'Sum', format)
136
+ worksheet.write(5, 1, '=SUM(B2:I2)')
137
+
138
+ worksheet.write(6, 0, 'Average', format)
139
+ worksheet.write(6, 1, '=AVERAGE(B2:I2)')
140
+
141
+ worksheet.write(7, 0, 'Min', format)
142
+ worksheet.write(7, 1, '=MIN(B2:I2)')
143
+
144
+ worksheet.write(8, 0, 'Max', format)
145
+ worksheet.write(8, 1, '=MAX(B2:I2)')
146
+
147
+ worksheet.write(9, 0, 'Standard Deviation', format)
148
+ worksheet.write(9, 1, '=STDEV(B2:I2)')
149
+
150
+ worksheet.write(10, 0, 'Kurtosis', format)
151
+ worksheet.write(10, 1, '=KURT(B2:I2)')
152
+
153
+ workbook.close
154
+
155
+ # do assertion
156
+ compare_file("#{PERL_OUTDIR}/regions.xls", @filename)
157
+ end
158
+
159
+ def test_hidden
160
+ workbook = Spreadsheet::WriteExcel.new(@filename)
161
+ worksheet1 = workbook.add_worksheet
162
+ worksheet2 = workbook.add_worksheet
163
+ worksheet3 = workbook.add_worksheet
164
+
165
+ # Sheet2 won't be visible until it is unhidden in Excel.
166
+ worksheet2.hide
167
+
168
+ worksheet1.write(0, 0, 'Sheet2 is hidden')
169
+ worksheet2.write(0, 0, 'How did you find me?')
170
+ worksheet3.write(0, 0, 'Sheet2 is hidden')
171
+
172
+ workbook.close
173
+
174
+ # do assertion
175
+ compare_file("#{PERL_OUTDIR}/hidden.xls", @filename)
176
+ end
177
+
178
+ def test_hyperlink1
179
+ # Create a new workbook and add a worksheet
180
+ workbook = Spreadsheet::WriteExcel.new(@filename)
181
+ worksheet = workbook.add_worksheet('Hyperlinks')
182
+
183
+ # Format the first column
184
+ worksheet.set_column('A:A', 30)
185
+ worksheet.set_selection('B1')
186
+
187
+
188
+ # Add a sample format
189
+ format = workbook.add_format
190
+ format.set_size(12)
191
+ format.set_bold
192
+ format.set_color('red')
193
+ format.set_underline
194
+
195
+
196
+ # Write some hyperlinks
197
+ worksheet.write('A1', 'http://www.perl.com/' )
198
+ worksheet.write('A3', 'http://www.perl.com/', 'Perl home' )
199
+ worksheet.write('A5', 'http://www.perl.com/', nil, format)
200
+ worksheet.write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me')
201
+
202
+ # Write a URL that isn't a hyperlink
203
+ worksheet.write_string('A9', 'http://www.perl.com/')
204
+
205
+ workbook.close
206
+
207
+ # do assertion
208
+ compare_file("#{PERL_OUTDIR}/hyperlink.xls", @filename)
209
+ end
210
+
211
+ def test_copyformat
212
+ # Create workbook1
213
+ workbook1 = Spreadsheet::WriteExcel.new(@filename)
214
+ worksheet1 = workbook1.add_worksheet
215
+ format1a = workbook1.add_format
216
+ format1b = workbook1.add_format
217
+
218
+ # Create workbook2
219
+ workbook2 = Spreadsheet::WriteExcel.new(@filename2)
220
+ worksheet2 = workbook2.add_worksheet
221
+ format2a = workbook2.add_format
222
+ format2b = workbook2.add_format
223
+
224
+ # Create a global format object that isn't tied to a workbook
225
+ global_format = Format.new
226
+
227
+ # Set the formatting
228
+ global_format.set_color('blue')
229
+ global_format.set_bold
230
+ global_format.set_italic
231
+
232
+ # Create another example format
233
+ format1b.set_color('red')
234
+
235
+ # Copy the global format properties to the worksheet formats
236
+ format1a.copy(global_format)
237
+ format2a.copy(global_format)
238
+
239
+ # Copy a format from worksheet1 to worksheet2
240
+ format2b.copy(format1b)
241
+
242
+ # Write some output
243
+ worksheet1.write(0, 0, "Ciao", format1a)
244
+ worksheet1.write(1, 0, "Ciao", format1b)
245
+
246
+ worksheet2.write(0, 0, "Hello", format2a)
247
+ worksheet2.write(1, 0, "Hello", format2b)
248
+ workbook1.close
249
+ workbook2.close
250
+
251
+ # do assertion
252
+ compare_file("#{PERL_OUTDIR}/workbook1.xls", @filename)
253
+ compare_file("#{PERL_OUTDIR}/workbook2.xls", @filename2)
254
+ end
255
+
256
+ def test_data_validate
257
+ workbook = Spreadsheet::WriteExcel.new(@filename)
258
+ worksheet = workbook.add_worksheet
259
+
260
+ # Add a format for the header cells.
261
+ header_format = workbook.add_format(
262
+ :border => 1,
263
+ :bg_color => 43,
264
+ :bold => 1,
265
+ :text_wrap => 1,
266
+ :valign => 'vcenter',
267
+ :indent => 1
268
+ )
269
+
270
+ # Set up layout of the worksheet.
271
+ worksheet.set_column('A:A', 64)
272
+ worksheet.set_column('B:B', 15)
273
+ worksheet.set_column('D:D', 15)
274
+ worksheet.set_row(0, 36)
275
+ worksheet.set_selection('B3')
276
+
277
+
278
+ # Write the header cells and some data that will be used in the examples.
279
+ row = 0
280
+ heading1 = 'Some examples of data validation in Spreadsheet::WriteExcel'
281
+ heading2 = 'Enter values in this column'
282
+ heading3 = 'Sample Data'
283
+
284
+ worksheet.write('A1', heading1, header_format)
285
+ worksheet.write('B1', heading2, header_format)
286
+ worksheet.write('D1', heading3, header_format)
287
+
288
+ worksheet.write('D3', ['Integers', 1, 10])
289
+ worksheet.write('D4', ['List data', 'open', 'high', 'close'])
290
+ worksheet.write('D5', ['Formula', '=AND(F5=50,G5=60)', 50, 60])
291
+
292
+
293
+ #
294
+ # Example 1. Limiting input to an integer in a fixed range.
295
+ #
296
+ txt = 'Enter an integer between 1 and 10'
297
+ row += 2
298
+
299
+ worksheet.write(row, 0, txt)
300
+ worksheet.data_validation(row, 1,
301
+ {
302
+ :validate => 'integer',
303
+ :criteria => 'between',
304
+ :minimum => 1,
305
+ :maximum => 10
306
+ })
307
+
308
+
309
+ #
310
+ # Example 2. Limiting input to an integer outside a fixed range.
311
+ #
312
+ txt = 'Enter an integer that is not between 1 and 10 (using cell references)'
313
+ row += 2
314
+
315
+ worksheet.write(row, 0, txt)
316
+ worksheet.data_validation(row, 1,
317
+ {
318
+ :validate => 'integer',
319
+ :criteria => 'not between',
320
+ :minimum => '=E3',
321
+ :maximum => '=F3'
322
+ })
323
+
324
+
325
+ #
326
+ # Example 3. Limiting input to an integer greater than a fixed value.
327
+ #
328
+ txt = 'Enter an integer greater than 0'
329
+ row += 2
330
+
331
+ worksheet.write(row, 0, txt)
332
+ worksheet.data_validation(row, 1,
333
+ {
334
+ :validate => 'integer',
335
+ :criteria => '>',
336
+ :value => 0
337
+ })
338
+
339
+
340
+ #
341
+ # Example 4. Limiting input to an integer less than a fixed value.
342
+ #
343
+ txt = 'Enter an integer less than 10'
344
+ row += 2
345
+
346
+ worksheet.write(row, 0, txt)
347
+ worksheet.data_validation(row, 1,
348
+ {
349
+ :validate => 'integer',
350
+ :criteria => '<',
351
+ :value => 10
352
+ })
353
+
354
+
355
+ #
356
+ # Example 5. Limiting input to a decimal in a fixed range.
357
+ #
358
+ txt = 'Enter a decimal between 0.1 and 0.5'
359
+ row += 2
360
+
361
+ worksheet.write(row, 0, txt)
362
+ worksheet.data_validation(row, 1,
363
+ {
364
+ :validate => 'decimal',
365
+ :criteria => 'between',
366
+ :minimum => 0.1,
367
+ :maximum => 0.5
368
+ })
369
+
370
+
371
+ #
372
+ # Example 6. Limiting input to a value in a dropdown list.
373
+ #
374
+ txt = 'Select a value from a drop down list'
375
+ row += 2
376
+ bp=1
377
+ worksheet.write(row, 0, txt)
378
+ worksheet.data_validation(row, 1,
379
+ {
380
+ :validate => 'list',
381
+ :source => ['open', 'high', 'close']
382
+ })
383
+
384
+
385
+ #
386
+ # Example 6. Limiting input to a value in a dropdown list.
387
+ #
388
+ txt = 'Select a value from a drop down list (using a cell range)'
389
+ row += 2
390
+
391
+ worksheet.write(row, 0, txt)
392
+ worksheet.data_validation(row, 1,
393
+ {
394
+ :validate => 'list',
395
+ :source => '=E4:G4'
396
+ })
397
+
398
+
399
+ #
400
+ # Example 7. Limiting input to a date in a fixed range.
401
+ #
402
+ txt = 'Enter a date between 1/1/2008 and 12/12/2008'
403
+ row += 2
404
+
405
+ worksheet.write(row, 0, txt)
406
+ worksheet.data_validation(row, 1,
407
+ {
408
+ :validate => 'date',
409
+ :criteria => 'between',
410
+ :minimum => '2008-01-01T',
411
+ :maximum => '2008-12-12T'
412
+ })
413
+
414
+
415
+ #
416
+ # Example 8. Limiting input to a time in a fixed range.
417
+ #
418
+ txt = 'Enter a time between 6:00 and 12:00'
419
+ row += 2
420
+
421
+ worksheet.write(row, 0, txt)
422
+ worksheet.data_validation(row, 1,
423
+ {
424
+ :validate => 'time',
425
+ :criteria => 'between',
426
+ :minimum => 'T06:00',
427
+ :maximum => 'T12:00'
428
+ })
429
+
430
+
431
+ #
432
+ # Example 9. Limiting input to a string greater than a fixed length.
433
+ #
434
+ txt = 'Enter a string longer than 3 characters'
435
+ row += 2
436
+
437
+ worksheet.write(row, 0, txt)
438
+ worksheet.data_validation(row, 1,
439
+ {
440
+ :validate => 'length',
441
+ :criteria => '>',
442
+ :value => 3
443
+ })
444
+
445
+
446
+ #
447
+ # Example 10. Limiting input based on a formula.
448
+ #
449
+ txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"'
450
+ row += 2
451
+
452
+ worksheet.write(row, 0, txt)
453
+ worksheet.data_validation(row, 1,
454
+ {
455
+ :validate => 'custom',
456
+ :value => '=AND(F5=50,G5=60)'
457
+ })
458
+
459
+
460
+ #
461
+ # Example 11. Displaying and modify data validation messages.
462
+ #
463
+ txt = 'Displays a message when you select the cell'
464
+ row += 2
465
+
466
+ worksheet.write(row, 0, txt)
467
+ worksheet.data_validation(row, 1,
468
+ {
469
+ :validate => 'integer',
470
+ :criteria => 'between',
471
+ :minimum => 1,
472
+ :maximum => 100,
473
+ :input_title => 'Enter an integer:',
474
+ :input_message => 'between 1 and 100'
475
+ })
476
+
477
+
478
+ #
479
+ # Example 12. Displaying and modify data validation messages.
480
+ #
481
+ txt = 'Display a custom error message when integer isn\'t between 1 and 100'
482
+ row += 2
483
+
484
+ worksheet.write(row, 0, txt)
485
+ worksheet.data_validation(row, 1,
486
+ {
487
+ :validate => 'integer',
488
+ :criteria => 'between',
489
+ :minimum => 1,
490
+ :maximum => 100,
491
+ :input_title => 'Enter an integer:',
492
+ :input_message => 'between 1 and 100',
493
+ :error_title => 'Input value is not valid!',
494
+ :error_message => 'It should be an integer between 1 and 100'
495
+ })
496
+
497
+
498
+ #
499
+ # Example 13. Displaying and modify data validation messages.
500
+ #
501
+ txt = 'Display a custom information message when integer isn\'t between 1 and 100'
502
+ row += 2
503
+
504
+ worksheet.write(row, 0, txt)
505
+ worksheet.data_validation(row, 1,
506
+ {
507
+ :validate => 'integer',
508
+ :criteria => 'between',
509
+ :minimum => 1,
510
+ :maximum => 100,
511
+ :input_title => 'Enter an integer:',
512
+ :input_message => 'between 1 and 100',
513
+ :error_title => 'Input value is not valid!',
514
+ :error_message => 'It should be an integer between 1 and 100',
515
+ :error_type => 'information'
516
+ })
517
+
518
+ workbook.close
519
+
520
+ # do assertion
521
+ compare_file("#{PERL_OUTDIR}/data_validate.xls", @filename)
522
+ end
523
+
524
+ def test_merge1
525
+ workbook = Spreadsheet::WriteExcel.new(@filename)
526
+ worksheet = workbook.add_worksheet
527
+
528
+ # Increase the cell size of the merged cells to highlight the formatting.
529
+ worksheet.set_column('B:D', 20)
530
+ worksheet.set_row(2, 30)
531
+
532
+ # Create a merge format
533
+ format = workbook.add_format(:center_across => 1)
534
+
535
+ # Only one cell should contain text, the others should be blank.
536
+ worksheet.write(2, 1, "Center across selection", format)
537
+ worksheet.write_blank(2, 2, format)
538
+ worksheet.write_blank(2, 3, format)
539
+
540
+ workbook.close
541
+
542
+ # do assertion
543
+ compare_file("#{PERL_OUTDIR}/merge1.xls", @filename)
544
+ end
545
+
546
+ def test_merge2
547
+ workbook = Spreadsheet::WriteExcel.new(@filename)
548
+ worksheet = workbook.add_worksheet
549
+
550
+ # Increase the cell size of the merged cells to highlight the formatting.
551
+ worksheet.set_column(1, 2, 30)
552
+ worksheet.set_row(2, 40)
553
+
554
+ # Create a merged format
555
+ format = workbook.add_format(
556
+ :center_across => 1,
557
+ :bold => 1,
558
+ :size => 15,
559
+ :pattern => 1,
560
+ :border => 6,
561
+ :color => 'white',
562
+ :fg_color => 'green',
563
+ :border_color => 'yellow',
564
+ :align => 'vcenter'
565
+ )
566
+
567
+ # Only one cell should contain text, the others should be blank.
568
+ worksheet.write(2, 1, "Center across selection", format)
569
+ worksheet.write_blank(2, 2, format)
570
+ workbook.close
571
+
572
+ # do assertion
573
+ compare_file("#{PERL_OUTDIR}/merge2.xls", @filename)
574
+ end
575
+
576
+ def test_merge3
577
+ workbook = Spreadsheet::WriteExcel.new(@filename)
578
+ worksheet = workbook.add_worksheet()
579
+
580
+ # Increase the cell size of the merged cells to highlight the formatting.
581
+ [1, 3,6,7].each { |row| worksheet.set_row(row, 30) }
582
+ worksheet.set_column('B:D', 20)
583
+
584
+ ###############################################################################
585
+ #
586
+ # Example 1: Merge cells containing a hyperlink using write_url_range()
587
+ # and the standard Excel 5+ merge property.
588
+ #
589
+ format1 = workbook.add_format(
590
+ :center_across => 1,
591
+ :border => 1,
592
+ :underline => 1,
593
+ :color => 'blue'
594
+ )
595
+
596
+ # Write the cells to be merged
597
+ worksheet.write_url_range('B2:D2', 'http://www.perl.com', format1)
598
+ worksheet.write_blank('C2', format1)
599
+ worksheet.write_blank('D2', format1)
600
+
601
+
602
+
603
+ ###############################################################################
604
+ #
605
+ # Example 2: Merge cells containing a hyperlink using merge_range().
606
+ #
607
+ format2 = workbook.add_format(
608
+ :border => 1,
609
+ :underline => 1,
610
+ :color => 'blue',
611
+ :align => 'center',
612
+ :valign => 'vcenter'
613
+ )
614
+
615
+ # Merge 3 cells
616
+ worksheet.merge_range('B4:D4', 'http://www.perl.com', format2)
617
+
618
+
619
+ # Merge 3 cells over two rows
620
+ worksheet.merge_range('B7:D8', 'http://www.perl.com', format2)
621
+
622
+ workbook.close
623
+
624
+ # do assertion
625
+ compare_file("#{PERL_OUTDIR}/merge3.xls", @filename)
626
+ end
627
+
628
+ def test_merge4
629
+ # Create a new workbook and add a worksheet
630
+ workbook = Spreadsheet::WriteExcel.new(@filename)
631
+ worksheet = workbook.add_worksheet
632
+
633
+ # Increase the cell size of the merged cells to highlight the formatting.
634
+ (1..11).each { |row| worksheet.set_row(row, 30) }
635
+ worksheet.set_column('B:D', 20)
636
+
637
+ ###############################################################################
638
+ #
639
+ # Example 1: Text centered vertically and horizontally
640
+ #
641
+ format1 = workbook.add_format(
642
+ :border => 6,
643
+ :bold => 1,
644
+ :color => 'red',
645
+ :valign => 'vcenter',
646
+ :align => 'center'
647
+ )
648
+
649
+ worksheet.merge_range('B2:D3', 'Vertical and horizontal', format1)
650
+
651
+
652
+ ###############################################################################
653
+ #
654
+ # Example 2: Text aligned to the top and left
655
+ #
656
+ format2 = workbook.add_format(
657
+ :border => 6,
658
+ :bold => 1,
659
+ :color => 'red',
660
+ :valign => 'top',
661
+ :align => 'left'
662
+ )
663
+
664
+ worksheet.merge_range('B5:D6', 'Aligned to the top and left', format2)
665
+
666
+ ###############################################################################
667
+ #
668
+ # Example 3: Text aligned to the bottom and right
669
+ #
670
+ format3 = workbook.add_format(
671
+ :border => 6,
672
+ :bold => 1,
673
+ :color => 'red',
674
+ :valign => 'bottom',
675
+ :align => 'right'
676
+ )
677
+
678
+ worksheet.merge_range('B8:D9', 'Aligned to the bottom and right', format3)
679
+
680
+ ###############################################################################
681
+ #
682
+ # Example 4: Text justified (i.e. wrapped) in the cell
683
+ #
684
+ format4 = workbook.add_format(
685
+ :border => 6,
686
+ :bold => 1,
687
+ :color => 'red',
688
+ :valign => 'top',
689
+ :align => 'justify'
690
+ )
691
+
692
+ worksheet.merge_range('B11:D12', 'Justified: '+'so on and '*18, format4)
693
+
694
+ workbook.close
695
+
696
+ # do assertion
697
+ compare_file("#{PERL_OUTDIR}/merge4.xls", @filename)
698
+ end
699
+
700
+ def test_merge5
701
+ # Create a new workbook and add a worksheet
702
+ workbook = Spreadsheet::WriteExcel.new(@filename)
703
+ worksheet = workbook.add_worksheet
704
+
705
+
706
+ # Increase the cell size of the merged cells to highlight the formatting.
707
+ (3..8).each { |col| worksheet.set_row(col, 36) }
708
+ [1, 3, 5].each { |n| worksheet.set_column(n, n, 15) }
709
+
710
+
711
+ ###############################################################################
712
+ #
713
+ # Rotation 1, letters run from top to bottom
714
+ #
715
+ format1 = workbook.add_format(
716
+ :border => 6,
717
+ :bold => 1,
718
+ :color => 'red',
719
+ :valign => 'vcentre',
720
+ :align => 'centre',
721
+ :rotation => 270
722
+ )
723
+
724
+
725
+ worksheet.merge_range('B4:B9', 'Rotation 270', format1)
726
+
727
+
728
+ ###############################################################################
729
+ #
730
+ # Rotation 2, 90° anticlockwise
731
+ #
732
+ format2 = workbook.add_format(
733
+ :border => 6,
734
+ :bold => 1,
735
+ :color => 'red',
736
+ :valign => 'vcentre',
737
+ :align => 'centre',
738
+ :rotation => 90
739
+ )
740
+
741
+
742
+ worksheet.merge_range('D4:D9', 'Rotation 90°', format2)
743
+
744
+
745
+
746
+ ###############################################################################
747
+ #
748
+ # Rotation 3, 90° clockwise
749
+ #
750
+ format3 = workbook.add_format(
751
+ :border => 6,
752
+ :bold => 1,
753
+ :color => 'red',
754
+ :valign => 'vcentre',
755
+ :align => 'centre',
756
+ :rotation => -90
757
+ )
758
+
759
+
760
+ worksheet.merge_range('F4:F9', 'Rotation -90°', format3)
761
+
762
+ workbook.close
763
+
764
+ # do assertion
765
+ compare_file("#{PERL_OUTDIR}/merge5.xls", @filename)
766
+ end
767
+
768
+ def test_images
769
+ # Create a new workbook called simple.xls and add a worksheet
770
+ workbook = Spreadsheet::WriteExcel.new(@filename)
771
+ worksheet1 = workbook.add_worksheet('Image 1')
772
+ worksheet2 = workbook.add_worksheet('Image 2')
773
+ worksheet3 = workbook.add_worksheet('Image 3')
774
+ worksheet4 = workbook.add_worksheet('Image 4')
775
+ bp=1
776
+
777
+ # Insert a basic image
778
+ worksheet1.write('A10', "Image inserted into worksheet.")
779
+ worksheet1.insert_image('A1', File.join(TEST_DIR,'republic.png'))
780
+
781
+
782
+ # Insert an image with an offset
783
+ worksheet2.write('A10', "Image inserted with an offset.")
784
+ worksheet2.insert_image('A1', File.join(TEST_DIR,'republic.png'), 32, 10)
785
+
786
+ # Insert a scaled image
787
+ worksheet3.write('A10', "Image scaled: width x 2, height x 0.8.")
788
+ worksheet3.insert_image('A1', File.join(TEST_DIR,'republic.png'), 0, 0, 2, 0.8)
789
+
790
+ # Insert an image over varied column and row sizes
791
+ # This does not require any additional work
792
+
793
+ # Set the cols and row sizes
794
+ # NOTE: you must do this before you call insert_image()
795
+ worksheet4.set_column('A:A', 5)
796
+ worksheet4.set_column('B:B', nil, nil, 1) # Hidden
797
+ worksheet4.set_column('C:D', 10)
798
+ worksheet4.set_row(0, 30)
799
+ worksheet4.set_row(3, 5)
800
+
801
+ worksheet4.write('A10', "Image inserted over scaled rows and columns.")
802
+ worksheet4.insert_image('A1', File.join(TEST_DIR,'republic.png'))
803
+
804
+ workbook.close
805
+
806
+ # do assertion
807
+ compare_file("#{PERL_OUTDIR}/images.xls", @filename)
808
+ end
809
+
810
+ def test_tab_colors
811
+ workbook = Spreadsheet::WriteExcel.new(@filename)
812
+
813
+ worksheet1 = workbook.add_worksheet
814
+ worksheet2 = workbook.add_worksheet
815
+ worksheet3 = workbook.add_worksheet
816
+ worksheet4 = workbook.add_worksheet
817
+
818
+ # Worsheet1 will have the default tab colour.
819
+ worksheet2.set_tab_color('red')
820
+ worksheet3.set_tab_color('green')
821
+ worksheet4.set_tab_color(0x35) # Orange
822
+
823
+ workbook.close
824
+
825
+ # do assertion
826
+ compare_file("#{PERL_OUTDIR}/tab_colors.xls", @filename)
827
+ end
828
+
829
+ def test_stocks
830
+ # Create a new workbook and add a worksheet
831
+ workbook = Spreadsheet::WriteExcel.new(@filename)
832
+ worksheet = workbook.add_worksheet
833
+
834
+ # Set the column width for columns 1, 2, 3 and 4
835
+ worksheet.set_column(0, 3, 15)
836
+
837
+
838
+ # Create a format for the column headings
839
+ header = workbook.add_format
840
+ header.set_bold
841
+ header.set_size(12)
842
+ header.set_color('blue')
843
+
844
+
845
+ # Create a format for the stock price
846
+ f_price = workbook.add_format
847
+ f_price.set_align('left')
848
+ f_price.set_num_format('$0.00')
849
+
850
+
851
+ # Create a format for the stock volume
852
+ f_volume = workbook.add_format
853
+ f_volume.set_align('left')
854
+ f_volume.set_num_format('#,##0')
855
+
856
+
857
+ # Create a format for the price change. This is an example of a conditional
858
+ # format. The number is formatted as a percentage. If it is positive it is
859
+ # formatted in green, if it is negative it is formatted in red and if it is
860
+ # zero it is formatted as the default font colour (in this case black).
861
+ # Note: the [Green] format produces an unappealing lime green. Try
862
+ # [Color 10] instead for a dark green.
863
+ #
864
+ f_change = workbook.add_format
865
+ f_change.set_align('left')
866
+ f_change.set_num_format('[Green]0.0%;[Red]-0.0%;0.0%')
867
+
868
+
869
+ # Write out the data
870
+ worksheet.write(0, 0, 'Company', header)
871
+ worksheet.write(0, 1, 'Price', header)
872
+ worksheet.write(0, 2, 'Volume', header)
873
+ worksheet.write(0, 3, 'Change', header)
874
+
875
+ worksheet.write(1, 0, 'Damage Inc.' )
876
+ worksheet.write(1, 1, 30.25, f_price) # $30.25
877
+ worksheet.write(1, 2, 1234567, f_volume) # 1,234,567
878
+ worksheet.write(1, 3, 0.085, f_change) # 8.5% in green
879
+
880
+ worksheet.write(2, 0, 'Dump Corp.' )
881
+ worksheet.write(2, 1, 1.56, f_price) # $1.56
882
+ worksheet.write(2, 2, 7564, f_volume) # 7,564
883
+ worksheet.write(2, 3, -0.015, f_change) # -1.5% in red
884
+
885
+ worksheet.write(3, 0, 'Rev Ltd.' )
886
+ worksheet.write(3, 1, 0.13, f_price) # $0.13
887
+ worksheet.write(3, 2, 321, f_volume) # 321
888
+ worksheet.write(3, 3, 0, f_change) # 0 in the font color (black)
889
+
890
+ workbook.close
891
+
892
+ # do assertion
893
+ compare_file("#{PERL_OUTDIR}/stocks.xls", @filename)
894
+ end
895
+
896
+ def test_protection
897
+ workbook = Spreadsheet::WriteExcel.new(@filename)
898
+ worksheet = workbook.add_worksheet
899
+
900
+ # Create some format objects
901
+ locked = workbook.add_format(:locked => 1)
902
+ unlocked = workbook.add_format(:locked => 0)
903
+ hidden = workbook.add_format(:hidden => 1)
904
+
905
+ # Format the columns
906
+ worksheet.set_column('A:A', 42)
907
+ worksheet.set_selection('B3:B3')
908
+
909
+ # Protect the worksheet
910
+ worksheet.protect
911
+
912
+ # Examples of cell locking and hiding
913
+ worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
914
+ worksheet.write('B1', '=1+2', locked)
915
+
916
+ worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')
917
+ worksheet.write('B2', '=1+2', unlocked)
918
+
919
+ worksheet.write('A3', "Cell B3 is hidden. The formula isn't visible.")
920
+ worksheet.write('B3', '=1+2', hidden)
921
+
922
+ worksheet.write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet')
923
+ worksheet.write('A6', 'to remove the worksheet protection. ')
924
+
925
+ workbook.close
926
+
927
+ # do assertion
928
+ compare_file("#{PERL_OUTDIR}/protection.xls", @filename)
929
+ end
930
+
931
+ def test_date_time
932
+ # Create a new workbook and add a worksheet
933
+ workbook = Spreadsheet::WriteExcel.new(@filename)
934
+ worksheet = workbook.add_worksheet
935
+ bold = workbook.add_format(:bold => 1)
936
+
937
+ # Expand the first column so that the date is visible.
938
+ worksheet.set_column("A:B", 30)
939
+
940
+ # Write the column headers
941
+ worksheet.write('A1', 'Formatted date', bold)
942
+ worksheet.write('B1', 'Format', bold)
943
+
944
+ # Examples date and time formats. In the output file compare how changing
945
+ # the format codes change the appearance of the date.
946
+ #
947
+ date_formats = [
948
+ 'dd/mm/yy',
949
+ 'mm/dd/yy',
950
+ '',
951
+ 'd mm yy',
952
+ 'dd mm yy',
953
+ '',
954
+ 'dd m yy',
955
+ 'dd mm yy',
956
+ 'dd mmm yy',
957
+ 'dd mmmm yy',
958
+ '',
959
+ 'dd mm y',
960
+ 'dd mm yyy',
961
+ 'dd mm yyyy',
962
+ '',
963
+ 'd mmmm yyyy',
964
+ '',
965
+ 'dd/mm/yy',
966
+ 'dd/mm/yy hh:mm',
967
+ 'dd/mm/yy hh:mm:ss',
968
+ 'dd/mm/yy hh:mm:ss.000',
969
+ '',
970
+ 'hh:mm',
971
+ 'hh:mm:ss',
972
+ 'hh:mm:ss.000',
973
+ ]
974
+
975
+ # Write the same date and time using each of the above formats. The empty
976
+ # string formats create a blank line to make the example clearer.
977
+ #
978
+ row = 0
979
+ date_formats.each do |date_format|
980
+ row += 1
981
+ next if date_format == ''
982
+
983
+ # Create a format for the date or time.
984
+ format = workbook.add_format(
985
+ :num_format => date_format,
986
+ :align => 'left'
987
+ )
988
+
989
+ # Write the same date using different formats.
990
+ worksheet.write_date_time(row, 0, '2004-08-01T12:30:45.123', format)
991
+ worksheet.write(row, 1, date_format)
992
+ end
993
+
994
+ # The following is an example of an invalid date. It is written as a string instead
995
+ # of a number. This is also Excel's default behaviour.
996
+ #
997
+ row += 2
998
+ worksheet.write_date_time(row, 0, '2004-13-01T12:30:45.123')
999
+ worksheet.write(row, 1, 'Invalid date. Written as string.', bold)
1000
+
1001
+ workbook.close
1002
+
1003
+ # do assertion
1004
+ compare_file("#{PERL_OUTDIR}/date_time.xls", @filename)
1005
+ end
1006
+ def test_diag_border
1007
+ workbook = Spreadsheet::WriteExcel.new(@filename)
1008
+ worksheet = workbook.add_worksheet
1009
+
1010
+ format1 = workbook.add_format(:diag_type => 1)
1011
+ format2 = workbook.add_format(:diag_type => 2)
1012
+ format3 = workbook.add_format(:diag_type => 3)
1013
+ format4 = workbook.add_format(
1014
+ :diag_type => 3,
1015
+ :diag_border => 7,
1016
+ :diag_color => 'red'
1017
+ )
1018
+
1019
+ worksheet.write('B3', 'Text', format1)
1020
+ worksheet.write('B6', 'Text', format2)
1021
+ worksheet.write('B9', 'Text', format3)
1022
+ worksheet.write('B12', 'Text', format4)
1023
+
1024
+ workbook.close
1025
+
1026
+ # do assertion
1027
+ compare_file("#{PERL_OUTDIR}/diag_border.xls", @filename)
1028
+ end
1029
+
1030
+ def test_header
1031
+ workbook = Spreadsheet::WriteExcel.new(@filename)
1032
+ preview = "Select Print Preview to see the header and footer"
1033
+
1034
+
1035
+ ######################################################################
1036
+ #
1037
+ # A simple example to start
1038
+ #
1039
+ worksheet1 = workbook.add_worksheet('Simple')
1040
+
1041
+ header1 = '&CHere is some centred text.'
1042
+
1043
+ footer1 = '&LHere is some left aligned text.'
1044
+
1045
+
1046
+ worksheet1.set_header(header1)
1047
+ worksheet1.set_footer(footer1)
1048
+
1049
+ worksheet1.set_column('A:A', 50)
1050
+ worksheet1.write('A1', preview)
1051
+
1052
+
1053
+ ######################################################################
1054
+ #
1055
+ # This is an example of some of the header/footer variables.
1056
+ #
1057
+ worksheet2 = workbook.add_worksheet('Variables')
1058
+
1059
+ header2 = '&LPage &P of &N'+
1060
+ '&CFilename: &F' +
1061
+ '&RSheetname: &A'
1062
+
1063
+ footer2 = '&LCurrent date: &D'+
1064
+ '&RCurrent time: &T'
1065
+
1066
+ worksheet2.set_header(header2)
1067
+ worksheet2.set_footer(footer2)
1068
+
1069
+
1070
+ worksheet2.set_column('A:A', 50)
1071
+ worksheet2.write('A1', preview)
1072
+ worksheet2.write('A21', "Next sheet")
1073
+ worksheet2.set_h_pagebreaks(20)
1074
+
1075
+
1076
+ ######################################################################
1077
+ #
1078
+ # This example shows how to use more than one font
1079
+ #
1080
+ worksheet3 = workbook.add_worksheet('Mixed fonts')
1081
+
1082
+ header3 = '&C' +
1083
+ '&"Courier New,Bold"Hello ' +
1084
+ '&"Arial,Italic"World'
1085
+
1086
+ footer3 = '&C' +
1087
+ '&"Symbol"e' +
1088
+ '&"Arial" = mc&X2'
1089
+
1090
+ worksheet3.set_header(header3)
1091
+ worksheet3.set_footer(footer3)
1092
+
1093
+ worksheet3.set_column('A:A', 50)
1094
+ worksheet3.write('A1', preview)
1095
+
1096
+
1097
+ ######################################################################
1098
+ #
1099
+ # Example of line wrapping
1100
+ #
1101
+ worksheet4 = workbook.add_worksheet('Word wrap')
1102
+
1103
+ header4 = "&CHeading 1\nHeading 2\nHeading 3"
1104
+
1105
+ worksheet4.set_header(header4)
1106
+
1107
+ worksheet4.set_column('A:A', 50)
1108
+ worksheet4.write('A1', preview)
1109
+
1110
+
1111
+ ######################################################################
1112
+ #
1113
+ # Example of inserting a literal ampersand &
1114
+ #
1115
+ worksheet5 = workbook.add_worksheet('Ampersand')
1116
+
1117
+ header5 = "&CCuriouser && Curiouser - Attorneys at Law"
1118
+
1119
+ worksheet5.set_header(header5)
1120
+
1121
+ worksheet5.set_column('A:A', 50)
1122
+ worksheet5.write('A1', preview)
1123
+
1124
+ workbook.close
1125
+
1126
+ # do assertion
1127
+ compare_file("#{PERL_OUTDIR}/headers.xls", @filename)
1128
+ end
1129
+
1130
+ def test_demo
1131
+ workbook = Spreadsheet::WriteExcel.new(@filename)
1132
+ worksheet = workbook.add_worksheet('Demo')
1133
+ worksheet2 = workbook.add_worksheet('Another sheet')
1134
+ worksheet3 = workbook.add_worksheet('And another')
1135
+
1136
+ bold = workbook.add_format(:bold => 1)
1137
+
1138
+ #######################################################################
1139
+ #
1140
+ # Write a general heading
1141
+ #
1142
+ worksheet.set_column('A:A', 36, bold)
1143
+ worksheet.set_column('B:B', 20 )
1144
+ worksheet.set_row(0, 40 )
1145
+
1146
+ heading = workbook.add_format(
1147
+ :bold => 1,
1148
+ :color => 'blue',
1149
+ :size => 16,
1150
+ :merge => 1,
1151
+ :align => 'vcenter'
1152
+ )
1153
+
1154
+ headings = ['Features of Spreadsheet::WriteExcel', '']
1155
+ worksheet.write_row('A1', headings, heading)
1156
+
1157
+
1158
+ #######################################################################
1159
+ #
1160
+ # Some text examples
1161
+ #
1162
+ text_format = workbook.add_format(
1163
+ :bold => 1,
1164
+ :italic => 1,
1165
+ :color => 'red',
1166
+ :size => 18,
1167
+ :font =>'Lucida Calligraphy'
1168
+ )
1169
+
1170
+ # A phrase in Cyrillic
1171
+ unicode = [
1172
+ "042d0442043e002004440440043004370430002004"+
1173
+ "3d043000200440044304410441043a043e043c0021"
1174
+ ].pack('H*')
1175
+
1176
+ worksheet.write('A2', "Text")
1177
+ worksheet.write('B2', "Hello Excel")
1178
+ worksheet.write('A3', "Formatted text")
1179
+ worksheet.write('B3', "Hello Excel", text_format)
1180
+ worksheet.write('A4', "Unicode text")
1181
+ worksheet.write_utf16be_string('B4', unicode)
1182
+
1183
+
1184
+ #######################################################################
1185
+ #
1186
+ # Some numeric examples
1187
+ #
1188
+ num1_format = workbook.add_format(:num_format => '$#,##0.00')
1189
+ num2_format = workbook.add_format(:num_format => ' d mmmm yyy')
1190
+
1191
+ worksheet.write('A5', "Numbers")
1192
+ worksheet.write('B5', 1234.56)
1193
+ worksheet.write('A6', "Formatted numbers")
1194
+ worksheet.write('B6', 1234.56, num1_format)
1195
+ worksheet.write('A7', "Formatted numbers")
1196
+ worksheet.write('B7', 37257, num2_format)
1197
+
1198
+
1199
+ #######################################################################
1200
+ #
1201
+ # Formulae
1202
+ #
1203
+ worksheet.set_selection('B8')
1204
+ worksheet.write('A8', 'Formulas and functions, "=SIN(PI()/4)"')
1205
+ worksheet.write('B8', '=SIN(PI()/4)')
1206
+
1207
+
1208
+ #######################################################################
1209
+ #
1210
+ # Hyperlinks
1211
+ #
1212
+ worksheet.write('A9', "Hyperlinks")
1213
+ worksheet.write('B9', 'http://www.perl.com/' )
1214
+
1215
+
1216
+ #######################################################################
1217
+ #
1218
+ # Images
1219
+ #
1220
+ worksheet.write('A10', "Images")
1221
+ worksheet.insert_image('B10', File.join(TEST_DIR,'republic.png'), 16, 8)
1222
+
1223
+
1224
+ #######################################################################
1225
+ #
1226
+ # Misc
1227
+ #
1228
+ worksheet.write('A18', "Page/printer setup")
1229
+ worksheet.write('A19', "Multiple worksheets")
1230
+
1231
+ workbook.close
1232
+
1233
+ # do assertion
1234
+ compare_file("#{PERL_OUTDIR}/demo.xls", @filename)
1235
+ end
1236
+
1237
+ def test_unicode_cyrillic
1238
+ # Create a Russian worksheet name in utf8.
1239
+ sheet = [0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
1240
+ 0x0446, 0x0430].pack("U*")
1241
+
1242
+ # Create a Russian string.
1243
+ str = [0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
1244
+ 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
1245
+ 0x0438, 0x0440, 0x0021].pack("U*")
1246
+
1247
+ workbook = Spreadsheet::WriteExcel.new(@filename)
1248
+ worksheet = workbook.add_worksheet(sheet + '1')
1249
+
1250
+ worksheet.set_column('A:A', 18)
1251
+ worksheet.write('A1', str)
1252
+
1253
+ workbook.close
1254
+
1255
+ # do assertion
1256
+ compare_file("#{PERL_OUTDIR}/unicode_cyrillic.xls", @filename)
1257
+ end
1258
+
1259
+ def compare_file(expected, target)
1260
+ fh_e = File.open(expected, "r")
1261
+ fh_t = File.open(target, "r")
1262
+ while true do
1263
+ e1 = fh_e.read(1)
1264
+ t1 = fh_t.read(1)
1265
+ if e1.nil?
1266
+ assert( t1.nil?, "#{expexted} is EOF but #{target} is NOT EOF.")
1267
+ break
1268
+ elsif t1.nil?
1269
+ assert( e1.nil?, '#{target} is EOF but #{expected} is NOT EOF.')
1270
+ break
1271
+ end
1272
+ assert_equal(e1, t1, sprintf(" #{expected} = '%s' but #{target} = '%s'", e1, t1))
1273
+ break
1274
+ end
1275
+ fh_e.close
1276
+ fh_t.close
1277
+ end
1278
+
1279
+
1280
+ end