writeexcel 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/.gitattributes +1 -1
  2. data/.gitignore +24 -24
  3. data/README.rdoc +34 -55
  4. data/VERSION +1 -1
  5. data/charts/chartex.rb +316 -316
  6. data/charts/demo1.rb +46 -46
  7. data/charts/demo2.rb +65 -65
  8. data/charts/demo3.rb +117 -117
  9. data/charts/demo4.rb +119 -119
  10. data/charts/demo5.rb +48 -48
  11. data/examples/a_simple.rb +43 -43
  12. data/examples/autofilter.rb +265 -265
  13. data/examples/bigfile.rb +30 -30
  14. data/examples/chart_area.rb +121 -121
  15. data/examples/chart_bar.rb +120 -120
  16. data/examples/chart_column.rb +120 -120
  17. data/examples/chart_line.rb +120 -120
  18. data/examples/chart_pie.rb +108 -108
  19. data/examples/chart_scatter.rb +121 -121
  20. data/examples/chart_stock.rb +148 -148
  21. data/examples/chess.rb +142 -142
  22. data/examples/colors.rb +129 -129
  23. data/examples/comments1.rb +27 -27
  24. data/examples/comments2.rb +352 -352
  25. data/examples/copyformat.rb +52 -52
  26. data/examples/data_validate.rb +279 -279
  27. data/examples/date_time.rb +87 -87
  28. data/examples/defined_name.rb +32 -32
  29. data/examples/demo.rb +124 -124
  30. data/examples/diag_border.rb +36 -36
  31. data/examples/formats.rb +490 -490
  32. data/examples/formula_result.rb +30 -30
  33. data/examples/header.rb +137 -137
  34. data/examples/hide_sheet.rb +29 -29
  35. data/examples/hyperlink.rb +43 -43
  36. data/examples/images.rb +63 -63
  37. data/examples/indent.rb +31 -31
  38. data/examples/merge1.rb +40 -40
  39. data/examples/merge2.rb +45 -45
  40. data/examples/merge3.rb +66 -66
  41. data/examples/merge4.rb +83 -83
  42. data/examples/merge5.rb +80 -80
  43. data/examples/merge6.rb +67 -67
  44. data/examples/outline.rb +255 -255
  45. data/examples/outline_collapsed.rb +209 -209
  46. data/examples/panes.rb +113 -113
  47. data/examples/properties.rb +34 -34
  48. data/examples/properties_jp.rb +33 -33
  49. data/examples/protection.rb +47 -47
  50. data/examples/regions.rb +53 -53
  51. data/examples/repeat.rb +43 -43
  52. data/examples/right_to_left.rb +27 -27
  53. data/examples/row_wrap.rb +53 -53
  54. data/examples/stats.rb +74 -74
  55. data/examples/stocks.rb +81 -81
  56. data/examples/tab_colors.rb +31 -31
  57. data/examples/utf8.rb +15 -15
  58. data/examples/write_arrays.rb +83 -83
  59. data/lib/writeexcel/biffwriter.rb +232 -232
  60. data/lib/writeexcel/caller_info.rb +12 -12
  61. data/lib/writeexcel/chart.rb +2190 -2177
  62. data/lib/writeexcel/charts/area.rb +154 -154
  63. data/lib/writeexcel/charts/bar.rb +177 -177
  64. data/lib/writeexcel/charts/column.rb +156 -156
  65. data/lib/writeexcel/charts/external.rb +66 -66
  66. data/lib/writeexcel/charts/line.rb +154 -154
  67. data/lib/writeexcel/charts/pie.rb +169 -169
  68. data/lib/writeexcel/charts/scatter.rb +192 -192
  69. data/lib/writeexcel/charts/stock.rb +213 -213
  70. data/lib/writeexcel/colors.rb +64 -64
  71. data/lib/writeexcel/compatibility.rb +0 -255
  72. data/lib/writeexcel/debug_info.rb +37 -33
  73. data/lib/writeexcel/excelformulaparser.rb +587 -587
  74. data/lib/writeexcel/format.rb +13 -4
  75. data/lib/writeexcel/formula.rb +26 -9
  76. data/lib/writeexcel/helper.rb +68 -64
  77. data/lib/writeexcel/olewriter.rb +311 -311
  78. data/lib/writeexcel/properties.rb +242 -240
  79. data/lib/writeexcel/storage_lite.rb +984 -978
  80. data/lib/writeexcel/workbook.rb +3210 -3192
  81. data/lib/writeexcel/worksheet.rb +143 -51
  82. data/lib/writeexcel/write_file.rb +44 -40
  83. data/lib/writeexcel.rb +1159 -1159
  84. data/test/helper.rb +31 -28
  85. data/test/perl_output/README +31 -31
  86. data/test/test_00_IEEE_double.rb +13 -13
  87. data/test/test_01_add_worksheet.rb +10 -10
  88. data/test/test_02_merge_formats.rb +53 -53
  89. data/test/test_04_dimensions.rb +392 -392
  90. data/test/test_05_rows.rb +179 -179
  91. data/test/test_06_extsst.rb +77 -77
  92. data/test/test_11_date_time.rb +479 -479
  93. data/test/test_12_date_only.rb +501 -501
  94. data/test/test_13_date_seconds.rb +481 -481
  95. data/test/test_21_escher.rb +637 -637
  96. data/test/test_22_mso_drawing_group.rb +745 -745
  97. data/test/test_23_note.rb +73 -73
  98. data/test/test_24_txo.rb +75 -75
  99. data/test/test_25_position_object.rb +84 -84
  100. data/test/test_26_autofilter.rb +314 -314
  101. data/test/test_27_autofilter.rb +131 -131
  102. data/test/test_28_autofilter.rb +161 -161
  103. data/test/test_29_process_jpg.rb +683 -683
  104. data/test/test_30_validation_dval.rb +77 -77
  105. data/test/test_31_validation_dv_strings.rb +126 -126
  106. data/test/test_32_validation_dv_formula.rb +206 -206
  107. data/test/test_40_property_types.rb +188 -188
  108. data/test/test_41_properties.rb +235 -235
  109. data/test/test_42_set_properties.rb +437 -437
  110. data/test/test_50_name_stored.rb +299 -299
  111. data/test/test_51_name_print_area.rb +357 -357
  112. data/test/test_52_name_print_titles.rb +454 -454
  113. data/test/test_53_autofilter.rb +203 -203
  114. data/test/test_60_chart_generic.rb +578 -578
  115. data/test/test_61_chart_subclasses.rb +95 -95
  116. data/test/test_62_chart_formats.rb +272 -272
  117. data/test/test_63_chart_area_formats.rb +649 -649
  118. data/test/test_biff.rb +75 -75
  119. data/test/test_compatibility.rb +12 -627
  120. data/test/test_example_match.rb +3144 -3144
  121. data/test/test_formula.rb +61 -61
  122. data/test/test_ole.rb +106 -106
  123. data/test/test_storage_lite.rb +125 -125
  124. data/test/test_workbook.rb +139 -139
  125. data/test/test_worksheet.rb +110 -110
  126. data/utils/add_magic_comment.rb +80 -80
  127. data/writeexcel.gemspec +4 -6
  128. data/writeexcel.rdoc +58 -15
  129. metadata +9 -6
  130. data/test/test_new_encoding.rb +0 -205
