ricardoo27-writeexcel 0.6.12.1

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