diff --git a/playlists-prod/excel.yaml b/playlists-prod/excel.yaml index 2c720aafb..aabb42f46 100644 --- a/playlists-prod/excel.yaml +++ b/playlists-prod/excel.yaml @@ -1449,4 +1449,16 @@ https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/99-just-for-fun/color-wheel.yaml group: Just For Fun api_set: - ExcelApi: '1.4' \ No newline at end of file + ExcelApi: '1.4' +- id: excel-custom-functions-sync-conditional-format + name: Synchronous custom function - conditional formatting + fileName: synchronous-function-conditional-format.yaml + description: >- + Sets up sample data and a conditional format rule that uses a synchronous + custom function to highlight cells exceeding a target. Import and register + the "Synchronous custom function" sample first. + rawUrl: >- + https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/preview-apis/synchronous-function-conditional-format.yaml + group: Preview APIs + api_set: + ExcelApi: '1.6' \ No newline at end of file diff --git a/playlists/excel.yaml b/playlists/excel.yaml index bce322d98..9b857355b 100644 --- a/playlists/excel.yaml +++ b/playlists/excel.yaml @@ -1450,3 +1450,15 @@ group: Just For Fun api_set: ExcelApi: '1.4' +- id: excel-custom-functions-sync-conditional-format + name: Synchronous custom function - conditional formatting + fileName: synchronous-function-conditional-format.yaml + description: >- + Sets up sample data and a conditional format rule that uses a synchronous + custom function to highlight cells exceeding a target. Import and register + the "Synchronous custom function" sample first. + rawUrl: >- + https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/preview-apis/synchronous-function-conditional-format.yaml + group: Preview APIs + api_set: + ExcelApi: '1.6' diff --git a/samples/excel/preview-apis/synchronous-function-conditional-format.yaml b/samples/excel/preview-apis/synchronous-function-conditional-format.yaml new file mode 100644 index 000000000..be2f7fad3 --- /dev/null +++ b/samples/excel/preview-apis/synchronous-function-conditional-format.yaml @@ -0,0 +1,129 @@ +order: 12 +id: excel-custom-functions-sync-conditional-format +name: Synchronous custom function - conditional formatting +description: Sets up sample data and a conditional format rule that uses a synchronous custom function to highlight cells exceeding a target. Import and register the "Synchronous custom function" sample first. +host: EXCEL +api_set: + ExcelApi: '1.6' +script: + content: |- + document.getElementById("run").addEventListener("click", () => tryCatch(run)); + + async function run() { + await Excel.run(async (context) => { + const sheet = context.workbook.worksheets.getActiveWorksheet(); + + // A1:A2 — label and target threshold. + sheet.getRange("A1").values = [["Target"]]; + sheet.getRange("A2").values = [[70]]; + sheet.getRange("A1:A2").format.font.bold = true; + + // B1:B8 — header and sample scores. + sheet.getRange("B1").values = [["Score"]]; + sheet.getRange("B1").format.font.bold = true; + sheet.getRange("B2:B8").values = [[45], [82], [91], [60], [78], [55], [88]]; + + // Apply a conditional format rule on B2:B8. + // The formula uses the synchronous custom function GETCELLVALUE to read + // the target from A2 during calculation. Scores exceeding the target + // are highlighted. + // NOTE: Replace SCRIPTLAB.SYNCHRONOUSCUSTOMFUNCTION with the namespace + // shown in the Custom Functions dashboard after registering the + // prerequisite snippet. + const range = sheet.getRange("B2:B8"); + const cf = range.conditionalFormats.add(Excel.ConditionalFormatType.custom); + cf.custom.rule.formula = + "=B2>SCRIPTLAB.SYNCHRONOUSCUSTOMFUNCTION.GETCELLVALUE(\"$A$2\")"; + cf.custom.format.fill.color = "#FFC7CE"; + cf.custom.format.font.color = "#9C0006"; + + await context.sync(); + console.log("Done. Scores in B2:B8 that exceed the target in A2 are highlighted."); + console.log("Change A2 to see the conditional format update live."); + }); + } + + /** 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: |- +
+

+ This snippet sets up sample data and applies a conditional format rule that + uses the GETCELLVALUE synchronous custom function to highlight + scores exceeding a target value. +

+

+ Prerequisite: Import and register the + Synchronous custom function sample first via the + Custom Functions dashboard. +

+
+
+

Try it out

+

Press Set up data and format to populate the sheet and apply the rule.

+ +
+ 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; + } + + code { + background: #f4f4f4; + padding: 1px 4px; + border-radius: 2px; + font-family: Consolas, monospace; + } + + button { + background-color: #f0f0f0; + color: #333333; + border: 1px solid #8a8a8a; + padding: 8px 16px; + font-size: 14px; + cursor: pointer; + border-radius: 2px; + margin-left: 20px; + min-width: 80px; + } + + button:hover { + background-color: #e0e0e0; + } + + button:active { + background-color: #d0d0d0; + } + language: css +libraries: |- + https://appsforoffice.microsoft.com/lib/beta/hosted/office.js + https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/office-js-preview/index.d.ts diff --git a/view-prod/excel.json b/view-prod/excel.json index b0cff1a24..1360aa052 100644 --- a/view-prod/excel.json +++ b/view-prod/excel.json @@ -149,5 +149,6 @@ "excel-just-for-fun-gradient": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/99-just-for-fun/gradient.yaml", "excel-just-for-fun-path-finder-game": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/99-just-for-fun/path-finder-game.yaml", "excel-just-for-fun-tetrominos": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/99-just-for-fun/tetrominos.yaml", - "excel-just-for-fun-color-wheel": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/99-just-for-fun/color-wheel.yaml" + "excel-just-for-fun-color-wheel": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/99-just-for-fun/color-wheel.yaml", + "excel-custom-functions-sync-conditional-format": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/preview-apis/synchronous-function-conditional-format.yaml" } \ No newline at end of file diff --git a/view/excel.json b/view/excel.json index 562e4b196..875736cb0 100644 --- a/view/excel.json +++ b/view/excel.json @@ -149,5 +149,6 @@ "excel-just-for-fun-gradient": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/99-just-for-fun/gradient.yaml", "excel-just-for-fun-path-finder-game": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/99-just-for-fun/path-finder-game.yaml", "excel-just-for-fun-tetrominos": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/99-just-for-fun/tetrominos.yaml", - "excel-just-for-fun-color-wheel": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/99-just-for-fun/color-wheel.yaml" + "excel-just-for-fun-color-wheel": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/99-just-for-fun/color-wheel.yaml", + "excel-custom-functions-sync-conditional-format": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/preview-apis/synchronous-function-conditional-format.yaml" } \ No newline at end of file