使用公式处理条件格式时的 ClosedXML - 公式不正确

ClosedXML when Working With Conditional Formats using formulas - the formula is incorrect

我在 ClosedXML 中使用条件格式,但遇到了 2 个问题。首先,如果我将条件设置为基于这样的值:

> RangeToAdd.AddConditionalFormat().WhenLessThan(0).Fill.SetBackgroundColor(XLColor.Red).Font.SetFontColor(XLColor.Red);

但是,当我需要将其设置为相关单元格时,它不起作用。这是我试过的:

RangeToAdd.AddConditionalFormat().WhenLessThan("\"=RC[-19]\"").Fill.SetBackgroundColor(XLColor.Yellow);
RangeToAdd.AddConditionalFormat().WhenGreaterThan("\"=RC[-19]+RC[-18]+RC[-17]\"").Fill.SetBackgroundColor(XLColor.BabyBlue);

它不起作用。它添加了 ="=,然后是不正确的公式。我按照文档 here 中的说明进行了操作,并且在不转义引号的情况下进行了尝试。

另一个问题很小,但我想不通。如何设置条件为真时停止。

您添加的引号太多:根据文档,它只是

WhenLessThan("=RC[-19]") // But Excel can't read it unfortunately 

可能的解决方法

WhenLessThan("=" + RC(RangeToAdd,0,-19))

同样

WhenGreaterThan("=" + RC(RangeToAdd,0,-19) + "+" + RC(RangeToAdd,0,-18) + "+" + RC(RangeToAdd,0,-17))

使用助手

    static string RC(IXLRange range, int r, int c)
    {
        return range.FirstCell().CellBelow(r).CellRight(c).Address.ToString(XLReferenceStyle.A1);
    }