writeexcel 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,6 +4,10 @@ Write to a cross-platform Excel binary file.
4
4
 
5
5
  == Recent Changes
6
6
 
7
+ v0.3.4
8
+ * Bug fix. add_comment doesn't work.
9
+ * convert examples.(chess, colors, comment1, comment2)
10
+
7
11
  v0.3.3
8
12
  * add recent change in this document.
9
13
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
data/examples/chess.rb ADDED
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ########################################################################
4
+ #
5
+ # Example of formatting using the Spreadsheet::WriteExcel module via
6
+ # property hashes.
7
+ #
8
+ # Setting format properties via hashes of values is useful when you have
9
+ # to deal with a large number of similar formats. Consider for example a
10
+ # chess board pattern with black squares, white unformatted squares and
11
+ # a border.
12
+ #
13
+ # This relatively simple example requires 14 separate Format
14
+ # objects although there are only 5 different properties: black
15
+ # background, top border, bottom border, left border and right border.
16
+ #
17
+ # Using property hashes it is possible to define these 5 sets of
18
+ # properties and then add them together to create the 14 Format
19
+ # configurations.
20
+ #
21
+ # reverse('©'), July 2001, John McNamara, jmcnamara@cpan.org
22
+ #
23
+ # original written in Perl by John McNamara
24
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
25
+ #
26
+ require 'writeexcel'
27
+
28
+ workbook = WriteExcel.new("chess.xls")
29
+ worksheet = workbook.add_worksheet()
30
+
31
+ # Some row and column formatting
32
+ worksheet.set_column('B:I', 10)
33
+
34
+ (1..8).each { |i| worksheet.set_row(i, 50) }
35
+
36
+ # Define the property hashes
37
+ #
38
+ black = {
39
+ 'fg_color' => 'black',
40
+ 'pattern' => 1,
41
+ }
42
+
43
+ top = { 'top' => 6 }
44
+ bottom = { 'bottom' => 6 }
45
+ left = { 'left' => 6 }
46
+ right = { 'right' => 6 }
47
+
48
+ # Define the formats
49
+ #
50
+ format01 = workbook.add_format(top.merge(left))
51
+ format02 = workbook.add_format(top.merge(black))
52
+ format03 = workbook.add_format(top)
53
+ format04 = workbook.add_format(top.merge(right).merge(black))
54
+
55
+ format05 = workbook.add_format(left)
56
+ format06 = workbook.add_format(black)
57
+ format07 = workbook.add_format
58
+ format08 = workbook.add_format(right.merge(black))
59
+ format09 = workbook.add_format(right)
60
+ format10 = workbook.add_format(left.merge(black))
61
+
62
+ format11 = workbook.add_format(bottom.merge(left).merge(black))
63
+ format12 = workbook.add_format(bottom)
64
+ format13 = workbook.add_format(bottom.merge(black))
65
+ format14 = workbook.add_format(bottom.merge(right))
66
+
67
+
68
+ # Draw the pattern
69
+ worksheet.write('B2', '', format01)
70
+ worksheet.write('C2', '', format02)
71
+ worksheet.write('D2', '', format03)
72
+ worksheet.write('E2', '', format02)
73
+ worksheet.write('F2', '', format03)
74
+ worksheet.write('G2', '', format02)
75
+ worksheet.write('H2', '', format03)
76
+ worksheet.write('I2', '', format04)
77
+
78
+ worksheet.write('B3', '', format10)
79
+ worksheet.write('C3', '', format07)
80
+ worksheet.write('D3', '', format06)
81
+ worksheet.write('E3', '', format07)
82
+ worksheet.write('F3', '', format06)
83
+ worksheet.write('G3', '', format07)
84
+ worksheet.write('H3', '', format06)
85
+ worksheet.write('I3', '', format09)
86
+
87
+ worksheet.write('B4', '', format05)
88
+ worksheet.write('C4', '', format06)
89
+ worksheet.write('D4', '', format07)
90
+ worksheet.write('E4', '', format06)
91
+ worksheet.write('F4', '', format07)
92
+ worksheet.write('G4', '', format06)
93
+ worksheet.write('H4', '', format07)
94
+ worksheet.write('I4', '', format08)
95
+
96
+ worksheet.write('B5', '', format10)
97
+ worksheet.write('C5', '', format07)
98
+ worksheet.write('D5', '', format06)
99
+ worksheet.write('E5', '', format07)
100
+ worksheet.write('F5', '', format06)
101
+ worksheet.write('G5', '', format07)
102
+ worksheet.write('H5', '', format06)
103
+ worksheet.write('I5', '', format09)
104
+
105
+ worksheet.write('B6', '', format05)
106
+ worksheet.write('C6', '', format06)
107
+ worksheet.write('D6', '', format07)
108
+ worksheet.write('E6', '', format06)
109
+ worksheet.write('F6', '', format07)
110
+ worksheet.write('G6', '', format06)
111
+ worksheet.write('H6', '', format07)
112
+ worksheet.write('I6', '', format08)
113
+
114
+ worksheet.write('B7', '', format10)
115
+ worksheet.write('C7', '', format07)
116
+ worksheet.write('D7', '', format06)
117
+ worksheet.write('E7', '', format07)
118
+ worksheet.write('F7', '', format06)
119
+ worksheet.write('G7', '', format07)
120
+ worksheet.write('H7', '', format06)
121
+ worksheet.write('I7', '', format09)
122
+
123
+ worksheet.write('B8', '', format05)
124
+ worksheet.write('C8', '', format06)
125
+ worksheet.write('D8', '', format07)
126
+ worksheet.write('E8', '', format06)
127
+ worksheet.write('F8', '', format07)
128
+ worksheet.write('G8', '', format06)
129
+ worksheet.write('H8', '', format07)
130
+ worksheet.write('I8', '', format08)
131
+
132
+ worksheet.write('B9', '', format11)
133
+ worksheet.write('C9', '', format12)
134
+ worksheet.write('D9', '', format13)
135
+ worksheet.write('E9', '', format12)
136
+ worksheet.write('F9', '', format13)
137
+ worksheet.write('G9', '', format12)
138
+ worksheet.write('H9', '', format13)
139
+ worksheet.write('I9', '', format14)
140
+
141
+ workbook.close
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ################################################################################
4
+ #
5
+ # Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color
6
+ # palette.
7
+ #
8
+ # The set_custom_color() Worksheet method can be used to override one of the
9
+ # built-in palette values with a more suitable colour. See the main docs.
10
+ #
11
+ # reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org
12
+ #
13
+ # original written in Perl by John McNamara
14
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
15
+ #
16
+
17
+ require 'writeexcel'
18
+
19
+ workbook = WriteExcel.new("colors.xls")
20
+
21
+ # Some common formats
22
+ center = workbook.add_format(:align => 'center')
23
+ heading = workbook.add_format(:align => 'center', :bold => 1)
24
+
25
+ ######################################################################
26
+ #
27
+ # Demonstrate the named colors.
28
+ #
29
+
30
+ order = [
31
+ 0x21,
32
+ 0x0B,
33
+ 0x35,
34
+ 0x11,
35
+ 0x16,
36
+ 0x12,
37
+ 0x0D,
38
+ 0x10,
39
+ 0x17,
40
+ 0x09,
41
+ 0x0C,
42
+ 0x0F,
43
+ 0x0E,
44
+ 0x14,
45
+ 0x08,
46
+ 0x0A
47
+ ]
48
+
49
+ colors = {
50
+ 0x08 => 'black',
51
+ 0x0C => 'blue',
52
+ 0x10 => 'brown',
53
+ 0x0F => 'cyan',
54
+ 0x17 => 'gray',
55
+ 0x11 => 'green',
56
+ 0x0B => 'lime',
57
+ 0x0E => 'magenta',
58
+ 0x12 => 'navy',
59
+ 0x35 => 'orange',
60
+ 0x21 => 'pink',
61
+ 0x14 => 'purple',
62
+ 0x0A => 'red',
63
+ 0x16 => 'silver',
64
+ 0x09 => 'white',
65
+ 0x0D => 'yellow',
66
+ }
67
+
68
+ worksheet1 = workbook.add_worksheet('Named colors')
69
+
70
+ worksheet1.set_column(0, 3, 15)
71
+
72
+ worksheet1.write(0, 0, "Index", heading)
73
+ worksheet1.write(0, 1, "Index", heading)
74
+ worksheet1.write(0, 2, "Name", heading)
75
+ worksheet1.write(0, 3, "Color", heading)
76
+
77
+ i = 1
78
+
79
+ # original was colors.each....
80
+ # order unmatch between perl and ruby (of cource, it's hash!)
81
+ # so i use order array to match perl's xls order.
82
+ #
83
+ order.each do |index|
84
+ format = workbook.add_format(
85
+ :fg_color => colors[index],
86
+ :pattern => 1,
87
+ :border => 1
88
+ )
89
+
90
+ worksheet1.write(i + 1, 0, index, center)
91
+ worksheet1.write(i + 1, 1, sprintf("0x%02X", index), center)
92
+ worksheet1.write(i + 1, 2, colors[index], center)
93
+ worksheet1.write(i + 1, 3, '', format)
94
+ i += 1
95
+ end
96
+
97
+ ######################################################################
98
+ #
99
+ # Demonstrate the standard Excel colors in the range 8..63.
100
+ #
101
+
102
+ worksheet2 = workbook.add_worksheet('Standard colors')
103
+
104
+ worksheet2.set_column(0, 3, 15)
105
+
106
+ worksheet2.write(0, 0, "Index", heading)
107
+ worksheet2.write(0, 1, "Index", heading)
108
+ worksheet2.write(0, 2, "Color", heading)
109
+ worksheet2.write(0, 3, "Name", heading)
110
+
111
+ (8..63).each do |i|
112
+ format = workbook.add_format(
113
+ :fg_color => i,
114
+ :pattern => 1,
115
+ :border => 1
116
+ )
117
+
118
+ worksheet2.write((i - 7), 0, i, center)
119
+ worksheet2.write((i - 7), 1, sprintf("0x%02X", i), center)
120
+ worksheet2.write((i - 7), 2, '', format)
121
+
122
+ # Add the color names
123
+ if colors.has_key?(i)
124
+ worksheet2.write((i - 7), 3, colors[i], center)
125
+ end
126
+ end
127
+
128
+ workbook.close
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # This example demonstrates writing cell comments.
6
+ #
7
+ # A cell comment is indicated in Excel by a small red triangle in the upper
8
+ # right-hand corner of the cell.
9
+ #
10
+ # For more advanced comment options see comments2.pl.
11
+ #
12
+ # reverse('©'), November 2005, John McNamara, jmcnamara@cpan.org
13
+ #
14
+ # original written in Perl by John McNamara
15
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
16
+ #
17
+
18
+ require 'writeexcel'
19
+
20
+ workbook = WriteExcel.new("comments1.xls")
21
+ worksheet = workbook.add_worksheet
22
+
23
+ worksheet.write('A1', 'Hello')
24
+ worksheet.write_comment('A1', 'This is a comment')
25
+
26
+ workbook.close
@@ -0,0 +1,351 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ###############################################################################
4
+ #
5
+ # This example demonstrates writing cell comments.
6
+ #
7
+ # A cell comment is indicated in Excel by a small red triangle in the upper
8
+ # right-hand corner of the cell.
9
+ #
10
+ # Each of the worksheets demonstrates different features of cell comments.
11
+ #
12
+ # reverse('©'), November 2005, John McNamara, jmcnamara@cpan.org
13
+ #
14
+ # original written in Perl by John McNamara
15
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
16
+ #
17
+
18
+ require 'writeexcel'
19
+
20
+ workbook = WriteExcel.new("comments2.xls")
21
+ text_wrap = workbook.add_format(:text_wrap => 1, :valign => 'top')
22
+ worksheet1 = workbook.add_worksheet
23
+ worksheet2 = workbook.add_worksheet
24
+ worksheet3 = workbook.add_worksheet
25
+ worksheet4 = workbook.add_worksheet
26
+ worksheet5 = workbook.add_worksheet
27
+ worksheet6 = workbook.add_worksheet
28
+ worksheet7 = workbook.add_worksheet
29
+ worksheet8 = workbook.add_worksheet
30
+
31
+ # Variables that we will use in each example.
32
+ cell_text = ''
33
+ comment = ''
34
+
35
+ ###############################################################################
36
+ #
37
+ # Example 1. Demonstrates a simple cell comment without formatting and Unicode
38
+ # comments encoded as UTF-16 and as UTF-8.
39
+ #
40
+
41
+ # Set up some formatting.
42
+ worksheet1.set_column('C:C', 25)
43
+ worksheet1.set_row(2, 50)
44
+ worksheet1.set_row(5, 50)
45
+
46
+ # Simple ascii string.
47
+ cell_text = 'Hold the mouse over this cell to see the comment.'
48
+
49
+ comment = 'This is a comment.'
50
+
51
+ worksheet1.write('C3', cell_text, text_wrap)
52
+ worksheet1.write_comment('C3', comment)
53
+
54
+ # UTF-16 string.
55
+ cell_text = 'This is a UTF-16 comment.'
56
+
57
+ comment = [0x263a].pack("n")
58
+
59
+ worksheet1.write('C6', cell_text, text_wrap)
60
+ worksheet1.write_comment('C6', comment, :encoding => 1)
61
+
62
+ # UTF-8 string in perl 5.8.
63
+ worksheet1.set_row(8, 50)
64
+ cell_text = 'This is a UTF-8 string.'
65
+ comment = '☺' # chr 0x263a in perl.
66
+
67
+ worksheet1.write('C9', cell_text, text_wrap)
68
+ worksheet1.write_comment('C9', comment)
69
+
70
+ ###############################################################################
71
+ #
72
+ # Example 2. Demonstrates visible and hidden comments.
73
+ #
74
+
75
+ # Set up some formatting.
76
+ worksheet2.set_column('C:C', 25)
77
+ worksheet2.set_row(2, 50)
78
+ worksheet2.set_row(5, 50)
79
+
80
+
81
+ cell_text = 'This cell comment is visible.'
82
+
83
+ comment = 'Hello.'
84
+
85
+ worksheet2.write('C3', cell_text, text_wrap)
86
+ worksheet2.write_comment('C3', comment, :visible => 1)
87
+
88
+
89
+ cell_text = "This cell comment isn't visible (the default)."
90
+
91
+ comment = 'Hello.'
92
+
93
+ worksheet2.write('C6', cell_text, text_wrap)
94
+ worksheet2.write_comment('C6', comment)
95
+
96
+ ###############################################################################
97
+ #
98
+ # Example 3. Demonstrates visible and hidden comments set at the worksheet
99
+ # level.
100
+ #
101
+
102
+ # Set up some formatting.
103
+ worksheet3.set_column('C:C', 25)
104
+ worksheet3.set_row(2, 50)
105
+ worksheet3.set_row(5, 50)
106
+ worksheet3.set_row(8, 50)
107
+
108
+ # Make all comments on the worksheet visible.
109
+ worksheet3.show_comments
110
+
111
+ cell_text = 'This cell comment is visible, explicitly.'
112
+
113
+ comment = 'Hello.'
114
+
115
+ worksheet3.write('C3', cell_text, text_wrap)
116
+ worksheet3.write_comment('C3', comment, :visible => 1)
117
+
118
+
119
+ cell_text = 'This cell comment is also visible because ' +
120
+ 'we used show_comments().'
121
+
122
+ comment = 'Hello.'
123
+
124
+ worksheet3.write('C6', cell_text, text_wrap)
125
+ worksheet3.write_comment('C6', comment)
126
+
127
+
128
+ cell_text = 'However, we can still override it locally.'
129
+
130
+ comment = 'Hello.'
131
+
132
+ worksheet3.write('C9', cell_text, text_wrap)
133
+ worksheet3.write_comment('C9', comment, :visible => 0)
134
+
135
+ ###############################################################################
136
+ #
137
+ # Example 4. Demonstrates changes to the comment box dimensions.
138
+ #
139
+
140
+ # Set up some formatting.
141
+ worksheet4.set_column('C:C', 25)
142
+ worksheet4.set_row(2, 50)
143
+ worksheet4.set_row(5, 50)
144
+ worksheet4.set_row(8, 50)
145
+ worksheet4.set_row(15, 50)
146
+
147
+ worksheet4.show_comments
148
+
149
+ cell_text = 'This cell comment is default size.'
150
+
151
+ comment = 'Hello.'
152
+
153
+ worksheet4.write('C3', cell_text, text_wrap)
154
+ worksheet4.write_comment('C3', comment)
155
+
156
+
157
+ cell_text = 'This cell comment is twice as wide.'
158
+
159
+ comment = 'Hello.'
160
+
161
+ worksheet4.write('C6', cell_text, text_wrap)
162
+ worksheet4.write_comment('C6', comment, :x_scale => 2)
163
+
164
+
165
+ cell_text = 'This cell comment is twice as high.'
166
+
167
+ comment = 'Hello.'
168
+
169
+ worksheet4.write('C9', cell_text, text_wrap)
170
+ worksheet4.write_comment('C9', comment, :y_scale => 2)
171
+
172
+
173
+ cell_text = 'This cell comment is scaled in both directions.'
174
+
175
+ comment = 'Hello.'
176
+
177
+ worksheet4.write('C16', cell_text, text_wrap)
178
+ worksheet4.write_comment('C16', comment, :x_scale => 1.2, :y_scale => 0.8)
179
+
180
+
181
+ cell_text = 'This cell comment has width and height specified in pixels.'
182
+
183
+ comment = 'Hello.'
184
+
185
+ worksheet4.write('C19', cell_text, text_wrap)
186
+ worksheet4.write_comment('C19', comment, :width => 200, :height => 20)
187
+
188
+ ###############################################################################
189
+ #
190
+ # Example 5. Demonstrates changes to the cell comment position.
191
+ #
192
+
193
+ worksheet5.set_column('C:C', 25)
194
+ worksheet5.set_row(2, 50)
195
+ worksheet5.set_row(5, 50)
196
+ worksheet5.set_row(8, 50)
197
+ worksheet5.set_row(11, 50)
198
+
199
+ worksheet5.show_comments
200
+
201
+ cell_text = 'This cell comment is in the default position.'
202
+
203
+ comment = 'Hello.'
204
+
205
+ worksheet5.write('C3', cell_text, text_wrap)
206
+ worksheet5.write_comment('C3', comment)
207
+
208
+
209
+ cell_text = 'This cell comment has been moved to another cell.'
210
+
211
+ comment = 'Hello.'
212
+
213
+ worksheet5.write('C6', cell_text, text_wrap)
214
+ worksheet5.write_comment('C6', comment, :start_cell => 'E4')
215
+
216
+
217
+ cell_text = 'This cell comment has been moved to another cell.'
218
+
219
+ comment = 'Hello.'
220
+
221
+ worksheet5.write('C9', cell_text, text_wrap)
222
+ worksheet5.write_comment('C9', comment, :start_row => 8, :start_col => 4)
223
+
224
+
225
+ cell_text = 'This cell comment has been shifted within its default cell.'
226
+
227
+ comment = 'Hello.'
228
+
229
+ worksheet5.write('C12', cell_text, text_wrap)
230
+ worksheet5.write_comment('C12', comment, :x_offset => 30, :y_offset => 12)
231
+
232
+ ###############################################################################
233
+ #
234
+ # Example 6. Demonstrates changes to the comment background colour.
235
+ #
236
+
237
+ worksheet6.set_column('C:C', 25)
238
+ worksheet6.set_row(2, 50)
239
+ worksheet6.set_row(5, 50)
240
+ worksheet6.set_row(8, 50)
241
+
242
+ worksheet6.show_comments
243
+
244
+ cell_text = 'This cell comment has a different colour.'
245
+
246
+ comment = 'Hello.'
247
+
248
+ worksheet6.write('C3', cell_text, text_wrap)
249
+ worksheet6.write_comment('C3', comment, :color => 'green')
250
+
251
+
252
+ cell_text = 'This cell comment has the default colour.'
253
+
254
+ comment = 'Hello.'
255
+
256
+ worksheet6.write('C6', cell_text, text_wrap)
257
+ worksheet6.write_comment('C6', comment)
258
+
259
+ cell_text = 'This cell comment has a different colour.'
260
+
261
+ comment = 'Hello.'
262
+
263
+ worksheet6.write('C9', cell_text, text_wrap)
264
+ worksheet6.write_comment('C9', comment, :color => 0x35)
265
+
266
+ ###############################################################################
267
+ #
268
+ # Example 7. Demonstrates how to set the cell comment author.
269
+ #
270
+
271
+ worksheet7.set_column('C:C', 30)
272
+ worksheet7.set_row(2, 50)
273
+ worksheet7.set_row(5, 50)
274
+ worksheet7.set_row(8, 50)
275
+ worksheet7.set_row(11, 50)
276
+
277
+ author = ''
278
+ cell = 'C3'
279
+
280
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented "+
281
+ "by #{author}' (blank) in the status bar at the bottom"
282
+
283
+ comment = 'Hello.'
284
+
285
+ worksheet7.write(cell, cell_text, text_wrap)
286
+ worksheet7.write_comment(cell, comment)
287
+
288
+ author = 'Perl'
289
+ cell = 'C6'
290
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented " +
291
+ "by #{author}' in the status bar at the bottom"
292
+
293
+ comment = 'Hello.'
294
+
295
+ worksheet7.write(cell, cell_text, text_wrap)
296
+ worksheet7.write_comment(cell, comment, :author => author)
297
+
298
+ author = [0x20AC].pack("n") # UTF-16 Euro
299
+ cell = 'C9'
300
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented " +
301
+ "by Euro' in the status bar at the bottom"
302
+
303
+ comment = 'Hello.'
304
+
305
+ worksheet7.write(cell, cell_text, text_wrap)
306
+ worksheet7.write_comment(cell, comment, :author => author,
307
+ :author_encoding => 1)
308
+
309
+ # UTF-8 string in perl 5.8.
310
+ author = '☺' # smiley
311
+ cell = 'C12'
312
+ cell_text = "Move the mouse over this cell and you will see 'Cell commented " +
313
+ "by #{author}' in the status bar at the bottom"
314
+ comment = 'Hello.'
315
+
316
+ worksheet7.write(cell, cell_text, text_wrap)
317
+ worksheet7.write_comment(cell, comment, :author => author)
318
+
319
+ ###############################################################################
320
+ #
321
+ # Example 8. Demonstrates the need to explicitly set the row height.
322
+ #
323
+
324
+ # Set up some formatting.
325
+ worksheet8.set_column('C:C', 25)
326
+ worksheet8.set_row(2, 80)
327
+
328
+ worksheet8.show_comments
329
+
330
+ cell_text = 'The height of this row has been adjusted explicitly using ' +
331
+ 'set_row(). The size of the comment box is adjusted ' +
332
+ 'accordingly by WriteExcel.'
333
+
334
+ comment = 'Hello.'
335
+
336
+ worksheet8.write('C3', cell_text, text_wrap)
337
+ worksheet8.write_comment('C3', comment)
338
+
339
+ cell_text = 'The height of this row has been adjusted by Excel due to the ' +
340
+ 'text wrap property being set. Unfortunately this means that ' +
341
+ 'the height of the row is unknown to WriteExcel at run time ' +
342
+ "and thus the comment box is stretched as well.\n\n" +
343
+ 'Use set_row() to specify the row height explicitly to avoid ' +
344
+ 'this problem.'
345
+
346
+ comment = 'Hello.'
347
+
348
+ worksheet8.write('C6', cell_text, text_wrap)
349
+ worksheet8.write_comment('C6', comment)
350
+
351
+ workbook.close