writeexcel 0.1.0 → 0.3.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 (143) hide show
  1. data/README +26 -31
  2. data/examples/a_simple.rb +42 -42
  3. data/examples/{autofilters.rb → autofilter.rb} +264 -266
  4. data/examples/bigfile.rb +29 -0
  5. data/examples/chart_area.rb +120 -0
  6. data/examples/chart_bar.rb +119 -0
  7. data/examples/chart_column.rb +119 -0
  8. data/examples/chart_line.rb +119 -0
  9. data/examples/chart_pie.rb +107 -0
  10. data/examples/chart_scatter.rb +120 -0
  11. data/examples/chart_stock.rb +147 -0
  12. data/examples/copyformat.rb +51 -51
  13. data/examples/data_validate.rb +278 -278
  14. data/examples/date_time.rb +86 -86
  15. data/examples/defined_name.rb +31 -0
  16. data/examples/demo.rb +120 -118
  17. data/examples/diag_border.rb +35 -35
  18. data/examples/formats.rb +489 -489
  19. data/examples/header.rb +136 -136
  20. data/examples/hidden.rb +28 -28
  21. data/examples/hyperlink.rb +42 -42
  22. data/examples/images.rb +52 -52
  23. data/examples/merge1.rb +39 -39
  24. data/examples/merge2.rb +44 -44
  25. data/examples/merge3.rb +65 -65
  26. data/examples/merge4.rb +82 -82
  27. data/examples/merge5.rb +79 -79
  28. data/examples/properties.rb +33 -0
  29. data/examples/properties_jp.rb +32 -0
  30. data/examples/protection.rb +46 -46
  31. data/examples/regions.rb +52 -52
  32. data/examples/repeat.rb +42 -42
  33. data/examples/stats.rb +75 -75
  34. data/examples/stocks.rb +80 -80
  35. data/examples/tab_colors.rb +30 -30
  36. data/examples/write_arrays.rb +82 -0
  37. data/lib/writeexcel.rb +1134 -18
  38. data/lib/writeexcel/biffwriter.rb +273 -260
  39. data/lib/writeexcel/chart.rb +2306 -217
  40. data/lib/writeexcel/charts/area.rb +152 -0
  41. data/lib/writeexcel/charts/bar.rb +177 -0
  42. data/lib/writeexcel/charts/column.rb +156 -0
  43. data/lib/writeexcel/charts/external.rb +61 -0
  44. data/lib/writeexcel/charts/line.rb +152 -0
  45. data/lib/writeexcel/charts/pie.rb +169 -0
  46. data/lib/writeexcel/charts/scatter.rb +192 -0
  47. data/lib/writeexcel/charts/stock.rb +211 -0
  48. data/lib/writeexcel/excelformulaparser.rb +208 -195
  49. data/lib/writeexcel/format.rb +1697 -1108
  50. data/lib/writeexcel/formula.rb +1050 -986
  51. data/lib/writeexcel/olewriter.rb +322 -322
  52. data/lib/writeexcel/properties.rb +251 -250
  53. data/lib/writeexcel/storage_lite.rb +968 -0
  54. data/lib/writeexcel/workbook.rb +3294 -2630
  55. data/lib/writeexcel/worksheet.rb +9012 -6377
  56. data/test/excelfile/Chart1.xls +0 -0
  57. data/test/excelfile/Chart2.xls +0 -0
  58. data/test/excelfile/Chart3.xls +0 -0
  59. data/test/excelfile/Chart4.xls +0 -0
  60. data/test/excelfile/Chart5.xls +0 -0
  61. data/test/perl_output/Chart1.xls.data +0 -0
  62. data/test/perl_output/Chart2.xls.data +0 -0
  63. data/test/perl_output/Chart3.xls.data +0 -0
  64. data/test/perl_output/Chart4.xls.data +0 -0
  65. data/test/perl_output/Chart5.xls.data +0 -0
  66. data/test/perl_output/a_simple.xls +0 -0
  67. data/test/perl_output/autofilter.xls +0 -0
  68. data/test/perl_output/chart_area.xls +0 -0
  69. data/test/perl_output/chart_bar.xls +0 -0
  70. data/test/perl_output/chart_column.xls +0 -0
  71. data/test/perl_output/chart_line.xls +0 -0
  72. data/test/perl_output/data_validate.xls +0 -0
  73. data/test/perl_output/date_time.xls +0 -0
  74. data/test/perl_output/demo.xls +0 -0
  75. data/test/perl_output/demo101.bin +0 -0
  76. data/test/perl_output/demo201.bin +0 -0
  77. data/test/perl_output/demo301.bin +0 -0
  78. data/test/perl_output/demo401.bin +0 -0
  79. data/test/perl_output/demo501.bin +0 -0
  80. data/test/perl_output/diag_border.xls +0 -0
  81. data/test/perl_output/headers.xls +0 -0
  82. data/test/perl_output/hyperlink.xls +0 -0
  83. data/test/perl_output/images.xls +0 -0
  84. data/test/perl_output/merge1.xls +0 -0
  85. data/test/perl_output/merge2.xls +0 -0
  86. data/test/perl_output/merge3.xls +0 -0
  87. data/test/perl_output/merge4.xls +0 -0
  88. data/test/perl_output/merge5.xls +0 -0
  89. data/test/perl_output/protection.xls +0 -0
  90. data/test/perl_output/regions.xls +0 -0
  91. data/test/perl_output/stats.xls +0 -0
  92. data/test/perl_output/stocks.xls +0 -0
  93. data/test/perl_output/tab_colors.xls +0 -0
  94. data/test/perl_output/unicode_cyrillic.xls +0 -0
  95. data/test/perl_output/workbook1.xls +0 -0
  96. data/test/perl_output/workbook2.xls +0 -0
  97. data/test/tc_all.rb +32 -31
  98. data/test/tc_biff.rb +104 -104
  99. data/test/tc_chart.rb +22 -22
  100. data/test/tc_example_match.rb +1944 -1280
  101. data/test/tc_format.rb +1254 -1267
  102. data/test/tc_formula.rb +63 -63
  103. data/test/tc_ole.rb +110 -110
  104. data/test/tc_storage_lite.rb +149 -0
  105. data/test/tc_workbook.rb +140 -115
  106. data/test/tc_worksheet.rb +115 -115
  107. data/test/test_00_IEEE_double.rb +14 -14
  108. data/test/test_01_add_worksheet.rb +12 -12
  109. data/test/test_02_merge_formats.rb +58 -58
  110. data/test/test_04_dimensions.rb +397 -397
  111. data/test/test_05_rows.rb +182 -182
  112. data/test/test_06_extsst.rb +80 -80
  113. data/test/test_11_date_time.rb +484 -484
  114. data/test/test_12_date_only.rb +506 -506
  115. data/test/test_13_date_seconds.rb +486 -486
  116. data/test/test_21_escher.rb +642 -629
  117. data/test/test_22_mso_drawing_group.rb +750 -739
  118. data/test/test_23_note.rb +78 -78
  119. data/test/test_24_txo.rb +80 -80
  120. data/test/test_25_position_object.rb +82 -0
  121. data/test/test_26_autofilter.rb +327 -327
  122. data/test/test_27_autofilter.rb +144 -144
  123. data/test/test_28_autofilter.rb +174 -174
  124. data/test/test_29_process_jpg.rb +681 -131
  125. data/test/test_30_validation_dval.rb +82 -82
  126. data/test/test_31_validation_dv_strings.rb +131 -131
  127. data/test/test_32_validation_dv_formula.rb +211 -211
  128. data/test/test_40_property_types.rb +191 -191
  129. data/test/test_41_properties.rb +238 -238
  130. data/test/test_42_set_properties.rb +442 -419
  131. data/test/test_50_name_stored.rb +305 -0
  132. data/test/test_51_name_print_area.rb +363 -0
  133. data/test/test_52_name_print_titles.rb +460 -0
  134. data/test/test_53_autofilter.rb +209 -0
  135. data/test/test_60_chart_generic.rb +576 -0
  136. data/test/test_61_chart_subclasses.rb +97 -0
  137. data/test/test_62_chart_formats.rb +270 -0
  138. data/test/test_63_chart_area_formats.rb +647 -0
  139. data/test/test_chartex.rb +35 -0
  140. data/test/ts_all.rb +46 -34
  141. data/writeexcel.gemspec +18 -0
  142. data/writeexcel.rdoc +583 -0
  143. metadata +162 -108
