writeexcel 0.4.2 → 0.4.3

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