Cell.GetPrecedents

Cell.GetPrecedents method

Gets all references appearing in this cell’s formula.

public ReferredAreaCollection GetPrecedents()

Return Value

Collection of all references appearing in this cell’s formula.

Remarks

Returns null if this is not a formula cell.All references appearing in this cell’s formula will be returned no matter they are referenced or not while calculating. For example, although cell A2 in formula “=IF(TRUE,A1,A2)” is not used while calculating, it is still taken as the formula’s precedents.To get those references which influence the calculation only, please use GetPrecedentsInCalculation.

Examples

[C#]

Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
cells["A1"].Formula = "=B1+SUM(B1:B10)+[Book1.xls]Sheet1!A1";
ReferredAreaCollection areas = cells["A1"].GetPrecedents();
for (int i = 0; i < areas.Count; i++)
{
     ReferredArea area = areas[i];
     StringBuilder stringBuilder = new StringBuilder();
     if (area.IsExternalLink)
     {
         stringBuilder.Append("[");
         stringBuilder.Append(area.ExternalFileName);
         stringBuilder.Append("]");
     }
     stringBuilder.Append(area.SheetName);
     stringBuilder.Append("!");
     stringBuilder.Append(CellsHelper.CellIndexToName(area.StartRow, area.StartColumn));
     if (area.IsArea)
     {
         stringBuilder.Append(":");
         stringBuilder.Append(CellsHelper.CellIndexToName(area.EndRow, area.EndColumn));
     }
     Console.WriteLine(stringBuilder.ToString());
}

[Visual Basic]

Dim workbook As Workbook = New Workbook()
Dim cells As Cells = workbook.Worksheets(0).Cells
cells("A1").Formula = "= B1 + SUM(B1:B10) + [Book1.xls]Sheet1!A1"
Dim areas As ReferredAreaCollection = cells("A1").GetPrecedents()
For i As Integer = 0 To areas.Count - 1
    Dim area As ReferredArea = areas(i)
    Dim stringBuilder As StringBuilder = New StringBuilder()
    If (area.IsExternalLink) Then
        stringBuilder.Append("[")
        stringBuilder.Append(area.ExternalFileName)
       stringBuilder.Append("]")
    End If
    stringBuilder.Append(area.SheetName)
    stringBuilder.Append("!")
    stringBuilder.Append(CellsHelper.CellIndexToName(area.StartRow, area.StartColumn))
    If (area.IsArea) Then
        stringBuilder.Append(":")
        stringBuilder.Append(CellsHelper.CellIndexToName(area.EndRow, area.EndColumn))
    End If
    Console.WriteLine(stringBuilder.ToString())
Next

See Also