【问题标题】:How to get an Excel cell fill color caused by conditional formatting using office-js如何使用office-js获取由条件格式引起的Excel单元格填充颜色
【发布时间】:2020-03-12 17:57:08
【问题描述】:

我正在使用执行不同类型错误检查的 Excel Javascript API 设置条件格式。例如,如果用户在 A1:A10 范围内输入“foo”或“bar”以外的任何内容,则条件格式会将单元格突出显示为红色。

我正在尝试编写一个函数,该函数返回 A1:A10 中以红色突出显示的所有单元格的列表而不再次循环遍历数据并检查值是否为“foo”或“酒吧”。我只想获取条件格式设置为红色的单元格列表。

当我使用 range.getCellProperties() 加载 format/fill/color 属性时,即使条件格式将单元格填充显示为红色,它也会返回白色 (#FFFFFF)。评估条件格式后如何获取单元格的填充颜色?

Click here for code 重现此问题。运行它的简单地方是 Script Lab。同时在下面发布代码:

await Excel.run(async (context) => {
        const sheet = context.workbook.worksheets.getActiveWorksheet();
        // setup
        var rangeWithValues = sheet.getRange("A1:A5");
        rangeWithValues.values = [["foo"], ["bar"], ["foo"], ["bar"], ["foo"]];
        var conditionalFormatRange = sheet.getRange("A1:A10");
        conditionalFormatRange.conditionalFormats.clearAll();
        var conditionalFormat = conditionalFormatRange.conditionalFormats.add(Excel.ConditionalFormatType.custom);
        conditionalFormat.custom.rule.formula = `=NOT(OR(A1="foo",A1="bar"))`;
        conditionalFormat.custom.format.fill.color = "red";
        await context.sync();
        // get cell props
        var rangeProps = conditionalFormatRange.getCellProperties({
          address: true,
          format: {
            fill: {
              color: true
            }
          }
        });
        await context.sync();
        // output vals
        rangeProps.value.forEach(rowProps => {
          rowProps.forEach(cellProp => {
            console.log("address: " + cellProp.address + "; fill color: " + cellProp.format.fill.color);
          })
        })
      }); 

【问题讨论】:

    标签: excel office-js office-addins excel-addins


    【解决方案1】:

    RangeFormat API 可以获取范围格式,但无法获取conditionalFormat 因为conditionalFormat没有改变真实范围格式,你可以尝试在A8的UI中改变背景颜色,无论你改变什么颜色,它都会显示作为红色(条件格式颜色)。

    因此,这将是一个检测范围显示格式的新请求,因此我建议您可以在 uservoice 中提交您的请求,并在https://officespdev.uservoice.com/forums/224641-feature-requests-and-feedback?category_id=163563 对此功能进行投票。并为此功能投票。感谢您的支持

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 2010-11-03
      • 2014-11-12
      • 2014-12-31
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多