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

I tried ur code with some modification and it sets the bg color as "white" even if certain conditions are met. What approach do I try? I want that the background color of the cells should be displayed as per the condition.
#!/usr/bin/perl use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("test.xls"); my $worksheet = $workbook->add_worksheet(); my $format_heading = $workbook->add_format(); my $condition = "a"; getFormat($condition); $worksheet->write("A1", "sometext", $format_heading); $condition = "c"; getFormat($condition); $worksheet->write("A2", "sometext", $format_heading); $condition = "d"; getFormat($condition); $worksheet->write("A3", "sometext", $format_heading); $condition = "b"; getFormat($condition); $worksheet->write("A4", "sometext", $format_heading); sub getFormat { $condition = shift; if ($condition eq 'a'){ $format_heading->set_bg_color('yellow'); } elsif ($condition eq 'b'){ $format_heading->set_bg_color('pink'); } elsif ($condition eq 'c'){ $format_heading->set_bg_color('blue'); } else { $format_heading->set_bg_color('green'); # Default color } }

Replies are listed 'Best First'.
Re^3: Conditional Formatting with Spreadsheet::WriteExcel
by jmcnamara (Monsignor) on Jan 04, 2006 at 10:14 UTC
    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.

Re^3: Conditional Formatting with Spreadsheet::WriteExcel
by prasadbabu (Prior) on Jan 04, 2006 at 09:34 UTC

    shilpam, the problem is you are using 'pink' color, which sets white color, you try someother color in that condition. But as per your logic the condition which get satisfied at last will be set as background color for rest.

    If i understood your requirement correctly, the following coding exactly doing your job. But as far as efficiency of the code, i cant guarantee you. You can minimize the following code.

    use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("test.xls"); my $worksheet = $workbook->add_worksheet(); my $format_heading1 = $workbook->add_format(); my $format_heading2 = $workbook->add_format(); my $format_heading3 = $workbook->add_format(); my $format_heading4 = $workbook->add_format(); my $condition = "a"; getFormat1($condition); $worksheet->write("A1", "sometext", $format_heading1); $condition = "c"; getFormat2($condition); $worksheet->write("A2", "sometext", $format_heading2); $condition = "d"; getFormat3($condition); $worksheet->write("A3", "sometext", $format_heading3); $condition = "b"; getFormat4($condition); $worksheet->write("A4", "sometext", $format_heading4); sub getFormat1 { $condition = shift; if ($condition eq 'a'){ $format_heading1->set_bg_color('yellow'); } elsif ($condition eq 'b'){ $format_heading1->set_bg_color('red'); } elsif ($condition eq 'c'){ $format_heading1->set_bg_color('blue'); } else { $format_heading1->set_bg_color('green'); # Default color } } sub getFormat2 { $condition = shift; if ($condition eq 'a'){ $format_heading2->set_bg_color('yellow'); } elsif ($condition eq 'b'){ $format_heading2->set_bg_color('red'); } elsif ($condition eq 'c'){ $format_heading2->set_bg_color('blue'); } else { $format_heading2->set_bg_color('green'); # Default color } } sub getFormat3 { $condition = shift; if ($condition eq 'a'){ $format_heading3->set_bg_color('yellow'); } elsif ($condition eq 'b'){ $format_heading3->set_bg_color('red'); } elsif ($condition eq 'c'){ $format_heading3->set_bg_color('blue'); } else { $format_heading3->set_bg_color('green'); # Default color } } sub getFormat4 { $condition = shift; if ($condition eq 'a'){ $format_heading4->set_bg_color('yellow'); } elsif ($condition eq 'b'){ $format_heading4->set_bg_color('red'); } elsif ($condition eq 'c'){ $format_heading4->set_bg_color('blue'); } else { $format_heading4->set_bg_color('green'); # Default color } }

    Cheers!!! :)

    Prasad