1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
#!/usr/bin/perl -w
###############################################################################
#
# This example demonstrates writing cell comments.
#
# A cell comment is indicated in Excel by a small red triangle in the upper
# right-hand corner of the cell.
#
# Each of the worksheets demonstrates different features of cell comments.
#
# reverse(''), November 2005, John McNamara, jmcnamara@cpan.org
#
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("comments2.xls");
my $text_wrap = $workbook->add_format(text_wrap => 1, valign => 'top');
my $worksheet1 = $workbook->add_worksheet();
my $worksheet2 = $workbook->add_worksheet();
my $worksheet3 = $workbook->add_worksheet();
my $worksheet4 = $workbook->add_worksheet();
my $worksheet5 = $workbook->add_worksheet();
my $worksheet6 = $workbook->add_worksheet();
my $worksheet7 = $workbook->add_worksheet();
my $worksheet8 = $workbook->add_worksheet();
# Variables that we will use in each example.
my $cell_text = '';
my $comment = '';
###############################################################################
#
# Example 1. Demonstrates a simple cell comment without formatting and Unicode
# comments encoded as UTF-16 and as UTF-8.
#
# Set up some formatting.
$worksheet1->set_column('C:C', 25);
$worksheet1->set_row(2, 50);
$worksheet1->set_row(5, 50);
# Simple ascii string.
$cell_text = 'Hold the mouse over this cell to see the comment.';
$comment = 'This is a comment.';
$worksheet1->write ('C3', $cell_text, $text_wrap);
$worksheet1->write_comment('C3', $comment);
# UTF-16 string.
$cell_text = 'This is a UTF-16 comment.';
$comment = pack "n", 0x263a;
$worksheet1->write ('C6', $cell_text, $text_wrap);
$worksheet1->write_comment('C6', $comment, encoding => 1);
# UTF-8 string in perl 5.8.
if ($] >= 5.008) {
$worksheet1->set_row(8, 50);
$cell_text = 'This is a UTF-8 string.';
$comment = chr 0x263a;
$worksheet1->write ('C9', $cell_text, $text_wrap);
$worksheet1->write_comment('C9', $comment);
}
###############################################################################
#
# Example 2. Demonstrates visible and hidden comments.
#
# Set up some formatting.
$worksheet2->set_column('C:C', 25);
$worksheet2->set_row(2, 50);
$worksheet2->set_row(5, 50);
$cell_text = 'This cell comment is visible.';
$comment = 'Hello.';
$worksheet2->write ('C3', $cell_text, $text_wrap);
$worksheet2->write_comment('C3', $comment, visible => 1);
$cell_text = "This cell comment isn't visible (the default).";
$comment = 'Hello.';
$worksheet2->write ('C6', $cell_text, $text_wrap);
$worksheet2->write_comment('C6', $comment);
###############################################################################
#
# Example 3. Demonstrates visible and hidden comments set at the worksheet
# level.
#
# Set up some formatting.
$worksheet3->set_column('C:C', 25);
$worksheet3->set_row(2, 50);
$worksheet3->set_row(5, 50);
$worksheet3->set_row(8, 50);
# Make all comments on the worksheet visible.
$worksheet3->show_comments();
$cell_text = 'This cell comment is visible, explicitly.';
$comment = 'Hello.';
$worksheet3->write ('C3', $cell_text, $text_wrap);
$worksheet3->write_comment('C3', $comment, visible => 1);
$cell_text = 'This cell comment is also visible because '.
'we used show_comments().';
$comment = 'Hello.';
$worksheet3->write ('C6', $cell_text, $text_wrap);
$worksheet3->write_comment('C6', $comment);
$cell_text = 'However, we can still override it locally.';
$comment = 'Hello.';
$worksheet3->write ('C9', $cell_text, $text_wrap);
$worksheet3->write_comment('C9', $comment, visible => 0);
###############################################################################
#
# Example 4. Demonstrates changes to the comment box dimensions.
#
# Set up some formatting.
$worksheet4->set_column('C:C', 25);
$worksheet4->set_row(2, 50);
$worksheet4->set_row(5, 50);
$worksheet4->set_row(8, 50);
$worksheet4->set_row(15, 50);
$worksheet4->show_comments();
$cell_text = 'This cell comment is default size.';
$comment = 'Hello.';
$worksheet4->write ('C3', $cell_text, $text_wrap);
$worksheet4->write_comment('C3', $comment);
$cell_text = 'This cell comment is twice as wide.';
$comment = 'Hello.';
$worksheet4->write ('C6', $cell_text, $text_wrap);
$worksheet4->write_comment('C6', $comment, x_scale => 2);
$cell_text = 'This cell comment is twice as high.';
$comment = 'Hello.';
$worksheet4->write ('C9', $cell_text, $text_wrap);
$worksheet4->write_comment('C9', $comment, y_scale => 2);
$cell_text = 'This cell comment is scaled in both directions.';
$comment = 'Hello.';
$worksheet4->write ('C16', $cell_text, $text_wrap);
$worksheet4->write_comment('C16', $comment, x_scale => 1.2, y_scale => 0.8);
$cell_text = 'This cell comment has width and height specified in pixels.';
$comment = 'Hello.';
$worksheet4->write ('C19', $cell_text, $text_wrap);
$worksheet4->write_comment('C19', $comment, width => 200, height => 20);
###############################################################################
#
# Example 5. Demonstrates changes to the cell comment position.
#
$worksheet5->set_column('C:C', 25);
$worksheet5->set_row(2, 50);
$worksheet5->set_row(5, 50);
$worksheet5->set_row(8, 50);
$worksheet5->set_row(11, 50);
$worksheet5->show_comments();
$cell_text = 'This cell comment is in the default position.';
$comment = 'Hello.';
$worksheet5->write ('C3', $cell_text, $text_wrap);
$worksheet5->write_comment('C3', $comment);
$cell_text = 'This cell comment has been moved to another cell.';
$comment = 'Hello.';
$worksheet5->write ('C6', $cell_text, $text_wrap);
$worksheet5->write_comment('C6', $comment, start_cell => 'E4');
$cell_text = 'This cell comment has been moved to another cell.';
$comment = 'Hello.';
$worksheet5->write ('C9', $cell_text, $text_wrap);
$worksheet5->write_comment('C9', $comment, start_row => 8, start_col => 4);
$cell_text = 'This cell comment has been shifted within its default cell.';
$comment = 'Hello.';
$worksheet5->write ('C12', $cell_text, $text_wrap);
$worksheet5->write_comment('C12', $comment, x_offset => 30, y_offset => 12);
###############################################################################
#
# Example 6. Demonstrates changes to the comment background colour.
#
$worksheet6->set_column('C:C', 25);
$worksheet6->set_row(2, 50);
$worksheet6->set_row(5, 50);
$worksheet6->set_row(8, 50);
$worksheet6->show_comments();
$cell_text = 'This cell comment has a different colour.';
$comment = 'Hello.';
$worksheet6->write ('C3', $cell_text, $text_wrap);
$worksheet6->write_comment('C3', $comment, color => 'green');
$cell_text = 'This cell comment has the default colour.';
$comment = 'Hello.';
$worksheet6->write ('C6', $cell_text, $text_wrap);
$worksheet6->write_comment('C6', $comment);
$cell_text = 'This cell comment has a different colour.';
$comment = 'Hello.';
$worksheet6->write ('C9', $cell_text, $text_wrap);
$worksheet6->write_comment('C9', $comment, color => 0x35);
###############################################################################
#
# Example 7. Demonstrates how to set the cell comment author.
#
$worksheet7->set_column('C:C', 30);
$worksheet7->set_row(2, 50);
$worksheet7->set_row(5, 50);
$worksheet7->set_row(8, 50);
$worksheet7->set_row(11, 50);
my $author = '';
my $cell = 'C3';
$cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
"by $author' (blank) in the status bar at the bottom";
$comment = 'Hello.';
$worksheet7->write ($cell, $cell_text, $text_wrap);
$worksheet7->write_comment($cell, $comment);
$author = 'Perl';
$cell = 'C6';
$cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
"by $author' in the status bar at the bottom";
$comment = 'Hello.';
$worksheet7->write ($cell, $cell_text, $text_wrap);
$worksheet7->write_comment($cell, $comment, author => $author);
$author = pack "n", 0x20AC; # UTF-16 Euro
$cell = 'C9';
$cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
"by Euro' in the status bar at the bottom";
$comment = 'Hello.';
$worksheet7->write ($cell, $cell_text, $text_wrap);
$worksheet7->write_comment($cell, $comment, author => $author,
author_encoding => 1 );
# UTF-8 string in perl 5.8.
if ($] >= 5.008) {
$author = chr 0x20AC;
$cell = 'C12';
$cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
"by $author' in the status bar at the bottom";
$comment = 'Hello.';
$worksheet7->write ($cell, $cell_text, $text_wrap);
$worksheet7->write_comment($cell, $comment, author => $author);
}
###############################################################################
#
# Example 8. Demonstrates the need to explicitly set the row height.
#
# Set up some formatting.
$worksheet8->set_column('C:C', 25);
$worksheet8->set_row(2, 80);
$worksheet8->show_comments();
$cell_text = 'The height of this row has been adjusted explicitly using ' .
'set_row(). The size of the comment box is adjusted ' .
'accordingly by WriteExcel.';
$comment = 'Hello.';
$worksheet8->write ('C3', $cell_text, $text_wrap);
$worksheet8->write_comment('C3', $comment);
$cell_text = 'The height of this row has been adjusted by Excel due to the ' .
'text wrap property being set. Unfortunately this means that ' .
'the height of the row is unknown to WriteExcel at run time ' .
"and thus the comment box is stretched as well.\n\n" .
'Use set_row() to specify the row height explicitly to avoid ' .
'this problem.';
$comment = 'Hello.';
$worksheet8->write ('C6', $cell_text, $text_wrap);
$worksheet8->write_comment('C6', $comment);
__END__
|