Skip to content

Commit

Permalink
table: fix issue where column formulas were overwritten by table data
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Oct 8, 2023
1 parent 829088f commit 41f194a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/Excel/Writer/XLSX/Worksheet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5059,11 +5059,7 @@ sub add_table {
$formula =~ s/@/[#This Row],/g;

$col_data->{_formula} = $formula;

for my $row ( $first_data_row .. $last_data_row ) {
$self->write_formula( $row, $col_num, $formula,
$user_data->{format} );
}
# We write the formulas below after the table data.
}

# Handle the function for the total row.
Expand Down Expand Up @@ -5165,6 +5161,26 @@ sub add_table {
}


# Write any columns formulas after the user supplied table data to
# overwrite it if required.
$col_id = 0;
for my $col_num ( $col1 .. $col2 ) {

my $column_data = $table{_columns}->[$col_id];

if ( $column_data && $column_data->{_formula} ) {
my $formula_format = $col_formats[$col_id];
my $formula = $column_data->{_formula};

for my $row ( $first_data_row .. $last_data_row ) {
$self->write_formula( $row, $col_num, $formula,
$formula_format );
}
}
$col_id++;
}


# Store the filter cell positions for use in the autofit calculation.
if ( $param->{autofilter} ) {
for my $col ( $col1 .. $col2 ) {
Expand Down
94 changes: 94 additions & 0 deletions t/regression/table34.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, [email protected]
#
# 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 = 'table34.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;


my $ignore_members = [ 'xl/calcChain.xml', '\[Content_Types\].xml', 'xl/_rels/workbook.xml.rels' ];
my $ignore_elements = { 'xl/workbook.xml' => ['<workbookView'] };

###############################################################################
#
# Test the creation of a simple Excel::Writer::XLSX file with tables.
#
use Excel::Writer::XLSX;

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

my $format1 = $workbook->add_format( num_format => "0.0000" );

my $data = [
[ 'Foo', 1234, 0, 4321 ],
[ 'Bar', 1256, 0, 4320 ],
[ 'Baz', 2234, 0, 4332 ],
[ 'Bop', 1324, 0, 4333 ],
];


# Set the column width to match the target worksheet.
$worksheet->set_column('C:F', 10.288);

# Add the table.
$worksheet->add_table(
'C2:F6',
{
data => $data,
columns => [
{},
{},
{},
{ formula => 'Table1[[#This Row],[Column3]]', format => $format1 }
],
}
);

$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/table34.xlsx
Binary file not shown.

0 comments on commit 41f194a

Please sign in to comment.