How to use Range.ShowDependents in the xlwings API way
The ShowDependents member of the Range object in Excel’s object model is a method used to visually highlight cells that depend on the selected range. These dependent cells contain formulas that reference the specified range, meaning their calculated values rely on the data in that range. When invoked, Excel draws tracer arrows from the selected range pointing to all its direct dependents, aiding in auditing and understanding formula relationships within a spreadsheet. This is particularly useful for debugging complex formulas, checking data flow, and ensuring calculation integrity. Note that this method affects the visual display in the Excel interface and does not return a value programmatically; it’s primarily for user inspection.
In xlwings, the ShowDependents method can be accessed through the api property of a Range object, which provides direct access to the underlying Excel object model. The syntax follows the pattern of the Excel VBA object model but is adapted for Python. The method does not take any parameters. It’s called directly on a Range object to display arrows to its dependents. To remove these arrows later, you would use the ShowPrecedents method with a False argument or the ClearArrows method, but note that ShowDependents itself is for display only.
Syntax in xlwings:
range.api.ShowDependents()
Here, range is an xlwings Range object (e.g., sheet.range('A1')). The .api property exposes the native Excel Range object, allowing you to call the ShowDependents method. No arguments are required or accepted.
Code Example:
Consider an Excel workbook where cell B1 contains the formula =A1*2, and cell C1 contains =B1+10. Both B1 and C1 depend on A1, either directly or indirectly. The following xlwings script selects cell A1 and uses ShowDependents to draw arrows to its direct dependents (B1 in this case, as C1 is an indirect dependent of A1).
import xlwings as xw
# Connect to the active Excel instance and workbook
app = xw.apps.active
wb = app.books.active
sheet = wb.sheets['Sheet1']
# Define the range (cell A1)
target_range = sheet.range('A1')
# Call ShowDependents to display tracer arrows
target_range.api.ShowDependents()
# To later remove the arrows, you could use:
# target_range.api.ClearArrows()
# Or, to remove arrows from dependents specifically (though less common):
# target_range.api.ShowDependents(False) # Note: False argument works in VBA but may vary in xlwings; ClearArrows is more reliable.