data/examples/formats.rb CHANGED
@@ -1,490 +1,490 @@
1
- #!/usr/bin/ruby -w
2
- # -*- coding: utf-8 -*-
3
-
4
- ######################################################################
5
- #
6
- # Examples of formatting using the WriteExcel module
7
- #
8
- # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
9
- #
10
- # original written in Perl by John McNamara
11
- # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
- #
13
-
14
- require 'rubygems'
15
- require 'writeexcel'
16
-
17
-
18
- ######################################################################
19
- #
20
- # Intro.
21
- #
22
- def intro(workbook)
23
-
24
- worksheet = workbook.add_worksheet('Introduction')
25
-
26
- worksheet.set_column(0, 0, 60)
27
-
28
- format = workbook.add_format()
29
- format.set_bold()
30
- format.set_size(14)
31
- format.set_color('blue')
32
- format.set_align('center')
33
-
34
- format2 = workbook.add_format()
35
- format2.set_bold()
36
- format2.set_color('blue')
37
-
38
- worksheet.write(2, 0, 'This workbook demonstrates some of', format)
39
- worksheet.write(3, 0, 'the formatting options provided by', format)
40
- worksheet.write(4, 0, 'the WriteExcel module.', format)
41
-
42
- worksheet.write('A7', 'Sections:', format2)
43
- worksheet.write('A8', "internal:Fonts!A1", 'Fonts' )
44
- worksheet.write('A9', "internal:'Named colors'!A1", 'Named colors' )
45
- worksheet.write('A10', "internal:'Standard colors'!A1", 'Standard colors')
46
- worksheet.write('A11', "internal:'Numeric formats'!A1", 'Numeric formats')
47
- worksheet.write('A12', "internal:Borders!A1", 'Borders' )
48
- worksheet.write('A13', "internal:Patterns!A1", 'Patterns' )
49
- worksheet.write('A14', "internal:Alignment!A1", 'Alignment' )
50
- worksheet.write('A15', "internal:Miscellaneous!A1", 'Miscellaneous' )
51
-
52
- end
53
-
54
-
55
- ######################################################################
56
- #
57
- # Demonstrate the named colors.
58
- #
59
- def named_colors(workbook, heading, center)
60
-
61
- worksheet = workbook.add_worksheet('Named colors')
62
-
63
- worksheet.set_column(0, 3, 15)
64
-
65
- worksheet.write(0, 0, "Index", heading)
66
- worksheet.write(0, 1, "Index", heading)
67
- worksheet.write(0, 2, "Name", heading)
68
- worksheet.write(0, 3, "Color", heading)
69
-
70
- i = 1
71
-
72
- COLORS.each do |index, color|
73
- format = workbook.add_format(
74
- :bg_color => color,
75
- :pattern => 1,
76
- :border => 1
77
- )
78
-
79
- worksheet.write(i+1, 0, index, center)
80
- worksheet.write(i+1, 1, sprintf("0x%02X", index), center)
81
- worksheet.write(i+1, 2, color, center)
82
- worksheet.write(i+1, 3, '', format)
83
- i += 1
84
- end
85
- end
86
-
87
-
88
- ######################################################################
89
- #
90
- # Demonstrate the standard Excel colors in the range 8..63.
91
- #
92
- def standard_colors(workbook, heading, center)
93
-
94
- worksheet = workbook.add_worksheet('Standard colors')
95
-
96
- worksheet.set_column(0, 3, 15)
97
-
98
- worksheet.write(0, 0, "Index", heading)
99
- worksheet.write(0, 1, "Index", heading)
100
- worksheet.write(0, 2, "Color", heading)
101
- worksheet.write(0, 3, "Name", heading)
102
-
103
- 8.upto(63) do |i|
104
- format = workbook.add_format(
105
- :bg_color => i,
106
- :pattern => 1,
107
- :border => 1
108
- )
109
-
110
- worksheet.write((i -7), 0, i, center)
111
- worksheet.write((i -7), 1, sprintf("0x%02X", i), center)
112
- worksheet.write((i -7), 2, '', format)
113
-
114
- # Add the color names
115
- if COLORS[i]
116
- worksheet.write((i -7), 3, COLORS[i], center)
117
-
118
- end
119
- end
120
- end
121
-
122
-
123
- ######################################################################
124
- #
125
- # Demonstrate the standard numeric formats.
126
- #
127
- def numeric_formats(workbook, heading, center)
128
-
129
- worksheet = workbook.add_worksheet('Numeric formats')
130
-
131
- worksheet.set_column(0, 4, 15)
132
- worksheet.set_column(5, 5, 45)
133
-
134
- worksheet.write(0, 0, "Index", heading)
135
- worksheet.write(0, 1, "Index", heading)
136
- worksheet.write(0, 2, "Unformatted", heading)
137
- worksheet.write(0, 3, "Formatted", heading)
138
- worksheet.write(0, 4, "Negative", heading)
139
- worksheet.write(0, 5, "Format", heading)
140
-
141
- formats = []
142
- formats.push([ 0x00, 1234.567, 0, 'General' ])
143
- formats.push([ 0x01, 1234.567, 0, '0' ])
144
- formats.push([ 0x02, 1234.567, 0, '0.00' ])
145
- formats.push([ 0x03, 1234.567, 0, '#,##0' ])
146
- formats.push([ 0x04, 1234.567, 0, '#,##0.00' ])
147
- formats.push([ 0x05, 1234.567, -1234.567, '($#,##0_);($#,##0)' ])
148
- formats.push([ 0x06, 1234.567, -1234.567, '($#,##0_);[Red]($#,##0)' ])
149
- formats.push([ 0x07, 1234.567, -1234.567, '($#,##0.00_);($#,##0.00)' ])
150
- formats.push([ 0x08, 1234.567, -1234.567, '($#,##0.00_);[Red]($#,##0.00)' ])
151
- formats.push([ 0x09, 0.567, 0, '0%' ])
152
- formats.push([ 0x0a, 0.567, 0, '0.00%' ])
153
- formats.push([ 0x0b, 1234.567, 0, '0.00E+00' ])
154
- formats.push([ 0x0c, 0.75, 0, '# ?/?' ])
155
- formats.push([ 0x0d, 0.3125, 0, '# ??/??' ])
156
- formats.push([ 0x0e, 36892.521, 0, 'm/d/yy' ])
157
- formats.push([ 0x0f, 36892.521, 0, 'd-mmm-yy' ])
158
- formats.push([ 0x10, 36892.521, 0, 'd-mmm' ])
159
- formats.push([ 0x11, 36892.521, 0, 'mmm-yy' ])
160
- formats.push([ 0x12, 36892.521, 0, 'h:mm AM/PM' ])
161
- formats.push([ 0x13, 36892.521, 0, 'h:mm:ss AM/PM' ])
162
- formats.push([ 0x14, 36892.521, 0, 'h:mm' ])
163
- formats.push([ 0x15, 36892.521, 0, 'h:mm:ss' ])
164
- formats.push([ 0x16, 36892.521, 0, 'm/d/yy h:mm' ])
165
- formats.push([ 0x25, 1234.567, -1234.567, '(#,##0_);(#,##0)' ])
166
- formats.push([ 0x26, 1234.567, -1234.567, '(#,##0_);[Red](#,##0)' ])
167
- formats.push([ 0x27, 1234.567, -1234.567, '(#,##0.00_);(#,##0.00)' ])
168
- formats.push([ 0x28, 1234.567, -1234.567, '(#,##0.00_);[Red](#,##0.00)' ])
169
- formats.push([ 0x29, 1234.567, -1234.567, '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)' ])
170
- formats.push([ 0x2a, 1234.567, -1234.567, '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)' ])
171
- formats.push([ 0x2b, 1234.567, -1234.567, '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' ])
172
- formats.push([ 0x2c, 1234.567, -1234.567, '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)' ])
173
- formats.push([ 0x2d, 36892.521, 0, 'mm:ss' ])
174
- formats.push([ 0x2e, 3.0153, 0, '[h]:mm:ss' ])
175
- formats.push([ 0x2f, 36892.521, 0, 'mm:ss.0' ])
176
- formats.push([ 0x30, 1234.567, 0, '##0.0E+0' ])
177
- formats.push([ 0x31, 1234.567, 0, '@' ])
178
-
179
- i = 0
180
- formats.each do |format|
181
- style = workbook.add_format()
182
- style.set_num_format(format[0])
183
-
184
- i += 1
185
- worksheet.write(i, 0, format[0], center)
186
- worksheet.write(i, 1, sprintf("0x%02X", format[0]), center)
187
- worksheet.write(i, 2, format[1], center)
188
- worksheet.write(i, 3, format[1], style)
189
-
190
- if format[2] != 0
191
- worksheet.write(i, 4, format[2], style)
192
- end
193
-
194
- worksheet.write_string(i, 5, format[3])
195
- end
196
- end
197
-
198
-
199
- ######################################################################
200
- #
201
- # Demonstrate the font options.
202
- #
203
- def fonts(workbook, heading, center)
204
-
205
- worksheet = workbook.add_worksheet('Fonts')
206
-
207
- worksheet.set_column(0, 0, 30)
208
- worksheet.set_column(1, 1, 10)
209
-
210
- worksheet.write(0, 0, "Font name", heading)
211
- worksheet.write(0, 1, "Font size", heading)
212
-
213
- fonts = []
214
- fonts.push([10, 'Arial' ])
215
- fonts.push([12, 'Arial' ])
216
- fonts.push([14, 'Arial' ])
217
- fonts.push([12, 'Arial Black' ])
218
- fonts.push([12, 'Arial Narrow' ])
219
- fonts.push([12, 'Century Schoolbook' ])
220
- fonts.push([12, 'Courier' ])
221
- fonts.push([12, 'Courier New' ])
222
- fonts.push([12, 'Garamond' ])
223
- fonts.push([12, 'Impact' ])
224
- fonts.push([12, 'Lucida Handwriting'] )
225
- fonts.push([12, 'Times New Roman' ])
226
- fonts.push([12, 'Symbol' ])
227
- fonts.push([12, 'Wingdings' ])
228
- fonts.push([12, 'A font that doesnt exist' ])
229
-
230
- i = 0
231
- fonts.each do |font|
232
- format = workbook.add_format()
233
-
234
- format.set_size(font[0])
235
- format.set_font(font[1])
236
-
237
- i += 1
238
- worksheet.write(i, 0, font[1], format)
239
- worksheet.write(i, 1, font[0], format)
240
- end
241
-
242
- end
243
-
244
-
245
- ######################################################################
246
- #
247
- # Demonstrate the standard Excel border styles.
248
- #
249
- def borders(workbook, heading, center)
250
-
251
- worksheet = workbook.add_worksheet('Borders')
252
-
253
- worksheet.set_column(0, 4, 10)
254
- worksheet.set_column(5, 5, 40)
255
-
256
- worksheet.write(0, 0, "Index", heading)
257
- worksheet.write(0, 1, "Index", heading)
258
- worksheet.write(0, 3, "Style", heading)
259
- worksheet.write(0, 5, "The style is highlighted in red for ", heading)
260
- worksheet.write(1, 5, "emphasis, the default color is black.", heading)
261
-
262
- 0.upto(13) do |i|
263
- format = workbook.add_format()
264
- format.set_border(i)
265
- format.set_border_color('red')
266
- format.set_align('center')
267
-
268
- worksheet.write((2*(i+1)), 0, i, center)
269
- worksheet.write((2*(i+1)), 1, sprintf("0x%02X", i), center)
270
-
271
- worksheet.write((2*(i+1)), 3, "Border", format)
272
- end
273
-
274
- worksheet.write(30, 0, "Diag type", heading)
275
- worksheet.write(30, 1, "Index", heading)
276
- worksheet.write(30, 3, "Style", heading)
277
- worksheet.write(30, 5, "Diagonal Boder styles", heading)
278
-
279
- 1.upto(3) do |i|
280
- format = workbook.add_format()
281
- format.set_diag_type(i)
282
- format.set_diag_border(1)
283
- format.set_diag_color('red')
284
- format.set_align('center')
285
-
286
- worksheet.write((2*(i+15)), 0, i, center)
287
- worksheet.write((2*(i+15)), 1, sprintf("0x%02X", i), center)
288
-
289
- worksheet.write((2*(i+15)), 3, "Border", format)
290
- end
291
- end
292
-
293
-
294
-
295
- ######################################################################
296
- #
297
- # Demonstrate the standard Excel cell patterns.
298
- #
299
- def patterns(workbook, heading, center)
300
-
301
- worksheet = workbook.add_worksheet('Patterns')
302
-
303
- worksheet.set_column(0, 4, 10)
304
- worksheet.set_column(5, 5, 50)
305
-
306
- worksheet.write(0, 0, "Index", heading)
307
- worksheet.write(0, 1, "Index", heading)
308
- worksheet.write(0, 3, "Pattern", heading)
309
-
310
- worksheet.write(0, 5, "The background colour has been set to silver.", heading)
311
- worksheet.write(1, 5, "The foreground colour has been set to green.", heading)
312
-
313
- 0.upto(18) do |i|
314
- format = workbook.add_format()
315
-
316
- format.set_pattern(i)
317
- format.set_bg_color('silver')
318
- format.set_fg_color('green')
319
- format.set_align('center')
320
-
321
- worksheet.write((2*(i+1)), 0, i, center)
322
- worksheet.write((2*(i+1)), 1, sprintf("0x%02X", i), center)
323
-
324
- worksheet.write((2*(i+1)), 3, "Pattern", format)
325
-
326
- if i == 1
327
- worksheet.write((2*(i+1)), 5, "This is solid colour, the most useful pattern.", heading)
328
- end
329
- end
330
- end
331
-
332
-
333
- ######################################################################
334
- #
335
- # Demonstrate the standard Excel cell alignments.
336
- #
337
- def alignment(workbook, heading, center)
338
-
339
- worksheet = workbook.add_worksheet('Alignment')
340
-
341
- worksheet.set_column(0, 7, 12)
342
- worksheet.set_row(0, 40)
343
- worksheet.set_selection(7, 0)
344
-
345
- format01 = workbook.add_format()
346
- format02 = workbook.add_format()
347
- format03 = workbook.add_format()
348
- format04 = workbook.add_format()
349
- format05 = workbook.add_format()
350
- format06 = workbook.add_format()
351
- format07 = workbook.add_format()
352
- format08 = workbook.add_format()
353
- format09 = workbook.add_format()
354
- format10 = workbook.add_format()
355
- format11 = workbook.add_format()
356
- format12 = workbook.add_format()
357
- format13 = workbook.add_format()
358
- format14 = workbook.add_format()
359
- format15 = workbook.add_format()
360
- format16 = workbook.add_format()
361
- format17 = workbook.add_format()
362
-
363
- format02.set_align('top')
364
- format03.set_align('bottom')
365
- format04.set_align('vcenter')
366
- format05.set_align('vjustify')
367
- format06.set_text_wrap()
368
-
369
- format07.set_align('left')
370
- format08.set_align('right')
371
- format09.set_align('center')
372
- format10.set_align('fill')
373
- format11.set_align('justify')
374
- format12.set_merge()
375
-
376
- format13.set_rotation(45)
377
- format14.set_rotation(-45)
378
- format15.set_rotation(270)
379
-
380
- format16.set_shrink()
381
- format17.set_indent(1)
382
-
383
- worksheet.write(0, 0, 'Vertical', heading)
384
- worksheet.write(0, 1, 'top', format02)
385
- worksheet.write(0, 2, 'bottom', format03)
386
- worksheet.write(0, 3, 'vcenter', format04)
387
- worksheet.write(0, 4, 'vjustify', format05)
388
- worksheet.write(0, 5, "text\nwrap", format06)
389
-
390
- worksheet.write(2, 0, 'Horizontal', heading)
391
- worksheet.write(2, 1, 'left', format07)
392
- worksheet.write(2, 2, 'right', format08)
393
- worksheet.write(2, 3, 'center', format09)
394
- worksheet.write(2, 4, 'fill', format10)
395
- worksheet.write(2, 5, 'justify', format11)
396
-
397
- worksheet.write(3, 1, 'merge', format12)
398
- worksheet.write(3, 2, '', format12)
399
-
400
- worksheet.write(3, 3, 'Shrink ' * 3, format16)
401
- worksheet.write(3, 4, 'Indent', format17)
402
-
403
-
404
- worksheet.write(5, 0, 'Rotation', heading)
405
- worksheet.write(5, 1, 'Rotate 45', format13)
406
- worksheet.write(6, 1, 'Rotate -45', format14)
407
- worksheet.write(7, 1, 'Rotate 270', format15)
408
- end
409
-
410
-
411
- ######################################################################
412
- #
413
- # Demonstrate other miscellaneous features.
414
- #
415
- def misc(workbook)
416
-
417
- worksheet = workbook.add_worksheet('Miscellaneous')
418
-
419
- worksheet.set_column(2, 2, 25)
420
-
421
- format01 = workbook.add_format()
422
- format02 = workbook.add_format()
423
- format03 = workbook.add_format()
424
- format04 = workbook.add_format()
425
- format05 = workbook.add_format()
426
- format06 = workbook.add_format()
427
- format07 = workbook.add_format()
428
-
429
- format01.set_underline(0x01)
430
- format02.set_underline(0x02)
431
- format03.set_underline(0x21)
432
- format04.set_underline(0x22)
433
- format05.set_font_strikeout()
434
- format06.set_font_outline()
435
- format07.set_font_shadow()
436
-
437
- worksheet.write(1, 2, 'Underline 0x01', format01)
438
- worksheet.write(3, 2, 'Underline 0x02', format02)
439
- worksheet.write(5, 2, 'Underline 0x21', format03)
440
- worksheet.write(7, 2, 'Underline 0x22', format04)
441
- worksheet.write(9, 2, 'Strikeout', format05)
442
- worksheet.write(11, 2, 'Outline (Macintosh only)', format06)
443
- worksheet.write(13, 2, 'Shadow (Macintosh only)', format07)
444
- end
445
-
446
-
447
- ######################################################################
448
- #
449
- # main
450
- #
451
- workbook = WriteExcel.new('formats.xls')
452
-
453
- # Some common formats
454
- center = workbook.add_format(:align => 'center')
455
- heading = workbook.add_format(:align => 'center', :bold => 1)
456
-
457
- # The named colors
458
- COLORS = {
459
- 0x08 => 'black',
460
- 0x0C => 'blue',
461
- 0x10 => 'brown',
462
- 0x0F => 'cyan',
463
- 0x17 => 'gray',
464
- 0x11 => 'green',
465
- 0x0B => 'lime',
466
- 0x0E => 'magenta',
467
- 0x12 => 'navy',
468
- 0x35 => 'orange',
469
- 0x21 => 'pink',
470
- 0x14 => 'purple',
471
- 0x0A => 'red',
472
- 0x16 => 'silver',
473
- 0x09 => 'white',
474
- 0x0D => 'yellow',
475
- }
476
-
477
- # Call these subroutines to demonstrate different formatting options
478
- intro(workbook)
479
- fonts(workbook, heading, center)
480
- named_colors(workbook, heading, center)
481
- standard_colors(workbook, heading, center)
482
- numeric_formats(workbook, heading, center)
483
- borders(workbook, heading, center)
484
- patterns(workbook, heading, center)
485
- alignment(workbook, heading, center)
486
- misc(workbook)
487
-
488
- # Note: this is required
489
- workbook.close
490
-
1
+ #!/usr/bin/ruby -w
2
+ # -*- coding: utf-8 -*-
3
+
4
+ ######################################################################
5
+ #
6
+ # Examples of formatting using the WriteExcel module
7
+ #
8
+ # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
9
+ #
10
+ # original written in Perl by John McNamara
11
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
12
+ #
13
+
14
+ require 'rubygems'
15
+ require 'writeexcel'
16
+
17
+
18
+ ######################################################################
19
+ #
20
+ # Intro.
21
+ #
22
+ def intro(workbook)
23
+
24
+ worksheet = workbook.add_worksheet('Introduction')
25
+
26
+ worksheet.set_column(0, 0, 60)
27
+
28
+ format = workbook.add_format()
29
+ format.set_bold()
30
+ format.set_size(14)
31
+ format.set_color('blue')
32
+ format.set_align('center')
33
+
34
+ format2 = workbook.add_format()
35
+ format2.set_bold()
36
+ format2.set_color('blue')
37
+
38
+ worksheet.write(2, 0, 'This workbook demonstrates some of', format)
39
+ worksheet.write(3, 0, 'the formatting options provided by', format)
40
+ worksheet.write(4, 0, 'the WriteExcel module.', format)
41
+
42
+ worksheet.write('A7', 'Sections:', format2)
43
+ worksheet.write('A8', "internal:Fonts!A1", 'Fonts' )
44
+ worksheet.write('A9', "internal:'Named colors'!A1", 'Named colors' )
45
+ worksheet.write('A10', "internal:'Standard colors'!A1", 'Standard colors')
46
+ worksheet.write('A11', "internal:'Numeric formats'!A1", 'Numeric formats')
47
+ worksheet.write('A12', "internal:Borders!A1", 'Borders' )
48
+ worksheet.write('A13', "internal:Patterns!A1", 'Patterns' )
49
+ worksheet.write('A14', "internal:Alignment!A1", 'Alignment' )
50
+ worksheet.write('A15', "internal:Miscellaneous!A1", 'Miscellaneous' )
51
+
52
+ end
53
+
54
+
55
+ ######################################################################
56
+ #
57
+ # Demonstrate the named colors.
58
+ #
59
+ def named_colors(workbook, heading, center)
60
+
61
+ worksheet = workbook.add_worksheet('Named colors')
62
+
63
+ worksheet.set_column(0, 3, 15)
64
+
65
+ worksheet.write(0, 0, "Index", heading)
66
+ worksheet.write(0, 1, "Index", heading)
67
+ worksheet.write(0, 2, "Name", heading)
68
+ worksheet.write(0, 3, "Color", heading)
69
+
70
+ i = 1
71
+
72
+ COLORS.each do |index, color|
73
+ format = workbook.add_format(
74
+ :bg_color => color,
75
+ :pattern => 1,
76
+ :border => 1
77
+ )
78
+
79
+ worksheet.write(i+1, 0, index, center)
80
+ worksheet.write(i+1, 1, sprintf("0x%02X", index), center)
81
+ worksheet.write(i+1, 2, color, center)
82
+ worksheet.write(i+1, 3, '', format)
83
+ i += 1
84
+ end
85
+ end
86
+
87
+
88
+ ######################################################################
89
+ #
90
+ # Demonstrate the standard Excel colors in the range 8..63.
91
+ #
92
+ def standard_colors(workbook, heading, center)
93
+
94
+ worksheet = workbook.add_worksheet('Standard colors')
95
+
96
+ worksheet.set_column(0, 3, 15)
97
+
98
+ worksheet.write(0, 0, "Index", heading)
99
+ worksheet.write(0, 1, "Index", heading)
100
+ worksheet.write(0, 2, "Color", heading)
101
+ worksheet.write(0, 3, "Name", heading)
102
+
103
+ 8.upto(63) do |i|
104
+ format = workbook.add_format(
105
+ :bg_color => i,
106
+ :pattern => 1,
107
+ :border => 1
108
+ )
109
+
110
+ worksheet.write((i -7), 0, i, center)
111
+ worksheet.write((i -7), 1, sprintf("0x%02X", i), center)
112
+ worksheet.write((i -7), 2, '', format)
113
+
114
+ # Add the color names
115
+ if COLORS[i]
116
+ worksheet.write((i -7), 3, COLORS[i], center)
117
+
118
+ end
119
+ end
120
+ end
121
+
122
+
123
+ ######################################################################
124
+ #
125
+ # Demonstrate the standard numeric formats.
126
+ #
127
+ def numeric_formats(workbook, heading, center)
128
+
129
+ worksheet = workbook.add_worksheet('Numeric formats')
130
+
131
+ worksheet.set_column(0, 4, 15)
132
+ worksheet.set_column(5, 5, 45)
133
+
134
+ worksheet.write(0, 0, "Index", heading)
135
+ worksheet.write(0, 1, "Index", heading)
136
+ worksheet.write(0, 2, "Unformatted", heading)
137
+ worksheet.write(0, 3, "Formatted", heading)
138
+ worksheet.write(0, 4, "Negative", heading)
139
+ worksheet.write(0, 5, "Format", heading)
140
+
141
+ formats = []
142
+ formats.push([ 0x00, 1234.567, 0, 'General' ])
143
+ formats.push([ 0x01, 1234.567, 0, '0' ])
144
+ formats.push([ 0x02, 1234.567, 0, '0.00' ])
145
+ formats.push([ 0x03, 1234.567, 0, '#,##0' ])
146
+ formats.push([ 0x04, 1234.567, 0, '#,##0.00' ])
147
+ formats.push([ 0x05, 1234.567, -1234.567, '($#,##0_);($#,##0)' ])
148
+ formats.push([ 0x06, 1234.567, -1234.567, '($#,##0_);[Red]($#,##0)' ])
149
+ formats.push([ 0x07, 1234.567, -1234.567, '($#,##0.00_);($#,##0.00)' ])
150
+ formats.push([ 0x08, 1234.567, -1234.567, '($#,##0.00_);[Red]($#,##0.00)' ])
151
+ formats.push([ 0x09, 0.567, 0, '0%' ])
152
+ formats.push([ 0x0a, 0.567, 0, '0.00%' ])
153
+ formats.push([ 0x0b, 1234.567, 0, '0.00E+00' ])
154
+ formats.push([ 0x0c, 0.75, 0, '# ?/?' ])
155
+ formats.push([ 0x0d, 0.3125, 0, '# ??/??' ])
156
+ formats.push([ 0x0e, 36892.521, 0, 'm/d/yy' ])
157
+ formats.push([ 0x0f, 36892.521, 0, 'd-mmm-yy' ])
158
+ formats.push([ 0x10, 36892.521, 0, 'd-mmm' ])
159
+ formats.push([ 0x11, 36892.521, 0, 'mmm-yy' ])
160
+ formats.push([ 0x12, 36892.521, 0, 'h:mm AM/PM' ])
161
+ formats.push([ 0x13, 36892.521, 0, 'h:mm:ss AM/PM' ])
162
+ formats.push([ 0x14, 36892.521, 0, 'h:mm' ])
163
+ formats.push([ 0x15, 36892.521, 0, 'h:mm:ss' ])
164
+ formats.push([ 0x16, 36892.521, 0, 'm/d/yy h:mm' ])
165
+ formats.push([ 0x25, 1234.567, -1234.567, '(#,##0_);(#,##0)' ])
166
+ formats.push([ 0x26, 1234.567, -1234.567, '(#,##0_);[Red](#,##0)' ])
167
+ formats.push([ 0x27, 1234.567, -1234.567, '(#,##0.00_);(#,##0.00)' ])
168
+ formats.push([ 0x28, 1234.567, -1234.567, '(#,##0.00_);[Red](#,##0.00)' ])
169
+ formats.push([ 0x29, 1234.567, -1234.567, '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)' ])
170
+ formats.push([ 0x2a, 1234.567, -1234.567, '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)' ])
171
+ formats.push([ 0x2b, 1234.567, -1234.567, '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' ])
172
+ formats.push([ 0x2c, 1234.567, -1234.567, '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)' ])
173
+ formats.push([ 0x2d, 36892.521, 0, 'mm:ss' ])
174
+ formats.push([ 0x2e, 3.0153, 0, '[h]:mm:ss' ])
175
+ formats.push([ 0x2f, 36892.521, 0, 'mm:ss.0' ])
176
+ formats.push([ 0x30, 1234.567, 0, '##0.0E+0' ])
177
+ formats.push([ 0x31, 1234.567, 0, '@' ])
178
+
179
+ i = 0
180
+ formats.each do |format|
181
+ style = workbook.add_format()
182
+ style.set_num_format(format[0])
183
+
184
+ i += 1
185
+ worksheet.write(i, 0, format[0], center)
186
+ worksheet.write(i, 1, sprintf("0x%02X", format[0]), center)
187
+ worksheet.write(i, 2, format[1], center)
188
+ worksheet.write(i, 3, format[1], style)
189
+
190
+ if format[2] != 0
191
+ worksheet.write(i, 4, format[2], style)
192
+ end
193
+
194
+ worksheet.write_string(i, 5, format[3])
195
+ end
196
+ end
197
+
198
+
199
+ ######################################################################
200
+ #
201
+ # Demonstrate the font options.
202
+ #
203
+ def fonts(workbook, heading, center)
204
+
205
+ worksheet = workbook.add_worksheet('Fonts')
206
+
207
+ worksheet.set_column(0, 0, 30)
208
+ worksheet.set_column(1, 1, 10)
209
+
210
+ worksheet.write(0, 0, "Font name", heading)
211
+ worksheet.write(0, 1, "Font size", heading)
212
+
213
+ fonts = []
214
+ fonts.push([10, 'Arial' ])
215
+ fonts.push([12, 'Arial' ])
216
+ fonts.push([14, 'Arial' ])
217
+ fonts.push([12, 'Arial Black' ])
218
+ fonts.push([12, 'Arial Narrow' ])
219
+ fonts.push([12, 'Century Schoolbook' ])
220
+ fonts.push([12, 'Courier' ])
221
+ fonts.push([12, 'Courier New' ])
222
+ fonts.push([12, 'Garamond' ])
223
+ fonts.push([12, 'Impact' ])
224
+ fonts.push([12, 'Lucida Handwriting'] )
225
+ fonts.push([12, 'Times New Roman' ])
226
+ fonts.push([12, 'Symbol' ])
227
+ fonts.push([12, 'Wingdings' ])
228
+ fonts.push([12, 'A font that doesnt exist' ])
229
+
230
+ i = 0
231
+ fonts.each do |font|
232
+ format = workbook.add_format()
233
+
234
+ format.set_size(font[0])
235
+ format.set_font(font[1])
236
+
237
+ i += 1
238
+ worksheet.write(i, 0, font[1], format)
239
+ worksheet.write(i, 1, font[0], format)
240
+ end
241
+
242
+ end
243
+
244
+
245
+ ######################################################################
246
+ #
247
+ # Demonstrate the standard Excel border styles.
248
+ #
249
+ def borders(workbook, heading, center)
250
+
251
+ worksheet = workbook.add_worksheet('Borders')
252
+
253
+ worksheet.set_column(0, 4, 10)
254
+ worksheet.set_column(5, 5, 40)
255
+
256
+ worksheet.write(0, 0, "Index", heading)
257
+ worksheet.write(0, 1, "Index", heading)
258
+ worksheet.write(0, 3, "Style", heading)
259
+ worksheet.write(0, 5, "The style is highlighted in red for ", heading)
260
+ worksheet.write(1, 5, "emphasis, the default color is black.", heading)
261
+
262
+ 0.upto(13) do |i|
263
+ format = workbook.add_format()
264
+ format.set_border(i)
265
+ format.set_border_color('red')
266
+ format.set_align('center')
267
+
268
+ worksheet.write((2*(i+1)), 0, i, center)
269
+ worksheet.write((2*(i+1)), 1, sprintf("0x%02X", i), center)
270
+
271
+ worksheet.write((2*(i+1)), 3, "Border", format)
272
+ end
273
+
274
+ worksheet.write(30, 0, "Diag type", heading)
275
+ worksheet.write(30, 1, "Index", heading)
276
+ worksheet.write(30, 3, "Style", heading)
277
+ worksheet.write(30, 5, "Diagonal Boder styles", heading)
278
+
279
+ 1.upto(3) do |i|
280
+ format = workbook.add_format()
281
+ format.set_diag_type(i)
282
+ format.set_diag_border(1)
283
+ format.set_diag_color('red')
284
+ format.set_align('center')
285
+
286
+ worksheet.write((2*(i+15)), 0, i, center)
287
+ worksheet.write((2*(i+15)), 1, sprintf("0x%02X", i), center)
288
+
289
+ worksheet.write((2*(i+15)), 3, "Border", format)
290
+ end
291
+ end
292
+
293
+
294
+
295
+ ######################################################################
296
+ #
297
+ # Demonstrate the standard Excel cell patterns.
298
+ #
299
+ def patterns(workbook, heading, center)
300
+
301
+ worksheet = workbook.add_worksheet('Patterns')
302
+
303
+ worksheet.set_column(0, 4, 10)
304
+ worksheet.set_column(5, 5, 50)
305
+
306
+ worksheet.write(0, 0, "Index", heading)
307
+ worksheet.write(0, 1, "Index", heading)
308
+ worksheet.write(0, 3, "Pattern", heading)
309
+
310
+ worksheet.write(0, 5, "The background colour has been set to silver.", heading)
311
+ worksheet.write(1, 5, "The foreground colour has been set to green.", heading)
312
+
313
+ 0.upto(18) do |i|
314
+ format = workbook.add_format()
315
+
316
+ format.set_pattern(i)
317
+ format.set_bg_color('silver')
318
+ format.set_fg_color('green')
319
+ format.set_align('center')
320
+
321
+ worksheet.write((2*(i+1)), 0, i, center)
322
+ worksheet.write((2*(i+1)), 1, sprintf("0x%02X", i), center)
323
+
324
+ worksheet.write((2*(i+1)), 3, "Pattern", format)
325
+
326
+ if i == 1
327
+ worksheet.write((2*(i+1)), 5, "This is solid colour, the most useful pattern.", heading)
328
+ end
329
+ end
330
+ end
331
+
332
+
333
+ ######################################################################
334
+ #
335
+ # Demonstrate the standard Excel cell alignments.
336
+ #
337
+ def alignment(workbook, heading, center)
338
+
339
+ worksheet = workbook.add_worksheet('Alignment')
340
+
341
+ worksheet.set_column(0, 7, 12)
342
+ worksheet.set_row(0, 40)
343
+ worksheet.set_selection(7, 0)
344
+
345
+ format01 = workbook.add_format()
346
+ format02 = workbook.add_format()
347
+ format03 = workbook.add_format()
348
+ format04 = workbook.add_format()
349
+ format05 = workbook.add_format()
350
+ format06 = workbook.add_format()
351
+ format07 = workbook.add_format()
352
+ format08 = workbook.add_format()
353
+ format09 = workbook.add_format()
354
+ format10 = workbook.add_format()
355
+ format11 = workbook.add_format()
356
+ format12 = workbook.add_format()
357
+ format13 = workbook.add_format()
358
+ format14 = workbook.add_format()
359
+ format15 = workbook.add_format()
360
+ format16 = workbook.add_format()
361
+ format17 = workbook.add_format()
362
+
363
+ format02.set_align('top')
364
+ format03.set_align('bottom')
365
+ format04.set_align('vcenter')
366
+ format05.set_align('vjustify')
367
+ format06.set_text_wrap()
368
+
369
+ format07.set_align('left')
370
+ format08.set_align('right')
371
+ format09.set_align('center')
372
+ format10.set_align('fill')
373
+ format11.set_align('justify')
374
+ format12.set_merge()
375
+
376
+ format13.set_rotation(45)
377
+ format14.set_rotation(-45)
378
+ format15.set_rotation(270)
379
+
380
+ format16.set_shrink()
381
+ format17.set_indent(1)
382
+
383
+ worksheet.write(0, 0, 'Vertical', heading)
384
+ worksheet.write(0, 1, 'top', format02)
385
+ worksheet.write(0, 2, 'bottom', format03)
386
+ worksheet.write(0, 3, 'vcenter', format04)
387
+ worksheet.write(0, 4, 'vjustify', format05)
388
+ worksheet.write(0, 5, "text\nwrap", format06)
389
+
390
+ worksheet.write(2, 0, 'Horizontal', heading)
391
+ worksheet.write(2, 1, 'left', format07)
392
+ worksheet.write(2, 2, 'right', format08)
393
+ worksheet.write(2, 3, 'center', format09)
394
+ worksheet.write(2, 4, 'fill', format10)
395
+ worksheet.write(2, 5, 'justify', format11)
396
+
397
+ worksheet.write(3, 1, 'merge', format12)
398
+ worksheet.write(3, 2, '', format12)
399
+
400
+ worksheet.write(3, 3, 'Shrink ' * 3, format16)
401
+ worksheet.write(3, 4, 'Indent', format17)
402
+
403
+
404
+ worksheet.write(5, 0, 'Rotation', heading)
405
+ worksheet.write(5, 1, 'Rotate 45', format13)
406
+ worksheet.write(6, 1, 'Rotate -45', format14)
407
+ worksheet.write(7, 1, 'Rotate 270', format15)
408
+ end
409
+
410
+
411
+ ######################################################################
412
+ #
413
+ # Demonstrate other miscellaneous features.
414
+ #
415
+ def misc(workbook)
416
+
417
+ worksheet = workbook.add_worksheet('Miscellaneous')
418
+
419
+ worksheet.set_column(2, 2, 25)
420
+
421
+ format01 = workbook.add_format()
422
+ format02 = workbook.add_format()
423
+ format03 = workbook.add_format()
424
+ format04 = workbook.add_format()
425
+ format05 = workbook.add_format()
426
+ format06 = workbook.add_format()
427
+ format07 = workbook.add_format()
428
+
429
+ format01.set_underline(0x01)
430
+ format02.set_underline(0x02)
431
+ format03.set_underline(0x21)
432
+ format04.set_underline(0x22)
433
+ format05.set_font_strikeout()
434
+ format06.set_font_outline()
435
+ format07.set_font_shadow()
436
+
437
+ worksheet.write(1, 2, 'Underline 0x01', format01)
438
+ worksheet.write(3, 2, 'Underline 0x02', format02)
439
+ worksheet.write(5, 2, 'Underline 0x21', format03)
440
+ worksheet.write(7, 2, 'Underline 0x22', format04)
441
+ worksheet.write(9, 2, 'Strikeout', format05)
442
+ worksheet.write(11, 2, 'Outline (Macintosh only)', format06)
443
+ worksheet.write(13, 2, 'Shadow (Macintosh only)', format07)
444
+ end
445
+
446
+
447
+ ######################################################################
448
+ #
449
+ # main
450
+ #
451
+ workbook = WriteExcel.new('formats.xls')
452
+
453
+ # Some common formats
454
+ center = workbook.add_format(:align => 'center')
455
+ heading = workbook.add_format(:align => 'center', :bold => 1)
456
+
457
+ # The named colors
458
+ COLORS = {
459
+ 0x08 => 'black',
460
+ 0x0C => 'blue',
461
+ 0x10 => 'brown',
462
+ 0x0F => 'cyan',
463
+ 0x17 => 'gray',
464
+ 0x11 => 'green',
465
+ 0x0B => 'lime',
466
+ 0x0E => 'magenta',
467
+ 0x12 => 'navy',
468
+ 0x35 => 'orange',
469
+ 0x21 => 'pink',
470
+ 0x14 => 'purple',
471
+ 0x0A => 'red',
472
+ 0x16 => 'silver',
473
+ 0x09 => 'white',
474
+ 0x0D => 'yellow',
475
+ }
476
+
477
+ # Call these subroutines to demonstrate different formatting options
478
+ intro(workbook)
479
+ fonts(workbook, heading, center)
480
+ named_colors(workbook, heading, center)
481
+ standard_colors(workbook, heading, center)
482
+ numeric_formats(workbook, heading, center)
483
+ borders(workbook, heading, center)
484
+ patterns(workbook, heading, center)
485
+ alignment(workbook, heading, center)
486
+ misc(workbook)
487
+
488
+ # Note: this is required
489
+ workbook.close
490
+