Tables :: Conditional formatting WriteExcel based on a different cell value

I am trying to add conditional formatting in my excel sheet. Unfortunately, the examples on the Spreadsheet :: WriteExcel page are too simple, and I don't know how to do this.

I wanted to change the backgroud color of the row to the value of cell RC10. In excel I will add a formatting formula

=IF(RC10="xxxx";1;0)

I tried to do something like this in Spreadsheet :: WriteExcel:

my $detail_rest_fmt = $excel->add_format(font => "Calibri", size => 11, valign  => "vcenter", align => "right", border => 1);
$detail_rest_fmt->set_num_format("[Green]=IF(RC10=\"xxxx\";1;0);[Red]=IF(RC10=\"yyyyyy\";1;0)"); 

but without any effects.

+5
source share
4 answers

The bad news: I think this can hardly be done with Spreadsheet :: WriteExcel.

, Excel:: Writer:: XLSX. Spreadsheet:: WriteExcel. , : : WriteExcel . Excel:: Writer:: XLSX

, ( A1 RC10, , ):

#!/usr/bin/perl -w
use strict;
use Excel::Writer::XLSX;

my @matrix = (
    ['xxxx', '<-- Change the value in cell A1 to change the colour of row 4'],
    [qw(Redyard Kipling)],
    [qw(If--)],
    [qw(If you can keep your head when all about you)],
    [qw(Are losing theirs and blaming it on you;)],
);

writeSpreadsheet('conditional.formatting.xlsx', \@matrix);

sub writeSpreadsheet {
    my ($outFile, $matrix) = @_;
    my $MIN_COL_WIDTH = 5;
    my $MAX_COL_WIDTH = 35;
    my $workbook = Excel::Writer::XLSX->new($outFile);
    my $worksheet = $workbook->add_worksheet();
    my $redFormat = $workbook->add_format(font => 'Arial', color => 'red');
    my $greenFormat = $workbook->add_format(font => 'Arial', color => 'green', bold => 1);
    $worksheet->set_row(0, undef,
        $workbook->add_format(font => 'Arial', align => 'center', bold => 1));
    $worksheet->conditional_formatting('A4:Z4',
        {
            type => 'formula',
            criteria => '=$A$1 = "xxxx"',
            format => $greenFormat
        }
    );
    $worksheet->conditional_formatting('A4:Z4',
        {
            type => 'formula',
            criteria => '=$A$1 = "yyyyyy"',
            format => $redFormat
        }
    );
    foreach my $row (0 .. $#$matrix) {
        foreach my $col (0 .. $#{$matrix->[$row]}) {
            $worksheet->write($row, $col, $matrix->[$row][$col] || '');
        }
    }
}
+3
, . :: WriteExcel.

, API , Excel:: Writer:: XLSX .

. Excel:: Writer:: XLSX example.

+1

. - :

my $yyy = $excel->add_format(font => "Calibri", size => 11, valign  => "vcenter", align => "right", border => 1, border_color => "black", bg_color => $green);

for my $section (@sections) {
   for my $sector (@sectors) {
       my $xxxx = $excel->add_format(font => "Calibri", size => 11, valign  => "vcenter", align => "right", border => 1, border_color => "black", bg_color => $green);
            $sheet->conditional_formatting("A1", 
            {
                type => "formula",
                criteria => '=J4="T1"',
                format => $yyy
            });
   }
}

$yyy, ( excel backgroud) $xxxx, . $ yyy $xxxx , ?

+1

:

, , , - .

, $workbook->close() , , .

.

0
source

All Articles