1

Is there a method with office-js API to determine if conditional formatting has been applied to a cell or cell range? I see there is a method to add conditional formatting but I don't see a method to check for existence of the conditional formatting. If conditional formatting has been applied and it can be checked, can the formatting be read as well?

jmadd
  • 115
  • 1
  • 1
  • 6

1 Answers1

1

Yes, I think you could get it from here (the "Applies to" range) enter image description here You may get the "Apply to" range by the following code:

worksheetRange.conditionalFormats.getItemAt(0).getRange();

Here is the code sample for you to reference:

    const sheet = context.workbook.worksheets.getItem("Sample");
    const worksheetRange = sheet.getRange();
    worksheetRange.conditionalFormats.load("type");
    await context.sync();

    let cfRangePairs: { cf: Excel.ConditionalFormat; range: Excel.Range }[] = [];
    worksheetRange.conditionalFormats.items.forEach((item) => {
      cfRangePairs.push({
        cf: item,
        range: item.getRange().load("address")
      });
    });

    await context.sync();

    if (cfRangePairs.length > 0) {
      cfRangePairs.forEach((item) => {
        console.log(item.cf.type);
        console.log(item.range.address);

      });
    } else {
      console.log("No conditional formats applied.");
    }
  });
Raymond Lu
  • 2,178
  • 1
  • 6
  • 19