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

hi all i am very new for perl i m trying to create some report in excel by using perl and i m almost sucessful except the following question , here are my codes
our $dest_book = Spreadsheet::WriteExcel->new("Report.xls"); my $diskusage = $dest_book->add_worksheet("01-diskcheck"); my $c3format = $dest_book->add_format(); my $lightgreen = $dest_book->set_custom_color(41,10,255,133); $c3format->set_valign('vcenter'); $c3format->set_align('center'); $c3format->set_border('1'); $c3format->set_bg_color($lightgreen); $diskusage->write('F2','=IF(E2<D2,"normal","failed")',$c3format); $diskusage->write('F3','=IF(E3<D3,"normal","failed")',$c3format); $diskusage->write('F4','=IF(E4<D4,"normal","failed")',$c3format); $dest_book->close();
i want to let excel autoselect cell's bg_color when its value is normal ,it turns green , alternative , it turns red. please guide me a way.

Replies are listed 'Best First'.
Re: set color base on cell's value in your Excel
by blindluke (Hermit) on Nov 12, 2015 at 14:11 UTC

    Welcome to the monastery, gbcbooks!

    What you are trying to do is called conditional formatting. You want to format the cells in a certain way (set background colour), when a certain condition is met.

    Unfortunately, the module you are using has very limited support for conditional formatting. Basically, you can only set the color tags for negative and positive values, like this:

    my $f_change = $workbook->add_format(); $f_change->set_num_format('[Green]0.0%;[Red]-0.0%;0.0%'); $worksheet->write(2, 3, -0.015, $f_change); # negative, will be printe +d in red

    If you want conditional formatting, I can only suggest that you switch to Excel::Writer::XLSX. It does exactly what you want, has excellent documentation, and a lot of examples, including an example for conditional formatting.

    Good luck!

    - Luke

      ok, one more question is Excel::Writer::XLSX modues's method simliar to Spreadsheet::WriteExcel?

        Even better. To quote the documentation:

        The Excel::Writer::XLSX module is a drop-in replacement for Spreadsheet::WriteExcel.

        It supports practically all the methods in Spreadsheet::WriteExcel, and it adds a lot of new ones, with familiar names and usage.

        For a list of differences, see Compatibility with Spreadsheet::WriteExcel section in the module docs.

        - Luke