Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Conditional Formatting with Spreadsheet::WriteExcel

by shilpam (Sexton)
on Jan 03, 2006 at 06:25 UTC ( [id://520507]=perlquestion: print w/replies, xml ) Need Help??

shilpam has asked for the wisdom of the Perl Monks concerning the following question:

I am using Spreadsheet::WriteExcel in my code. I need conditional formatting in the sense that if certain conditions are fulfilled the background color should change. My code goes something like this -
But, it is not working. Can you pl. help me with it? Thanks in advance...
my $workbook = Spreadsheet::WriteExcel->new($file_name); my $worksheet = $workbook->add_worksheet(); my $format_heading = $workbook->add_format(); my $condition = "some value"; getFormat($condition); $worksheet->write($row_position, $column_position, $some_info,$format_ +heading); sub getFormat { $condition = shift; if (Condition 1){ $format_heading->set_bgcolor('yellow'); } elsif (Condition 2){ $format_heading->set_bgcolor('pink'); $format_heading->set_color('red'); } elsif (Condition 3){ $format_heading->set_bgcolor('blue'); } else { $format_heading->set_bgcolor('white'); # Default color } }

Replies are listed 'Best First'.
Re: Conditional Formatting with Spreadsheet::WriteExcel
by prasadbabu (Prior) on Jan 03, 2006 at 06:55 UTC

    Hi shilpam, you change 'set_bgcolor' method into 'set_bg_color'. The following code works for me.

    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); sub getFormat { $condition = shift; if ($condition eq 'a'){ $format_heading->set_bg_color('yellow'); } elsif ($condition eq 'b'){ $format_heading->set_bg_color('pink'); $format_heading->set_color('red'); } elsif ($condition eq 'c'){ $format_heading->set_bg_color('blue'); } else { $format_heading->set_bg_color('white'); # Default color } }

    Prasad

      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 } }
        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.

        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

Re: Conditional Formatting with Spreadsheet::WriteExcel
by thor (Priest) on Jan 03, 2006 at 22:38 UTC
    I need conditional formatting in the sense that if certain conditions are fulfilled the background color should change.
    The thing is that the way you've structured your code, it will only happen at spreadsheet generation time, not when the user opens up the spreadsheet and changes the value. If you look at the docs for Spreadsheet::WriteExcel and search for "condtional formatting", you'll see a more flexible solution. It may or may not work for you depending on what your conditions are, but my guess is that it will.

    thor

    The only easy day was yesterday

Re: Conditional Formatting with Spreadsheet::WriteExcel
by davidrw (Prior) on Jan 03, 2006 at 22:10 UTC
    If you're going to use that global variable approach, then make sure that every rung of the if/elsif ladder has a set_color() call in it .. otherwise once you hit condition 2, everything after that will be red.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://520507]
Approved by prasadbabu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-19 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found