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.


In reply to Re^3: Conditional Formatting with Spreadsheet::WriteExcel by jmcnamara
in thread Conditional Formatting with Spreadsheet::WriteExcel by shilpam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.