writeexcel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +103 -0
- data/examples/a_simple.rb +42 -0
- data/examples/autofilters.rb +266 -0
- data/examples/copyformat.rb +51 -0
- data/examples/data_validate.rb +278 -0
- data/examples/date_time.rb +86 -0
- data/examples/demo.rb +118 -0
- data/examples/diag_border.rb +35 -0
- data/examples/formats.rb +489 -0
- data/examples/header.rb +136 -0
- data/examples/hidden.rb +28 -0
- data/examples/hyperlink.rb +42 -0
- data/examples/images.rb +52 -0
- data/examples/merge1.rb +39 -0
- data/examples/merge2.rb +44 -0
- data/examples/merge3.rb +65 -0
- data/examples/merge4.rb +82 -0
- data/examples/merge5.rb +79 -0
- data/examples/protection.rb +46 -0
- data/examples/regions.rb +52 -0
- data/examples/repeat.rb +42 -0
- data/examples/republic.png +0 -0
- data/examples/stats.rb +75 -0
- data/examples/stocks.rb +80 -0
- data/examples/tab_colors.rb +30 -0
- data/lib/writeexcel/biffwriter.rb +260 -0
- data/lib/writeexcel/chart.rb +217 -0
- data/lib/writeexcel/excelformulaparser.rb +573 -0
- data/lib/writeexcel/format.rb +1108 -0
- data/lib/writeexcel/formula.rb +986 -0
- data/lib/writeexcel/olewriter.rb +322 -0
- data/lib/writeexcel/properties.rb +250 -0
- data/lib/writeexcel/workbook.rb +2630 -0
- data/lib/writeexcel/worksheet.rb +6377 -0
- data/lib/writeexcel.rb +18 -0
- data/test/perl_output/README +31 -0
- data/test/perl_output/a_simple.xls +0 -0
- data/test/perl_output/biff_add_continue_testdata +0 -0
- data/test/perl_output/data_validate.xls +0 -0
- data/test/perl_output/date_time.xls +0 -0
- data/test/perl_output/demo.xls +0 -0
- data/test/perl_output/diag_border.xls +0 -0
- data/test/perl_output/f_font_biff +0 -0
- data/test/perl_output/f_font_key +1 -0
- data/test/perl_output/f_xf_biff +0 -0
- data/test/perl_output/file_font_biff +0 -0
- data/test/perl_output/file_font_key +1 -0
- data/test/perl_output/file_xf_biff +0 -0
- data/test/perl_output/headers.xls +0 -0
- data/test/perl_output/hidden.xls +0 -0
- data/test/perl_output/hyperlink.xls +0 -0
- data/test/perl_output/images.xls +0 -0
- data/test/perl_output/merge1.xls +0 -0
- data/test/perl_output/merge2.xls +0 -0
- data/test/perl_output/merge3.xls +0 -0
- data/test/perl_output/merge4.xls +0 -0
- data/test/perl_output/merge5.xls +0 -0
- data/test/perl_output/ole_write_header +0 -0
- data/test/perl_output/protection.xls +0 -0
- data/test/perl_output/regions.xls +0 -0
- data/test/perl_output/stats.xls +0 -0
- data/test/perl_output/stocks.xls +0 -0
- data/test/perl_output/tab_colors.xls +0 -0
- data/test/perl_output/unicode_cyrillic.xls +0 -0
- data/test/perl_output/workbook1.xls +0 -0
- data/test/perl_output/workbook2.xls +0 -0
- data/test/perl_output/ws_colinfo +1 -0
- data/test/perl_output/ws_store_colinfo +0 -0
- data/test/perl_output/ws_store_dimensions +0 -0
- data/test/perl_output/ws_store_filtermode +0 -0
- data/test/perl_output/ws_store_filtermode_off +0 -0
- data/test/perl_output/ws_store_filtermode_on +0 -0
- data/test/perl_output/ws_store_selection +0 -0
- data/test/perl_output/ws_store_window2 +1 -0
- data/test/republic.png +0 -0
- data/test/tc_all.rb +31 -0
- data/test/tc_biff.rb +104 -0
- data/test/tc_chart.rb +22 -0
- data/test/tc_example_match.rb +1280 -0
- data/test/tc_format.rb +1267 -0
- data/test/tc_formula.rb +63 -0
- data/test/tc_ole.rb +110 -0
- data/test/tc_workbook.rb +115 -0
- data/test/tc_worksheet.rb +115 -0
- data/test/test_00_IEEE_double.rb +14 -0
- data/test/test_01_add_worksheet.rb +12 -0
- data/test/test_02_merge_formats.rb +58 -0
- data/test/test_04_dimensions.rb +397 -0
- data/test/test_05_rows.rb +182 -0
- data/test/test_06_extsst.rb +80 -0
- data/test/test_11_date_time.rb +484 -0
- data/test/test_12_date_only.rb +506 -0
- data/test/test_13_date_seconds.rb +486 -0
- data/test/test_21_escher.rb +629 -0
- data/test/test_22_mso_drawing_group.rb +739 -0
- data/test/test_23_note.rb +78 -0
- data/test/test_24_txo.rb +80 -0
- data/test/test_26_autofilter.rb +327 -0
- data/test/test_27_autofilter.rb +144 -0
- data/test/test_28_autofilter.rb +174 -0
- data/test/test_29_process_jpg.rb +131 -0
- data/test/test_30_validation_dval.rb +82 -0
- data/test/test_31_validation_dv_strings.rb +131 -0
- data/test/test_32_validation_dv_formula.rb +211 -0
- data/test/test_40_property_types.rb +191 -0
- data/test/test_41_properties.rb +238 -0
- data/test/test_42_set_properties.rb +419 -0
- data/test/ts_all.rb +34 -0
- metadata +170 -0
data/examples/formats.rb
ADDED
|
@@ -0,0 +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
|
+
|
data/examples/header.rb
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#!/usr/bin/ruby -w
|
|
2
|
+
|
|
3
|
+
######################################################################
|
|
4
|
+
#
|
|
5
|
+
# This program shows several examples of how to set up headers and
|
|
6
|
+
# footers with Spreadsheet::WriteExcel.
|
|
7
|
+
#
|
|
8
|
+
# The control characters used in the header/footer strings are:
|
|
9
|
+
#
|
|
10
|
+
# Control Category Description
|
|
11
|
+
# ======= ======== ===========
|
|
12
|
+
# &L Justification Left
|
|
13
|
+
# &C Center
|
|
14
|
+
# &R Right
|
|
15
|
+
#
|
|
16
|
+
# &P Information Page number
|
|
17
|
+
# &N Total number of pages
|
|
18
|
+
# &D Date
|
|
19
|
+
# &T Time
|
|
20
|
+
# &F File name
|
|
21
|
+
# &A Worksheet name
|
|
22
|
+
#
|
|
23
|
+
# &fontsize Font Font size
|
|
24
|
+
# &"font,style" Font name and style
|
|
25
|
+
# &U Single underline
|
|
26
|
+
# &E Double underline
|
|
27
|
+
# &S Strikethrough
|
|
28
|
+
# &X Superscript
|
|
29
|
+
# &Y Subscript
|
|
30
|
+
#
|
|
31
|
+
# && Miscellaneous Literal ampersand &
|
|
32
|
+
#
|
|
33
|
+
#
|
|
34
|
+
# reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org
|
|
35
|
+
#
|
|
36
|
+
# original written in Perl by John McNamara
|
|
37
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
|
38
|
+
#
|
|
39
|
+
|
|
40
|
+
require 'rubygems'
|
|
41
|
+
require 'writeexcel'
|
|
42
|
+
|
|
43
|
+
workbook = Spreadsheet::WriteExcel.new("headers.xls")
|
|
44
|
+
preview = "Select Print Preview to see the header and footer"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
######################################################################
|
|
48
|
+
#
|
|
49
|
+
# A simple example to start
|
|
50
|
+
#
|
|
51
|
+
worksheet1 = workbook.add_worksheet('Simple')
|
|
52
|
+
|
|
53
|
+
header1 = '&CHere is some centred text.'
|
|
54
|
+
|
|
55
|
+
footer1 = '&LHere is some left aligned text.'
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
worksheet1.set_header(header1)
|
|
59
|
+
worksheet1.set_footer(footer1)
|
|
60
|
+
|
|
61
|
+
worksheet1.set_column('A:A', 50)
|
|
62
|
+
worksheet1.write('A1', preview)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
######################################################################
|
|
66
|
+
#
|
|
67
|
+
# This is an example of some of the header/footer variables.
|
|
68
|
+
#
|
|
69
|
+
worksheet2 = workbook.add_worksheet('Variables')
|
|
70
|
+
|
|
71
|
+
header2 = '&LPage &P of &N'+
|
|
72
|
+
'&CFilename: &F' +
|
|
73
|
+
'&RSheetname: &A'
|
|
74
|
+
|
|
75
|
+
footer2 = '&LCurrent date: &D'+
|
|
76
|
+
'&RCurrent time: &T'
|
|
77
|
+
|
|
78
|
+
worksheet2.set_header(header2)
|
|
79
|
+
worksheet2.set_footer(footer2)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
worksheet2.set_column('A:A', 50)
|
|
83
|
+
worksheet2.write('A1', preview)
|
|
84
|
+
worksheet2.write('A21', "Next sheet")
|
|
85
|
+
worksheet2.set_h_pagebreaks(20)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
######################################################################
|
|
89
|
+
#
|
|
90
|
+
# This example shows how to use more than one font
|
|
91
|
+
#
|
|
92
|
+
worksheet3 = workbook.add_worksheet('Mixed fonts')
|
|
93
|
+
|
|
94
|
+
header3 = '&C' +
|
|
95
|
+
'&"Courier New,Bold"Hello ' +
|
|
96
|
+
'&"Arial,Italic"World'
|
|
97
|
+
|
|
98
|
+
footer3 = '&C' +
|
|
99
|
+
'&"Symbol"e' +
|
|
100
|
+
'&"Arial" = mc&X2'
|
|
101
|
+
|
|
102
|
+
worksheet3.set_header(header3)
|
|
103
|
+
worksheet3.set_footer(footer3)
|
|
104
|
+
|
|
105
|
+
worksheet3.set_column('A:A', 50)
|
|
106
|
+
worksheet3.write('A1', preview)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
######################################################################
|
|
110
|
+
#
|
|
111
|
+
# Example of line wrapping
|
|
112
|
+
#
|
|
113
|
+
worksheet4 = workbook.add_worksheet('Word wrap')
|
|
114
|
+
|
|
115
|
+
header4 = "&CHeading 1\nHeading 2\nHeading 3"
|
|
116
|
+
|
|
117
|
+
worksheet4.set_header(header4)
|
|
118
|
+
|
|
119
|
+
worksheet4.set_column('A:A', 50)
|
|
120
|
+
worksheet4.write('A1', preview)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
######################################################################
|
|
124
|
+
#
|
|
125
|
+
# Example of inserting a literal ampersand &
|
|
126
|
+
#
|
|
127
|
+
worksheet5 = workbook.add_worksheet('Ampersand')
|
|
128
|
+
|
|
129
|
+
header5 = "&CCuriouser && Curiouser - Attorneys at Law"
|
|
130
|
+
|
|
131
|
+
worksheet5.set_header(header5)
|
|
132
|
+
|
|
133
|
+
worksheet5.set_column('A:A', 50)
|
|
134
|
+
worksheet5.write('A1', preview)
|
|
135
|
+
|
|
136
|
+
workbook.close
|
data/examples/hidden.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/ruby -w
|
|
2
|
+
|
|
3
|
+
#######################################################################
|
|
4
|
+
#
|
|
5
|
+
# Example of how to hide a worksheet with Spreadsheet::WriteExcel.
|
|
6
|
+
#
|
|
7
|
+
# reverse('©'), April 2005, 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
|
+
require 'rubygems'
|
|
13
|
+
require 'writeexcel'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
workbook = Spreadsheet::WriteExcel.new('hidden.xls')
|
|
17
|
+
worksheet1 = workbook.add_worksheet
|
|
18
|
+
worksheet2 = workbook.add_worksheet
|
|
19
|
+
worksheet3 = workbook.add_worksheet
|
|
20
|
+
|
|
21
|
+
# Sheet2 won't be visible until it is unhidden in Excel.
|
|
22
|
+
worksheet2.hide
|
|
23
|
+
|
|
24
|
+
worksheet1.write(0, 0, 'Sheet2 is hidden')
|
|
25
|
+
worksheet2.write(0, 0, 'How did you find me?')
|
|
26
|
+
worksheet3.write(0, 0, 'Sheet2 is hidden')
|
|
27
|
+
|
|
28
|
+
workbook.close
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/ruby -w
|
|
2
|
+
###############################################################################
|
|
3
|
+
#
|
|
4
|
+
# Example of how to use the WriteExcel module to write hyperlinks
|
|
5
|
+
#
|
|
6
|
+
# See also hyperlink2.pl for worksheet URL examples.
|
|
7
|
+
#
|
|
8
|
+
# reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
|
|
9
|
+
#
|
|
10
|
+
# original written in Perl by John McNamara
|
|
11
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
|
12
|
+
#
|
|
13
|
+
require 'rubygems'
|
|
14
|
+
require 'writeexcel'
|
|
15
|
+
|
|
16
|
+
# Create a new workbook and add a worksheet
|
|
17
|
+
workbook = Spreadsheet::WriteExcel.new("hyperlink.xls")
|
|
18
|
+
worksheet = workbook.add_worksheet('Hyperlinks')
|
|
19
|
+
|
|
20
|
+
# Format the first column
|
|
21
|
+
worksheet.set_column('A:A', 30)
|
|
22
|
+
worksheet.set_selection('B1')
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Add a sample format
|
|
26
|
+
format = workbook.add_format
|
|
27
|
+
format.set_size(12)
|
|
28
|
+
format.set_bold
|
|
29
|
+
format.set_color('red')
|
|
30
|
+
format.set_underline
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Write some hyperlinks
|
|
34
|
+
worksheet.write('A1', 'http://www.perl.com/' )
|
|
35
|
+
worksheet.write('A3', 'http://www.perl.com/', 'Perl home' )
|
|
36
|
+
worksheet.write('A5', 'http://www.perl.com/', nil, format)
|
|
37
|
+
worksheet.write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me')
|
|
38
|
+
|
|
39
|
+
# Write a URL that isn't a hyperlink
|
|
40
|
+
worksheet.write_string('A9', 'http://www.perl.com/')
|
|
41
|
+
|
|
42
|
+
workbook.close
|