WriteExcel 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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,1264 @@
1
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require "test/unit"
4
+ require "WriteExcel"
5
+
6
+ class TC_Format < 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
+ @ruby_file = File.join(Dir.tmpdir, path)
15
+ @format = Format.new
16
+ end
17
+
18
+ def teardown
19
+ File.delete(@ruby_file) if File.exist?(@ruby_file)
20
+ @format = nil
21
+ end
22
+
23
+ def test_set_format_properties
24
+ end
25
+
26
+ def test_format_properties_with_valid_value
27
+ # set_format_properties( propty => val )
28
+ valid_properties = get_valid_format_properties
29
+ valid_properties.each do |k,v|
30
+ fmt = Format.new
31
+ before = get_format_property(fmt)
32
+ fmt.set_format_properties(k => v)
33
+ after = get_format_property(fmt)
34
+ after.delete_if {|key, val| before[key] == val }
35
+ assert_equal(1, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
36
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
37
+ end
38
+
39
+ # set_format_properties( propty_1 => val1, propty_2 => val2)
40
+ valid_properties.each do |k,v|
41
+ fmt = Format.new
42
+ before = get_format_property(fmt)
43
+ fmt.set_format_properties(k => v, :bold => 1)
44
+ after = get_format_property(fmt)
45
+ after.delete_if {|key, val| before[key] == val }
46
+ assert_equal(2, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
47
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
48
+ assert_equal(700, after[:bold])
49
+ end
50
+
51
+ # set_format_properties( hash_variable )
52
+ valid_properties = get_valid_format_properties
53
+ valid_properties.each do |k,v|
54
+ arg = {k => v}
55
+ fmt = Format.new
56
+ before = get_format_property(fmt)
57
+ fmt.set_format_properties(arg)
58
+ after = get_format_property(fmt)
59
+ after.delete_if {|key, val| before[key] == val }
60
+ assert_equal(1, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
61
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
62
+ end
63
+
64
+ # set_format_properties( hash_variable, hash_variable... )
65
+ valid_properties = get_valid_format_properties
66
+ valid_properties.each do |k,v|
67
+ arg = {k => v}
68
+ arg2 = {:bold => 1}
69
+ fmt = Format.new
70
+ before = get_format_property(fmt)
71
+ fmt.set_format_properties(arg, arg2)
72
+ after = get_format_property(fmt)
73
+ after.delete_if {|key, val| before[key] == val }
74
+ assert_equal(2, after.size, "change 1 property[:#{k}] but #{after.size} was changed.#{after.inspect}")
75
+ assert_equal(v, after[k], "[:#{k}] doesn't match.")
76
+ assert_equal(700, after[:bold])
77
+ end
78
+
79
+ # set_color by string
80
+ valid_color_string_number = get_valid_color_string_number
81
+ [:color , :bg_color, :fg_color].each do |coltype|
82
+ valid_color_string_number.each do |str, num|
83
+ fmt = Format.new
84
+ before = get_format_property(fmt)
85
+ fmt.set_format_properties(coltype => str)
86
+ after = get_format_property(fmt)
87
+ after.delete_if {|key, val| before[key] == val }
88
+ assert_equal(1, after.size, "change 1 property[:#{coltype}:#{str}] but #{after.size} was changed.#{after.inspect}")
89
+ assert_equal(num, after[:"#{coltype}"], "[:#{coltype}:#{str}] doesn't match.")
90
+ end
91
+ end
92
+
93
+
94
+ end
95
+
96
+ def test_format_properties_with_invalid_value
97
+ end
98
+
99
+ def test_set_font
100
+ end
101
+
102
+ =begin
103
+ set_size()
104
+ Default state: Font size is 10
105
+ Default action: Set font size to 1
106
+ Valid args: Integer values from 1 to as big as your screen.
107
+ Set the font size. Excel adjusts the height of a row to accommodate the largest font size in the row. You can also explicitly specify the height of a row using the set_row() worksheet method.
108
+ =end
109
+ def test_set_size
110
+ # default state
111
+ assert_equal(10, @format.size)
112
+
113
+ # valid size from low to high
114
+ [1, 100, 100**10].each do |size|
115
+ fmt = Format.new
116
+ fmt.set_size(size)
117
+ assert_equal(size, fmt.size, "valid size:#{size} - doesn't match.")
118
+ end
119
+
120
+ # invalid size -- size doesn't change
121
+ [-1, 0, 1/2.0, 'hello', true, false, nil, [0,0], {:invalid => "val"}].each do |size|
122
+ fmt = Format.new
123
+ default = fmt.size
124
+ fmt.set_size(size)
125
+ assert_equal(default, fmt.size, "size:#{size.inspect} doesn't match.")
126
+ end
127
+ end
128
+
129
+ =begin
130
+ set_color()
131
+
132
+ Default state: Excels default color, usually black
133
+ Default action: Set the default color
134
+ Valid args: Integers from 8..63 or the following strings:
135
+ 'black'
136
+ 'blue'
137
+ 'brown'
138
+ 'cyan'
139
+ 'gray'
140
+ 'green'
141
+ 'lime'
142
+ 'magenta'
143
+ 'navy'
144
+ 'orange'
145
+ 'pink'
146
+ 'purple'
147
+ 'red'
148
+ 'silver'
149
+ 'white'
150
+ 'yellow'
151
+
152
+ Set the font colour. The set_color() method is used as follows:
153
+
154
+ format = workbook.add_format()
155
+ format.set_color('red')
156
+ worksheet.write(0, 0, 'wheelbarrow', format)
157
+
158
+ Note: The set_color() method is used to set the colour of the font in a cell.
159
+ To set the colour of a cell use the set_bg_color() and set_pattern() methods.
160
+ =end
161
+ def test_set_color
162
+ # default state
163
+ default_col = 0x7FFF
164
+ assert_equal(default_col, @format.color)
165
+
166
+ # valid color
167
+ # set by string
168
+ str_num = get_valid_color_string_number
169
+ str_num.each do |str,num|
170
+ fmt = Format.new
171
+ fmt.set_color(str)
172
+ assert_equal(num, fmt.color)
173
+ end
174
+
175
+ # valid color
176
+ # set by number
177
+ [8, 36, 63].each do |color|
178
+ fmt = Format.new
179
+ fmt.set_color(color)
180
+ assert_equal(color, fmt.color)
181
+ end
182
+
183
+ # invalid color
184
+ ['color', :col, -1, 63.5, 10*10].each do |color|
185
+ fmt = Format.new
186
+ fmt.set_color(color)
187
+ assert_equal(default_col, fmt.color, "color : #{color}")
188
+ end
189
+
190
+ # invalid color ...but...
191
+ # 0 <= color < 8 then color += 8 in order to valid value
192
+ [0, 7.5].each do |color|
193
+ fmt = Format.new
194
+ fmt.set_color(color)
195
+ assert_equal((color + 8).to_i, fmt.color, "color : #{color}")
196
+ end
197
+
198
+
199
+ end
200
+
201
+ =begin
202
+ set_bold()
203
+
204
+ Default state: bold is off (internal value = 400)
205
+ Default action: Turn bold on
206
+ Valid args: 0, 1 [1]
207
+
208
+ Set the bold property of the font:
209
+
210
+ $format->set_bold(); # Turn bold on
211
+
212
+ [1] Actually, values in the range 100..1000 are also valid.
213
+ 400 is normal, 700 is bold and 1000 is very bold indeed.
214
+ It is probably best to set the value to 1 and use normal bold.
215
+ =end
216
+
217
+ def test_set_bold
218
+ # default state
219
+ assert_equal(400, @format.bold)
220
+
221
+ # valid weight
222
+ fmt = Format.new
223
+ fmt.set_bold
224
+ assert_equal(700, fmt.bold)
225
+ {0 => 400, 1 => 700, 100 => 100, 1000 => 1000}.each do |weight, value|
226
+ fmt = Format.new
227
+ fmt.set_bold(weight)
228
+ assert_equal(value, fmt.bold)
229
+ end
230
+
231
+ # invalid weight
232
+ [-1, 99, 1001, 'bold'].each do |weight|
233
+ fmt = Format.new
234
+ fmt.set_bold(weight)
235
+ assert_equal(400, fmt.bold, "weight : #{weight}")
236
+ end
237
+ end
238
+
239
+ =begin
240
+ set_italic()
241
+
242
+ Default state: Italic is off
243
+ Default action: Turn italic on
244
+ Valid args: 0, 1
245
+
246
+ Set the italic property of the font:
247
+
248
+ format.set_italic() # Turn italic on
249
+ =end
250
+ def test_set_italic
251
+ # default state
252
+ assert_equal(0, @format.italic)
253
+
254
+ # valid arg
255
+ fmt = Format.new
256
+ fmt.set_italic
257
+ assert_equal(1, fmt.italic)
258
+ {0=>0, 1=>1}.each do |arg,value|
259
+ fmt = Format.new
260
+ fmt.set_italic(arg)
261
+ assert_equal(value, fmt.italic, "arg : #{arg}")
262
+ end
263
+
264
+ # invalid arg
265
+ [-1, 0.2, 100, 'italic', true, false, nil].each do |arg|
266
+ assert_raise(ArgumentError,
267
+ "set_italic(#{arg}) : invalid arg. arg must be 0, 1 or none."){
268
+ fmt = Format.new
269
+ fmt.set_italic(arg)
270
+ }
271
+ end
272
+ end
273
+
274
+ =begin
275
+ set_underline()
276
+
277
+ Default state: Underline is off
278
+ Default action: Turn on single underline
279
+ Valid args: 0 = No underline
280
+ 1 = Single underline
281
+ 2 = Double underline
282
+ 33 = Single accounting underline
283
+ 34 = Double accounting underline
284
+
285
+ Set the underline property of the font.
286
+
287
+ format.set_underline() # Single underline
288
+ =end
289
+ def test_set_underline
290
+ # default state
291
+ assert_equal(0, @format.underline, "default state")
292
+
293
+ # valid args
294
+ fmt = Format.new
295
+ fmt.set_underline
296
+ assert_equal(1, fmt.underline, "No arg")
297
+
298
+ [0, 1, 2, 33, 34].each do |arg|
299
+ fmt = Format.new
300
+ fmt.set_underline(arg)
301
+ assert_equal(arg, fmt.underline, "arg : #{arg}")
302
+ end
303
+
304
+ # invalid args
305
+ [-1, 0.2, 100, 'under', true, false, nil].each do |arg|
306
+ assert_raise(ArgumentError,
307
+ "set_underline(#{arg}) : arg must be 0, 1 or none, 2, 33, 34."){
308
+ fmt = Format.new
309
+ fmt.set_underline(arg)
310
+ }
311
+ end
312
+ end
313
+
314
+ =begin
315
+ set_font_strikeout()
316
+
317
+ Default state: Strikeout is off
318
+ Default action: Turn strikeout on
319
+ Valid args: 0, 1
320
+
321
+ Set the strikeout property of the font.
322
+ =end
323
+ def test_set_font_strikeout
324
+ # default state
325
+ assert_equal(0, @format.font_strikeout, "default state")
326
+
327
+ # valid args
328
+ fmt = Format.new
329
+ fmt.set_font_strikeout
330
+ assert_equal(1, fmt.font_strikeout, "No arg")
331
+
332
+ [0, 1].each do |arg|
333
+ fmt = Format.new
334
+ fmt.set_font_strikeout(arg)
335
+ assert_equal(arg, fmt.font_strikeout, "arg : #{arg}")
336
+ end
337
+
338
+ # invalid args
339
+ [-1, 0.2, 100, 'strikeout', true, false, nil].each do |arg|
340
+ assert_raise(ArgumentError,
341
+ "set_font_strikeout(#{arg}) : arg must be 0, 1 or none."){
342
+ fmt = Format.new
343
+ fmt.set_font_strikeout(arg)
344
+ }
345
+ end
346
+ end
347
+
348
+ =begin
349
+ set_font_script()
350
+
351
+ Default state: Super/Subscript is off
352
+ Default action: Turn Superscript on
353
+ Valid args: 0 = Normal
354
+ 1 = Superscript
355
+ 2 = Subscript
356
+
357
+ Set the superscript/subscript property of the font. This format is currently not very useful.
358
+ =end
359
+ def test_set_font_script
360
+ # default state
361
+ assert_equal(0, @format.font_script, "default state")
362
+
363
+ # valid args
364
+ fmt = Format.new
365
+ fmt.set_font_script
366
+ assert_equal(1, fmt.font_script, "No arg")
367
+
368
+ [0, 1, 2].each do |arg|
369
+ fmt = Format.new
370
+ fmt.set_font_script(arg)
371
+ assert_equal(arg, fmt.font_script, "arg : #{arg}")
372
+ end
373
+
374
+ # invalid args
375
+ [-1, 0.2, 100, 'script', true, false, nil].each do |arg|
376
+ assert_raise(ArgumentError,
377
+ "set_font_script(#{arg}) : arg must be 0, 1 or none, or 2."){
378
+ fmt = Format.new
379
+ fmt.set_font_script(arg)
380
+ }
381
+ end
382
+
383
+ end
384
+
385
+ =begin
386
+ set_font_outline()
387
+
388
+ Default state: Outline is off
389
+ Default action: Turn outline on
390
+ Valid args: 0, 1
391
+
392
+ Macintosh only.
393
+ =end
394
+ def test_set_font_outline
395
+ # default state
396
+ assert_equal(0, @format.font_outline, "default state")
397
+
398
+ # valid args
399
+ fmt = Format.new
400
+ fmt.set_font_outline
401
+ assert_equal(1, fmt.font_outline, "No arg")
402
+
403
+ [0, 1].each do |arg|
404
+ fmt = Format.new
405
+ fmt.set_font_outline(arg)
406
+ assert_equal(arg, fmt.font_outline, "arg : #{arg}")
407
+ end
408
+
409
+ # invalid args
410
+ [-1, 0.2, 100, 'outline', true, false, nil].each do |arg|
411
+ assert_raise(ArgumentError,
412
+ "set_font_outline(#{arg}) : arg must be 0, 1 or none."){
413
+ fmt = Format.new
414
+ fmt.set_font_outline(arg)
415
+ }
416
+ end
417
+ end
418
+
419
+ =begin
420
+ set_font_shadow()
421
+
422
+ Default state: Shadow is off
423
+ Default action: Turn shadow on
424
+ Valid args: 0, 1
425
+
426
+ Macintosh only.
427
+ =end
428
+ def test_set_font_shadow
429
+ # default state
430
+ assert_equal(0, @format.font_shadow, "default state")
431
+
432
+ # valid args
433
+ fmt = Format.new
434
+ fmt.set_font_shadow
435
+ assert_equal(1, fmt.font_shadow, "No arg")
436
+
437
+ [0, 1].each do |arg|
438
+ fmt = Format.new
439
+ fmt.set_font_shadow(arg)
440
+ assert_equal(arg, fmt.font_shadow, "arg : #{arg}")
441
+ end
442
+
443
+ # invalid args
444
+ [-1, 0.2, 100, 'shadow', true, false, nil].each do |arg|
445
+ assert_raise(ArgumentError,
446
+ "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
447
+ fmt = Format.new
448
+ fmt.set_font_shadow(arg)
449
+ }
450
+ end
451
+ end
452
+
453
+ =begin
454
+ set_num_format()
455
+
456
+ Default state: General format
457
+ Default action: Format index 1
458
+ Valid args: See the following table
459
+
460
+ This method is used to define the numerical format of a number in Excel. It controls whether a number is displayed as an integer, a floating point number, a date, a currency value or some other user defined format.
461
+ =end
462
+ def test_set_num_format
463
+ # default state
464
+ assert_equal(0, @format.num_format)
465
+
466
+ # Excel built in Format Index (0 .. 49)
467
+ [0, 49].each do |n|
468
+ fmt = Format.new
469
+ fmt.set_num_format(n)
470
+ assert_equal(n, fmt.num_format, "n: #{n}")
471
+ end
472
+
473
+ # Format string
474
+ ["#,##0", "m/d/yy", "hh:mm:ss"].each do |string|
475
+ fmt = Format.new
476
+ fmt.set_num_format(string)
477
+ assert_equal(string, fmt.num_format, "string: #{string}")
478
+ end
479
+ end
480
+
481
+ =begin
482
+ set_locked()
483
+
484
+ Default state: Cell locking is on
485
+ Default action: Turn locking on
486
+ Valid args: 0, 1
487
+
488
+ This property can be used to prevent modification of a cells
489
+ contents. Following Excel's convention, cell locking is
490
+ turned on by default. However, it only has an effect if
491
+ the worksheet has been protected, see the worksheet protect()
492
+ method.
493
+
494
+ locked = workbook.add_format()
495
+ locked.set_locked(1) # A non-op
496
+
497
+ unlocked = workbook.add_format()
498
+ locked.set_locked(0)
499
+
500
+ # Enable worksheet protection
501
+ worksheet.protect()
502
+
503
+ # This cell cannot be edited.
504
+ worksheet.write('A1', '=1+2', locked)
505
+
506
+ # This cell can be edited.
507
+ worksheet->write('A2', '=1+2', unlocked)
508
+
509
+ Note: This offers weak protection even with a password,
510
+ see the note in relation to the protect() method.
511
+ =end
512
+ def test_set_locked
513
+ # default state
514
+ assert_equal(1, @format.locked, "default state")
515
+
516
+ # valid args
517
+ fmt = Format.new
518
+ fmt.set_locked
519
+ assert_equal(1, fmt.locked, "No arg")
520
+
521
+ [0, 1].each do |arg|
522
+ fmt = Format.new
523
+ fmt.set_locked(arg)
524
+ assert_equal(arg, fmt.locked, "arg : #{arg}")
525
+ end
526
+
527
+ # invalid args
528
+ [-1, 0.2, 100, 'locked', true, false, nil].each do |arg|
529
+ assert_raise(ArgumentError,
530
+ "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
531
+ fmt = Format.new
532
+ fmt.set_locked(arg)
533
+ }
534
+ end
535
+ end
536
+ =begin
537
+ set_hidden()
538
+
539
+ Default state: Formula hiding is off
540
+ Default action: Turn hiding on
541
+ Valid args: 0, 1
542
+
543
+ This property is used to hide a formula while still displaying
544
+ its result. This is generally used to hide complex calculations
545
+ from end users who are only interested in the result.
546
+ It only has an effect if the worksheet has been protected,
547
+ see the worksheet protect() method.
548
+
549
+ my hidden = workbook.add_format()
550
+ hidden.set_hidden()
551
+
552
+ # Enable worksheet protection
553
+ worksheet.protect()
554
+
555
+ # The formula in this cell isn't visible
556
+ worksheet.write('A1', '=1+2', hidden)
557
+
558
+ Note: This offers weak protection even with a password,
559
+ see the note in relation to the protect() method.
560
+ =end
561
+ def test_set_hidden
562
+ # default state
563
+ assert_equal(0, @format.hidden, "default state")
564
+
565
+ # valid args
566
+ fmt = Format.new
567
+ fmt.set_hidden
568
+ assert_equal(1, fmt.hidden, "No arg")
569
+
570
+ [0, 1].each do |arg|
571
+ fmt = Format.new
572
+ fmt.set_hidden(arg)
573
+ assert_equal(arg, fmt.hidden, "arg : #{arg}")
574
+ end
575
+
576
+ # invalid args
577
+ [-1, 0.2, 100, 'hidden', true, false, nil].each do |arg|
578
+ assert_raise(ArgumentError,
579
+ "set_font_shadow(#{arg}) : arg must be 0, 1 or none."){
580
+ fmt = Format.new
581
+ fmt.set_hidden(arg)
582
+ }
583
+ end
584
+ end
585
+
586
+ =begin
587
+ set_align()
588
+
589
+ Default state: Alignment is off
590
+ Default action: Left alignment
591
+ Valid args: 'left' Horizontal
592
+ 'center'
593
+ 'right'
594
+ 'fill'
595
+ 'justify'
596
+ 'center_across'
597
+
598
+ 'top' Vertical
599
+ 'vcenter'
600
+ 'bottom'
601
+ 'vjustify'
602
+
603
+ This method is used to set the horizontal and vertical text alignment
604
+ within a cell. Vertical and horizontal alignments can be combined.
605
+ The method is used as follows:
606
+
607
+ my $format = $workbook->add_format();
608
+ $format->set_align('center');
609
+ $format->set_align('vcenter');
610
+ $worksheet->set_row(0, 30);
611
+ $worksheet->write(0, 0, 'X', $format);
612
+
613
+ Text can be aligned across two or more adjacent cells using
614
+ the center_across property. However, for genuine merged cells
615
+ it is better to use the merge_range() worksheet method.
616
+
617
+ The vjustify (vertical justify) option can be used to provide
618
+ automatic text wrapping in a cell. The height of the cell will be
619
+ adjusted to accommodate the wrapped text. To specify where the text
620
+ wraps use the set_text_wrap() method.
621
+ =end
622
+ def test_set_align
623
+ # default state
624
+ assert_equal(0, @format.text_h_align)
625
+ assert_equal(2, @format.text_v_align)
626
+
627
+ # valid arg
628
+ valid_args = {'left'=>1, 'center'=>2, 'centre'=>2, 'right'=>3,
629
+ 'fill'=>4, 'justify'=>5, 'center_across'=>6,
630
+ 'centre_across'=>6, 'merge'=>6,
631
+ 'top'=>0, 'vcenter'=>1, 'vcentre'=>1, 'bottom'=>2,
632
+ 'vjustify'=>3 }
633
+ valid_args.each do |arg, value|
634
+ fmt = Format.new
635
+ fmt.set_align(arg)
636
+ case arg
637
+ when 'left', 'center', 'centre', 'right', 'fill', 'justify',
638
+ 'center_across', 'centre_across', 'merge'
639
+ assert_equal(value, fmt.text_h_align, "arg: #{arg}")
640
+ when 'top', 'vcenter', 'vcentre', 'bottom', 'vjustify'
641
+ assert_equal(value, fmt.text_v_align, "arg: #{arg}")
642
+ end
643
+ end
644
+
645
+ # invalid arg
646
+ [-1, 0, 1.5, nil, true, false, ['left','top'], {'top'=>0}].each do |arg|
647
+ fmt = Format.new
648
+ val = get_format_property(fmt)
649
+ #print val.inspect
650
+ #exit
651
+ fmt.set_align(arg)
652
+ assert_equal(val[:align], fmt.text_h_align, "arg: #{arg} - text_h_align changed.")
653
+ assert_equal(val[:valign], fmt.text_v_align, "arg: #{arg} - text_v_align changed.")
654
+ end
655
+ end
656
+
657
+ =begin
658
+ set_center_across()
659
+
660
+ Default state: Center across selection is off
661
+ Default action: Turn center across on
662
+ Valid args: 1
663
+
664
+ Text can be aligned across two or more adjacent cells
665
+ using the set_center_across() method. This is an alias
666
+ for the set_align('center_across') method call.
667
+
668
+ Only one cell should contain the text,
669
+ the other cells should be blank:
670
+
671
+ format = workbook.add_format()
672
+ format.set_center_across()
673
+
674
+ worksheet.write(1, 1, 'Center across selection', format)
675
+ worksheet.write_blank(1, 2, format)
676
+ =end
677
+ def test_set_center_across
678
+ # default state
679
+ assert_equal(0, @format.text_h_align)
680
+
681
+ # method call then center_across is set. if arg is none, numeric, string, whatever.
682
+ @format.set_center_across
683
+ assert_equal(6, @format.text_h_align)
684
+ end
685
+
686
+ =begin
687
+ set_text_wrap()
688
+
689
+ Default state: Text wrap is off
690
+ Default action: Turn text wrap on
691
+ Valid args: 0, 1
692
+
693
+ Here is an example using the text wrap property, the escape
694
+ character \n is used to indicate the end of line:
695
+
696
+ format = workbook.add_format()
697
+ format.set_text_wrap()
698
+ worksheet.write(0, 0, "It's\na bum\nwrap", format)
699
+ =end
700
+ def test_set_text_wrap
701
+ # default state
702
+ assert_equal(0, @format.text_wrap, "default state")
703
+
704
+ # valid args
705
+ fmt = Format.new
706
+ fmt.set_text_wrap
707
+ assert_equal(1, fmt.text_wrap, "No arg")
708
+
709
+ [0, 1].each do |arg|
710
+ fmt = Format.new
711
+ fmt.set_text_wrap(arg)
712
+ assert_equal(arg, fmt.text_wrap, "arg : #{arg}")
713
+ end
714
+
715
+ # invalid args
716
+ [-1, 0.2, 100, 'text_wrap', true, false, nil].each do |arg|
717
+ assert_raise(ArgumentError,
718
+ "set_text_wrap(#{arg}) : arg must be 0, 1 or none."){
719
+ fmt = Format.new
720
+ fmt.set_text_wrap(arg)
721
+ }
722
+ end
723
+ end
724
+
725
+ =begin
726
+ set_rotation()
727
+
728
+ Default state: Text rotation is off
729
+ Default action: None
730
+ Valid args: Integers in the range -90 to 90 and 270
731
+
732
+ Set the rotation of the text in a cell. The rotation can be
733
+ any angle in the range -90 to 90 degrees.
734
+
735
+ format = workbook.add_format()
736
+ format.set_rotation(30)
737
+ worksheet.write(0, 0, 'This text is rotated', format)
738
+
739
+ The angle 270 is also supported. This indicates text where
740
+ the letters run from top to bottom.
741
+ =end
742
+ def test_set_rotation
743
+ # default state
744
+ assert(0, @format.rotation)
745
+
746
+ # # valid args -90 <= angle <= 90, 270 angle can be float or double
747
+ # [-90.0, 89, 0, 89, 90, 270].each do |angle|
748
+ # fmt = Format.new
749
+ # fmt.set_rotation(angle)
750
+ # assert_equal(angle, fmt.rotation, "angle: #{angle}")
751
+ # end
752
+ end
753
+
754
+ =begin
755
+ set_indent()
756
+
757
+ Default state: Text indentation is off
758
+ Default action: Indent text 1 level
759
+ Valid args: Positive integers
760
+
761
+ This method can be used to indent text. The argument, which should
762
+ be an integer, is taken as the level of indentation:
763
+
764
+ format = workbook.add_format()
765
+ format.set_indent(2)
766
+ worksheet.write(0, 0, 'This text is indented', format)
767
+
768
+ Indentation is a horizontal alignment property. It will override
769
+ any other horizontal properties but it can be used in conjunction
770
+ with vertical properties.
771
+ =end
772
+ def test_set_indent
773
+ # default state
774
+ assert_equal(0, @format.indent)
775
+
776
+ # valid arg -- Positive integers
777
+ [1, 10000000].each do |indent|
778
+ fmt = Format.new
779
+ fmt.set_indent(indent)
780
+ assert_equal(indent, fmt.indent, "indent: #{indent}")
781
+ end
782
+
783
+ # invalid arg
784
+ [].each do |indent|
785
+
786
+ end
787
+ end
788
+
789
+ =begin
790
+ set_shrink()
791
+
792
+ Default state: Text shrinking is off
793
+ Default action: Turn "shrink to fit" on
794
+ Valid args: 1
795
+
796
+ This method can be used to shrink text so that it fits in a cell.
797
+
798
+ format = workbook.add_format()
799
+ format.set_shrink()
800
+ worksheet.write(0, 0, 'Honey, I shrunk the text!', format)
801
+ =end
802
+ def test_set_shrink
803
+ # default state
804
+ assert_equal(0, @format.shrink)
805
+ end
806
+
807
+ =begin
808
+ set_text_justlast()
809
+
810
+ Default state: Justify last is off
811
+ Default action: Turn justify last on
812
+ Valid args: 0, 1
813
+
814
+ Only applies to Far Eastern versions of Excel.
815
+ =end
816
+ def test_set_text_justlast
817
+ # default state
818
+ assert_equal(0, @format.text_justlast)
819
+ end
820
+
821
+ =begin
822
+ set_pattern()
823
+
824
+ Default state: Pattern is off
825
+ Default action: Solid fill is on
826
+ Valid args: 0 .. 18
827
+
828
+ Set the background pattern of a cell.
829
+ =end
830
+ def test_set_pattern
831
+ # default state
832
+ assert_equal(0, @format.pattern)
833
+ end
834
+
835
+ =begin
836
+ set_bg_color()
837
+
838
+ Default state: Color is off
839
+ Default action: Solid fill.
840
+ Valid args: See set_color()
841
+
842
+ The set_bg_color() method can be used to set the background
843
+ colour of a pattern. Patterns are defined via the set_pattern()
844
+ method. If a pattern hasn't been defined then a solid fill
845
+ pattern is used as the default.
846
+
847
+ Here is an example of how to set up a solid fill in a cell:
848
+
849
+ format = workbook.add_format()
850
+ format.set_pattern() # This is optional when using a solid fill
851
+ format.set_bg_color('green')
852
+ worksheet.write('A1', 'Ray', format)
853
+ =end
854
+ def test_set_bg_color
855
+ end
856
+
857
+ =begin
858
+ set_fg_color()
859
+
860
+ Default state: Color is off
861
+ Default action: Solid fill.
862
+ Valid args: See set_color()
863
+
864
+ The set_fg_color() method can be used to set
865
+ the foreground colour of a pattern.
866
+ =end
867
+ def test_set_fg_color
868
+ end
869
+
870
+ =begin
871
+ set_border()
872
+
873
+ Also applies to: set_bottom()
874
+ set_top()
875
+ set_left()
876
+ set_right()
877
+
878
+ Default state: Border is off
879
+ Default action: Set border type 1
880
+ Valid args: 0-13, See below.
881
+
882
+ A cell border is comprised of a border on the bottom, top,
883
+ left and right. These can be set to the same value using
884
+ set_border() or individually using the relevant method
885
+ calls shown above.
886
+
887
+ The following shows the border styles sorted
888
+ by Spreadsheet::WriteExcel index number:
889
+
890
+ Index Name Weight Style
891
+ ===== ============= ====== ===========
892
+ 0 None 0
893
+ 1 Continuous 1 -----------
894
+ 2 Continuous 2 -----------
895
+ 3 Dash 1 - - - - - -
896
+ 4 Dot 1 . . . . . .
897
+ 5 Continuous 3 -----------
898
+ 6 Double 3 ===========
899
+ 7 Continuous 0 -----------
900
+ 8 Dash 2 - - - - - -
901
+ 9 Dash Dot 1 - . - . - .
902
+ 10 Dash Dot 2 - . - . - .
903
+ 11 Dash Dot Dot 1 - . . - . .
904
+ 12 Dash Dot Dot 2 - . . - . .
905
+ 13 SlantDash Dot 2 / - . / - .
906
+
907
+ The following shows the borders sorted by style:
908
+
909
+ Name Weight Style Index
910
+ ============= ====== =========== =====
911
+ Continuous 0 ----------- 7
912
+ Continuous 1 ----------- 1
913
+ Continuous 2 ----------- 2
914
+ Continuous 3 ----------- 5
915
+ Dash 1 - - - - - - 3
916
+ Dash 2 - - - - - - 8
917
+ Dash Dot 1 - . - . - . 9
918
+ Dash Dot 2 - . - . - . 10
919
+ Dash Dot Dot 1 - . . - . . 11
920
+ Dash Dot Dot 2 - . . - . . 12
921
+ Dot 1 . . . . . . 4
922
+ Double 3 =========== 6
923
+ None 0 0
924
+ SlantDash Dot 2 / - . / - . 13
925
+
926
+ The following shows the borders in the order shown in the Excel Dialog.
927
+
928
+ Index Style Index Style
929
+ ===== ===== ===== =====
930
+ 0 None 12 - . . - . .
931
+ 7 ----------- 13 / - . / - .
932
+ 4 . . . . . . 10 - . - . - .
933
+ 11 - . . - . . 8 - - - - - -
934
+ 9 - . - . - . 2 -----------
935
+ 3 - - - - - - 5 -----------
936
+ 1 ----------- 6 ===========
937
+ =end
938
+ def test_set_border
939
+ end
940
+
941
+ =begin
942
+ set_border_color()
943
+
944
+ Also applies to: set_bottom_color()
945
+ set_top_color()
946
+ set_left_color()
947
+ set_right_color()
948
+
949
+ Default state: Color is off
950
+ Default action: Undefined
951
+ Valid args: See set_color()
952
+
953
+ Set the colour of the cell borders. A cell border is comprised of a border
954
+ on the bottom, top, left and right. These can be set to the same colour
955
+ using set_border_color() or individually using the relevant method
956
+ calls shown above.
957
+ =end
958
+ def test_set_border_color
959
+ end
960
+
961
+ =begin
962
+ copy($format)
963
+
964
+ This method is used to copy all of the properties
965
+ from one Format object to another:
966
+
967
+ lorry1 = workbook.add_format()
968
+ lorry1.set_bold()
969
+ lorry1.set_italic()
970
+ lorry1.set_color('red') # lorry1 is bold, italic and red
971
+
972
+ my lorry2 = workbook.add_format()
973
+ lorry2.copy(lorry1)
974
+ lorry2.set_color('yellow') # lorry2 is bold, italic and yellow
975
+
976
+ The copy() method is only useful if you are using the method interface
977
+ to Format properties. It generally isn't required if you are setting
978
+ Format properties directly using hashes.
979
+
980
+ Note: this is not a copy constructor, both objects must exist prior to copying.
981
+ =end
982
+
983
+ def test_xf_biff_size
984
+ perl_file = "#{PERL_OUTDIR}/file_xf_biff"
985
+ size = File.size(perl_file)
986
+ @fh = File.new(@ruby_file,"w+")
987
+ @fh.print(@format.get_xf)
988
+ @fh.close
989
+ rsize = File.size(@ruby_file)
990
+ assert_equal(size,rsize,"File sizes not the same")
991
+
992
+ end
993
+
994
+ # Because of the modifications to bg_color and fg_color, I know this
995
+ # test will fail. This is ok.
996
+ #def test_xf_biff_contents
997
+ # perl_file = "perl_output/f_xf_biff"
998
+ # @fh = File.new(@ruby_file,"w+")
999
+ # @fh.print(@format.xf_biff)
1000
+ # @fh.close
1001
+ # contents = IO.readlines(perl_file)
1002
+ # rcontents = IO.readlines(@ruby_file)
1003
+ # assert_equal(contents,rcontents,"Contents not the same")
1004
+ #end
1005
+
1006
+ def test_font_biff_size
1007
+ perl_file = "#{PERL_OUTDIR}/file_font_biff"
1008
+ @fh = File.new(@ruby_file,"w+")
1009
+ @fh.print(@format.get_font)
1010
+ @fh.close
1011
+ contents = IO.readlines(perl_file)
1012
+ rcontents = IO.readlines(@ruby_file)
1013
+ assert_equal(contents,rcontents,"Contents not the same")
1014
+ end
1015
+
1016
+ def test_font_biff_contents
1017
+ perl_file = "#{PERL_OUTDIR}/file_font_biff"
1018
+ @fh = File.new(@ruby_file,"w+")
1019
+ @fh.print(@format.get_font)
1020
+ @fh.close
1021
+ contents = IO.readlines(perl_file)
1022
+ rcontents = IO.readlines(@ruby_file)
1023
+ assert_equal(contents,rcontents,"Contents not the same")
1024
+ end
1025
+
1026
+ def test_get_font_key_size
1027
+ perl_file = "#{PERL_OUTDIR}/file_font_key"
1028
+ @fh = File.new(@ruby_file,"w+")
1029
+ @fh.print(@format.get_font_key)
1030
+ @fh.close
1031
+ assert_equal(File.size(perl_file),File.size(@ruby_file),"Bad file size")
1032
+ end
1033
+
1034
+ def test_get_font_key_contents
1035
+ perl_file = "#{PERL_OUTDIR}/file_font_key"
1036
+ @fh = File.new(@ruby_file,"w+")
1037
+ @fh.print(@format.get_font_key)
1038
+ @fh.close
1039
+ contents = IO.readlines(perl_file)
1040
+ rcontents = IO.readlines(@ruby_file)
1041
+ assert_equal(contents,rcontents,"Contents not the same")
1042
+ end
1043
+
1044
+ def test_initialize
1045
+ assert_nothing_raised {
1046
+ Format.new(:bold => true, :size => 10, :color => 'black',
1047
+ :fg_color => 43, :align => 'top', :text_wrap => true,
1048
+ :border => 1)
1049
+ }
1050
+ end
1051
+
1052
+ # added by Nakamura
1053
+
1054
+ def test_get_xf
1055
+ perl_file = "#{PERL_OUTDIR}/file_xf_biff"
1056
+ size = File.size(perl_file)
1057
+ @fh = File.new(@ruby_file,"w+")
1058
+ @fh.print(@format.get_xf)
1059
+ @fh.close
1060
+ rsize = File.size(@ruby_file)
1061
+ assert_equal(size,rsize,"File sizes not the same")
1062
+
1063
+ fh_p = File.open(perl_file, "r")
1064
+ fh_r = File.open(@ruby_file, "r")
1065
+ while true do
1066
+ p1 = fh_p.read(1)
1067
+ r1 = fh_r.read(1)
1068
+ if p1.nil?
1069
+ assert( r1.nil?, 'p1 is EOF but r1 is NOT EOF.')
1070
+ break
1071
+ elsif r1.nil?
1072
+ assert( p1.nil?, 'r1 is EOF but p1 is NOT EOF.')
1073
+ break
1074
+ end
1075
+ assert_equal(p1, r1, sprintf(" p1 = %s but r1 = %s", p1, r1))
1076
+ break
1077
+ end
1078
+ fh_p.close
1079
+ fh_r.close
1080
+ end
1081
+
1082
+ def test_get_font
1083
+ perl_file = "#{PERL_OUTDIR}/file_font_biff"
1084
+ size = File.size(perl_file)
1085
+ @fh = File.new(@ruby_file,"w+")
1086
+ @fh.print(@format.get_font)
1087
+ @fh.close
1088
+ rsize = File.size(@ruby_file)
1089
+ assert_equal(size,rsize,"File sizes not the same")
1090
+
1091
+ fh_p = File.open(perl_file, "r")
1092
+ fh_r = File.open(@ruby_file, "r")
1093
+ while true do
1094
+ p1 = fh_p.read(1)
1095
+ r1 = fh_r.read(1)
1096
+ if p1.nil?
1097
+ assert( r1.nil?, 'p1 is EOF but r1 is NOT EOF.')
1098
+ break
1099
+ elsif r1.nil?
1100
+ assert( p1.nil?, 'r1 is EOF but p1 is NOT EOF.')
1101
+ break
1102
+ end
1103
+ assert_equal(p1, r1, sprintf(" p1 = %s but r1 = %s", p1, r1))
1104
+ break
1105
+ end
1106
+ fh_p.close
1107
+ fh_r.close
1108
+ end
1109
+
1110
+ def test_get_font_key
1111
+ perl_file = "#{PERL_OUTDIR}/file_font_key"
1112
+ size = File.size(perl_file)
1113
+ @fh = File.new(@ruby_file,"w+")
1114
+ @fh.print(@format.get_font_key)
1115
+ @fh.close
1116
+ rsize = File.size(@ruby_file)
1117
+ assert_equal(size,rsize,"File sizes not the same")
1118
+
1119
+ fh_p = File.open(perl_file, "r")
1120
+ fh_r = File.open(@ruby_file, "r")
1121
+ while true do
1122
+ p1 = fh_p.read(1)
1123
+ r1 = fh_r.read(1)
1124
+ if p1.nil?
1125
+ assert( r1.nil?, 'p1 is EOF but r1 is NOT EOF.')
1126
+ break
1127
+ elsif r1.nil?
1128
+ assert( p1.nil?, 'r1 is EOF but p1 is NOT EOF.')
1129
+ break
1130
+ end
1131
+ assert_equal(p1, r1, sprintf(" p1 = %s but r1 = %s", p1, r1))
1132
+ break
1133
+ end
1134
+ fh_p.close
1135
+ fh_r.close
1136
+ end
1137
+
1138
+ def test_get_xf_index
1139
+ end
1140
+
1141
+ def test_get_color
1142
+ end
1143
+
1144
+ def test_method_missing
1145
+ end
1146
+
1147
+ def test_copy
1148
+ format1 = Format.new
1149
+ format2 = Format.new
1150
+
1151
+ format1.set_size(12)
1152
+
1153
+ format2.copy(format1)
1154
+
1155
+ assert_equal(format1.size, format2.size)
1156
+ end
1157
+
1158
+
1159
+ # -----------------------------------------------------------------------------
1160
+
1161
+ def get_valid_format_properties
1162
+ {
1163
+ :font => 'Times New Roman',
1164
+ :size => 30,
1165
+ :color => 8,
1166
+ :italic => 1,
1167
+ :underline => 1,
1168
+ :font_strikeout => 1,
1169
+ :font_script => 1,
1170
+ :font_outline => 1,
1171
+ :font_shadow => 1,
1172
+ :locked => 0,
1173
+ :hidden => 1,
1174
+ :text_wrap => 1,
1175
+ :text_justlast => 1,
1176
+ :indent => 2,
1177
+ :shrink => 1,
1178
+ :pattern => 18,
1179
+ :bg_color => 30,
1180
+ :fg_color => 63
1181
+ }
1182
+ end
1183
+
1184
+ def get_valid_color_string_number
1185
+ return {
1186
+ 'black' => 8,
1187
+ 'blue' => 12,
1188
+ 'brown' => 16,
1189
+ 'cyan' => 15,
1190
+ 'gray' => 23,
1191
+ 'green' => 17,
1192
+ 'lime' => 11,
1193
+ 'magenta' => 14,
1194
+ 'navy' => 18,
1195
+ 'orange' => 53,
1196
+ 'pink' => 33,
1197
+ 'purple' => 20,
1198
+ 'red' => 10,
1199
+ 'silver' => 22,
1200
+ 'white' => 9,
1201
+ 'yellow' => 13
1202
+ }
1203
+ end
1204
+ # :rotation => -90,
1205
+ # :center_across => 1,
1206
+ # :align => 'left',
1207
+
1208
+ def get_format_property(format)
1209
+ text_h_align = {
1210
+ 1 => 'left',
1211
+ 2 => 'center/centre',
1212
+ 3 => 'right',
1213
+ 4 => 'fill',
1214
+ 5 => 'justiry',
1215
+ 6 => 'center_across/centre_across/merge',
1216
+ 7 => 'distributed/equal_space'
1217
+ }
1218
+
1219
+ text_v_align = {
1220
+ 0 => 'top',
1221
+ 1 => 'vcenter/vcentre',
1222
+ 2 => 'bottom',
1223
+ 3 => 'vjustify',
1224
+ 4 => 'vdistributed/vequal_space'
1225
+ }
1226
+
1227
+ return {
1228
+ :font => format.font,
1229
+ :size => format.size,
1230
+ :color => format.color,
1231
+ :bold => format.bold,
1232
+ :italic => format.italic,
1233
+ :underline => format.underline,
1234
+ :font_strikeout => format.font_strikeout,
1235
+ :font_script => format.font_script,
1236
+ :font_outline => format.font_outline,
1237
+ :font_shadow => format.font_shadow,
1238
+ :num_format => format.num_format,
1239
+ :locked => format.locked,
1240
+ :hidden => format.hidden,
1241
+ :align => format.text_h_align,
1242
+ :valign => format.text_v_align,
1243
+ :rotation => format.rotation,
1244
+ :text_wrap => format.text_wrap,
1245
+ :text_justlast => format.text_justlast,
1246
+ :center_across => format.text_h_align,
1247
+ :indent => format.indent,
1248
+ :shrink => format.shrink,
1249
+ :pattern => format.pattern,
1250
+ :bg_color => format.bg_color,
1251
+ :fg_color => format.fg_color,
1252
+ :border => format.border,
1253
+ :bottom => format.bottom,
1254
+ :top => format.top,
1255
+ :left => format.left,
1256
+ :right => format.right,
1257
+ :bottom_color => format.bottom_color,
1258
+ :top_color => format.top_color,
1259
+ :left_color => format.left_color,
1260
+ :right_color => format.right_color
1261
+ }
1262
+ end
1263
+
1264
+ end