Skip to content

Commit

Permalink
wip: add url support to embed_image()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Feb 17, 2024
1 parent 71245b7 commit c7da387
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 26 deletions.
59 changes: 33 additions & 26 deletions lib/Excel/Writer/XLSX/Worksheet.pm
Expand Up @@ -235,6 +235,7 @@ sub new {
$self->{_has_dynamic_functions} = 0;
$self->{_has_embedded_images} = 0;
$self->{_use_future_functions} = 0;
$self->{_ignore_write_string} = 0;

$self->{_rstring} = '';
$self->{_previous_row} = 0;
Expand Down Expand Up @@ -3437,18 +3438,22 @@ sub write_url {
return -5;
}

# Write previous row if in in-line string optimization mode.
if ( $self->{_optimization} == 1 && $row > $self->{_previous_row} ) {
$self->_write_single_row( $row );
}

# Add the default URL format.
if ( !defined $xf ) {
$xf = $self->{_default_url_format};
}

# Write the hyperlink string.
$self->write_string( $row, $col, $str, $xf );

if ( !$self->{_ignore_write_string} ) {

# Write previous row if in in-line string optimization mode.
if ( $self->{_optimization} == 1 && $row > $self->{_previous_row} ) {
$self->_write_single_row( $row );
}

# Write the hyperlink string.
$self->write_string( $row, $col, $str, $xf );
}

# Store the hyperlink data in a separate structure.
$self->{_hyperlinks}->{$row}->{$col} = {
Expand All @@ -3458,6 +3463,7 @@ sub write_url {
_tip => $tip
};


return $str_error;
}

Expand Down Expand Up @@ -6540,15 +6546,10 @@ sub embed_image {
@_ = $self->_substitute_cellref( @_ );
}

my $row = $_[0];
my $col = $_[1];
my $image = $_[2];
my $xf = undef;
my $x_offset;
my $y_offset;
my $x_scale;
my $y_scale;
my $anchor;
my $row = $_[0];
my $col = $_[1];
my $image = $_[2];
my $xf = undef;
my $url;
my $tip;
my $description;
Expand All @@ -6558,29 +6559,36 @@ sub embed_image {
return -2 if $self->_check_dimensions( $row, $col );

croak "Insufficient arguments in embed_image()" unless @_ >= 3;
croak "Couldn't locate $image: $!" unless -e $image;
croak "Couldn't locate $image: $!" unless -e $image;

if ( ref $_[3] eq 'HASH' ) {

# Newer hashref bashed options.
my $options = $_[3];
$xf = $options->{format};
$x_offset = $options->{x_offset} || 0;
$y_offset = $options->{y_offset} || 0;
$x_scale = $options->{x_scale} || 1;
$y_scale = $options->{y_scale} || 1;
$anchor = $options->{object_position} || 2;
$url = $options->{url};
$tip = $options->{tip};
$description = $options->{description};
$decorative = $options->{decorative};
}

# Write the url without writing a string.
if ( $url ) {
if ( !defined $xf ) {
$xf = $self->{_default_url_format};
}

$self->{_ignore_write_string} = 1;
$self->write_url( $row, $col, $url, $xf, undef, $tip );
$self->{_ignore_write_string} = 0;
}

my ( $type, $width, $height, $name, $x_dpi, $y_dpi, $md5 ) =
# Get the image properties, mainly for the type and checksum.
my ( $type, undef, undef, undef, undef, undef, $md5 ) =
get_image_properties( $image );

# Check for duplicate images.
my $image_index = ${ $self->{_embedded_images_indexes} }->{$md5};

if ( !$image_index ) {
push @{ ${ $self->{_embedded_images} } },
[ $image, $type, $description, $decorative ];
Expand All @@ -6589,9 +6597,8 @@ sub embed_image {
${ $self->{_embedded_images_indexes} }->{$md5} = $image_index;
}


# Write the cell placeholder.
$self->{_table}->{$row}->{$col} = [ 'e', "#VALUE!", $xf, $image_index];
$self->{_table}->{$row}->{$col} = [ 'e', "#VALUE!", $xf, $image_index ];
$self->{_has_embedded_images} = 1;
}

Expand Down
69 changes: 69 additions & 0 deletions t/regression/embed_image10.t
@@ -0,0 +1,69 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, jmcnamara@cpan.org
#
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#

use lib 't/lib';
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
use strict;
use warnings;

use Test::More tests => 1;

###############################################################################
#
# Tests setup.
#
my $filename = 'embed_image10.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;

my $ignore_members = [];
my $ignore_elements = {};


###############################################################################
#
# Test the creation of a simple Excel::Writer::XLSX file with image(s).
#
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new( $got_filename );
my $worksheet = $workbook->add_worksheet();

$worksheet->embed_image( 0, 0, $dir . 'images/red.png', {url => "http://www.cpan.org/"} );

$workbook->close();


###############################################################################
#
# Compare the generated and existing Excel files.
#

my ( $got, $expected, $caption ) = _compare_xlsx_files(

$got_filename,
$exp_filename,
$ignore_members,
$ignore_elements,
);

_is_deep_diff( $got, $expected, $caption );


###############################################################################
#
# Cleanup.
#
unlink $got_filename;

__END__
72 changes: 72 additions & 0 deletions t/regression/embed_image11.t
@@ -0,0 +1,72 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, jmcnamara@cpan.org
#
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#

use lib 't/lib';
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
use strict;
use warnings;

use Test::More tests => 1;

###############################################################################
#
# Tests setup.
#
my $filename = 'embed_image11.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;

my $ignore_members = [];
my $ignore_elements = {};


###############################################################################
#
# Test the creation of a simple Excel::Writer::XLSX file with image(s).
#
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new( $got_filename );
my $worksheet = $workbook->add_worksheet();

$worksheet->set_column(0, 0, 11);
$worksheet->set_row(0, 72);

$worksheet->embed_image( 0, 0, $dir . 'images/red.png' );

$workbook->close();


###############################################################################
#
# Compare the generated and existing Excel files.
#

my ( $got, $expected, $caption ) = _compare_xlsx_files(

$got_filename,
$exp_filename,
$ignore_members,
$ignore_elements,
);

_is_deep_diff( $got, $expected, $caption );


###############################################################################
#
# Cleanup.
#
unlink $got_filename;

__END__
Binary file added t/regression/xlsx_files/embed_image10.xlsx
Binary file not shown.
Binary file added t/regression/xlsx_files/embed_image11.xlsx
Binary file not shown.

0 comments on commit c7da387

Please sign in to comment.