Skip to content

[Excel] Undoing conditionalFormatRule.delete() causes the format rule to be corrupted. #6396

@DavidVanDeursen

Description

@DavidVanDeursen

When undoing a conditionalFormatRule.delete() action using ctrl+z, loading any property of the format rule or calling any of its methods causes the following error Uncaught (in promise) RichApi.Error: The requested resource doesn’t exist.

Reopening the workbook fixes the issue.

Your Environment

  • Platform: PC
  • Host: Excel
  • Office version number: 2511
  • Operating System: Windows 11

Expected behavior

When undoing a conditionalFormatRule.delete() , the format rule should be usable.

Current behavior

Undoing conditionalFormatRule.delete() makes the conditional format rule that was added back unsuable as it causes a crash.

Steps to reproduce

  1. Load the code snippet below.
  2. Press the setup button to add a conditional format rule to the sheet.
  3. Press the clear button to delete it.
  4. Press CTRL+Z to undo the delete action.
  5. Press the check button to try and load a property of the format rule and observe that it throws an error.

Link to live example(s)

Script lab code:

name: Undo delete conditional format error.
description: A snippet to show how to reproduce a conditional format error
host: EXCEL
api_set: {}
script:
  content: >
    document.getElementById("setup").addEventListener("click", () =>
    tryCatch(setup));

    document.getElementById("clear").addEventListener("click", () =>
    tryCatch(clear));

    document.getElementById("check").addEventListener("click", () =>
    tryCatch(check));


    async function setup() {
      Excel.run(async (context) => {
        const worksheet = context.workbook.worksheets.getFirst();
        const range = worksheet.getRange("A1");

        const conditionalFormat = range.conditionalFormats.add(Excel.ConditionalFormatType.custom);
        conditionalFormat.custom.format.fill.color = "#FEF07B";
        conditionalFormat.custom.rule.formula = "=TRUE";
        await context.sync();
      });
    }


    async function clear() {
      Excel.run(async (context) => {
        const worksheet = context.workbook.worksheets.getFirst();
        const range = worksheet.getRange();
        range.conditionalFormats.load("items");
        await context.sync();

        for (let conditionalFormat of range.conditionalFormats.items) {
          conditionalFormat.delete();
          await context.sync();
        }
      });
    }


    async function check() {
      Excel.run(async (context) => {
        const worksheet = context.workbook.worksheets.getFirst();
        const range = worksheet.getRange();
        range.conditionalFormats.load("items");
        await context.sync();

        for (let conditionalFormat of range.conditionalFormats.items) {
          conditionalFormat.load("custom/rule");
          console.log(conditionalFormat);
          await context.sync().catch((error) => console.log(error));
        }
      });
    }


    /** Default helper for invoking an action and handling errors. */

    async function tryCatch(callback) {
      try {
        await callback();
      } catch (error) {
        // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
        console.error(error);
      }
    }
  language: typescript
template:
  content: "<button id=\"setup\">Setup</button>\r\n<button id=\"clear\">Clear</button>\r\n<button id=\"check\">Check</button>"
  language: html
style:
  content: |-
    body {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        font-size: 14px;
        line-height: 1.5;
        padding: 10px;
    }

    section {
        margin-bottom: 20px;
    }

    h3 {
        margin-top: 0;
        margin-bottom: 10px;
        font-size: 16px;
    }

    p {
        margin: 0 0 10px 0;
    }

    button {
        background-color: #f0f0f0;
        color: #333333;
        border: 1px solid #8a8a8a;
        padding: 8px 16px;
        font-size: 14px;
        cursor: pointer;
        border-radius: 2px;
        margin-left: 20px;
        margin-bottom: 5px;
        min-width: 80px;
        display: block;
    }

    button:hover {
        background-color: #e0e0e0;
    }

    button:active {
        background-color: #d0d0d0;
    }

    input {
        padding: 8px;
        margin: 5px 0;
        border: 1px solid #ccc;
        border-radius: 2px;
        font-size: 14px;
    }

    .header {
        text-align: center;
        background-color: #f3f2f1;
        padding: 10px;
    }
  language: css
libraries: >-
  https://appsforoffice.microsoft.com/lib/1/hosted/office.js

  https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/office-js/index.d.ts

Context

We are making an addin to automatically add/remove conditional formats to highlight interesting information in a worksheet. If a user presses CTRL+Z whilst we have just removed conditional formats this error occurs. The only way to fix this is for a user to reopen their spreadsheet.

Metadata

Metadata

Labels

Area: ExcelIssue related to Excel add-insStatus: fix pendingFix is complete but awaiting prod deployment

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions