Centering an image

3 views
Skip to first unread message

rpn...@ibksoftware.com

unread,
Nov 3, 2009, 2:03:44 AM11/3/09
to Spreadsheet::WriteExcel
John;

I have several png files that represent trends (up, down and no
change). They are 16px x 16px, Can they be centered using formats?
Currently they are flush left. Do I need to create the graphic as wide
as the column? Thge column width is 10 row height is 22.

jmcnamara

unread,
Nov 3, 2009, 5:07:14 AM11/3/09
to Spreadsheet::WriteExcel
On Nov 3, 7:03 am, "rpno...@ibksoftware.com" <rpno...@ibksoftware.com>
wrote:
Hi Rob,

Images (and other objects) are layered on top of a worksheet so they
aren't in general controlled by formatting.

In the case of images you will need to specify your own offsets to
centre the image using the optional $x, $y parameters in insert_image
().

http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm#insert_image($row,_$col,_$filename,_$x,_$y,_$scale_x,_$scale_y)

The default cell width is 64 pixels and the default height is 17
pixels. So to centre a 16x16 image you will need the following
offsets:

x = int( ( 64 - 16) / 2 ) = 24
y = int ( ( 17 - 16) /2 ) = 0

Here is an example:

#!/usr/bin/perl

use strict;
use warnings;
use Spreadsheet::WriteExcel;

my $workbook = Spreadsheet::WriteExcel->new('test.xls');
my $worksheet = $workbook->add_worksheet();

# Left top align.
$worksheet->insert_image('B3', '16x16.png');

# Centred. x = int( ( 64 - 16) / 2 ), y = int ( ( 17 - 16) /2 ).
$worksheet->insert_image('B5', '16x16.png', 24, 0);

# Right bottom align. x = ( 64 - 16), y = ( 17 - 16).
$worksheet->insert_image('B7', '16x16.png', 48, 1);

__END__

John.
--



Reply all
Reply to author
Forward
0 new messages