in reply to Re^2: Conditional Formatting with Spreadsheet::WriteExcel
in thread Conditional Formatting with Spreadsheet::WriteExcel

There are a couple of problems here.

The first is that you must create a unique format for each colour. Otherwise the format object will have the background colour of the last call to bg_colour. This is explained here.

Secondly, Spreadsheet::WriteExcel doesn't support a named colour called "pink"*. Thus the colour reverts to the default value of white. The colour closest to what you require is "magenta" (I think that this adheres to the Windows colour naming convention).

So changing your code around to account for these issues will give you something like this.

#!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("test.xls"); my $worksheet = $workbook->add_worksheet(); my $format_heading_yellow = $workbook->add_format(bg_color => 'yellow +' ); my $format_heading_pink = $workbook->add_format(bg_color => 'magent +a'); my $format_heading_blue = $workbook->add_format(bg_color => 'blue' + ); my $format_heading_green = $workbook->add_format(bg_color => 'green' + ); my $condition = "a"; my $format_heading = getFormat($condition); $worksheet->write("A1", "sometext", $format_heading); $condition = "c"; $format_heading = getFormat($condition); $worksheet->write("A2", "sometext", $format_heading); $condition = "d"; $format_heading = getFormat($condition); $worksheet->write("A3", "sometext", $format_heading); $condition = "b"; $format_heading = getFormat($condition); $worksheet->write("A4", "sometext", $format_heading); sub getFormat { $condition = shift; if ($condition eq 'a'){ return $format_heading_yellow; } elsif ($condition eq 'b'){ return $format_heading_pink; } elsif ($condition eq 'c'){ return $format_heading_blue; } else { return $format_heading_green; # Default color } }

--
John.

* Although it probably should.