I am trying to write some simple TypeScript code to change a conditional formatting rule in an Excel file. To repro my issue:
- Open a blank Excel file
- Enter 1 in A1
- Click “Home”, Conditional formatting”, “New Rule”
- Choose “Format only cells that contain”
- Select “Specific text” in the Format only cells with: drop-down on
the left - Select “Containing” in the second drop-down
- Enter 123 in the box.
- OK back to Excel.
- Open Script lab and import the code below and run it
- Click the Run button, the console should state 123, followed by 456.
- Click Conditional formatting, Manager Rules.
- You should see that the contains text rule is unchanged!
What is wrong with this code?
================Script lab code================
name: CheckCF
description: ''
host: EXCEL
api_set: {}
script:
content: |
$("#run").on("click", () => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
let cfRules = sheet.getRange("A1").conditionalFormats;
let cf = cfRules.getItemAt(0);
let containsTextCF = cf.textComparisonOrNullObject;
containsTextCF.load();
await context.sync();
console.log(containsTextCF.rule.text);
containsTextCF.rule.text = "456";
await context.sync();
console.log(containsTextCF.rule.text);
});
}
/** 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="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
[email protected]
@types/[email protected]
================End of Script lab code================