Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added fix for fg/bg colours in conditional formats.

Fix for fg/bg colours in conditional formats which are shared
with cell formats. Cell formats reverse the meaning of background
and foreground with solid fills. Conditional formats don't.
  • Loading branch information...
commit b4ac9ec6d210fdb80823a1a10cfc4434405a02f2 1 parent 38223d0
@jmcnamara authored
View
7 lib/Excel/Writer/XLSX/Package/Styles.pm
@@ -512,6 +512,13 @@ sub _write_fill {
my $bg_color = $format->{_bg_color};
my $fg_color = $format->{_fg_color};
+ # Colors for dxf formats are handled differently from normal formats since
+ # the normal format reverses the meaning of BG and FG for solid fills.
+ if ( $dxf_format ) {
+ $bg_color = $format->{_dxf_bg_color};
+ $fg_color = $format->{_dxf_fg_color};
+ }
+
my @patterns = qw(
none
View
42 lib/Excel/Writer/XLSX/Workbook.pm
@@ -1166,6 +1166,20 @@ sub _prepare_fills {
$fills{'0:0:0'} = 0;
$fills{'17:0:0'} = 1;
+
+ # Store the DXF colours separately since them may be reversed below.
+ for my $format ( @{ $self->{_dxf_formats} } ) {
+ if ( $format->{_pattern}
+ || $format->{_bg_color}
+ || $format->{_fg_color} )
+ {
+ $format->{_has_dxf_fill} = 1;
+ $format->{_dxf_bg_color} = $format->{_bg_color};
+ $format->{_dxf_fg_color} = $format->{_fg_color};
+ }
+ }
+
+
for my $format ( @{ $self->{_xf_formats} } ) {
# The following logical statements jointly take care of special cases
@@ -1176,9 +1190,18 @@ sub _prepare_fills {
# a pattern they probably wanted a solid fill, so we fill in the
# defaults.
#
+ if ( $format->{_pattern} == 1
+ && $format->{_bg_color} ne '0'
+ && $format->{_fg_color} ne '0' )
+ {
+ my $tmp = $format->{_fg_color};
+ $format->{_fg_color} = $format->{_bg_color};
+ $format->{_bg_color} = $tmp;
+ }
+
if ( $format->{_pattern} <= 1
- && $format->{_bg_color} != 0
- && $format->{_fg_color} == 0 )
+ && $format->{_bg_color} ne '0'
+ && $format->{_fg_color} eq '0' )
{
$format->{_fg_color} = $format->{_bg_color};
$format->{_bg_color} = 0;
@@ -1186,13 +1209,14 @@ sub _prepare_fills {
}
if ( $format->{_pattern} <= 1
- && $format->{_bg_color} == 0
- && $format->{_fg_color} != 0 )
+ && $format->{_bg_color} eq '0'
+ && $format->{_fg_color} ne '0' )
{
$format->{_bg_color} = 0;
$format->{_pattern} = 1;
}
+
my $key = $format->get_fill_key();
if ( exists $fills{$key} ) {
@@ -1214,16 +1238,6 @@ sub _prepare_fills {
$self->{_fill_count} = $index;
- # For the DXF formats we only need to check if the properties have changed.
- for my $format ( @{ $self->{_dxf_formats} } ) {
-
- if ( $format->{_pattern}
- || $format->{_bg_color}
- || $format->{_fg_color} )
- {
- $format->{_has_dxf_fill} = 1;
- }
- }
}
View
86 t/regression/cond_format10.t
@@ -0,0 +1,86 @@
+###############################################################################
+#
+# Tests the output of Excel::Writer::XLSX against Excel generated files.
+#
+# reverse('©'), October 2011, John McNamara, jmcnamara@cpan.org
+#
+
+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 = 'cond_format10.xlsx';
+my $dir = 't/regression/';
+my $got_filename = $dir . $filename;
+my $exp_filename = $dir . 'xlsx_files/' . $filename;
+
+my $ignore_members = [];
+my $ignore_elements = { 'xl/workbook.xml' => ['<workbookView'], };
+
+
+###############################################################################
+#
+# Test the creation of a simple Excel::Writer::XLSX file with conditional
+# formatting.
+#
+use Excel::Writer::XLSX;
+
+my $workbook = Excel::Writer::XLSX->new( $got_filename );
+my $worksheet = $workbook->add_worksheet();
+
+my $format = $workbook->add_format( bold => 1, italic => 1 );
+
+$worksheet->write( 'A1', 'Hello', $format );
+
+
+$worksheet->write( 'B3', 10 );
+$worksheet->write( 'B4', 20 );
+$worksheet->write( 'B5', 30 );
+$worksheet->write( 'B6', 40 );
+
+$worksheet->conditional_formatting( 'B3:B6',
+ {
+ type => 'cell',
+ format => $format,
+ criteria => 'greater than',
+ value => 20
+ }
+);
+
+
+$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__
+
+
+
View
86 t/regression/cond_format11.t
@@ -0,0 +1,86 @@
+###############################################################################
+#
+# Tests the output of Excel::Writer::XLSX against Excel generated files.
+#
+# reverse('©'), October 2011, John McNamara, jmcnamara@cpan.org
+#
+
+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 = 'cond_format11.xlsx';
+my $dir = 't/regression/';
+my $got_filename = $dir . $filename;
+my $exp_filename = $dir . 'xlsx_files/' . $filename;
+
+my $ignore_members = [];
+my $ignore_elements = { 'xl/workbook.xml' => ['<workbookView'], };
+
+
+###############################################################################
+#
+# Test the creation of a simple Excel::Writer::XLSX file with conditional
+# formatting.
+#
+use Excel::Writer::XLSX;
+
+my $workbook = Excel::Writer::XLSX->new( $got_filename );
+my $worksheet = $workbook->add_worksheet();
+
+my $format = $workbook->add_format( bg_color => '#FFFF00', fg_color => '#FF0000', pattern => 1 );
+
+$worksheet->write( 'A1', 'Hello', $format );
+
+
+$worksheet->write( 'B3', 10 );
+$worksheet->write( 'B4', 20 );
+$worksheet->write( 'B5', 30 );
+$worksheet->write( 'B6', 40 );
+
+$worksheet->conditional_formatting( 'B3:B6',
+ {
+ type => 'cell',
+ format => $format,
+ criteria => 'greater than',
+ value => 20
+ }
+);
+
+
+$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__
+
+
+
View
86 t/regression/cond_format12.t
@@ -0,0 +1,86 @@
+###############################################################################
+#
+# Tests the output of Excel::Writer::XLSX against Excel generated files.
+#
+# reverse('©'), October 2011, John McNamara, jmcnamara@cpan.org
+#
+
+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 = 'cond_format12.xlsx';
+my $dir = 't/regression/';
+my $got_filename = $dir . $filename;
+my $exp_filename = $dir . 'xlsx_files/' . $filename;
+
+my $ignore_members = [];
+my $ignore_elements = { 'xl/workbook.xml' => ['<workbookView'], };
+
+
+###############################################################################
+#
+# Test the creation of a simple Excel::Writer::XLSX file with conditional
+# formatting.
+#
+use Excel::Writer::XLSX;
+
+my $workbook = Excel::Writer::XLSX->new( $got_filename );
+my $worksheet = $workbook->add_worksheet();
+
+my $format = $workbook->add_format( bg_color => '#FFFF00', fg_color => '#FF0000', pattern => 12 );
+
+$worksheet->write( 'A1', 'Hello', $format );
+
+
+$worksheet->write( 'B3', 10 );
+$worksheet->write( 'B4', 20 );
+$worksheet->write( 'B5', 30 );
+$worksheet->write( 'B6', 40 );
+
+$worksheet->conditional_formatting( 'B3:B6',
+ {
+ type => 'cell',
+ format => $format,
+ criteria => 'greater than',
+ value => 20
+ }
+);
+
+
+$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__
+
+
+
View
BIN  t/regression/xlsx_files/cond_format10.xlsx
Binary file not shown
View
BIN  t/regression/xlsx_files/cond_format11.xlsx
Binary file not shown
View
BIN  t/regression/xlsx_files/cond_format12.xlsx
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.