|
| 1 | +--- |
| 2 | +page_type: sample |
| 3 | +urlFragment: excel-custom-functions-sync-conditional-format |
| 4 | +products: |
| 5 | + - office-excel |
| 6 | + - office |
| 7 | +languages: |
| 8 | + - typescript |
| 9 | +extensions: |
| 10 | + contentType: samples |
| 11 | + technologies: |
| 12 | + - Add-ins |
| 13 | + createdDate: 03/19/2026 4:00:00 PM |
| 14 | +description: "Shows how to use a synchronous custom function in a conditional format rule to highlight cells that exceed a target value." |
| 15 | +--- |
| 16 | + |
| 17 | +# Use Synchronous Custom Functions with Conditional Formatting in Excel |
| 18 | + |
| 19 | +<img src="./assets/thumbnail.png" width="800" alt="A workbook with conditional formatting powered by a synchronous custom function."> |
| 20 | + |
| 21 | +This sample shows how to use a synchronous custom function inside a conditional format rule to highlight cells dynamically. You'll learn how to: |
| 22 | + |
| 23 | +- Create synchronous custom functions in Excel |
| 24 | +- Apply conditional format rules that use custom functions |
| 25 | + |
| 26 | +## How to run this sample |
| 27 | + |
| 28 | +### Prerequisites |
| 29 | + |
| 30 | +- Download and install [Visual Studio Code](https://visualstudio.microsoft.com/downloads/). |
| 31 | +- Install the latest version of the [Office Add-ins Development Kit](https://marketplace.visualstudio.com/items?itemName=msoffice.microsoft-office-add-in-debugger) into Visual Studio Code. |
| 32 | +- Node.js (the latest LTS version). Visit the [Node.js site](https://nodejs.org/) to download and install the right version for your operating system. To verify if you've already installed these tools, run the commands `node -v` and `npm -v` in your terminal. |
| 33 | +- Microsoft Office connected to a Microsoft 365 subscription. You might qualify for a Microsoft 365 E5 developer subscription through the [Microsoft 365 Developer Program](https://developer.microsoft.com/microsoft-365/dev-program), see [FAQ](https://learn.microsoft.com/office/developer-program/microsoft-365-developer-program-faq#who-qualifies-for-a-microsoft-365-e5-developer-subscription-) for details. Alternatively, you can [sign up for a 1-month free trial](https://www.microsoft.com/microsoft-365/try?rtc=1) or [purchase a Microsoft 365 plan](https://www.microsoft.com/microsoft-365/buy/compare-all-microsoft-365-products). |
| 34 | + |
| 35 | +### Run the add-in from the Office Add-ins Development Kit |
| 36 | + |
| 37 | +1. Create a new project with the sample code. |
| 38 | + |
| 39 | + Open the Office Add-ins Development Kit extension and view samples in the **Sample gallery**. Select the **Create** button in the top-right corner of the sample page. The new project will open in a second Visual Studio Code window. Close the original VSC window. |
| 40 | + |
| 41 | +1. Open the Office Add-ins Development Kit. |
| 42 | + |
| 43 | + Select the <img src="./assets/Icon_Office_Add-ins_Development_Kit.png" width="30" alt="The Office Add-ins Development Kit icon in the activity bar of VSCode"/> icon in the **Activity Bar** to open the extension. |
| 44 | + |
| 45 | +1. Preview Your Office Add-in (F5). |
| 46 | + |
| 47 | + Select **Preview Your Office Add-in(F5)** to launch the add-in and debug the code. In the drop down menu, select the option **Desktop (Edge Chromium)**. |
| 48 | + |
| 49 | + <img src="./assets/devkit_preview.png" width="500" alt="The 'Preview your Office Add-in' option in the Office Add-ins Development Kit's task pane."/> |
| 50 | + |
| 51 | + The extension checks that the prerequisites are met before debugging starts. The terminal will alert you to any issues with your environment. After this process, the Excel desktop application launches and opens a new workbook with the sample add-in sideloaded. The add-in automatically opens as well. |
| 52 | + |
| 53 | + If this is the first time that you have sideloaded an Office Add-in on your computer (or the first time in over a month), you may be prompted to delete an old certificate and/or to install a new one. Agree to both prompts. The first run requires installing dependency of this project, which might take 2~3 minutes or longer. During this time, there might be a dialog pop up at the lower right of the VSC screen. You should not interact with this dialog before the Office application launched. |
| 54 | + |
| 55 | +1. Stop Previewing Your Office Add-in. |
| 56 | + |
| 57 | + Once you are finished testing and debugging the add-in, select the <img src="./assets/Icon_Office_Add-ins_Development_Kit.png" width="30" alt="The Office Add-ins Development Kit icon in the activity bar of VSCode"/> icon and then select **Stop Previewing Your Office Add-in**. This closes the web server and removes the add-in from the registry and cache. |
| 58 | + |
| 59 | +### Run the add-in locally |
| 60 | + |
| 61 | +1. Open a terminal in the `Samples/excel-custom-functions-sync-conditional-format` folder. |
| 62 | +2. Run `npm install`. |
| 63 | +3. Run `npm start`. |
| 64 | + |
| 65 | +## Use the sample add-in |
| 66 | + |
| 67 | +1. Select the **Set up data and format** button in the task pane. |
| 68 | +2. The add-in populates cell A1 with "Target", A2 with the target value (70), and B1:B8 with a header and sample scores. |
| 69 | +3. A conditional format rule is applied to B2:B8 using the formula `=B2>SyncCFConditionalFormat.GETCELLVALUE("$A$2")`. |
| 70 | +4. Scores that exceed the target in A2 are highlighted in red. |
| 71 | +5. Change the value in A2 to see the conditional format highlights update live. |
| 72 | + |
| 73 | +> **Note:** Synchronous custom functions are currently in public preview and require the beta Office JavaScript API. Do not use in production add-ins. |
| 74 | +
|
| 75 | +## Explore sample files |
| 76 | + |
| 77 | +These are the important files in the sample project. |
| 78 | + |
| 79 | +``` |
| 80 | +| .eslintrc.json |
| 81 | +| .gitignore |
| 82 | +| .vscode/ |
| 83 | +| | extensions.json |
| 84 | +| | launch.json Launch and debug configurations |
| 85 | +| | settings.json |
| 86 | +| | tasks.json |
| 87 | +| assets/ Static assets, such as images |
| 88 | +| babel.config.json |
| 89 | +| manifest*.xml Manifest file |
| 90 | +| package.json |
| 91 | +| README.md |
| 92 | +| RUN_WITH_EXTENSION.md |
| 93 | +| src/ Add-ins source code |
| 94 | +| | taskpane/ |
| 95 | +| | | taskpane.html Task pane entry HTML |
| 96 | +| | | taskpane.js Add API calls and logic here |
| 97 | +| | functions/ |
| 98 | +| | | functions.ts Custom function TypeScript |
| 99 | +| tsconfig.json |
| 100 | +| webpack.config.js Webpack config |
| 101 | +``` |
| 102 | + |
| 103 | +## Details |
| 104 | + |
| 105 | +### Create the synchronous custom function |
| 106 | + |
| 107 | +The `getCellValue` function in [functions.ts](src/functions/functions.ts) is marked with `@supportSync` so that it evaluates during Excel's calculation cycle. It creates a new `Excel.RequestContext`, calls `setInvocation` with the invocation parameter, and reads a cell value. For more details see the `getCellValue` function in [functions.ts](src/functions/functions.ts). |
| 108 | + |
| 109 | +### Set up data and apply the conditional format rule |
| 110 | + |
| 111 | +The [taskpane.js](src/taskpane/taskpane.js) file: |
| 112 | + |
| 113 | +1. Populates sample data with a target threshold in A2 and scores in B2:B8. |
| 114 | +1. Adds a conditional format rule on B2:B8 using the formula `=B2>SyncCFConditionalFormat.GETCELLVALUE("$A$2")`. |
| 115 | +1. Highlights scores that exceed the target with a red fill. |
| 116 | + |
| 117 | +When you change the value in A2, the conditional format updates live because the synchronous custom function re-evaluates during Excel's calculation. |
| 118 | + |
| 119 | +## Troubleshooting |
| 120 | + |
| 121 | +If you have problems running the sample, take the following steps. |
| 122 | + |
| 123 | +- Close any open instances of Excel. |
| 124 | +- Close the previous web server started for the sample with the **Stop Previewing Your Office Add-in** Office Add-ins Development Kit extension option. |
| 125 | +- Try to run the sample again. |
| 126 | + |
| 127 | +If you still have problems, see [troubleshoot development errors](https://learn.microsoft.com//office/dev/add-ins/testing/troubleshoot-development-errors) or [create a GitHub issue](https://aka.ms/officedevkitnewissue) and we'll help you. |
| 128 | + |
| 129 | +For information on running the sample on Excel on the web, see [Sideload Office Add-ins to Office on the web](https://learn.microsoft.com/office/dev/add-ins/testing/sideload-office-add-ins-for-testing). |
| 130 | + |
| 131 | +For information on debugging on older versions of Office, see [Debug add-ins using developer tools in Microsoft Edge Legacy](https://learn.microsoft.com/office/dev/add-ins/testing/debug-add-ins-using-devtools-edge-legacy). |
| 132 | + |
| 133 | +## Make code changes |
| 134 | + |
| 135 | +Once you understand the sample, make it your own! All the information about Office Add-ins is found in our [official documentation](https://learn.microsoft.com/office/dev/add-ins/overview/office-add-ins). You can also explore more samples in the Office Add-ins Development Kit. Select **View Samples** to see more samples of real-world scenarios. |
| 136 | + |
| 137 | +If you edit the manifest as part of your changes, use the **Validate Manifest File** option in the Office Add-ins Development Kit. This shows you any errors in the manifest syntax. |
| 138 | + |
| 139 | +## Engage with the team |
| 140 | + |
| 141 | +Did you experience any problems with the sample? [Create an issue](https://github.com/OfficeDev/Office-Add-in-samples/issues/new/choose) and we'll help you out. |
| 142 | + |
| 143 | +Want to learn more about new features and best practices for the Office platform? [Join the Microsoft Office Add-ins community call](https://learn.microsoft.com/office/dev/add-ins/overview/office-add-ins-community-call). |
| 144 | + |
| 145 | +## Copyright |
| 146 | + |
| 147 | +Copyright (c) 2026 Microsoft Corporation. All rights reserved. |
| 148 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |
| 149 | + |
| 150 | +<img src="https://pnptelemetry.azurewebsites.net/pnp-officeaddins/samples/excel-custom-functions-sync-conditional-format" /> |
0 commit comments