writeexcel 0.1.0 → 0.3.0

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