Skip to content

Commit 7dda1e3

Browse files
committed
worksheet: add embed_image()
Add support for embedded worksheet images from newer versions of Excel.
1 parent a703853 commit 7dda1e3

37 files changed

+2485
-311
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ TAGS
1212
*#
1313
~*xlsx
1414
*.bak
15+
.vscode
1516

lib/Excel/Writer/XLSX.pm

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ The following methods are available through a new worksheet:
724724
set_comments_author()
725725
add_write_handler()
726726
insert_image()
727+
embed_image()
727728
insert_chart()
728729
insert_shape()
729730
insert_button()
@@ -1711,6 +1712,8 @@ This method can be used to insert a image into a worksheet. The image can be in
17111712
$worksheet2->insert_image( 'A1', '../images/perl.bmp' );
17121713
$worksheet3->insert_image( 'A1', '.c:\images\perl.bmp' );
17131714
1715+
This is the equivalent of Excel's menu option to insert an image using the option to "Place over Cells". See C<embed_image()> below for the equivalent method to "Place in Cell".
1716+
17141717
The optional C<options> hash/hashref parameter can be used to set various options for the image. The defaults are:
17151718
17161719
%options = (
@@ -1780,6 +1783,66 @@ BMP images must be 24 bit, true colour, bitmaps. In general it is best to avoid
17801783
17811784
17821785
1786+
=head2 embed_image( $row, $col, $filename, { %options } )
1787+
1788+
This method can be used to embed an image into a worksheet. The image can be in PNG, JPEG, GIF or BMP format.
1789+
1790+
$worksheet1->embed_image( 'A1', 'perl.bmp' );
1791+
$worksheet2->embed_image( 'A1', '../images/perl.bmp' );
1792+
$worksheet3->embed_image( 'A1', '.c:\images\perl.bmp' );
1793+
1794+
This method can be used to embed a image into a worksheet cell and have the
1795+
image automatically scale to the width and height of the cell. The X/Y scaling
1796+
of the image is preserved but the size of the image is adjusted to fit the
1797+
largest possible width or height depending on the cell dimensions.
1798+
1799+
This is the equivalent of Excel's menu option to insert an image using the
1800+
option to "Place in Cell". See C<insert_image()> for the equivalent method to
1801+
"Place over Cells".
1802+
1803+
The optional C<options> hash/hashref parameter can be used to set various options for the image. The defaults are:
1804+
1805+
%options = (
1806+
cell_format => format,
1807+
url => undef,
1808+
tip => undef,
1809+
description => $filename,
1810+
decorative => 0,
1811+
);
1812+
1813+
The C<cell_format> parameters can be an standard Format to set the formatting of the cell behind the image.
1814+
1815+
The C<url> option can be use to used to add a hyperlink to an image:
1816+
1817+
$worksheet->insert_image( 'A1', 'logo.png',
1818+
{ url => 'https://github.com/jmcnamara' } );
1819+
1820+
The supported url formats are the same as those supported by the C<write_url()> method and the same rules/limits apply.
1821+
1822+
The C<tip> option can be use to used to add a mouseover tip to the hyperlink:
1823+
1824+
$worksheet->insert_image( 'A1', 'logo.png',
1825+
{
1826+
url => 'https://github.com/jmcnamara',
1827+
tip => 'GitHub'
1828+
}
1829+
);
1830+
1831+
The C<description> parameter can be used to specify a description or "alt text" string for the image. In general this would be used to provide a text description of the image to help accessibility. It is an optional parameter and defaults to the filename of the image. It can be used as follows:
1832+
1833+
$worksheet->insert_image( 'E9', 'logo.png',
1834+
{description => "This is some alternative text"} );
1835+
1836+
The optional C<decorative> parameter is also used to help accessibility. It is used to mark the image as decorative, and thus uninformative, for automated screen readers. As in Excel, if this parameter is in use the C<description> field isn't written. It is used as follows:
1837+
1838+
$worksheet->insert_image( 'E9', 'logo.png', {decorative => 1} );
1839+
1840+
Note: you must call C<set_row()> or C<set_column()> before C<insert_image()> if you wish to change the default dimensions of any of the rows or columns that the image occupies. The height of a row can also change if you use a font that is larger than the default. This in turn will affect the scaling of your image. To avoid this you should explicitly set the height of the row using C<set_row()> if it contains a font size that will change the row height.
1841+
1842+
BMP images must be 24 bit, true colour, bitmaps. In general it is best to avoid BMP images since they aren't compressed.
1843+
1844+
1845+
17831846
17841847
=head2 insert_chart( $row, $col, $chart, { %options } )
17851848

lib/Excel/Writer/XLSX/Package/ContentTypes.pm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,38 @@ sub _add_metadata {
350350
}
351351

352352

353+
###############################################################################
354+
#
355+
# _add_richvalue()
356+
#
357+
# Add the RichValue files to the ContentTypes overrides.
358+
#
359+
sub _add_richvalue {
360+
361+
my $self = shift;
362+
363+
$self->_add_override(
364+
'/xl/richData/rdRichValueTypes.xml',
365+
'application/vnd.ms-excel.rdrichvaluetypes+xml'
366+
);
367+
368+
$self->_add_override(
369+
'/xl/richData/rdrichvalue.xml',
370+
'application/vnd.ms-excel.rdrichvalue+xml'
371+
);
372+
373+
$self->_add_override(
374+
'/xl/richData/rdrichvaluestructure.xml',
375+
'application/vnd.ms-excel.rdrichvaluestructure+xml'
376+
);
377+
378+
$self->_add_override(
379+
'/xl/richData/richValueRel.xml',
380+
'application/vnd.ms-excel.richvaluerel+xml'
381+
);
382+
}
383+
384+
353385
###############################################################################
354386
#
355387
# Internal methods.

0 commit comments

Comments
 (0)