先例和从属
介绍
- 先例单元格是由另一个单元格中的公式引用的单元格。例如,如果单元格 D10 包含公式 =B5,则单元格 B5 是单元格 D10 的先例。
- 依赖单元格包含引用其他单元格的公式。例如,如果单元格 D10 包含公式 =B5,则单元格 D10 依赖于单元格 B5。
为了使电子表格易于阅读,您可能希望清楚地显示电子表格中用于公式的单元格。同样,您可能需要提取其他单元格的依赖单元格。
Aspose.Cells 允许您跟踪单元格并找出哪些是相互关联的。
跟踪先例和依赖单元格:Microsoft Excel
公式可能会根据客户做出的修改而改变。例如,如果单元格 C1 依赖于包含公式的 C3 和 C4,并且更改了 C1(使公式被覆盖),则根据业务规则需要更改 C3 和 C4,或其他单元格,以使电子表格保持平衡。
类似地,假设 C1 包含公式"=(B122)/(M2N32)"。我想找到 C1 依赖的单元格,即先例单元格 B1、M2 和 N32。
您可能需要跟踪特定单元格到其他单元格的依赖关系。如果业务规则嵌入在公式中,我们希望找出依赖关系,并根据此执行一些规则。同样,如果特定单元格的值被修改,那么工作表中哪些单元格受到此变化的影响?
Microsoft Excel 允许用户跟踪先例和依赖。
- 在查看工具栏上,选择公式审计
- 跟踪先例:
- 选择包含您想要查找先例单元格的公式的单元格。
- 要向每个直接提供数据给活动单元格的单元格显示跟踪箭头,请单击公式审计工具栏上的跟踪先例。
- 跟踪引用特定单元格的公式(依赖项)
- 选择要识别其依赖单元格的单元格。
- 要向每个依赖于活动单元格的单元格显示跟踪箭头,请单击公式审计工具栏上的跟踪依赖。
跟踪先例和依赖单元格:Aspose.Cells
跟踪先例
Aspose.Cells 使得获取先例单元格变得容易。它不仅可以检索为简单公式先例提供数据的单元格,还可以找到为具有命名范围的复杂公式先例提供数据的单元格。
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String samplePrecedentsAndDependents = dirPath + u"samplePrecedentsAndDependents.xlsx"; | |
//Load source Excel file | |
Workbook workbook(samplePrecedentsAndDependents); | |
//Calculate workbook formula | |
workbook.CalculateFormula(); | |
//Access first worksheet | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Access cell F6 | |
Cell cell = worksheet.GetCells().Get(u"F6"); | |
//Get precedents of the cells and print them on console | |
std::cout << "Printing Precedents of Cell: "; | |
std::cout << cell.GetName().ToUtf8() << std::endl; | |
std::cout << u"-------------------------------" << std::endl; | |
ReferredAreaCollection refac = cell.GetPrecedents(); | |
int count = refac.GetCount(); | |
for (int i = 0; i < count; i++) | |
{ | |
ReferredArea refa = refac.Get(i); | |
int row = refa.GetStartRow(); | |
int col = refa.GetStartColumn(); | |
cell = worksheet.GetCells().GetCell(row, col); | |
std::cout<<cell.GetName().ToUtf8() << std::endl; | |
} | |
Aspose::Cells::Cleanup(); |
跟踪依赖项
Aspose.Cells允许您获取表格中的相关单元格。Aspose.Cells不仅可以检索提供有关简单公式的数据的单元格,还可以找到提供数据给具有命名范围的复杂公式相关单元格。
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Path of input | |
U16String dirPath(u""); | |
//Path of output | |
U16String outPath(u""); | |
//Path of input excel file | |
U16String samplePrecedentsAndDependents = dirPath + u"samplePrecedentsAndDependents.xlsx"; | |
//Load source Excel file | |
Workbook workbook(samplePrecedentsAndDependents); | |
//Calculate workbook formula | |
workbook.CalculateFormula(); | |
//Access first worksheet | |
Worksheet worksheet = workbook.GetWorksheets().Get(0); | |
//Access cell F6 | |
Cell cell = worksheet.GetCells().Get(u"F6"); | |
//Get dependents of the cells and print them on console | |
std::cout << "Printing Dependents of Cell: "; | |
std::cout << cell.GetName().ToUtf8() << std::endl; | |
std::cout << "-------------------------------" << std::endl; | |
//Parameter false means we do not want to search other sheets | |
Vector<Cell> depCells = cell.GetDependents(false); | |
//Get the length of the array | |
int len = depCells.GetLength(); | |
//Print the names of all the cells inside the array | |
for (int i = 0; i < len; i++) | |
{ | |
Cell dCell = depCells[i]; | |
std::cout << dCell.GetName().ToUtf8() << std::endl; | |
} | |
Aspose::Cells::Cleanup(); |