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


In reply to Re^3: Conditional Formatting with Spreadsheet::WriteExcel by prasadbabu
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.