如何使用 Node.js 在 Excel 中应用条件格式

本简短指南介绍了如何使用 Node.js 在 Excel 中应用条件格式。它包含设置使用该功能的开发环境的详细信息、编写应用程序的步骤列表,以及使用 Node.js 在电子表格中添加条件格式的可运行示例代码。通过添加条件并设置条件为 True 时的文本样式,您将了解完整的过程。

使用 Node.js 在 Excel 中添加条件格式的步骤

  1. 将 IDE 设置为使用 Aspose.Cells for Node.js 通过 Java 添加条件格式
  2. 使用 Workbook 类创建电子表格并访问工作表
  3. 启动所选工作表的 ConditionalFormattings 对象并作为 FormatConditionCollection 类的对象访问其引用
  4. 创建要应用格式的单元格区域并将其添加到 FormatConditionCollection 类的对象中
  5. 将条件添加到 FormatConditionCollection 并设置其背景
  6. 使用条件格式保存生成的工作簿

这些步骤定义了使用 Node.js* 根据单元格值在 Excel 中添加*条件格式的过程。在此过程中,将 FormatConditionCollection 对象添加到工作表的 ConditionalFormattings 集合中,然后使用 CellArea 类添加单元格区域并使用 FormatCondition 类添加条件。稍后,在新添加的FormatCondition对象中自定义条件的样式。

使用 Node.js 在 Excel 中创建条件格式的代码

var aspose = aspose || {};
aspose.cells = require("aspose.cells");
//Set the license
new aspose.cells.License().setLicense("License.lic");
// Create a Workbook for conditional formatting and access its first sheet
var spreadsheet = new aspose.cells.Workbook();
var sheet = spreadsheet.getWorksheets().get(0);
// Initiate a conditional formatting object
var indexCondFormatting = sheet.getConditionalFormattings().add();
var fcs = sheet.getConditionalFormattings().get(indexCondFormatting);
// Create a cell range and add to the format conditions collection
var cellArea = new aspose.cells.CellArea();
cellArea.StartRow = 0;
cellArea.EndRow = 10;
cellArea.StartColumn = 0;
cellArea.EndColumn = 1;
fcs.addArea(cellArea);
// Create the first condition and get the reference to it
var conditionIndex = fcs.addCondition(aspose.cells.FormatConditionType.CELL_VALUE, aspose.cells.OperatorType.BETWEEN, "25", "100");
var formatCondition = fcs.get(conditionIndex);
// Set the desired formatting
formatCondition.getStyle().setBackgroundColor(aspose.cells.Color.getRed());
// Save the workbook with a conditional formatting
spreadsheet.save("output.xlsx");
console.log("Conditional formatting applied successfully");

此示例代码使用 Node.js* 根据单元格值应用*Excel 条件格式。您可以使用 addCondition() 方法并为每个条件设置所需的样式,根据需要添加任意数量的条件。此方法提供了各种条件类型,例如 BEGINS_WITH、ABOVE_AVERAGE、COLOR_SCALE 和 CONTAINS_BLANKS 等。

本教程指导我们使用 Node.js 在 MS Excel 中添加条件格式。如果您想在 Excel 中应用过滤器,请参阅有关 如何使用 Node.js 在 Excel 中应用过滤器 的文章。

 简体中文