data/test/tc_format.rb CHANGED
@@ -1,1267 +1,1254 @@
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
- def test_copy
983
- end
984
-
985
-
986
- def test_xf_biff_size
987
- perl_file = "#{PERL_OUTDIR}/file_xf_biff"
988
- size = File.size(perl_file)
989
- @fh = File.new(@ruby_file,"w+")
990
- @fh.print(@format.get_xf)
991
- @fh.close
992
- rsize = File.size(@ruby_file)
993
- assert_equal(size,rsize,"File sizes not the same")
994
-
995
- end
996
-
997
- # Because of the modifications to bg_color and fg_color, I know this
998
- # test will fail. This is ok.
999
- #def test_xf_biff_contents
1000
- # perl_file = "perl_output/f_xf_biff"
1001
- # @fh = File.new(@ruby_file,"w+")
1002
- # @fh.print(@format.xf_biff)
1003
- # @fh.close
1004
- # contents = IO.readlines(perl_file)
1005
- # rcontents = IO.readlines(@ruby_file)
1006
- # assert_equal(contents,rcontents,"Contents not the same")
1007
- #end
1008
-
1009
- def test_font_biff_size
1010
- perl_file = "#{PERL_OUTDIR}/file_font_biff"
1011
- @fh = File.new(@ruby_file,"w+")
1012
- @fh.print(@format.get_font)
1013
- @fh.close
1014
- contents = IO.readlines(perl_file)
1015
- rcontents = IO.readlines(@ruby_file)
1016
- assert_equal(contents,rcontents,"Contents not the same")
1017
- end
1018
-
1019
- def test_font_biff_contents
1020
- perl_file = "#{PERL_OUTDIR}/file_font_biff"
1021
- @fh = File.new(@ruby_file,"w+")
1022
- @fh.print(@format.get_font)
1023
- @fh.close
1024
- contents = IO.readlines(perl_file)
1025
- rcontents = IO.readlines(@ruby_file)
1026
- assert_equal(contents,rcontents,"Contents not the same")
1027
- end
1028
-
1029
- def test_get_font_key_size
1030
- perl_file = "#{PERL_OUTDIR}/file_font_key"
1031
- @fh = File.new(@ruby_file,"w+")
1032
- @fh.print(@format.get_font_key)
1033
- @fh.close
1034
- assert_equal(File.size(perl_file),File.size(@ruby_file),"Bad file size")
1035
- end
1036
-
1037
- def test_get_font_key_contents
1038
- perl_file = "#{PERL_OUTDIR}/file_font_key"
1039
- @fh = File.new(@ruby_file,"w+")
1040
- @fh.print(@format.get_font_key)
1041
- @fh.close
1042
- contents = IO.readlines(perl_file)
1043
- rcontents = IO.readlines(@ruby_file)
1044
- assert_equal(contents,rcontents,"Contents not the same")
1045
- end
1046
-
1047
- def test_initialize
1048
- assert_nothing_raised {
1049
- Format.new(:bold => true, :size => 10, :color => 'black',
1050
- :fg_color => 43, :align => 'top', :text_wrap => true,
1051
- :border => 1)
1052
- }
1053
- end
1054
-
1055
- # added by Nakamura
1056
-
1057
- def test_get_xf
1058
- perl_file = "#{PERL_OUTDIR}/file_xf_biff"
1059
- size = File.size(perl_file)
1060
- @fh = File.new(@ruby_file,"w+")
1061
- @fh.print(@format.get_xf)
1062
- @fh.close
1063
- rsize = File.size(@ruby_file)
1064
- assert_equal(size,rsize,"File sizes not the same")
1065
-
1066
- fh_p = File.open(perl_file, "r")
1067
- fh_r = File.open(@ruby_file, "r")
1068
- while true do
1069
- p1 = fh_p.read(1)
1070
- r1 = fh_r.read(1)
1071
- if p1.nil?
1072
- assert( r1.nil?, 'p1 is EOF but r1 is NOT EOF.')
1073
- break
1074
- elsif r1.nil?
1075
- assert( p1.nil?, 'r1 is EOF but p1 is NOT EOF.')
1076
- break
1077
- end
1078
- assert_equal(p1, r1, sprintf(" p1 = %s but r1 = %s", p1, r1))
1079
- break
1080
- end
1081
- fh_p.close
1082
- fh_r.close
1083
- end
1084
-
1085
- def test_get_font
1086
- perl_file = "#{PERL_OUTDIR}/file_font_biff"
1087
- size = File.size(perl_file)
1088
- @fh = File.new(@ruby_file,"w+")
1089
- @fh.print(@format.get_font)
1090
- @fh.close
1091
- rsize = File.size(@ruby_file)
1092
- assert_equal(size,rsize,"File sizes not the same")
1093
-
1094
- fh_p = File.open(perl_file, "r")
1095
- fh_r = File.open(@ruby_file, "r")
1096
- while true do
1097
- p1 = fh_p.read(1)
1098
- r1 = fh_r.read(1)
1099
- if p1.nil?
1100
- assert( r1.nil?, 'p1 is EOF but r1 is NOT EOF.')
1101
- break
1102
- elsif r1.nil?
1103
- assert( p1.nil?, 'r1 is EOF but p1 is NOT EOF.')
1104
- break
1105
- end
1106
- assert_equal(p1, r1, sprintf(" p1 = %s but r1 = %s", p1, r1))
1107
- break
1108
- end
1109
- fh_p.close
1110
- fh_r.close
1111
- end
1112
-
1113
- def test_get_font_key
1114
- perl_file = "#{PERL_OUTDIR}/file_font_key"
1115
- size = File.size(perl_file)
1116
- @fh = File.new(@ruby_file,"w+")
1117
- @fh.print(@format.get_font_key)
1118
- @fh.close
1119
- rsize = File.size(@ruby_file)
1120
- assert_equal(size,rsize,"File sizes not the same")
1121
-
1122
- fh_p = File.open(perl_file, "r")
1123
- fh_r = File.open(@ruby_file, "r")
1124
- while true do
1125
- p1 = fh_p.read(1)
1126
- r1 = fh_r.read(1)
1127
- if p1.nil?
1128
- assert( r1.nil?, 'p1 is EOF but r1 is NOT EOF.')
1129
- break
1130
- elsif r1.nil?
1131
- assert( p1.nil?, 'r1 is EOF but p1 is NOT EOF.')
1132
- break
1133
- end
1134
- assert_equal(p1, r1, sprintf(" p1 = %s but r1 = %s", p1, r1))
1135
- break
1136
- end
1137
- fh_p.close
1138
- fh_r.close
1139
- end
1140
-
1141
- def test_get_xf_index
1142
- end
1143
-
1144
- def test_get_color
1145
- end
1146
-
1147
- def test_method_missing
1148
- end
1149
-
1150
- def test_copy
1151
- format1 = Format.new
1152
- format2 = Format.new
1153
-
1154
- format1.set_size(12)
1155
-
1156
- format2.copy(format1)
1157
-
1158
- assert_equal(format1.size, format2.size)
1159
- end
1160
-
1161
-
1162
- # -----------------------------------------------------------------------------
1163
-
1164
- def get_valid_format_properties
1165
- {
1166
- :font => 'Times New Roman',
1167
- :size => 30,
1168
- :color => 8,
1169
- :italic => 1,
1170
- :underline => 1,
1171
- :font_strikeout => 1,
1172
- :font_script => 1,
1173
- :font_outline => 1,
1174
- :font_shadow => 1,
1175
- :locked => 0,
1176
- :hidden => 1,
1177
- :text_wrap => 1,
1178
- :text_justlast => 1,
1179
- :indent => 2,
1180
- :shrink => 1,
1181
- :pattern => 18,
1182
- :bg_color => 30,
1183
- :fg_color => 63
1184
- }
1185
- end
1186
-
1187
- def get_valid_color_string_number
1188
- return {
1189
- 'black' => 8,
1190
- 'blue' => 12,
1191
- 'brown' => 16,
1192
- 'cyan' => 15,
1193
- 'gray' => 23,
1194
- 'green' => 17,
1195
- 'lime' => 11,
1196
- 'magenta' => 14,
1197
- 'navy' => 18,
1198
- 'orange' => 53,
1199
- 'pink' => 33,
1200
- 'purple' => 20,
1201
- 'red' => 10,
1202
- 'silver' => 22,
1203
- 'white' => 9,
1204
- 'yellow' => 13
1205
- }
1206
- end
1207
- # :rotation => -90,
1208
- # :center_across => 1,
1209
- # :align => 'left',
1210
-
1211
- def get_format_property(format)
1212
- text_h_align = {
1213
- 1 => 'left',
1214
- 2 => 'center/centre',
1215
- 3 => 'right',
1216
- 4 => 'fill',
1217
- 5 => 'justiry',
1218
- 6 => 'center_across/centre_across/merge',
1219
- 7 => 'distributed/equal_space'
1220
- }
1221
-
1222
- text_v_align = {
1223
- 0 => 'top',
1224
- 1 => 'vcenter/vcentre',
1225
- 2 => 'bottom',
1226
- 3 => 'vjustify',
1227
- 4 => 'vdistributed/vequal_space'
1228
- }
1229
-
1230
- return {
1231
- :font => format.font,
1232
- :size => format.size,
1233
- :color => format.color,
1234
- :bold => format.bold,
1235
- :italic => format.italic,
1236
- :underline => format.underline,
1237
- :font_strikeout => format.font_strikeout,
1238
- :font_script => format.font_script,
1239
- :font_outline => format.font_outline,
1240
- :font_shadow => format.font_shadow,
1241
- :num_format => format.num_format,
1242
- :locked => format.locked,
1243
- :hidden => format.hidden,
1244
- :align => format.text_h_align,
1245
- :valign => format.text_v_align,
1246
- :rotation => format.rotation,
1247
- :text_wrap => format.text_wrap,
1248
- :text_justlast => format.text_justlast,
1249
- :center_across => format.text_h_align,
1250
- :indent => format.indent,
1251
- :shrink => format.shrink,
1252
- :pattern => format.pattern,
1253
- :bg_color => format.bg_color,
1254
- :fg_color => format.fg_color,
1255
- :border => format.border,
1256
- :bottom => format.bottom,
1257
- :top => format.top,
1258
- :left => format.left,
1259
- :right => format.right,
1260
- :bottom_color => format.bottom_color,
1261
- :top_color => format.top_color,
1262
- :left_color => format.left_color,
1263
- :right_color => format.right_color
1264
- }
1265
- end
1266
-
1267
- end
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 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_copy
1139
+ format1 = Format.new
1140
+ format2 = Format.new
1141
+
1142
+ format1.set_size(12)
1143
+
1144
+ format2.copy(format1)
1145
+
1146
+ assert_equal(format1.size, format2.size)
1147
+ end
1148
+
1149
+
1150
+ # -----------------------------------------------------------------------------
1151
+
1152
+ def get_valid_format_properties
1153
+ {
1154
+ :font => 'Times New Roman',
1155
+ :size => 30,
1156
+ :color => 8,
1157
+ :italic => 1,
1158
+ :underline => 1,
1159
+ :font_strikeout => 1,
1160
+ :font_script => 1,
1161
+ :font_outline => 1,
1162
+ :font_shadow => 1,
1163
+ :locked => 0,
1164
+ :hidden => 1,
1165
+ :text_wrap => 1,
1166
+ :text_justlast => 1,
1167
+ :indent => 2,
1168
+ :shrink => 1,
1169
+ :pattern => 18,
1170
+ :bg_color => 30,
1171
+ :fg_color => 63
1172
+ }
1173
+ end
1174
+
1175
+ def get_valid_color_string_number
1176
+ return {
1177
+ 'black' => 8,
1178
+ 'blue' => 12,
1179
+ 'brown' => 16,
1180
+ 'cyan' => 15,
1181
+ 'gray' => 23,
1182
+ 'green' => 17,
1183
+ 'lime' => 11,
1184
+ 'magenta' => 14,
1185
+ 'navy' => 18,
1186
+ 'orange' => 53,
1187
+ 'pink' => 33,
1188
+ 'purple' => 20,
1189
+ 'red' => 10,
1190
+ 'silver' => 22,
1191
+ 'white' => 9,
1192
+ 'yellow' => 13
1193
+ }
1194
+ end
1195
+ # :rotation => -90,
1196
+ # :center_across => 1,
1197
+ # :align => 'left',
1198
+
1199
+ def get_format_property(format)
1200
+ text_h_align = {
1201
+ 1 => 'left',
1202
+ 2 => 'center/centre',
1203
+ 3 => 'right',
1204
+ 4 => 'fill',
1205
+ 5 => 'justiry',
1206
+ 6 => 'center_across/centre_across/merge',
1207
+ 7 => 'distributed/equal_space'
1208
+ }
1209
+
1210
+ text_v_align = {
1211
+ 0 => 'top',
1212
+ 1 => 'vcenter/vcentre',
1213
+ 2 => 'bottom',
1214
+ 3 => 'vjustify',
1215
+ 4 => 'vdistributed/vequal_space'
1216
+ }
1217
+
1218
+ return {
1219
+ :font => format.font,
1220
+ :size => format.size,
1221
+ :color => format.color,
1222
+ :bold => format.bold,
1223
+ :italic => format.italic,
1224
+ :underline => format.underline,
1225
+ :font_strikeout => format.font_strikeout,
1226
+ :font_script => format.font_script,
1227
+ :font_outline => format.font_outline,
1228
+ :font_shadow => format.font_shadow,
1229
+ :num_format => format.num_format,
1230
+ :locked => format.locked,
1231
+ :hidden => format.hidden,
1232
+ :align => format.text_h_align,
1233
+ :valign => format.text_v_align,
1234
+ :rotation => format.rotation,
1235
+ :text_wrap => format.text_wrap,
1236
+ :text_justlast => format.text_justlast,
1237
+ :center_across => format.text_h_align,
1238
+ :indent => format.indent,
1239
+ :shrink => format.shrink,
1240
+ :pattern => format.pattern,
1241
+ :bg_color => format.bg_color,
1242
+ :fg_color => format.fg_color,
1243
+ :bottom => format.bottom,
1244
+ :top => format.top,
1245
+ :left => format.left,
1246
+ :right => format.right,
1247
+ :bottom_color => format.bottom_color,
1248
+ :top_color => format.top_color,
1249
+ :left_color => format.left_color,
1250
+ :right_color => format.right_color
1251
+ }
1252
+ end
1253
+
1254
+ end