gpt4 book ai didi

perl - Spreadsheet::WriteExcel 基于其他单元格值的条件格式

转载 作者:行者123 更新时间:2023-12-02 07:46:07 25 4
gpt4 key购买 nike

我正在尝试在 Excel 工作表中添加条件格式。不幸的是,Spreadsheet::WriteExcel 页面上的示例太简单,我不知道该怎么做。

我想通过 RC10 单元格值更改行背景颜色。在Excel中我将添加格式化公式

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

我尝试在 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)");

但没有任何效果。

最佳答案

坏消息是我认为这很难用 Spreadsheet::WriteExcel 来完成。

好消息是可以使用 Excel::Writer::XLSX 轻松完成。它恰好是 Spreadsheet::WriteExcel 的一种后代。请阅读文章: Spreadsheet::WriteExcel is dead. Long live 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] || '');
}
}
}

关于perl - Spreadsheet::WriteExcel 基于其他单元格值的条件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11133403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com