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
@@ -1,1108 +1,1697 @@
1
- ##############################################################################
2
- #
3
- # Format - A class for defining Excel formatting.
4
- #
5
- #
6
- # Used in conjunction with Spreadsheet::WriteExcel
7
- #
8
- # Copyright 2000-2008, John McNamara, jmcnamara@cpan.org
9
- #
10
- # original written in Perl by John McNamara
11
- # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
- #
13
- require 'nkf'
14
-
15
- class Format
16
-
17
- COLORS = {
18
- 'aqua' => 0x0F,
19
- 'cyan' => 0x0F,
20
- 'black' => 0x08,
21
- 'blue' => 0x0C,
22
- 'brown' => 0x10,
23
- 'magenta' => 0x0E,
24
- 'fuchsia' => 0x0E,
25
- 'gray' => 0x17,
26
- 'grey' => 0x17,
27
- 'green' => 0x11,
28
- 'lime' => 0x0B,
29
- 'navy' => 0x12,
30
- 'orange' => 0x35,
31
- 'pink' => 0x21,
32
- 'purple' => 0x14,
33
- 'red' => 0x0A,
34
- 'silver' => 0x16,
35
- 'white' => 0x09,
36
- 'yellow' => 0x0D,
37
- }
38
- NonAscii = /[^!"#\$%&'\(\)\*\+,\-\.\/\:\;<=>\?@0-9A-Za-z_\[\\\]^` ~\0\n]/
39
-
40
- attr_accessor :xf_index, :used_merge
41
- attr_accessor :bold, :text_wrap, :text_justlast
42
- attr_accessor :fg_color, :bg_color, :color, :font, :size, :font_outline, :font_shadow
43
- attr_accessor :align, :border
44
- attr_accessor :font_index
45
- attr_accessor :num_format
46
- attr_reader :type
47
- attr_reader :font, :size, :font_family, :font_strikeout, :font_script, :font_charset
48
- attr_reader :font_encoding, :merge_range, :reading_order
49
- attr_reader :diag_type, :diag_color, :diag_border
50
- attr_reader :num_format_enc, :locked, :hidden
51
- attr_reader :rotation, :indent, :shrink, :pattern, :bottom, :top, :left, :right
52
- attr_reader :bottom_color, :top_color, :left_color, :right_color
53
- attr_reader :italic, :underline, :font_strikeout
54
- attr_reader :text_h_align, :text_v_align, :font_only
55
-
56
- ###############################################################################
57
- #
58
- # initialize(xf_index=0, properties = {})
59
- # xf_index :
60
- # properties : Hash of property => value
61
- #
62
- # Constructor
63
- #
64
- def initialize(xf_index = 0, properties = {})
65
- @xf_index = xf_index
66
-
67
- @type = 0
68
- @font_index = 0
69
- @font = 'Arial'
70
- @size = 10
71
- @bold = 0x0190
72
- @italic = 0
73
- @color = 0x7FFF
74
- @underline = 0
75
- @font_strikeout = 0
76
- @font_outline = 0
77
- @font_shadow = 0
78
- @font_script = 0
79
- @font_family = 0
80
- @font_charset = 0
81
- @font_encoding = 0
82
-
83
- @num_format = 0
84
- @num_format_enc = 0
85
-
86
- @hidden = 0
87
- @locked = 1
88
-
89
- @text_h_align = 0
90
- @text_wrap = 0
91
- @text_v_align = 2
92
- @text_justlast = 0
93
- @rotation = 0
94
-
95
- @fg_color = 0x40
96
- @bg_color = 0x41
97
-
98
- @pattern = 0
99
-
100
- @bottom = 0
101
- @top = 0
102
- @left = 0
103
- @right = 0
104
-
105
- @bottom_color = 0x40
106
- @top_color = 0x40
107
- @left_color = 0x40
108
- @right_color = 0x40
109
-
110
- @indent = 0
111
- @shrink = 0
112
- @merge_range = 0
113
- @reading_order = 0
114
-
115
- @diag_type = 0
116
- @diag_color = 0x40
117
- @diag_border = 0
118
-
119
- @font_only = 0
120
-
121
- # Temp code to prevent merged formats in non-merged cells.
122
- @used_merge = 0
123
-
124
- set_format_properties(properties) unless properties.empty?
125
- end
126
-
127
-
128
- ###############################################################################
129
- #
130
- # copy($format)
131
- #
132
- # Copy the attributes of another Spreadsheet::WriteExcel::Format object.
133
- #
134
- def copy(other)
135
- return unless other.kind_of?(Format)
136
-
137
- # copy properties except xf, merge_range, used_merge
138
- # Copy properties
139
- @type = other.type
140
- @font_index = other.font_index
141
- @font = other.font
142
- @size = other.size
143
- @bold = other.bold
144
- @italic = other.italic
145
- @color = other.color
146
- @underline = other.underline
147
- @font_strikeout = other.font_strikeout
148
- @font_outline = other.font_outline
149
- @font_shadow = other.font_shadow
150
- @font_script = other.font_script
151
- @font_family = other.font_family
152
- @font_charset = other.font_charset
153
- @font_encoding = other.font_encoding
154
-
155
- @num_format = other.num_format
156
- @num_format_enc = other.num_format_enc
157
-
158
- @hidden = other.hidden
159
- @locked = other.locked
160
-
161
- @text_h_align = other.text_h_align
162
- @text_wrap = other.text_wrap
163
- @text_v_align = other.text_v_align
164
- @text_justlast = other.text_justlast
165
- @rotation = other.rotation
166
-
167
- @fg_color = other.fg_color
168
- @bg_color = other.bg_color
169
-
170
- @pattern = other.pattern
171
-
172
- @bottom = other.bottom
173
- @top = other.top
174
- @left = other.left
175
- @right = other.right
176
-
177
- @bottom_color = other.bottom_color
178
- @top_color = other.top_color
179
- @left_color = other.left_color
180
- @right_color = other.right_color
181
-
182
- @indent = other.indent
183
- @shrink = other.shrink
184
- @reading_order = other.reading_order
185
-
186
- @diag_type = other.diag_type
187
- @diag_color = other.diag_color
188
- @diag_border = other.diag_border
189
-
190
- @font_only = other.font_only
191
- end
192
-
193
- ###############################################################################
194
- #
195
- # get_xf($style)
196
- #
197
- # Generate an Excel BIFF XF record.
198
- #
199
- def get_xf
200
-
201
- # Local Variable
202
- # record; # Record identifier
203
- # length; # Number of bytes to follow
204
- #
205
- # ifnt; # Index to FONT record
206
- # ifmt; # Index to FORMAT record
207
- # style; # Style and other options
208
- # align; # Alignment
209
- # indent; #
210
- # icv; # fg and bg pattern colors
211
- # border1; # Border line options
212
- # border2; # Border line options
213
- # border3; # Border line options
214
-
215
- # Set the type of the XF record and some of the attributes.
216
- if @type == 0xFFF5 then
217
- style = 0xFFF5
218
- else
219
- style = @locked
220
- style |= @hidden << 1
221
- end
222
-
223
- # Flags to indicate if attributes have been set.
224
- atr_num = (@num_format != 0) ? 1 : 0
225
- atr_fnt = (@font_index != 0) ? 1 : 0
226
- atr_alc = (@text_h_align != 0 ||
227
- @text_v_align != 2 ||
228
- @shrink != 0 ||
229
- @merge_range != 0 ||
230
- @text_wrap != 0 ||
231
- @indent != 0) ? 1 : 0
232
- atr_bdr = (@bottom != 0 ||
233
- @top != 0 ||
234
- @left != 0 ||
235
- @right != 0 ||
236
- @diag_type != 0) ? 1 : 0
237
- atr_pat = (@fg_color != 0x40 ||
238
- @bg_color != 0x41 ||
239
- @pattern != 0x00) ? 1 : 0
240
- atr_prot = (@hidden != 0 ||
241
- @locked != 1) ? 1 : 0
242
-
243
- # Set attribute changed flags for the style formats.
244
- if @xf_index != 0 and @type == 0xFFF5
245
- if @xf_index >= 16
246
- atr_num = 0
247
- atr_fnt = 1
248
- else
249
- atr_num = 1
250
- atr_fnt = 0
251
- end
252
- atr_alc = 1
253
- atr_bdr = 1
254
- atr_pat = 1
255
- atr_prot = 1
256
- end
257
-
258
- # Set a default diagonal border style if none was specified.
259
- @diag_border = 1 if (@diag_border ==0 and @diag_type != 0)
260
-
261
- # Reset the default colours for the non-font properties
262
- @fg_color = 0x40 if @fg_color == 0x7FFF
263
- @bg_color = 0x41 if @bg_color == 0x7FFF
264
- @bottom_color = 0x40 if @bottom_color == 0x7FFF
265
- @top_color = 0x40 if @top_color == 0x7FFF
266
- @left_color = 0x40 if @left_color == 0x7FFF
267
- @right_color = 0x40 if @right_color == 0x7FFF
268
- @diag_color = 0x40 if @diag_color == 0x7FFF
269
-
270
- # Zero the default border colour if the border has not been set.
271
- @bottom_color = 0 if @bottom == 0
272
- @top_color = 0 if @top == 0
273
- @right_color = 0 if @right == 0
274
- @left_color = 0 if @left == 0
275
- @diag_color = 0 if @diag_type == 0
276
-
277
- # The following 2 logical statements take care of special cases in relation
278
- # to cell colours and patterns:
279
- # 1. For a solid fill (_pattern == 1) Excel reverses the role of foreground
280
- # and background colours.
281
- # 2. If the user specifies a foreground or background colour without a
282
- # pattern they probably wanted a solid fill, so we fill in the defaults.
283
- #
284
- if (@pattern <= 0x01 && @bg_color != 0x41 && @fg_color == 0x40)
285
- @fg_color = @bg_color
286
- @bg_color = 0x40
287
- @pattern = 1
288
- end
289
-
290
- if (@pattern <= 0x01 && @bg_color == 0x41 && @fg_color != 0x40)
291
- @bg_color = 0x40
292
- @pattern = 1
293
- end
294
-
295
- # Set default alignment if indent is set.
296
- @text_h_align = 1 if @indent != 0 and @text_h_align == 0
297
-
298
-
299
- record = 0x00E0
300
- length = 0x0014
301
-
302
- ifnt = @font_index
303
- ifmt = @num_format
304
-
305
-
306
- align = @text_h_align
307
- align |= @text_wrap << 3
308
- align |= @text_v_align << 4
309
- align |= @text_justlast << 7
310
- align |= @rotation << 8
311
-
312
- indent = @indent
313
- indent |= @shrink << 4
314
- indent |= @merge_range << 5
315
- indent |= @reading_order << 6
316
- indent |= atr_num << 10
317
- indent |= atr_fnt << 11
318
- indent |= atr_alc << 12
319
- indent |= atr_bdr << 13
320
- indent |= atr_pat << 14
321
- indent |= atr_prot << 15
322
-
323
-
324
- border1 = @left
325
- border1 |= @right << 4
326
- border1 |= @top << 8
327
- border1 |= @bottom << 12
328
-
329
- border2 = @left_color
330
- border2 |= @right_color << 7
331
- border2 |= @diag_type << 14
332
-
333
- border3 = 0
334
- border3 |= @top_color
335
- border3 |= @bottom_color << 7
336
- border3 |= @diag_color << 14
337
- border3 |= @diag_border << 21
338
- border3 |= @pattern << 26
339
-
340
- icv = @fg_color
341
- icv |= @bg_color << 7
342
-
343
- header = [record, length].pack("vv")
344
- data = [ifnt, ifmt, style, align, indent,
345
- border1, border2, border3, icv].pack("vvvvvvvVv")
346
-
347
- return header + data
348
- end
349
-
350
- ###############################################################################
351
- #
352
- # get_font()
353
- #
354
- # Generate an Excel BIFF FONT record.
355
- #
356
- def get_font
357
-
358
- # my $record; # Record identifier
359
- # my $length; # Record length
360
-
361
- # my $dyHeight; # Height of font (1/20 of a point)
362
- # my $grbit; # Font attributes
363
- # my $icv; # Index to color palette
364
- # my $bls; # Bold style
365
- # my $sss; # Superscript/subscript
366
- # my $uls; # Underline
367
- # my $bFamily; # Font family
368
- # my $bCharSet; # Character set
369
- # my $reserved; # Reserved
370
- # my $cch; # Length of font name
371
- # my $rgch; # Font name
372
- # my $encoding; # Font name character encoding
373
-
374
-
375
- dyHeight = @size * 20
376
- icv = @color
377
- bls = @bold
378
- sss = @font_script
379
- uls = @underline
380
- bFamily = @font_family
381
- bCharSet = @font_charset
382
- rgch = @font
383
- encoding = @font_encoding
384
-
385
- # Handle utf8 strings
386
- if rgch =~ NonAscii
387
- rgch = NKF.nkf('-w16B0 -m0 -W', rgch)
388
- encoding = 1
389
- end
390
-
391
- cch = rgch.length
392
- #
393
- # Handle Unicode font names.
394
- if (encoding == 1)
395
- raise "Uneven number of bytes in Unicode font name" if cch % 2 != 0
396
- cch /= 2 if encoding !=0
397
- rgch = rgch.unpack('n*').pack('v*')
398
- end
399
-
400
- record = 0x31
401
- length = 0x10 + rgch.length
402
- reserved = 0x00
403
-
404
- grbit = 0x00
405
- grbit |= 0x02 if @italic != 0
406
- grbit |= 0x08 if @font_strikeout != 0
407
- grbit |= 0x10 if @font_outline != 0
408
- grbit |= 0x20 if @font_shadow != 0
409
-
410
-
411
- header = [record, length].pack("vv")
412
- data = [dyHeight, grbit, icv, bls,
413
- sss, uls, bFamily,
414
- bCharSet, reserved, cch, encoding].pack('vvvvvCCCCCC')
415
-
416
- return header + data + rgch
417
- end
418
-
419
- ###############################################################################
420
- #
421
- # get_font_key()
422
- #
423
- # Returns a unique hash key for a font. Used by Workbook->_store_all_fonts()
424
- #
425
- def get_font_key
426
- # The following elements are arranged to increase the probability of
427
- # generating a unique key. Elements that hold a large range of numbers
428
- # e.g. _color are placed between two binary elements such as _italic
429
-
430
- key = "#{@font}#{@size}#{@font_script}#{@underline}#{@font_strikeout}#{@bold}#{@font_outline}"
431
- key += "#{@font_family}#{@font_charset}#{@font_shadow}#{@color}#{@italic}#{@font_encoding}"
432
- result = key.gsub(' ', '_') # Convert the key to a single word
433
-
434
- return result
435
- end
436
-
437
- ###############################################################################
438
- #
439
- # get_xf_index()
440
- #
441
- # Returns the used by Worksheet->_XF()
442
- #
443
- def get_xf_index
444
- return @xf_index
445
- end
446
-
447
-
448
- ###############################################################################
449
- #
450
- # get_color(colour)
451
- #
452
- # Used in conjunction with the set_xxx_color methods to convert a color
453
- # string into a number. Color range is 0..63 but we will restrict it
454
- # to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
455
- #
456
- def get_color(colour = nil)
457
- # Return the default color, 0x7FFF, if undef,
458
- return 0x7FFF if colour.nil?
459
-
460
- if colour.kind_of?(Numeric)
461
- if colour < 0
462
- return 0x7FFF
463
-
464
- # or an index < 8 mapped into the correct range,
465
- elsif colour < 8
466
- return (colour + 8).to_i
467
-
468
- # or the default color if arg is outside range,
469
- elsif colour > 63
470
- return 0x7FFF
471
-
472
- # or an integer in the valid range
473
- else
474
- return colour.to_i
475
- end
476
- elsif colour.kind_of?(String)
477
- # or the color string converted to an integer,
478
- if COLORS.has_key?(colour)
479
- return COLORS[colour]
480
-
481
- # or the default color if string is unrecognised,
482
- else
483
- return 0x7FFF
484
- end
485
- else
486
- return 0x7FFF
487
- end
488
- end
489
-
490
- ###############################################################################
491
- #
492
- # class method Format._get_color(colour)
493
- #
494
- # used from Worksheet.rb
495
- #
496
- # this is cut & copy of get_color().
497
- #
498
- def self._get_color(colour)
499
- # Return the default color, 0x7FFF, if undef,
500
- return 0x7FFF if colour.nil?
501
-
502
- if colour.kind_of?(Numeric)
503
- if colour < 0
504
- return 0x7FFF
505
-
506
- # or an index < 8 mapped into the correct range,
507
- elsif colour < 8
508
- return (colour + 8).to_i
509
-
510
- # or the default color if arg is outside range,
511
- elsif 63 < colour
512
- return 0x7FFF
513
-
514
- # or an integer in the valid range
515
- else
516
- return colour.to_i
517
- end
518
- elsif colour.kind_of?(String)
519
- # or the color string converted to an integer,
520
- if COLORS.has_key?(colour)
521
- return COLORS[colour]
522
-
523
- # or the default color if string is unrecognised,
524
- else
525
- return 0x7FFF
526
- end
527
- else
528
- return 0x7FFF
529
- end
530
- end
531
-
532
- ###############################################################################
533
- #
534
- # set_type()
535
- #
536
- # Set the XF object type as 0 = cell XF or 0xFFF5 = style XF.
537
- #
538
- def set_type(type = nil)
539
-
540
- if !type.nil? and type == 0
541
- @type = 0x0000
542
- else
543
- @type = 0xFFF5
544
- end
545
- end
546
-
547
- ###############################################################################
548
- #
549
- # set_size(size)
550
- #
551
- # Default state: Font size is 10
552
- # Default action: Set font size to 1
553
- # Valid args: Integer values from 1 to as big as your screen.
554
- # Set the font size. Excel adjusts the height of a row
555
- # to accommodate the
556
- #
557
- # largest font size in the row. You can also explicitly specify the height
558
- # of a row using the set_row() worksheet method.Set cell alignment.
559
- #
560
- def set_size(size = 1)
561
- if size.kind_of?(Numeric) && size >= 1
562
- @size = size.to_i
563
- end
564
- end
565
-
566
- ###############################################################################
567
- #
568
- # set_color(color)
569
- #
570
- # Default state: Excels default color, usually black
571
- # Default action: Set the default color
572
- # Valid args: Integers from 8..63 or the following strings:
573
- # 'black', 'blue', 'brown', 'cyan', 'gray'
574
- # 'green', 'lime', 'magenta', 'navy', 'orange'
575
- # 'pink', 'purple', 'red', 'silver', 'white', 'yellow'
576
- #
577
- # Set the font colour. The set_color() method is used as follows:
578
- #
579
- # format = workbook.add_format()
580
- # format.set_color('red')
581
- # worksheet.write(0, 0, 'wheelbarrow', format)
582
- #
583
- # Note: The set_color() method is used to set the colour of the font in a cell.
584
- # To set the colour of a cell use the set_bg_color()
585
- # and set_pattern() methods.
586
- #
587
- def set_color(color = 0x7FFF)
588
- @color = get_color(color)
589
- end
590
-
591
- ###############################################################################
592
- #
593
- # set_italic()
594
- #
595
- # Default state: Italic is off
596
- # Default action: Turn italic on
597
- # Valid args: 0, 1
598
- #
599
- # Set the italic property of the font:
600
- #
601
- # format.set_italic() # Turn italic on
602
- #
603
- def set_italic(arg = 1)
604
- begin
605
- if arg == 1 then @italic = 1 # italic on
606
- elsif arg == 0 then @italic = 0 # italic off
607
- else
608
- raise ArgumentError,
609
- "\n\n set_italic(#{arg.inspect})\n arg must be 0, 1, or none. ( 0:OFF , 1 and none:ON )\n"
610
- end
611
- end
612
- end
613
-
614
- ###############################################################################
615
- #
616
- # set_underline()
617
- #
618
- # Default state: Underline is off
619
- # Default action: Turn on single underline
620
- # Valid args: 0 = No underline
621
- # 1 = Single underline
622
- # 2 = Double underline
623
- # 33 = Single accounting underline
624
- # 34 = Double accounting underline
625
- #
626
- # Set the underline property of the font.
627
- #
628
- def set_underline(arg = 1)
629
- begin
630
- case arg
631
- when 0 then @underline = 0 # off
632
- when 1 then @underline = 1 # Single
633
- when 2 then @underline = 2 # Double
634
- when 33 then @underline = 33 # Single accounting
635
- when 34 then @underline = 34 # Double accounting
636
- else
637
- raise ArgumentError,
638
- "\n\n set_underline(#{arg.inspect})\n arg must be 0, 1, or none, 2, 33, 34.\n"
639
- " ( 0:OFF, 1 and none:Single, 2:Double, 33:Single accounting, 34:Double accounting )\n"
640
- end
641
- end
642
- end
643
-
644
- ###############################################################################
645
- #
646
- # set_font_strikeout()
647
- #
648
- # Default state: Strikeout is off
649
- # Default action: Turn strikeout on
650
- # Valid args: 0, 1
651
- #
652
- # Set the strikeout property of the font.
653
- #
654
- def set_font_strikeout(arg = 1)
655
- begin
656
- if arg == 0 then @font_strikeout = 0
657
- elsif arg == 1 then @font_strikeout = 1
658
- else
659
- raise ArgumentError,
660
- "\n\n set_font_strikeout(#{arg.inspect})\n arg must be 0, 1, or none.\n"
661
- " ( 0:OFF, 1 and none:Strikeout )\n"
662
- end
663
- end
664
- end
665
-
666
- ###############################################################################
667
- #
668
- # set_font_script()
669
- #
670
- # Default state: Super/Subscript is off
671
- # Default action: Turn Superscript on
672
- # Valid args: 0 = Normal
673
- # 1 = Superscript
674
- # 2 = Subscript
675
- #
676
- # Set the superscript/subscript property of the font.
677
- # This format is currently not very useful.
678
- #
679
- def set_font_script(arg = 1)
680
- begin
681
- if arg == 0 then @font_script = 0
682
- elsif arg == 1 then @font_script = 1
683
- elsif arg == 2 then @font_script = 2
684
- else
685
- raise ArgumentError,
686
- "\n\n set_font_script(#{arg.inspect})\n arg must be 0, 1, or none. or 2\n"
687
- " ( 0:OFF, 1 and none:Superscript, 2:Subscript )\n"
688
- end
689
- end
690
- end
691
-
692
- ###############################################################################
693
- #
694
- # set_font_outline()
695
- #
696
- # Default state: Outline is off
697
- # Default action: Turn outline on
698
- # Valid args: 0, 1
699
- #
700
- # Macintosh only.
701
- #
702
- def set_font_outline(arg = 1)
703
- begin
704
- if arg == 0 then @font_outline = 0
705
- elsif arg == 1 then @font_outline = 1
706
- else
707
- raise ArgumentError,
708
- "\n\n set_font_outline(#{arg.inspect})\n arg must be 0, 1, or none.\n"
709
- " ( 0:OFF, 1 and none:outline on )\n"
710
- end
711
- end
712
- end
713
-
714
- ###############################################################################
715
- #
716
- # set_font_shadow()
717
- #
718
- # Default state: Shadow is off
719
- # Default action: Turn shadow on
720
- # Valid args: 0, 1
721
- #
722
- # Macintosh only.
723
- #
724
- def set_font_shadow(arg = 1)
725
- begin
726
- if arg == 0 then @font_shadow = 0
727
- elsif arg == 1 then @font_shadow = 1
728
- else
729
- raise ArgumentError,
730
- "\n\n set_font_shadow(#{arg.inspect})\n arg must be 0, 1, or none.\n"
731
- " ( 0:OFF, 1 and none:shadow on )\n"
732
- end
733
- end
734
- end
735
-
736
- ###############################################################################
737
- #
738
- # set_locked()
739
- #
740
- # Default state: Cell locking is on
741
- # Default action: Turn locking on
742
- # Valid args: 0, 1
743
- #
744
- # This property can be used to prevent modification of a cells contents.
745
- # Following Excel's convention, cell locking is turned on by default.
746
- # However, it only has an effect if the worksheet has been protected,
747
- # see the worksheet protect() method.
748
- #
749
- # locked = workbook.add_format()
750
- # locked.set_locked(1) # A non-op
751
- #
752
- # unlocked = workbook.add_format()
753
- # locked.set_locked(0)
754
- #
755
- # # Enable worksheet protection
756
- # worksheet.protect()
757
- #
758
- # # This cell cannot be edited.
759
- # worksheet.write('A1', '=1+2', locked)
760
- #
761
- # # This cell can be edited.
762
- # worksheet.write('A2', '=1+2', unlocked)
763
- #
764
- # Note: This offers weak protection even with a password, see the note
765
- # in relation to the protect() method.
766
- #
767
- def set_locked(arg = 1)
768
- begin
769
- if arg == 0 then @locked = 0
770
- elsif arg == 1 then @locked = 1
771
- else
772
- raise ArgumentError,
773
- "\n\n set_locked(#{arg.inspect})\n arg must be 0, 1, or none.\n"
774
- " ( 0:OFF, 1 and none:Lock On )\n"
775
- end
776
- end
777
- end
778
-
779
- ###############################################################################
780
- #
781
- # set_hidden()
782
- #
783
- # Default state: Formula hiding is off
784
- # Default action: Turn hiding on
785
- # Valid args: 0, 1
786
- #
787
- # This property is used to hide a formula while still displaying
788
- # its result. This is generally used to hide complex calculations
789
- # from end users who are only interested in the result. It only has
790
- # an effect if the worksheet has been protected,
791
- # see the worksheet protect() method.
792
- #
793
- # my hidden = workbook.add_format()
794
- # hidden.set_hidden()
795
- #
796
- # # Enable worksheet protection
797
- # worksheet.protect()
798
- #
799
- # # The formula in this cell isn't visible
800
- # worksheet.write('A1', '=1+2', hidden)
801
- #
802
- # Note: This offers weak protection even with a password,
803
- # see the note in relation to the protect() method .
804
- #
805
- def set_hidden(arg = 1)
806
- begin
807
- if arg == 0 then @hidden = 0
808
- elsif arg == 1 then @hidden = 1
809
- else
810
- raise ArgumentError,
811
- "\n\n set_hidden(#{arg.inspect})\n arg must be 0, 1, or none.\n"
812
- " ( 0:OFF, 1 and none:hiding On )\n"
813
- end
814
- end
815
- end
816
-
817
- ###############################################################################
818
- #
819
- # set_align()
820
- #
821
- # Set cell alignment.
822
- #
823
- # Default state: Alignment is off
824
- # Default action: Left alignment
825
- # Valid args: 'left' Horizontal
826
- # 'center'
827
- # 'right'
828
- # 'fill'
829
- # 'justify'
830
- # 'center_across'
831
- #
832
- # 'top' Vertical
833
- # 'vcenter'
834
- # 'bottom'
835
- # 'vjustify'
836
- #
837
- # This method is used to set the horizontal and vertical text alignment
838
- # within a cell. Vertical and horizontal alignments can be combined.
839
- # The method is used as follows:
840
- #
841
- # my $format = $workbook->add_format();
842
- # $format->set_align('center');
843
- # $format->set_align('vcenter');
844
- # $worksheet->set_row(0, 30);
845
- # $worksheet->write(0, 0, 'X', $format);
846
- #
847
- # Text can be aligned across two or more adjacent cells using
848
- # the center_across property. However, for genuine merged cells
849
- # it is better to use the merge_range() worksheet method.
850
- #
851
- # The vjustify (vertical justify) option can be used to provide
852
- # automatic text wrapping in a cell. The height of the cell will be
853
- # adjusted to accommodate the wrapped text. To specify where the text
854
- # wraps use the set_text_wrap() method.
855
- #
856
- def set_align(align = 'left')
857
-
858
- return unless align.kind_of?(String)
859
-
860
- location = align.downcase
861
-
862
- case location
863
- when 'left' then set_text_h_align(1)
864
- when 'centre', 'center' then set_text_h_align(2)
865
- when 'right' then set_text_h_align(3)
866
- when 'fill' then set_text_h_align(4)
867
- when 'justify' then set_text_h_align(5)
868
- when 'center_across', 'centre_across' then set_text_h_align(6)
869
- when 'merge' then set_text_h_align(6) # S:WE name
870
- when 'distributed' then set_text_h_align(7)
871
- when 'equal_space' then set_text_h_align(7) # ParseExcel
872
-
873
- when 'top' then set_text_v_align(0)
874
- when 'vcentre' then set_text_v_align(1)
875
- when 'vcenter' then set_text_v_align(1)
876
- when 'bottom' then set_text_v_align(2)
877
- when 'vjustify' then set_text_v_align(3)
878
- when 'vdistributed' then set_text_v_align(4)
879
- when 'vequal_space' then set_text_v_align(4) # ParseExcel
880
- end
881
- end
882
-
883
- ###############################################################################
884
- #
885
- # set_valign()
886
- #
887
- # Set vertical cell alignment. This is required by the set_format_properties()
888
- # method to differentiate between the vertical and horizontal properties.
889
- #
890
- def set_valign(alignment)
891
- set_align(alignment);
892
- end
893
-
894
- ###############################################################################
895
- #
896
- # set_center_across()
897
- #
898
- # Implements the Excel5 style "merge".
899
- #
900
- def set_center_across(val=true)
901
- set_text_h_align(6)
902
- end
903
-
904
- ###############################################################################
905
- #
906
- # set_merge()
907
- #
908
- # This was the way to implement a merge in Excel5. However it should have been
909
- # called "center_across" and not "merge".
910
- # This is now deprecated. Use set_center_across() or better merge_range().
911
- #
912
- #
913
- def set_merge(val=true)
914
- set_text_h_align(6)
915
- end
916
-
917
- ###############################################################################
918
- #
919
- #set_text_wrap()
920
- #
921
- # Default state: Text wrap is off
922
- # Default action: Turn text wrap on
923
- # Valid args: 0, 1
924
- #
925
- # Here is an example using the text wrap property, the escape
926
- # character \n is used to indicate the end of line:
927
- #
928
- # format = workbook.add_format()
929
- # format.set_text_wrap()
930
- # worksheet.write(0, 0, "It's\na bum\nwrap", format)
931
- #
932
- def set_text_wrap(arg = 1)
933
- begin
934
- if arg == 0 then @text_wrap = 0
935
- elsif arg == 1 then @text_wrap = 1
936
- else
937
- raise ArgumentError,
938
- "\n\n set_text_wrap(#{arg.inspect})\n arg must be 0, 1, or none.\n"
939
- " ( 0:OFF, 1 and none:text wrap On )\n"
940
- end
941
- end
942
- end
943
-
944
- ###############################################################################
945
- #
946
- # set_bold()
947
- #
948
- # Default state: bold is off (internal value = 400)
949
- # Default action: Turn bold on
950
- # Valid args: 0, 1 [1]
951
- #
952
- # Set the bold property of the font:
953
- #
954
- # format.set_bold() # Turn bold on
955
- #
956
- #[1] Actually, values in the range 100..1000 are also valid.
957
- # 400 is normal, 700 is bold and 1000 is very bold indeed.
958
- # It is probably best to set the value to 1 and use normal bold.
959
- #
960
- def set_bold(weight = nil)
961
- if weight.nil?
962
- weight = 0x2BC
963
- elsif !weight.kind_of?(Numeric)
964
- weight = 0x190
965
- elsif weight == 1 # Bold text
966
- weight = 0x2BC
967
- elsif weight == 0 # Normal text
968
- weight = 0x190
969
- elsif weight < 0x064 # Lower bound
970
- weight = 0x190
971
- elsif weight > 0x3E8 # Upper bound
972
- weight = 0x190
973
- else
974
- weight = weight.to_i
975
- end
976
-
977
- @bold = weight
978
- end
979
-
980
-
981
- ###############################################################################
982
- #
983
- # set_border($style)
984
- #
985
- # Set cells borders to the same style
986
- #
987
- def set_border(style)
988
- set_bottom(style)
989
- set_top(style)
990
- set_left(style)
991
- set_right(style)
992
- end
993
-
994
-
995
- ###############################################################################
996
- #
997
- # set_border_color($color)
998
- #
999
- # Set cells border to the same color
1000
- #
1001
- def set_border_color(color)
1002
- set_bottom_color(color);
1003
- set_top_color(color);
1004
- set_left_color(color);
1005
- set_right_color(color);
1006
- end
1007
-
1008
- ###############################################################################
1009
- #
1010
- # set_rotation($angle)
1011
- #
1012
- # Set the rotation angle of the text. An alignment property.
1013
- #
1014
- def set_rotation(rotation)
1015
- # Argument should be a number
1016
- return unless rotation.kind_of?(Numeric)
1017
-
1018
- # The arg type can be a double but the Excel dialog only allows integers.
1019
- rotation = rotation.to_i
1020
-
1021
- # if (rotation == 270)
1022
- # rotation = 255
1023
- # elsif (rotation >= -90 or rotation <= 90)
1024
- # rotation = -rotation +90 if rotation < 0;
1025
- # else
1026
- # # carp "Rotation $rotation outside range: -90 <= angle <= 90";
1027
- # rotation = 0;
1028
- # end
1029
- #
1030
- if rotation == 270
1031
- rotation = 255
1032
- elsif rotation >= -90 && rotation <= 90
1033
- rotation = -rotation + 90 if rotation < 0
1034
- else
1035
- rotation = 0
1036
- end
1037
-
1038
- @rotation = rotation;
1039
- end
1040
-
1041
-
1042
- ###############################################################################
1043
- #
1044
- # set_format_properties(*properties)
1045
- # properties : Hash of properies
1046
- # ex) font = { :color => 'red', :bold => 1 }
1047
- # shade = { :bg_color => 'green', :pattern => 1 }
1048
- # 1) set_format_properties( :bold => 1 [, :color => 'red'..] )
1049
- # 2) set_format_properties( font [, shade, ..])
1050
- # 3) set_format_properties( :bold => 1, font, ...)
1051
- #
1052
- # Convert hashes of properties to method calls.
1053
- #
1054
- def set_format_properties(*properties)
1055
- return if properties.empty?
1056
- properties.each do |property|
1057
- property.each do |key, value|
1058
- # Strip leading "-" from Tk style properties e.g. -color => 'red'.
1059
- key.sub!(/^-/, '') if key.kind_of?(String)
1060
-
1061
- # Create a sub to set the property.
1062
- if value.kind_of?(String)
1063
- s = "set_#{key}('#{value}')"
1064
- else
1065
- s = "set_#{key}(#{value})"
1066
- end
1067
- eval s
1068
- end
1069
- end
1070
- end
1071
-
1072
- # Renamed rarely used set_properties() to set_format_properties() to avoid
1073
- # confusion with Workbook method of the same name. The following acts as an
1074
- # alias for any code that uses the old name.
1075
- # *set_properties = *set_format_properties;
1076
-
1077
- # Dynamically create set methods that aren't already defined.
1078
- def method_missing(name, *args)
1079
- # -- original perl comment --
1080
- # There are two types of set methods: set_property() and
1081
- # set_property_color(). When a method is AUTOLOADED we store a new anonymous
1082
- # sub in the appropriate slot in the symbol table. The speeds up subsequent
1083
- # calls to the same method.
1084
-
1085
- method = "#{name}"
1086
-
1087
- # Check for a valid method names, i.e. "set_xxx_yyy".
1088
- method =~ /set_(\w+)/ or raise "Unknown method: #{method}\n"
1089
-
1090
- # Match the attribute, i.e. "@xxx_yyy".
1091
- attribute = "@#{$1}"
1092
-
1093
- # Check that the attribute exists
1094
- # ........
1095
- if method =~ /set\w+color$/ # for "set_property_color" methods
1096
- value = get_color(args[0])
1097
- else # for "set_xxx" methods
1098
- value = args[0].nil? ? 1 : args[0]
1099
- end
1100
- if value.kind_of?(String)
1101
- s = "#{attribute} = \"#{value.to_s}\""
1102
- else
1103
- s = "#{attribute} = #{value.to_s}"
1104
- end
1105
- eval s
1106
- end
1107
-
1108
- end
1
+ ##############################################################################
2
+ #
3
+ # Format - A class for defining Excel formatting.
4
+ #
5
+ #
6
+ # Used in conjunction with WriteExcel
7
+ #
8
+ # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
9
+ #
10
+ # original written in Perl by John McNamara
11
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+ require 'nkf'
14
+
15
+ #
16
+ # Format - A class for defining Excel formatting.
17
+ #
18
+ # See CELL FORMATTING, FORMAT METHODS, COLOURS IN EXCEL in WriteExcel's rdoc.
19
+ #
20
+ class Format
21
+
22
+ COLORS = {
23
+ 'aqua' => 0x0F,
24
+ 'cyan' => 0x0F,
25
+ 'black' => 0x08,
26
+ 'blue' => 0x0C,
27
+ 'brown' => 0x10,
28
+ 'magenta' => 0x0E,
29
+ 'fuchsia' => 0x0E,
30
+ 'gray' => 0x17,
31
+ 'grey' => 0x17,
32
+ 'green' => 0x11,
33
+ 'lime' => 0x0B,
34
+ 'navy' => 0x12,
35
+ 'orange' => 0x35,
36
+ 'pink' => 0x21,
37
+ 'purple' => 0x14,
38
+ 'red' => 0x0A,
39
+ 'silver' => 0x16,
40
+ 'white' => 0x09,
41
+ 'yellow' => 0x0D,
42
+ } # :nodoc:
43
+ NonAscii = /[^!"#\$%&'\(\)\*\+,\-\.\/\:\;<=>\?@0-9A-Za-z_\[\\\]^` ~\0\n]/ # :nodoc:
44
+
45
+ ###############################################################################
46
+ #
47
+ # initialize(xf_index=0, properties = {})
48
+ # xf_index :
49
+ # properties : Hash of property => value
50
+ #
51
+ # Constructor
52
+ #
53
+ def initialize(xf_index = 0, properties = {}) # :nodoc:
54
+ @xf_index = xf_index
55
+
56
+ @type = 0
57
+ @font_index = 0
58
+ @font = 'Arial'
59
+ @size = 10
60
+ @bold = 0x0190
61
+ @italic = 0
62
+ @color = 0x7FFF
63
+ @underline = 0
64
+ @font_strikeout = 0
65
+ @font_outline = 0
66
+ @font_shadow = 0
67
+ @font_script = 0
68
+ @font_family = 0
69
+ @font_charset = 0
70
+ @font_encoding = 0
71
+
72
+ @num_format = 0
73
+ @num_format_enc = 0
74
+
75
+ @hidden = 0
76
+ @locked = 1
77
+
78
+ @text_h_align = 0
79
+ @text_wrap = 0
80
+ @text_v_align = 2
81
+ @text_justlast = 0
82
+ @rotation = 0
83
+
84
+ @fg_color = 0x40
85
+ @bg_color = 0x41
86
+
87
+ @pattern = 0
88
+
89
+ @bottom = 0
90
+ @top = 0
91
+ @left = 0
92
+ @right = 0
93
+
94
+ @bottom_color = 0x40
95
+ @top_color = 0x40
96
+ @left_color = 0x40
97
+ @right_color = 0x40
98
+
99
+ @indent = 0
100
+ @shrink = 0
101
+ @merge_range = 0
102
+ @reading_order = 0
103
+
104
+ @diag_type = 0
105
+ @diag_color = 0x40
106
+ @diag_border = 0
107
+
108
+ @font_only = 0
109
+
110
+ # Temp code to prevent merged formats in non-merged cells.
111
+ @used_merge = 0
112
+
113
+ set_format_properties(properties) unless properties.empty?
114
+ end
115
+
116
+
117
+ #
118
+ # :call-seq:
119
+ # copy(format)
120
+ #
121
+ # Copy the attributes of another Format object.
122
+ #
123
+ # This method is used to copy all of the properties from one Format object
124
+ # to another:
125
+ #
126
+ # lorry1 = workbook.add_format
127
+ # lorry1.set_bold
128
+ # lorry1.set_italic
129
+ # lorry1.set_color('red') # lorry1 is bold, italic and red
130
+ #
131
+ # lorry2 = workbook.add_format
132
+ # lorry2.copy(lorry1)
133
+ # lorry2.set_color('yellow') # lorry2 is bold, italic and yellow
134
+ #
135
+ # The copy() method is only useful if you are using the method interface
136
+ # to Format properties. It generally isn't required if you are setting
137
+ # Format properties directly using hashes.
138
+ #
139
+ # Note: this is not a copy constructor, both objects must exist prior to
140
+ # copying.
141
+ #
142
+ def copy(other)
143
+ return unless other.kind_of?(Format)
144
+
145
+ # copy properties except xf, merge_range, used_merge
146
+ # Copy properties
147
+ @type = other.type
148
+ @font_index = other.font_index
149
+ @font = other.font
150
+ @size = other.size
151
+ @bold = other.bold
152
+ @italic = other.italic
153
+ @color = other.color
154
+ @underline = other.underline
155
+ @font_strikeout = other.font_strikeout
156
+ @font_outline = other.font_outline
157
+ @font_shadow = other.font_shadow
158
+ @font_script = other.font_script
159
+ @font_family = other.font_family
160
+ @font_charset = other.font_charset
161
+ @font_encoding = other.font_encoding
162
+
163
+ @num_format = other.num_format
164
+ @num_format_enc = other.num_format_enc
165
+
166
+ @hidden = other.hidden
167
+ @locked = other.locked
168
+
169
+ @text_h_align = other.text_h_align
170
+ @text_wrap = other.text_wrap
171
+ @text_v_align = other.text_v_align
172
+ @text_justlast = other.text_justlast
173
+ @rotation = other.rotation
174
+
175
+ @fg_color = other.fg_color
176
+ @bg_color = other.bg_color
177
+
178
+ @pattern = other.pattern
179
+
180
+ @bottom = other.bottom
181
+ @top = other.top
182
+ @left = other.left
183
+ @right = other.right
184
+
185
+ @bottom_color = other.bottom_color
186
+ @top_color = other.top_color
187
+ @left_color = other.left_color
188
+ @right_color = other.right_color
189
+
190
+ @indent = other.indent
191
+ @shrink = other.shrink
192
+ @reading_order = other.reading_order
193
+
194
+ @diag_type = other.diag_type
195
+ @diag_color = other.diag_color
196
+ @diag_border = other.diag_border
197
+
198
+ @font_only = other.font_only
199
+ end
200
+
201
+ ###############################################################################
202
+ #
203
+ # get_xf($style)
204
+ #
205
+ # Generate an Excel BIFF XF record.
206
+ #
207
+ def get_xf # :nodoc:
208
+
209
+ # Local Variable
210
+ # record; # Record identifier
211
+ # length; # Number of bytes to follow
212
+ #
213
+ # ifnt; # Index to FONT record
214
+ # ifmt; # Index to FORMAT record
215
+ # style; # Style and other options
216
+ # align; # Alignment
217
+ # indent; #
218
+ # icv; # fg and bg pattern colors
219
+ # border1; # Border line options
220
+ # border2; # Border line options
221
+ # border3; # Border line options
222
+
223
+ # Set the type of the XF record and some of the attributes.
224
+ if @type == 0xFFF5 then
225
+ style = 0xFFF5
226
+ else
227
+ style = @locked
228
+ style |= @hidden << 1
229
+ end
230
+
231
+ # Flags to indicate if attributes have been set.
232
+ atr_num = (@num_format != 0) ? 1 : 0
233
+ atr_fnt = (@font_index != 0) ? 1 : 0
234
+ atr_alc = (@text_h_align != 0 ||
235
+ @text_v_align != 2 ||
236
+ @shrink != 0 ||
237
+ @merge_range != 0 ||
238
+ @text_wrap != 0 ||
239
+ @indent != 0) ? 1 : 0
240
+ atr_bdr = (@bottom != 0 ||
241
+ @top != 0 ||
242
+ @left != 0 ||
243
+ @right != 0 ||
244
+ @diag_type != 0) ? 1 : 0
245
+ atr_pat = (@fg_color != 0x40 ||
246
+ @bg_color != 0x41 ||
247
+ @pattern != 0x00) ? 1 : 0
248
+ atr_prot = (@hidden != 0 ||
249
+ @locked != 1) ? 1 : 0
250
+
251
+ # Set attribute changed flags for the style formats.
252
+ if @xf_index != 0 and @type == 0xFFF5
253
+ if @xf_index >= 16
254
+ atr_num = 0
255
+ atr_fnt = 1
256
+ else
257
+ atr_num = 1
258
+ atr_fnt = 0
259
+ end
260
+ atr_alc = 1
261
+ atr_bdr = 1
262
+ atr_pat = 1
263
+ atr_prot = 1
264
+ end
265
+
266
+ # Set a default diagonal border style if none was specified.
267
+ @diag_border = 1 if (@diag_border ==0 and @diag_type != 0)
268
+
269
+ # Reset the default colours for the non-font properties
270
+ @fg_color = 0x40 if @fg_color == 0x7FFF
271
+ @bg_color = 0x41 if @bg_color == 0x7FFF
272
+ @bottom_color = 0x40 if @bottom_color == 0x7FFF
273
+ @top_color = 0x40 if @top_color == 0x7FFF
274
+ @left_color = 0x40 if @left_color == 0x7FFF
275
+ @right_color = 0x40 if @right_color == 0x7FFF
276
+ @diag_color = 0x40 if @diag_color == 0x7FFF
277
+
278
+ # Zero the default border colour if the border has not been set.
279
+ @bottom_color = 0 if @bottom == 0
280
+ @top_color = 0 if @top == 0
281
+ @right_color = 0 if @right == 0
282
+ @left_color = 0 if @left == 0
283
+ @diag_color = 0 if @diag_type == 0
284
+
285
+ # The following 2 logical statements take care of special cases in relation
286
+ # to cell colours and patterns:
287
+ # 1. For a solid fill (_pattern == 1) Excel reverses the role of foreground
288
+ # and background colours.
289
+ # 2. If the user specifies a foreground or background colour without a
290
+ # pattern they probably wanted a solid fill, so we fill in the defaults.
291
+ #
292
+ if (@pattern <= 0x01 && @bg_color != 0x41 && @fg_color == 0x40)
293
+ @fg_color = @bg_color
294
+ @bg_color = 0x40
295
+ @pattern = 1
296
+ end
297
+
298
+ if (@pattern <= 0x01 && @bg_color == 0x41 && @fg_color != 0x40)
299
+ @bg_color = 0x40
300
+ @pattern = 1
301
+ end
302
+
303
+ # Set default alignment if indent is set.
304
+ @text_h_align = 1 if @indent != 0 and @text_h_align == 0
305
+
306
+
307
+ record = 0x00E0
308
+ length = 0x0014
309
+
310
+ ifnt = @font_index
311
+ ifmt = @num_format
312
+
313
+
314
+ align = @text_h_align
315
+ align |= @text_wrap << 3
316
+ align |= @text_v_align << 4
317
+ align |= @text_justlast << 7
318
+ align |= @rotation << 8
319
+
320
+ indent = @indent
321
+ indent |= @shrink << 4
322
+ indent |= @merge_range << 5
323
+ indent |= @reading_order << 6
324
+ indent |= atr_num << 10
325
+ indent |= atr_fnt << 11
326
+ indent |= atr_alc << 12
327
+ indent |= atr_bdr << 13
328
+ indent |= atr_pat << 14
329
+ indent |= atr_prot << 15
330
+
331
+
332
+ border1 = @left
333
+ border1 |= @right << 4
334
+ border1 |= @top << 8
335
+ border1 |= @bottom << 12
336
+
337
+ border2 = @left_color
338
+ border2 |= @right_color << 7
339
+ border2 |= @diag_type << 14
340
+
341
+ border3 = @top_color
342
+ border3 |= @bottom_color << 7
343
+ border3 |= @diag_color << 14
344
+ border3 |= @diag_border << 21
345
+ border3 |= @pattern << 26
346
+
347
+ icv = @fg_color
348
+ icv |= @bg_color << 7
349
+
350
+ header = [record, length].pack("vv")
351
+ data = [ifnt, ifmt, style, align, indent,
352
+ border1, border2, border3, icv].pack("vvvvvvvVv")
353
+
354
+ return header + data
355
+ end
356
+
357
+ ###############################################################################
358
+ #
359
+ # get_font()
360
+ #
361
+ # Generate an Excel BIFF FONT record.
362
+ #
363
+ def get_font # :nodoc:
364
+
365
+ # my $record; # Record identifier
366
+ # my $length; # Record length
367
+
368
+ # my $dyHeight; # Height of font (1/20 of a point)
369
+ # my $grbit; # Font attributes
370
+ # my $icv; # Index to color palette
371
+ # my $bls; # Bold style
372
+ # my $sss; # Superscript/subscript
373
+ # my $uls; # Underline
374
+ # my $bFamily; # Font family
375
+ # my $bCharSet; # Character set
376
+ # my $reserved; # Reserved
377
+ # my $cch; # Length of font name
378
+ # my $rgch; # Font name
379
+ # my $encoding; # Font name character encoding
380
+
381
+
382
+ dyHeight = @size * 20
383
+ icv = @color
384
+ bls = @bold
385
+ sss = @font_script
386
+ uls = @underline
387
+ bFamily = @font_family
388
+ bCharSet = @font_charset
389
+ rgch = @font
390
+ encoding = @font_encoding
391
+
392
+ # Handle utf8 strings
393
+ if rgch =~ NonAscii
394
+ rgch = NKF.nkf('-w16B0 -m0 -W', rgch)
395
+ encoding = 1
396
+ end
397
+
398
+ cch = rgch.length
399
+ #
400
+ # Handle Unicode font names.
401
+ if (encoding == 1)
402
+ raise "Uneven number of bytes in Unicode font name" if cch % 2 != 0
403
+ cch /= 2 if encoding !=0
404
+ rgch = rgch.unpack('n*').pack('v*')
405
+ end
406
+
407
+ record = 0x31
408
+ length = 0x10 + rgch.length
409
+ reserved = 0x00
410
+
411
+ grbit = 0x00
412
+ grbit |= 0x02 if @italic != 0
413
+ grbit |= 0x08 if @font_strikeout != 0
414
+ grbit |= 0x10 if @font_outline != 0
415
+ grbit |= 0x20 if @font_shadow != 0
416
+
417
+
418
+ header = [record, length].pack("vv")
419
+ data = [dyHeight, grbit, icv, bls,
420
+ sss, uls, bFamily,
421
+ bCharSet, reserved, cch, encoding].pack('vvvvvCCCCCC')
422
+
423
+ return header + data + rgch
424
+ end
425
+
426
+ ###############################################################################
427
+ #
428
+ # get_font_key()
429
+ #
430
+ # Returns a unique hash key for a font. Used by Workbook->_store_all_fonts()
431
+ #
432
+ def get_font_key # :nodoc:
433
+ # The following elements are arranged to increase the probability of
434
+ # generating a unique key. Elements that hold a large range of numbers
435
+ # e.g. _color are placed between two binary elements such as _italic
436
+
437
+ key = "#{@font}#{@size}#{@font_script}#{@underline}#{@font_strikeout}#{@bold}#{@font_outline}"
438
+ key += "#{@font_family}#{@font_charset}#{@font_shadow}#{@color}#{@italic}#{@font_encoding}"
439
+ result = key.gsub(' ', '_') # Convert the key to a single word
440
+
441
+ return result
442
+ end
443
+
444
+ ###############################################################################
445
+ #
446
+ # xf_index()
447
+ #
448
+ # Returns the used by Worksheet->_XF()
449
+ #
450
+ def xf_index # :nodoc:
451
+ return @xf_index
452
+ end
453
+
454
+ def used_merge # :nodoc:
455
+ @used_merge
456
+ end
457
+
458
+ def used_merge=(val) # :nodoc:
459
+ @used_merge = val
460
+ end
461
+
462
+ def type # :nodoc:
463
+ @type
464
+ end
465
+
466
+ def font_index # :nodoc:
467
+ @font_index
468
+ end
469
+
470
+ def font_index=(val) # :nodoc:
471
+ @font_index = val
472
+ end
473
+
474
+ def font # :nodoc:
475
+ @font
476
+ end
477
+
478
+ def size # :nodoc:
479
+ @size
480
+ end
481
+
482
+ def bold # :nodoc:
483
+ @bold
484
+ end
485
+
486
+ def italic # :nodoc:
487
+ @italic
488
+ end
489
+
490
+ def color # :nodoc:
491
+ @color
492
+ end
493
+
494
+ def underline # :nodoc:
495
+ @underline
496
+ end
497
+
498
+ def font_strikeout # :nodoc:
499
+ @font_strikeout
500
+ end
501
+
502
+ def font_outline # :nodoc:
503
+ @font_outline
504
+ end
505
+
506
+ def font_shadow # :nodoc:
507
+ @font_shadow
508
+ end
509
+
510
+ def font_script # :nodoc:
511
+ @font_script
512
+ end
513
+
514
+ def font_family # :nodoc:
515
+ @font_family
516
+ end
517
+
518
+ def font_charset # :nodoc:
519
+ @font_charset
520
+ end
521
+
522
+ def font_encoding # :nodoc:
523
+ @font_encoding
524
+ end
525
+
526
+ def num_format # :nodoc:
527
+ @num_format
528
+ end
529
+
530
+ def num_format=(val) # :nodoc:
531
+ @num_format = val
532
+ end
533
+
534
+ def num_format_enc # :nodoc:
535
+ @num_format_enc
536
+ end
537
+
538
+ def hidden # :nodoc:
539
+ @hidden
540
+ end
541
+
542
+ def locked # :nodoc:
543
+ @locked
544
+ end
545
+
546
+ def text_h_align # :nodoc:
547
+ @text_h_align
548
+ end
549
+
550
+ def text_wrap # :nodoc:
551
+ @text_wrap
552
+ end
553
+
554
+ def text_v_align # :nodoc:
555
+ @text_v_align
556
+ end
557
+
558
+ def text_justlast # :nodoc:
559
+ @text_justlast
560
+ end
561
+
562
+ def rotation # :nodoc:
563
+ @rotation
564
+ end
565
+
566
+ def fg_color # :nodoc:
567
+ @fg_color
568
+ end
569
+
570
+ def bg_color # :nodoc:
571
+ @bg_color
572
+ end
573
+
574
+ def pattern # :nodoc:
575
+ @pattern
576
+ end
577
+
578
+ def bottom # :nodoc:
579
+ @bottom
580
+ end
581
+
582
+ def top # :nodoc:
583
+ @top
584
+ end
585
+
586
+ def left # :nodoc:
587
+ @left
588
+ end
589
+
590
+ def right # :nodoc:
591
+ @right
592
+ end
593
+
594
+ def bottom_color # :nodoc:
595
+ @bottom_color
596
+ end
597
+
598
+ def top_color # :nodoc:
599
+ @top_color
600
+ end
601
+
602
+ def left_color # :nodoc:
603
+ @left_color
604
+ end
605
+
606
+ def right_color # :nodoc:
607
+ @right_color
608
+ end
609
+
610
+ def indent # :nodoc:
611
+ @indent
612
+ end
613
+
614
+ def shrink # :nodoc:
615
+ @shrink
616
+ end
617
+
618
+ def reading_order # :nodoc:
619
+ @reading_order
620
+ end
621
+
622
+ def diag_type # :nodoc:
623
+ @diag_type
624
+ end
625
+
626
+ def diag_color # :nodoc:
627
+ @diag_color
628
+ end
629
+
630
+ def diag_border # :nodoc:
631
+ @diag_border
632
+ end
633
+
634
+ def font_only # :nodoc:
635
+ @font_only
636
+ end
637
+
638
+ ###############################################################################
639
+ #
640
+ # get_color(colour)
641
+ #
642
+ # Used in conjunction with the set_xxx_color methods to convert a color
643
+ # string into a number. Color range is 0..63 but we will restrict it
644
+ # to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.
645
+ #
646
+ def get_color(colour = nil) # :nodoc:
647
+ # Return the default color, 0x7FFF, if undef,
648
+ return 0x7FFF if colour.nil?
649
+
650
+ if colour.kind_of?(Numeric)
651
+ if colour < 0
652
+ return 0x7FFF
653
+
654
+ # or an index < 8 mapped into the correct range,
655
+ elsif colour < 8
656
+ return (colour + 8).to_i
657
+
658
+ # or the default color if arg is outside range,
659
+ elsif colour > 63
660
+ return 0x7FFF
661
+
662
+ # or an integer in the valid range
663
+ else
664
+ return colour.to_i
665
+ end
666
+ elsif colour.kind_of?(String)
667
+ # or the color string converted to an integer,
668
+ if COLORS.has_key?(colour)
669
+ return COLORS[colour]
670
+
671
+ # or the default color if string is unrecognised,
672
+ else
673
+ return 0x7FFF
674
+ end
675
+ else
676
+ return 0x7FFF
677
+ end
678
+ end
679
+
680
+ ###############################################################################
681
+ #
682
+ # class method Format._get_color(colour)
683
+ #
684
+ # used from Worksheet.rb
685
+ #
686
+ # this is cut & copy of get_color().
687
+ #
688
+ def self._get_color(colour) # :nodoc:
689
+ # Return the default color, 0x7FFF, if undef,
690
+ return 0x7FFF if colour.nil?
691
+
692
+ if colour.kind_of?(Numeric)
693
+ if colour < 0
694
+ return 0x7FFF
695
+
696
+ # or an index < 8 mapped into the correct range,
697
+ elsif colour < 8
698
+ return (colour + 8).to_i
699
+
700
+ # or the default color if arg is outside range,
701
+ elsif 63 < colour
702
+ return 0x7FFF
703
+
704
+ # or an integer in the valid range
705
+ else
706
+ return colour.to_i
707
+ end
708
+ elsif colour.kind_of?(String)
709
+ # or the color string converted to an integer,
710
+ if COLORS.has_key?(colour)
711
+ return COLORS[colour]
712
+
713
+ # or the default color if string is unrecognised,
714
+ else
715
+ return 0x7FFF
716
+ end
717
+ else
718
+ return 0x7FFF
719
+ end
720
+ end
721
+
722
+ ###############################################################################
723
+ #
724
+ # set_type()
725
+ #
726
+ # Set the XF object type as 0 = cell XF or 0xFFF5 = style XF.
727
+ #
728
+ def set_type(type = nil) # :nodoc:
729
+
730
+ if !type.nil? and type == 0
731
+ @type = 0x0000
732
+ else
733
+ @type = 0xFFF5
734
+ end
735
+ end
736
+
737
+ #
738
+ # Default state: Font size is 10
739
+ # Default action: Set font size to 1
740
+ # Valid args: Integer values from 1 to as big as your screen.
741
+ #
742
+ # Set the font size. Excel adjusts the height of a row to accommodate the
743
+ # largest font size in the row. You can also explicitly specify the height
744
+ # of a row using the set_row() worksheet method.
745
+ #
746
+ # format = workbook.add_format
747
+ # format.set_size(30)
748
+ #
749
+ def set_size(size = 1)
750
+ if size.kind_of?(Numeric) && size >= 1
751
+ @size = size.to_i
752
+ end
753
+ end
754
+
755
+ #
756
+ # Set the font colour.
757
+ #
758
+ # Default state: Excels default color, usually black
759
+ # Default action: Set the default color
760
+ # Valid args: Integers from 8..63 or the following strings:
761
+ # 'black', 'blue', 'brown', 'cyan', 'gray'
762
+ # 'green', 'lime', 'magenta', 'navy', 'orange'
763
+ # 'pink', 'purple', 'red', 'silver', 'white', 'yellow'
764
+ #
765
+ # The set_color() method is used as follows:
766
+ #
767
+ # format = workbook.add_format()
768
+ # format.set_color('red')
769
+ # worksheet.write(0, 0, 'wheelbarrow', format)
770
+ #
771
+ # Note: The set_color() method is used to set the colour of the font in a cell.
772
+ # To set the colour of a cell use the set_bg_color()
773
+ # and set_pattern() methods.
774
+ #
775
+ def set_color(color = 0x7FFF)
776
+ @color = get_color(color)
777
+ end
778
+
779
+ #
780
+ # Set the italic property of the font:
781
+ #
782
+ # Default state: Italic is off
783
+ # Default action: Turn italic on
784
+ # Valid args: 0, 1
785
+ #
786
+ # format.set_italic # Turn italic on
787
+ #
788
+ def set_italic(arg = 1)
789
+ begin
790
+ if arg == 1 then @italic = 1 # italic on
791
+ elsif arg == 0 then @italic = 0 # italic off
792
+ else
793
+ raise ArgumentError,
794
+ "\n\n set_italic(#{arg.inspect})\n arg must be 0, 1, or none. ( 0:OFF , 1 and none:ON )\n"
795
+ end
796
+ end
797
+ end
798
+
799
+ #
800
+ # Set the bold property of the font:
801
+ #
802
+ # Default state: bold is off
803
+ # Default action: Turn bold on
804
+ # Valid args: 0, 1 [1]
805
+ #
806
+ # format.set_bold() # Turn bold on
807
+ #
808
+ # [1] Actually, values in the range 100..1000 are also valid. 400 is normal,
809
+ # 700 is bold and 1000 is very bold indeed. It is probably best to set the
810
+ # value to 1 and use normal bold.
811
+ #
812
+ def set_bold(weight = nil)
813
+ if weight.nil?
814
+ weight = 0x2BC
815
+ elsif !weight.kind_of?(Numeric)
816
+ weight = 0x190
817
+ elsif weight == 1 # Bold text
818
+ weight = 0x2BC
819
+ elsif weight == 0 # Normal text
820
+ weight = 0x190
821
+ elsif weight < 0x064 # Lower bound
822
+ weight = 0x190
823
+ elsif weight > 0x3E8 # Upper bound
824
+ weight = 0x190
825
+ else
826
+ weight = weight.to_i
827
+ end
828
+
829
+ @bold = weight
830
+ end
831
+
832
+ #
833
+ # Set the underline property of the font.
834
+ #
835
+ # Default state: Underline is off
836
+ # Default action: Turn on single underline
837
+ # Valid args: 0 = No underline
838
+ # 1 = Single underline
839
+ # 2 = Double underline
840
+ # 33 = Single accounting underline
841
+ # 34 = Double accounting underline
842
+ #
843
+ # format.set_underline(); # Single underline
844
+ #
845
+ def set_underline(arg = 1)
846
+ begin
847
+ case arg
848
+ when 0 then @underline = 0 # off
849
+ when 1 then @underline = 1 # Single
850
+ when 2 then @underline = 2 # Double
851
+ when 33 then @underline = 33 # Single accounting
852
+ when 34 then @underline = 34 # Double accounting
853
+ else
854
+ raise ArgumentError,
855
+ "\n\n set_underline(#{arg.inspect})\n arg must be 0, 1, or none, 2, 33, 34.\n"
856
+ " ( 0:OFF, 1 and none:Single, 2:Double, 33:Single accounting, 34:Double accounting )\n"
857
+ end
858
+ end
859
+ end
860
+
861
+ #
862
+ # Set the strikeout property of the font.
863
+ #
864
+ # Default state: Strikeout is off
865
+ # Default action: Turn strikeout on
866
+ # Valid args: 0, 1
867
+ #
868
+ def set_font_strikeout(arg = 1)
869
+ begin
870
+ if arg == 0 then @font_strikeout = 0
871
+ elsif arg == 1 then @font_strikeout = 1
872
+ else
873
+ raise ArgumentError,
874
+ "\n\n set_font_strikeout(#{arg.inspect})\n arg must be 0, 1, or none.\n"
875
+ " ( 0:OFF, 1 and none:Strikeout )\n"
876
+ end
877
+ end
878
+ end
879
+
880
+ #
881
+ # Set the superscript/subscript property of the font.
882
+ # This format is currently not very useful.
883
+ #
884
+ # Default state: Super/Subscript is off
885
+ # Default action: Turn Superscript on
886
+ # Valid args: 0 = Normal
887
+ # 1 = Superscript
888
+ # 2 = Subscript
889
+ #
890
+ def set_font_script(arg = 1)
891
+ begin
892
+ if arg == 0 then @font_script = 0
893
+ elsif arg == 1 then @font_script = 1
894
+ elsif arg == 2 then @font_script = 2
895
+ else
896
+ raise ArgumentError,
897
+ "\n\n set_font_script(#{arg.inspect})\n arg must be 0, 1, or none. or 2\n"
898
+ " ( 0:OFF, 1 and none:Superscript, 2:Subscript )\n"
899
+ end
900
+ end
901
+ end
902
+
903
+ #
904
+ # Macintosh only.
905
+ #
906
+ # Default state: Outline is off
907
+ # Default action: Turn outline on
908
+ # Valid args: 0, 1
909
+ #
910
+ def set_font_outline(arg = 1)
911
+ begin
912
+ if arg == 0 then @font_outline = 0
913
+ elsif arg == 1 then @font_outline = 1
914
+ else
915
+ raise ArgumentError,
916
+ "\n\n set_font_outline(#{arg.inspect})\n arg must be 0, 1, or none.\n"
917
+ " ( 0:OFF, 1 and none:outline on )\n"
918
+ end
919
+ end
920
+ end
921
+
922
+ #
923
+ # Macintosh only.
924
+ #
925
+ # Default state: Shadow is off
926
+ # Default action: Turn shadow on
927
+ # Valid args: 0, 1
928
+ #
929
+ def set_font_shadow(arg = 1)
930
+ begin
931
+ if arg == 0 then @font_shadow = 0
932
+ elsif arg == 1 then @font_shadow = 1
933
+ else
934
+ raise ArgumentError,
935
+ "\n\n set_font_shadow(#{arg.inspect})\n arg must be 0, 1, or none.\n"
936
+ " ( 0:OFF, 1 and none:shadow on )\n"
937
+ end
938
+ end
939
+ end
940
+
941
+ #
942
+ # prevent modification of a cells contents.
943
+ #
944
+ # Default state: Cell locking is on
945
+ # Default action: Turn locking on
946
+ # Valid args: 0, 1
947
+ #
948
+ # This property can be used to prevent modification of a cells contents.
949
+ # Following Excel's convention, cell locking is turned on by default.
950
+ # However, it only has an effect if the worksheet has been protected,
951
+ # see the worksheet protect() method.
952
+ #
953
+ # locked = workbook.add_format()
954
+ # locked.set_locked(1) # A non-op
955
+ #
956
+ # unlocked = workbook.add_format()
957
+ # locked.set_locked(0)
958
+ #
959
+ # # Enable worksheet protection
960
+ # worksheet.protect()
961
+ #
962
+ # # This cell cannot be edited.
963
+ # worksheet.write('A1', '=1+2', locked)
964
+ #
965
+ # # This cell can be edited.
966
+ # worksheet.write('A2', '=1+2', unlocked)
967
+ #
968
+ # Note: This offers weak protection even with a password, see the note
969
+ # in relation to the protect() method.
970
+ #
971
+ def set_locked(arg = 1)
972
+ begin
973
+ if arg == 0 then @locked = 0
974
+ elsif arg == 1 then @locked = 1
975
+ else
976
+ raise ArgumentError,
977
+ "\n\n set_locked(#{arg.inspect})\n arg must be 0, 1, or none.\n"
978
+ " ( 0:OFF, 1 and none:Lock On )\n"
979
+ end
980
+ end
981
+ end
982
+
983
+ #
984
+ # hide a formula while still displaying its result.
985
+ #
986
+ # Default state: Formula hiding is off
987
+ # Default action: Turn hiding on
988
+ # Valid args: 0, 1
989
+ #
990
+ # This property is used to hide a formula while still displaying
991
+ # its result. This is generally used to hide complex calculations
992
+ # from end users who are only interested in the result. It only has
993
+ # an effect if the worksheet has been protected,
994
+ # see the worksheet protect() method.
995
+ #
996
+ # hidden = workbook.add_format
997
+ # hidden.set_hidden
998
+ #
999
+ # # Enable worksheet protection
1000
+ # worksheet.protect
1001
+ #
1002
+ # # The formula in this cell isn't visible
1003
+ # worksheet.write('A1', '=1+2', hidden)
1004
+ #
1005
+ # Note: This offers weak protection even with a password,
1006
+ # see the note in relation to the protect() method .
1007
+ #
1008
+ def set_hidden(arg = 1)
1009
+ begin
1010
+ if arg == 0 then @hidden = 0
1011
+ elsif arg == 1 then @hidden = 1
1012
+ else
1013
+ raise ArgumentError,
1014
+ "\n\n set_hidden(#{arg.inspect})\n arg must be 0, 1, or none.\n"
1015
+ " ( 0:OFF, 1 and none:hiding On )\n"
1016
+ end
1017
+ end
1018
+ end
1019
+
1020
+ #
1021
+ # Set cell alignment.
1022
+ #
1023
+ # Default state: Alignment is off
1024
+ # Default action: Left alignment
1025
+ # Valid args: 'left' Horizontal
1026
+ # 'center'
1027
+ # 'right'
1028
+ # 'fill'
1029
+ # 'justify'
1030
+ # 'center_across'
1031
+ #
1032
+ # 'top' Vertical
1033
+ # 'vcenter'
1034
+ # 'bottom'
1035
+ # 'vjustify'
1036
+ #
1037
+ # This method is used to set the horizontal and vertical text alignment
1038
+ # within a cell. Vertical and horizontal alignments can be combined.
1039
+ # The method is used as follows:
1040
+ #
1041
+ # format = workbook.add_format
1042
+ # format->set_align('center')
1043
+ # format->set_align('vcenter')
1044
+ # worksheet->set_row(0, 30)
1045
+ # worksheet->write(0, 0, 'X', format)
1046
+ #
1047
+ # Text can be aligned across two or more adjacent cells using
1048
+ # the center_across property. However, for genuine merged cells
1049
+ # it is better to use the merge_range() worksheet method.
1050
+ #
1051
+ # The vjustify (vertical justify) option can be used to provide
1052
+ # automatic text wrapping in a cell. The height of the cell will be
1053
+ # adjusted to accommodate the wrapped text. To specify where the text
1054
+ # wraps use the set_text_wrap() method.
1055
+ #
1056
+ # For further examples see the 'Alignment' worksheet created by formats.rb.
1057
+ #
1058
+ def set_align(align = 'left')
1059
+
1060
+ return unless align.kind_of?(String)
1061
+
1062
+ location = align.downcase
1063
+
1064
+ case location
1065
+ when 'left' then set_text_h_align(1)
1066
+ when 'centre', 'center' then set_text_h_align(2)
1067
+ when 'right' then set_text_h_align(3)
1068
+ when 'fill' then set_text_h_align(4)
1069
+ when 'justify' then set_text_h_align(5)
1070
+ when 'center_across', 'centre_across' then set_text_h_align(6)
1071
+ when 'merge' then set_text_h_align(6) # S:WE name
1072
+ when 'distributed' then set_text_h_align(7)
1073
+ when 'equal_space' then set_text_h_align(7) # ParseExcel
1074
+
1075
+ when 'top' then set_text_v_align(0)
1076
+ when 'vcentre' then set_text_v_align(1)
1077
+ when 'vcenter' then set_text_v_align(1)
1078
+ when 'bottom' then set_text_v_align(2)
1079
+ when 'vjustify' then set_text_v_align(3)
1080
+ when 'vdistributed' then set_text_v_align(4)
1081
+ when 'vequal_space' then set_text_v_align(4) # ParseExcel
1082
+ end
1083
+ end
1084
+
1085
+ ###############################################################################
1086
+ #
1087
+ # set_valign()
1088
+ #
1089
+ # Set vertical cell alignment. This is required by the set_format_properties()
1090
+ # method to differentiate between the vertical and horizontal properties.
1091
+ #
1092
+ def set_valign(alignment) # :nodoc:
1093
+ set_align(alignment);
1094
+ end
1095
+
1096
+ #
1097
+ # Implements the Excel5 style "merge".
1098
+ #
1099
+ # Default state: Center across selection is off
1100
+ # Default action: Turn center across on
1101
+ # Valid args: 1
1102
+ #
1103
+ # Text can be aligned across two or more adjacent cells using the
1104
+ # set_center_across() method. This is an alias for the
1105
+ # set_align('center_across') method call.
1106
+ #
1107
+ # Only one cell should contain the text, the other cells should be blank:
1108
+ #
1109
+ # format = workbook.add_format
1110
+ # format.set_center_across
1111
+ #
1112
+ # worksheet.write(1, 1, 'Center across selection', format)
1113
+ # worksheet.write_blank(1, 2, format)
1114
+ #
1115
+ # See also the merge1.pl to merge6.rb programs in the examples directory and
1116
+ # the merge_range() method.
1117
+ #
1118
+ def set_center_across(arg = 1)
1119
+ set_text_h_align(6)
1120
+ end
1121
+
1122
+ ###############################################################################
1123
+ #
1124
+ # set_merge()
1125
+ #
1126
+ # This was the way to implement a merge in Excel5. However it should have been
1127
+ # called "center_across" and not "merge".
1128
+ # This is now deprecated. Use set_center_across() or better merge_range().
1129
+ #
1130
+ #
1131
+ def set_merge(val=true) # :nodoc:
1132
+ set_text_h_align(6)
1133
+ end
1134
+
1135
+ #
1136
+ # Default state: Text wrap is off
1137
+ # Default action: Turn text wrap on
1138
+ # Valid args: 0, 1
1139
+ #
1140
+ # Here is an example using the text wrap property, the escape
1141
+ # character \n is used to indicate the end of line:
1142
+ #
1143
+ # format = workbook.add_format()
1144
+ # format.set_text_wrap()
1145
+ # worksheet.write(0, 0, "It's\na bum\nwrap", format)
1146
+ #
1147
+ def set_text_wrap(arg = 1)
1148
+ begin
1149
+ if arg == 0 then @text_wrap = 0
1150
+ elsif arg == 1 then @text_wrap = 1
1151
+ else
1152
+ raise ArgumentError,
1153
+ "\n\n set_text_wrap(#{arg.inspect})\n arg must be 0, 1, or none.\n"
1154
+ " ( 0:OFF, 1 and none:text wrap On )\n"
1155
+ end
1156
+ end
1157
+ end
1158
+
1159
+ #
1160
+ # Set cells borders to the same style
1161
+ #
1162
+ # Also applies to: set_bottom()
1163
+ # set_top()
1164
+ # set_left()
1165
+ # set_right()
1166
+ #
1167
+ # Default state: Border is off
1168
+ # Default action: Set border type 1
1169
+ # Valid args: 0-13, See below.
1170
+ #
1171
+ # A cell border is comprised of a border on the bottom, top, left and right.
1172
+ # These can be set to the same value using set_border() or individually
1173
+ # using the relevant method calls shown above.
1174
+ #
1175
+ # The following shows the border styles sorted by WriteExcel index number:
1176
+ #
1177
+ # Index Name Weight Style
1178
+ # ===== ============= ====== ===========
1179
+ # 0 None 0
1180
+ # 1 Continuous 1 -----------
1181
+ # 2 Continuous 2 -----------
1182
+ # 3 Dash 1 - - - - - -
1183
+ # 4 Dot 1 . . . . . .
1184
+ # 5 Continuous 3 -----------
1185
+ # 6 Double 3 ===========
1186
+ # 7 Continuous 0 -----------
1187
+ # 8 Dash 2 - - - - - -
1188
+ # 9 Dash Dot 1 - . - . - .
1189
+ # 10 Dash Dot 2 - . - . - .
1190
+ # 11 Dash Dot Dot 1 - . . - . .
1191
+ # 12 Dash Dot Dot 2 - . . - . .
1192
+ # 13 SlantDash Dot 2 / - . / - .
1193
+ #
1194
+ # The following shows the borders sorted by style:
1195
+ #
1196
+ # Name Weight Style Index
1197
+ # ============= ====== =========== =====
1198
+ # Continuous 0 ----------- 7
1199
+ # Continuous 1 ----------- 1
1200
+ # Continuous 2 ----------- 2
1201
+ # Continuous 3 ----------- 5
1202
+ # Dash 1 - - - - - - 3
1203
+ # Dash 2 - - - - - - 8
1204
+ # Dash Dot 1 - . - . - . 9
1205
+ # Dash Dot 2 - . - . - . 10
1206
+ # Dash Dot Dot 1 - . . - . . 11
1207
+ # Dash Dot Dot 2 - . . - . . 12
1208
+ # Dot 1 . . . . . . 4
1209
+ # Double 3 =========== 6
1210
+ # None 0 0
1211
+ # SlantDash Dot 2 / - . / - . 13
1212
+ #
1213
+ # The following shows the borders in the order shown in the Excel Dialog.
1214
+ #
1215
+ # Index Style Index Style
1216
+ # ===== ===== ===== =====
1217
+ # 0 None 12 - . . - . .
1218
+ # 7 ----------- 13 / - . / - .
1219
+ # 4 . . . . . . 10 - . - . - .
1220
+ # 11 - . . - . . 8 - - - - - -
1221
+ # 9 - . - . - . 2 -----------
1222
+ # 3 - - - - - - 5 -----------
1223
+ # 1 ----------- 6 ===========
1224
+ #
1225
+ # Examples of the available border styles are shown in the 'Borders' worksheet
1226
+ # created by formats.rb.
1227
+ #
1228
+ def set_border(style)
1229
+ set_bottom(style)
1230
+ set_top(style)
1231
+ set_left(style)
1232
+ set_right(style)
1233
+ end
1234
+
1235
+ #
1236
+ # set bottom border of the cell.
1237
+ # see set_border() about style.
1238
+ #
1239
+ def set_bottom(style)
1240
+ @bottom = style
1241
+ end
1242
+
1243
+ #
1244
+ # set top border of the cell.
1245
+ # see set_border() about style.
1246
+ #
1247
+ def set_top(style)
1248
+ @top = style
1249
+ end
1250
+
1251
+ #
1252
+ # set left border of the cell.
1253
+ # see set_border() about style.
1254
+ #
1255
+ def set_left(style)
1256
+ @left = style
1257
+ end
1258
+
1259
+ #
1260
+ # set right border of the cell.
1261
+ # see set_border() about style.
1262
+ #
1263
+ def set_right(style)
1264
+ @right = style
1265
+ end
1266
+
1267
+ #
1268
+ # Set cells border to the same color
1269
+ #
1270
+ # Also applies to: set_bottom_color()
1271
+ # set_top_color()
1272
+ # set_left_color()
1273
+ # set_right_color()
1274
+ #
1275
+ # Default state: Color is off
1276
+ # Default action: Undefined
1277
+ # Valid args: See set_color()
1278
+ #
1279
+ # Set the colour of the cell borders. A cell border is comprised of a border
1280
+ # on the bottom, top, left and right. These can be set to the same colour
1281
+ # using set_border_color() or individually using the relevant method calls
1282
+ # shown above. Examples of the border styles and colours are shown in the
1283
+ # 'Borders' worksheet created by formats.rb.
1284
+ #
1285
+ def set_border_color(color)
1286
+ set_bottom_color(color);
1287
+ set_top_color(color);
1288
+ set_left_color(color);
1289
+ set_right_color(color);
1290
+ end
1291
+
1292
+ #
1293
+ # set bottom border color of the cell.
1294
+ # see set_border_color() about color.
1295
+ #
1296
+ def set_bottom_color(color)
1297
+ @bottom_color = get_color(color)
1298
+ end
1299
+
1300
+ #
1301
+ # set top border color of the cell.
1302
+ # see set_border_color() about color.
1303
+ #
1304
+ def set_top_color(color)
1305
+ @top_color = get_color(color)
1306
+ end
1307
+
1308
+ #
1309
+ # set left border color of the cell.
1310
+ # see set_border_color() about color.
1311
+ #
1312
+ def set_left_color(color)
1313
+ @left_color = get_color(color)
1314
+ end
1315
+
1316
+ #
1317
+ # set right border color of the cell.
1318
+ # see set_border_color() about color.
1319
+ #
1320
+ def set_right_color(color)
1321
+ @right_color = get_color(color)
1322
+ end
1323
+
1324
+ #
1325
+ # Set the rotation angle of the text. An alignment property.
1326
+ #
1327
+ # Default state: Text rotation is off
1328
+ # Default action: None
1329
+ # Valid args: Integers in the range -90 to 90 and 270
1330
+ #
1331
+ # Set the rotation of the text in a cell. The rotation can be any angle in
1332
+ # the range -90 to 90 degrees.
1333
+ #
1334
+ # format = workbook.add_format
1335
+ # format.set_rotation(30)
1336
+ # worksheet.write(0, 0, 'This text is rotated', format)
1337
+ #
1338
+ # The angle 270 is also supported. This indicates text where the letters run
1339
+ # from top to bottom.
1340
+ #
1341
+ def set_rotation(rotation)
1342
+ # Argument should be a number
1343
+ return unless rotation.kind_of?(Numeric)
1344
+
1345
+ # The arg type can be a double but the Excel dialog only allows integers.
1346
+ rotation = rotation.to_i
1347
+
1348
+ # if (rotation == 270)
1349
+ # rotation = 255
1350
+ # elsif (rotation >= -90 or rotation <= 90)
1351
+ # rotation = -rotation +90 if rotation < 0;
1352
+ # else
1353
+ # # carp "Rotation $rotation outside range: -90 <= angle <= 90";
1354
+ # rotation = 0;
1355
+ # end
1356
+ #
1357
+ if rotation == 270
1358
+ rotation = 255
1359
+ elsif rotation >= -90 && rotation <= 90
1360
+ rotation = -rotation + 90 if rotation < 0
1361
+ else
1362
+ rotation = 0
1363
+ end
1364
+
1365
+ @rotation = rotation;
1366
+ end
1367
+
1368
+
1369
+ #
1370
+ # :call-seq:
1371
+ # set_format_properties( :bold => 1 [, :color => 'red'..] )
1372
+ # set_format_properties( font [, shade, ..])
1373
+ # set_format_properties( :bold => 1, font, ...)
1374
+ # *) font = { :color => 'red', :bold => 1 }
1375
+ # shade = { :bg_color => 'green', :pattern => 1 }
1376
+ #
1377
+ # Convert hashes of properties to method calls.
1378
+ #
1379
+ # The properties of an existing Format object can be also be set by means
1380
+ # of set_format_properties():
1381
+ #
1382
+ # format = workbook.add_format
1383
+ # format.set_format_properties(:bold => 1, :color => 'red');
1384
+ #
1385
+ # However, this method is here mainly for legacy reasons. It is preferable
1386
+ # to set the properties in the format constructor:
1387
+ #
1388
+ # format = workbook.add_format(:bold => 1, :color => 'red');
1389
+ #
1390
+ def set_format_properties(*properties) # :nodoc:
1391
+ return if properties.empty?
1392
+ properties.each do |property|
1393
+ property.each do |key, value|
1394
+ # Strip leading "-" from Tk style properties e.g. -color => 'red'.
1395
+ key.sub!(/^-/, '') if key.kind_of?(String)
1396
+
1397
+ # Create a sub to set the property.
1398
+ if value.kind_of?(String)
1399
+ s = "set_#{key}('#{value}')"
1400
+ else
1401
+ s = "set_#{key}(#{value})"
1402
+ end
1403
+ eval s
1404
+ end
1405
+ end
1406
+ end
1407
+
1408
+ #
1409
+ # Default state: Font is Arial
1410
+ # Default action: None
1411
+ # Valid args: Any valid font name
1412
+ #
1413
+ # Specify the font used:
1414
+ #
1415
+ # format.set_font('Times New Roman');
1416
+ #
1417
+ # Excel can only display fonts that are installed on the system that it is
1418
+ # running on. Therefore it is best to use the fonts that come as standard
1419
+ # such as 'Arial', 'Times New Roman' and 'Courier New'. See also the Fonts
1420
+ # worksheet created by formats.rb
1421
+ #
1422
+ def set_font(fontname)
1423
+ @font = fontname
1424
+ end
1425
+
1426
+ #
1427
+ # This method is used to define the numerical format of a number in Excel.
1428
+ #
1429
+ # Default state: General format
1430
+ # Default action: Format index 1
1431
+ # Valid args: See the following table
1432
+ #
1433
+ # It controls whether a number is displayed as an integer, a floating point
1434
+ # number, a date, a currency value or some other user defined format.
1435
+ #
1436
+ # The numerical format of a cell can be specified by using a format string
1437
+ # or an index to one of Excel's built-in formats:
1438
+ #
1439
+ # format1 = workbook.add_format
1440
+ # format2 = workbook.add_format
1441
+ # format1.set_num_format('d mmm yyyy') # Format string
1442
+ # format2.set_num_format(0x0f) # Format index
1443
+ #
1444
+ # worksheet.write(0, 0, 36892.521, format1) # 1 Jan 2001
1445
+ # worksheet.write(0, 0, 36892.521, format2) # 1-Jan-01
1446
+ #
1447
+ # Using format strings you can define very sophisticated formatting of
1448
+ # numbers.
1449
+ #
1450
+ # format01.set_num_format('0.000')
1451
+ # worksheet.write(0, 0, 3.1415926, format01) # 3.142
1452
+ #
1453
+ # format02.set_num_format('#,##0')
1454
+ # worksheet.write(1, 0, 1234.56, format02) # 1,235
1455
+ #
1456
+ # format03.set_num_format('#,##0.00')
1457
+ # worksheet.write(2, 0, 1234.56, format03) # 1,234.56
1458
+ #
1459
+ # format04.set_num_format('0.00')
1460
+ # worksheet.write(3, 0, 49.99, format04) # 49.99
1461
+ #
1462
+ # # Note you can use other currency symbols such as the pound or yen as well.
1463
+ # # Other currencies may require the use of Unicode.
1464
+ #
1465
+ # format07.set_num_format('mm/dd/yy')
1466
+ # worksheet.write(6, 0, 36892.521, format07) # 01/01/01
1467
+ #
1468
+ # format08.set_num_format('mmm d yyyy')
1469
+ # worksheet.write(7, 0, 36892.521, format08) # Jan 1 2001
1470
+ #
1471
+ # format09.set_num_format('d mmmm yyyy')
1472
+ # worksheet.write(8, 0, 36892.521, format09) # 1 January 2001
1473
+ #
1474
+ # format10.set_num_format('dd/mm/yyyy hh:mm AM/PM')
1475
+ # worksheet.write(9, 0, 36892.521, format10) # 01/01/2001 12:30 AM
1476
+ #
1477
+ # format11.set_num_format('0 "dollar and" .00 "cents"')
1478
+ # worksheet.write(10, 0, 1.87, format11) # 1 dollar and .87 cents
1479
+ #
1480
+ # # Conditional formatting
1481
+ # format12.set_num_format('[Green]General;[Red]-General;General')
1482
+ # worksheet.write(11, 0, 123, format12) # > 0 Green
1483
+ # worksheet.write(12, 0, -45, format12) # < 0 Red
1484
+ # worksheet.write(13, 0, 0, format12) # = 0 Default colour
1485
+ #
1486
+ # # Zip code
1487
+ # format13.set_num_format('00000')
1488
+ # worksheet.write(14, 0, '01209', format13)
1489
+ #
1490
+ # The number system used for dates is described in "DATES AND TIME IN EXCEL".
1491
+ #
1492
+ # The colour format should have one of the following values:
1493
+ #
1494
+ # [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
1495
+ #
1496
+ # Alternatively you can specify the colour based on a colour index as follows:
1497
+ # [Color n], where n is a standard Excel colour index - 7. See the
1498
+ # 'Standard colors' worksheet created by formats.rb.
1499
+ #
1500
+ # For more information refer to the documentation on formatting in the doc
1501
+ # directory of the WriteExcel distro, the Excel on-line help or
1502
+ # http://office.microsoft.com/en-gb/assistance/HP051995001033.aspx
1503
+ #
1504
+ # You should ensure that the format string is valid in Excel prior to using
1505
+ # it in WriteExcel.
1506
+ #
1507
+ # Excel's built-in formats are shown in the following table:
1508
+ #
1509
+ # Index Index Format String
1510
+ # 0 0x00 General
1511
+ # 1 0x01 0
1512
+ # 2 0x02 0.00
1513
+ # 3 0x03 #,##0
1514
+ # 4 0x04 #,##0.00
1515
+ # 5 0x05 ($#,##0_);($#,##0)
1516
+ # 6 0x06 ($#,##0_);[Red]($#,##0)
1517
+ # 7 0x07 ($#,##0.00_);($#,##0.00)
1518
+ # 8 0x08 ($#,##0.00_);[Red]($#,##0.00)
1519
+ # 9 0x09 0%
1520
+ # 10 0x0a 0.00%
1521
+ # 11 0x0b 0.00E+00
1522
+ # 12 0x0c # ?/?
1523
+ # 13 0x0d # ??/??
1524
+ # 14 0x0e m/d/yy
1525
+ # 15 0x0f d-mmm-yy
1526
+ # 16 0x10 d-mmm
1527
+ # 17 0x11 mmm-yy
1528
+ # 18 0x12 h:mm AM/PM
1529
+ # 19 0x13 h:mm:ss AM/PM
1530
+ # 20 0x14 h:mm
1531
+ # 21 0x15 h:mm:ss
1532
+ # 22 0x16 m/d/yy h:mm
1533
+ # .. .... ...........
1534
+ # 37 0x25 (#,##0_);(#,##0)
1535
+ # 38 0x26 (#,##0_);[Red](#,##0)
1536
+ # 39 0x27 (#,##0.00_);(#,##0.00)
1537
+ # 40 0x28 (#,##0.00_);[Red](#,##0.00)
1538
+ # 41 0x29 _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
1539
+ # 42 0x2a _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)
1540
+ # 43 0x2b _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)
1541
+ # 44 0x2c _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)
1542
+ # 45 0x2d mm:ss
1543
+ # 46 0x2e [h]:mm:ss
1544
+ # 47 0x2f mm:ss.0
1545
+ # 48 0x30 ##0.0E+0
1546
+ # 49 0x31 @
1547
+ #
1548
+ # For examples of these formatting codes see the 'Numerical formats' worksheet
1549
+ # created by formats.rb.
1550
+ #--
1551
+ # See also the number_formats1.html and the number_formats2.html documents in
1552
+ # the doc directory of the distro.
1553
+ #++
1554
+ #
1555
+ # Note 1. Numeric formats 23 to 36 are not documented by Microsoft and may
1556
+ # differ in international versions.
1557
+ #
1558
+ # Note 2. In Excel 5 the dollar sign appears as a dollar sign. In Excel
1559
+ # 97-2000 it appears as the defined local currency symbol.
1560
+ #
1561
+ # Note 3. The red negative numeric formats display slightly differently in
1562
+ # Excel 5 and Excel 97-2000.
1563
+ #
1564
+ def set_num_format(num_format)
1565
+ @num_format = num_format
1566
+ end
1567
+
1568
+ #
1569
+ # This method can be used to indent text. The argument, which should be an
1570
+ # integer, is taken as the level of indentation:
1571
+ #
1572
+ # Default state: Text indentation is off
1573
+ # Default action: Indent text 1 level
1574
+ # Valid args: Positive integers
1575
+ #
1576
+ # format = workbook.add_format
1577
+ # format.set_indent(2)
1578
+ # worksheet.write(0, 0, 'This text is indented', format)
1579
+ #
1580
+ # Indentation is a horizontal alignment property. It will override any
1581
+ # other horizontal properties but it can be used in conjunction with
1582
+ # vertical properties.
1583
+ #
1584
+ def set_indent(indent = 1)
1585
+ @indent = indent
1586
+ end
1587
+
1588
+ #
1589
+ # This method can be used to shrink text so that it fits in a cell.
1590
+ #
1591
+ # Default state: Text shrinking is off
1592
+ # Default action: Turn "shrink to fit" on
1593
+ # Valid args: 1
1594
+ #
1595
+ # format = workbook.add_format
1596
+ # format.set_shrink
1597
+ # worksheet.write(0, 0, 'Honey, I shrunk the text!', format)
1598
+ #
1599
+ def set_shrink(arg = 1)
1600
+ @shrink = 1
1601
+ end
1602
+
1603
+ #
1604
+ # Default state: Justify last is off
1605
+ # Default action: Turn justify last on
1606
+ # Valid args: 0, 1
1607
+ #
1608
+ # Only applies to Far Eastern versions of Excel.
1609
+ #
1610
+ def set_text_justlast(arg = 1)
1611
+ @text_justlast = 1
1612
+ end
1613
+
1614
+ #
1615
+ # Default state: Pattern is off
1616
+ # Default action: Solid fill is on
1617
+ # Valid args: 0 .. 18
1618
+ #
1619
+ # Set the background pattern of a cell.
1620
+ #
1621
+ # Examples of the available patterns are shown in the 'Patterns' worksheet
1622
+ # created by formats.rb. However, it is unlikely that you will ever need
1623
+ # anything other than Pattern 1 which is a solid fill of the background color.
1624
+ #
1625
+ def set_pattern(pattern = 1)
1626
+ @pattern = pattern
1627
+ end
1628
+
1629
+ #
1630
+ # The set_bg_color() method can be used to set the background colour of a
1631
+ # pattern. Patterns are defined via the set_pattern() method. If a pattern
1632
+ # hasn't been defined then a solid fill pattern is used as the default.
1633
+ #
1634
+ # Default state: Color is off
1635
+ # Default action: Solid fill.
1636
+ # Valid args: See set_color()
1637
+ #
1638
+ # Here is an example of how to set up a solid fill in a cell:
1639
+ #
1640
+ # format = workbook.add_format
1641
+ #
1642
+ # format.set_pattern() # This is optional when using a solid fill
1643
+ #
1644
+ # format.set_bg_color('green')
1645
+ # worksheet.write('A1', 'Ray', format)
1646
+ #
1647
+ # For further examples see the 'Patterns' worksheet created by formats.rb.
1648
+ #
1649
+ def set_bg_color(color = 0x41)
1650
+ @bg_color = get_color(color)
1651
+ end
1652
+
1653
+ #
1654
+ # The set_fg_color() method can be used to set the foreground colour
1655
+ # of a pattern.
1656
+ #
1657
+ # Default state: Color is off
1658
+ # Default action: Solid fill.
1659
+ # Valid args: See set_color()
1660
+ #
1661
+ # For further examples see the 'Patterns' worksheet created by formats.rb.
1662
+ #
1663
+ def set_fg_color(color = 0x40)
1664
+ @fg_color = get_color(color)
1665
+ end
1666
+
1667
+ # Dynamically create set methods that aren't already defined.
1668
+ def method_missing(name, *args) # :nodoc:
1669
+ # -- original perl comment --
1670
+ # There are two types of set methods: set_property() and
1671
+ # set_property_color(). When a method is AUTOLOADED we store a new anonymous
1672
+ # sub in the appropriate slot in the symbol table. The speeds up subsequent
1673
+ # calls to the same method.
1674
+
1675
+ method = "#{name}"
1676
+
1677
+ # Check for a valid method names, i.e. "set_xxx_yyy".
1678
+ method =~ /set_(\w+)/ or raise "Unknown method: #{method}\n"
1679
+
1680
+ # Match the attribute, i.e. "@xxx_yyy".
1681
+ attribute = "@#{$1}"
1682
+
1683
+ # Check that the attribute exists
1684
+ # ........
1685
+ if method =~ /set\w+color$/ # for "set_property_color" methods
1686
+ value = get_color(args[0])
1687
+ else # for "set_xxx" methods
1688
+ value = args[0].nil? ? 1 : args[0]
1689
+ end
1690
+ if value.kind_of?(String)
1691
+ s = "#{attribute} = \"#{value.to_s}\""
1692
+ else
1693
+ s = "#{attribute} = #{value.to_s}"
1694
+ end
1695
+ eval s
1696
+ end
1697
+ end