This is now possible. In the examples below I use cells A1:C1. Cell A1 contains the value of 1. Cell B1 contains the formula =A1 + 1
and cell C1 contains the formula =B1
. The active cell used is cell B2.
This example uses and modifies Microsoft's code on their documentation. Their documentation used precedents. So I've added additional example using dependents.
Note: If you do not use an active cell that has both precedents and dependents (like B1 in this case), the code will raise an error:
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
let range = context.workbook.getActiveCell();
let precedents = range.getPrecedents();
let dependents = range.getDependents();
let directPrecedents = range.getDirectPrecedents();
let directDependents = range.getDirectDependents();
range.load("address");
precedents.areas.load("address");
directPrecedents.areas.load("address");
dependents.areas.load("address");
directDependents.areas.load("address");
await context.sync();
for (let i = 0; i < precedents.areas.items.length; i++) {
console.log(` ${precedents.areas.items[i].address}`);
}
for (let i = 0; i < directPrecedents.areas.items.length; i++) {
console.log(` ${directPrecedents.areas.items[i].address}`);
}
for (let i = 0; i < dependents.areas.items.length; i++) {
console.log(` ${dependents.areas.items[i].address}`);
}
for (let i = 0; i < directDependents.areas.items.length; i++) {
console.log(` ${directDependents.areas.items[i].address}`);
}
});
}
/** 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);
}
}
the difference between the direct and non-direct methods is that direct only returns direct cell dependents / precedents. The non-direct methods also return indirect cell references. E.g. if cell B1 refers to A1 and cell C1 refers to B1, getDependents()
for cell A1 will return 2 cells for the direct (B1) and indirect (C1) references. Whereas getDirectDependents()
for cell A1 will only return 1 cell (B1)
You can read more here