﻿ Documents for Excel, .NET Edition Documentation - Precedents and Dependents
Documents for Excel, .NET Edition Documentation
Manage Formulas / Precedents and Dependents
In This Topic
Precedents and Dependents
In This Topic

Sometimes, in worksheets containing lots of formulas, it becomes difficult to identify which cell values or ranges are taken into consideration while doing calculations or how the result is calculated. Also, which cells are impacted if a cell value is modified. Hence, comes the need for precedent and dependent cells or ranges. GcExcel library provides GetPrecedents and GetDependents methods in the IRange interface, which help in identifying the precedent and dependent cells or ranges in excel worksheets.

• Precedents: Cells or ranges which are referred to, by the formulas in other cells
• Dependents: Cells or ranges which contain formulas that refer to other cells

For example, the value in cell A1 =10, A2 = 20 and B1 = Sum (A1+A2), then A1 and A2 are the precedent cells of B1 which are used for calculating the value of B1. Also, B1 is the dependent cell for A1 and A2 whose value is calculated based on values of cell A1 and A2.

### Precedents

Refer to the following example code to get the Precedent ranges in a worksheet.

C#
Copy Code
```public void GetPrecedents()
{
// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.Worksheets;

// Set Formula in Cell E2
worksheet.Range["E2"].Formula = "=sum(A1:A2, B4,C1:C3)";
// Set Value of Cells
worksheet.Range["A1"].Value = 1;
worksheet.Range["A2"].Value = 2;
worksheet.Range["B4"].Value = 3;
worksheet.Range["C1"].Value = 4;
worksheet.Range["C2"].Value = 5;
worksheet.Range["C3"].Value = 6;

// Get Precedent cells of Range E2
foreach (var item in worksheet.Range["E2"].GetPrecedents())
{
item.Interior.Color = Color.Pink;
}

// Saving workbook to Xlsx
workbook.Save(@"Precedents.xlsx", SaveFileFormat.Xlsx);

}```

The below image shows the precedent ranges (highlighted in pink). ### Dependents

Refer to the following example code to get dependent ranges in a worksheet.

C#
Copy Code
```public void GetDependents()
{
// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.Worksheets;

// Set Value of Cell A1
worksheet.Range["A1"].Value = 100;
// Set Formula in Cell C1
worksheet.Range["C1"].Formula = "=\$A\$1";
// Set Formula in Range E1:E5
worksheet.Range["E1:E5"].Formula = "=\$A\$1";

// Get Dependent cells of Range A1
foreach (var item in worksheet.Range["A1"].GetDependents())
{
item.Interior.Color = Color.LightGreen;
}

// Saving workbook to Xlsx
workbook.Save(@"Dependents.xlsx", SaveFileFormat.Xlsx);
}```

The below image shows the dependent ranges (highlighted in green). ### All Precedents

Often multiple precedent ranges are used to calculate cell formula. Refer to the following example code to get all the precedent ranges in a worksheet.

C#
Copy Code
```public void GetAllPrecedents()
{
// Initialize workbook
Workbook workbook = new Workbook();
// Fetch default worksheet
IWorksheet worksheet = workbook.Worksheets;

// Set Formula in Cell E2
worksheet.Range["E2"].Formula = "=sum(C1:C2)";
// Set Formula in Cell C1
worksheet.Range["C1"].Formula = "=B1";
// Set Formula in Cell B1
worksheet.Range["B1"].Formula = "=sum(A1:A2)";
// Set Value of Cells
worksheet.Range["A1"].Value = 1;
worksheet.Range["A2"].Value = 2;
worksheet.Range["C2"].Value = 3;

// Get Precedent cells of Range E2
List<IRange> list = new List<IRange>();
foreach (var item in worksheet.Range["E2"].GetPrecedents())
{
}

while (list.Count > 0)
{
var temp = list;
list = new List<IRange>();
foreach (var item in temp)
{
for (int i = 0; i < item.RowCount; i++)
{
for (int j = 0; j < item.ColumnCount; j++)
{
var dependents = item.Cells[i, j].GetPrecedents();
if (dependents.Count == 0)
{
item.Cells[i, j].Interior.Color = Color.SkyBlue;
}
else
{
item.Cells[i, j].Interior.Color = Color.LightGreen; 