本文帮助您使用 Node.js 跟踪 Excel 中的先例和依赖项。它包含设置应用程序开发环境的详细信息、描述流程的步骤列表以及使用 Node.js 跟踪特定先例单元格的所有Excel 依赖单元格的示例代码,反之亦然。您将学习如何获取有关先例单元格区域和依赖单元格列表的所有详细信息。
使用 Node.js 识别 Excel 单元格引用的步骤
- 设置环境使用 通过 Java 实现 Node.js 的 Aspose.Cells 来跟踪从属项和先例
- 使用 Workbook 类创建 Excel 文件,访问工作表,并使用示例数据和公式填充单元格
- 使用 Cell.getPrecedents() 方法获取依赖单元格 precedents 的集合
- 解析所有单元格区域并显示相关信息
- 获取先前单元格的依赖单元格列表
- 显示每个单元格的详细信息
这些步骤描述了使用 Node.js 跟踪 Excel 公式引用 的过程。使用 Workbook 类对象创建或加载 Excel 文件,从目标工作表中获取单元格集合,根据需要在几个单元格中设置一些示例数据,并设置引用 Workbook 中其他单元格的公式。最后,访问先行单元格和从属单元格,并获取要在控制台上显示的信息。
使用 Node.js 编写的 Excel 公式跟踪工具代码
var aspose = aspose || {}; | |
aspose.cells = require("aspose.cells"); | |
// Set the license | |
new aspose.cells.License().setLicense("License.lic"); | |
// Create an Excel file and access the cells collection | |
var book = new aspose.cells.Workbook(); | |
var cellsColl = book.getWorksheets().get(0).getCells(); | |
// Access target cells and set a formula | |
var precedentCell = cellsColl.get("A1"); | |
var dependentCell = cellsColl.get("B1"); | |
dependentCell.setFormula("=A1"); | |
// Access the precedents from s dependent cell | |
var precedents = dependentCell.getPrecedents(); | |
// Parse all the precedent areas and display the information | |
console.log("PRECEDENTS"); | |
for (var i = 0; i < precedents.getCount(); i++) | |
{ | |
var area = precedents.get(i); | |
console.log("Sheet Name = " + area.getSheetName()); | |
console.log("Start Row/Column = " + area.getStartRow() + "/"+ area.getStartColumn()); | |
console.log("End Row/Column = " + area.getEndRow() + "/"+ area.getEndColumn()); | |
} | |
// Parse all the cells collection | |
console.log("DEPENDENTS"); | |
var dependents = precedentCell.getDependents(true); | |
for (var i = 0; i < dependents.length; i++) | |
{ | |
var cell = dependents[i]; | |
console.log("Name =" + cell.getName()); | |
console.log("Formula =" + cell.getFormula()); | |
console.log("Row = " + cell.getRow()); | |
console.log("Column =" + cell.getColumn()); | |
} | |
console.log("Precedents and Dependents traced successfully"); |
此代码演示如何使用 Node.js* 跟踪依赖项和 Excel 先例。getDependents() 方法使用布尔标志来检查其他工作表中的公式是否有效,例如使用true”来检查其他工作表中的依赖单元格。getPrecedents() 方法返回源单元格使用的单元格区域。
在本主题中,我们学习了如何使用 Node.js* 在 Excel 中跟踪从属和先例。要应用条件格式,请参阅有关 如何使用 Nodejs 在 Excel 中应用条件格式 的文章。