GetPrecedents

Cell.GetPrecedents method

获取出现在此单元格公式中的所有引用。

public ReferredAreaCollection GetPrecedents()

返回值

出现在此单元格公式中的所有引用的集合。

评论

如果这不是公式单元格,则返回 null。此单元格公式中出现的所有引用,无论在计算时是否被引用,都会返回。 例如,虽然计算时不使用公式“=IF(TRUE,A1,A2)”中的单元格A2, 仍然是作为公式的先例。要获取仅影响计算的参考,请使用GetPrecedentsInCalculation.

例子

[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

也可以看看