本文指导 如何使用 Node.js 在 Excel 中制作下拉列表。它包含设置开发环境的所有详细信息、步骤列表以及可运行的示例代码,显示如何使用 Node.js 在 Excel 中添加数据验证。您将学习不同的选项来自定义验证功能并设置您自己选择的参考数据。
使用 Node.js 在 Excel 中插入数据验证的步骤
- 设置 IDE 使用 Aspose.Cells for Node.js 通过 Java 添加下拉列表
- 创建一个 workbook 并添加两张表以分别添加验证和参考数据
- 在引用sheet上创建一个范围,设置范围名称和其中的引用数据
- 使用目标工作表中定义的单元格区域创建验证
- 使用 setFormula1() 方法设置验证的不同特征,包括参考范围
- 保存生成的工作簿,其中一张纸上有下拉列表,第二张纸上有参考数据
这些步骤总结了如何使用 Node.js 在 Excel 中创建数据验证的过程。通过在工作簿中创建工作表并将验证添加到目标工作表的验证集合中来实例化该过程。最后,设置验证所需的所有参数,并将生成的工作簿保存在磁盘上。
使用 Node.js 在 Excel 中添加数据验证的代码
var aspose = aspose || {}; | |
aspose.cells = require("aspose.cells"); | |
//Set the license | |
new aspose.cells.License().setLicense("License.lic"); | |
// Create a workbook | |
var wb = new aspose.cells.Workbook(); | |
// Access the target sheet | |
var ws1 = wb.getWorksheets().get(0); | |
// Create another sheet | |
var i = wb.getWorksheets().add(); | |
// It will be used for reference data therefore get access to it | |
var ws2 = wb.getWorksheets().get(i); | |
// Create a reference range | |
var refRange = ws2.getCells().createRange("E1", "E4"); | |
// Set the name | |
refRange.setName("ReferenceRange"); | |
// Fill the reference data | |
refRange.get(0, 0).putValue("Circle"); | |
refRange.get(1, 0).putValue("Rectangle"); | |
refRange.get(2, 0).putValue("Line"); | |
refRange.get(3, 0).putValue("Polygon"); | |
// Get a reference to the validations | |
var validations = ws1.getValidations(); | |
// Create a cell Area | |
var area = new aspose.cells.CellArea(); | |
area.StartRow = 0; | |
area.EndRow = 4; | |
area.StartColumn = 0; | |
area.EndColumn = 0; | |
// Create a new validation using the defined area | |
var newValidation = validations.get(validations.add(area)); | |
// Set validation type | |
newValidation.setType (aspose.cells.ValidationType.LIST); | |
// Set the operator type | |
newValidation.setOperator(aspose.cells.OperatorType.NONE); | |
// Set flag to set in-cell drop-down | |
newValidation.setInCellDropDown(true); | |
// Set the formula by using the reference data range name | |
newValidation.setFormula1("=ReferenceRange"); | |
// Enable the show error flag | |
newValidation.setShowError(true); | |
// Set the validation alert type | |
newValidation.setAlertStyle(aspose.cells.ValidationAlertType.STOP); | |
// Set the error title | |
newValidation.setErrorTitle("Error Title"); | |
// Set the error message | |
newValidation.setErrorMessage("Please select data from the list"); | |
// Save the output file | |
wb.save("output.xls"); | |
console.log("Validation added successfully"); |
此示例代码演示如何使用 Node.js 在 Excel 中插入数据验证。在此代码中,第一个工作表中的验证集合用于在定义的单元格区域内添加新的验证,然后设置验证类型、操作符类型和验证警报类型以及许多其他属性。第二个工作表包含使用单元格集合中的 createRange() 方法定义的参考数据,并使用 Validation.setFormula1() 方法与第一个工作表上的验证链接。
本文指导我们如何使用 Node.js 在 Excel 中添加验证。如果您想了解在工作表中插入图像的过程,请参阅 如何使用 Nodejs 在 Excel 中插入图像 上的文章。