Office Scripts


link - learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-worksheet-functions


Calling built-in worksheet functions

Excel.run(function (context) { 
   var _sheet = context.workbook.worksheets.getActiveWorksheet();
   var _column = _sheet.getRange("A:A");
   var _rowMatching = context.workbook.functions.match("Better", _column, 0 /*exact match*/);
   _rowMatching.load("value");
   context.sync();
   console.log(_rowMatching.value);
   return context.sync();
}).catch(function (error) {
   console.log(error);
});

Identifying Cell Formatting

export const officeReady: Promise<{ 
   host: Office.HostType | null;
   platform: Office.PlatformType | null
   }> = new Promise((resolve) => {
Office.onReady() => {
await initBoldTracker();
const root = createRoot(document.getElementById("root")!);
root.render(<ExcelApp />);
});
});

const TRACK_RANGE = "A1:F20";
const NAME_BOLD = "propertiesBOLD";
const POLL_INTERVAL_MS = 2000;

let debounceTimer: number | null = null;
let updating = false;
let lastBoldSnapshot = "";

export async function initBoldTracker() {
try {
await registerEventHandlers();
} catch (e) {
console.warn("Could not register format/selection event handlers:", e);
}
await updateBoldNameConstant();
startPolling();
}

function startPolling() {
window.setInterval(() => {
updateBoldNameConstant().catch(console.error);
}, POLL_INTERVAL_MS);
}

function scheduleUpdate() {
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = window.setTimeout(() => {
updateBoldNameConstant().catch(console.error);
}, 150);
}

async function registerEventHandlers() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();

sheet.onSelectionChanged.add(async () => {
scheduleUpdate();
});

await context.sync();
});
}

async function updateBoldNameConstant() {
if (updating) return;
updating = true;

try {
await Excel.run(async (context) => {
const wb = context.workbook;
const sheet = wb.worksheets.getActiveWorksheet();
const target = sheet.getRange(TRACK_RANGE);

target.load(["rowCount", "columnCount"]);
await context.sync();

const cells: Excel.Range[] = [];
for (let r = 0; r < target.rowCount; r++) {
for (let c = 0; c < target.columnCount; c++) {
const cell = target.getCell(r, c);
cell.load("address");
cell.format.font.load("bold");
cells.push(cell);
}
}

await context.sync();

const boldAddrs: string[] = [];
for (const cell of cells) {
if (cell.format.font.bold === true) {
const a1 = cell.address.split("!")[1];
boldAddrs.push(a1);
}
}

const items = boldAddrs
.map((a) => `"${a.replaceAll(`"`, `""`)}"`)
.join(",");

const refersTo = items.length ? `={${items}}` : `={""}`;

if (refersTo === lastBoldSnapshot) return;
lastBoldSnapshot = refersTo;

// Delete existing name if present (check both workbook and sheet scope).
const wbName = wb.names.getItemOrNullObject(NAME_BOLD);
const sheetName = sheet.names.getItemOrNullObject(NAME_BOLD);
await context.sync();

if (!wbName.isNullObject) {
wbName.delete();
await context.sync();
}
if (!sheetName.isNullObject) {
sheetName.delete();
await context.sync();
}

sheet.names.add(NAME_BOLD, refersTo);
await context.sync();
});
} finally {
updating = false;
}
}

Creating user defined functions

link - learn.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-overview
[[Custom Functions]]


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext