【问题标题】:Spreadsheet::WriteExcel conditional formatting based on other cell valueSpreadsheet::WriteExcel 基于其他单元格值的条件格式
【发布时间】:2012-06-23 09:46:03
【问题描述】:

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

但没有任何效果。

【问题讨论】:

    标签: perl excel


    【解决方案1】:

    坏消息是我认为使用 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] || '');
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      安东,是对的。 Spreadsheet::WriteExcel 并不真正支持条件格式。

      不过,更新的 API 兼容替代品Excel::Writer::XLSX 提供了一组丰富的条件格式功能。

      请参阅 Excel::Writer::XLSX 中更新的 Conditional Formatting 文档和此 example

      【讨论】:

      【解决方案3】:

      conditional_formatting 的行为很奇怪。 我得到了类似的东西:

      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 中有设置的图案填充而不是背景颜色) 当我使用 $xxxx 时,它工作正常。 $yyy 和 $xxxx 是一样的,为什么不行呢?

      【讨论】:

      • 看来我以前不能使用 $format ,它必须是“新鲜的”新的......这没有意义。
      【解决方案4】:

      第二个问题:

      由于范围问题,您可能会遇到格式在您开始使用它们之前被垃圾收集的问题。

      如果是范围问题,请尝试在程序末尾添加 $workbook-&gt;close() 以查看是否可以解决问题。

      否则我们需要一个更完整的示例程序来调试它。

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-29
        • 1970-01-01
        • 2023-02-15
        相关资源
        最近更新 更多