Apply Conditional Formatting in Worksheets

Using Aspose.Cells to Apply Conditional Formatting Based on Cell Value

  1. Download and Install Aspose.Cells.
    1. Download Aspose.Cells for Node.js via C++.
  2. Install it on your development computer. All Aspose components, when installed, work in evaluation mode. The evaluation mode has no time limit and it only injects watermarks into produced documents.
  3. Create a project. Start your Node.js project by initializing it. This example creates a Node.js console application.
  4. Add references. Add a reference to Aspose.Cells to your project, for example by requiring the package as follows:
    const AsposeCells = require("aspose.cells.node");
    
  5. Apply conditional formatting based on cell value. Below is the code used to accomplish the task. It applies conditional formatting on a cell.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const outputFilePath = path.join(dataDir, "output.out.xls");
// Create directory if it is not already present.
const fs = require("fs");
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().getCount();
// Get the collection of conditional formatting
const fcs = sheet.getConditionalFormattings();
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 0, 0);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.getStyle().setBackgroundColor(AsposeCells.Color.Red);
// Saving the Excel file
workbook.save(outputFilePath, AsposeCells.SaveFormat.Auto);

When the above code is executed, conditional formatting is applied to cell “A1” in the first worksheet of the output file (output.xls). The conditional formatting applied to A1 depends on the cell value. If the cell value of A1 is between 50 and 100 the background color is red due to the conditional formatting applied.

Using Aspose.Cells to Apply Conditional Formatting Based on Formula

  1. Applying conditional formatting depending on formula (Code Snippet) Below is the code to accomplish the task. It applies conditional formatting on B3.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
const fs = require("fs");
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().getCount();
sheet.getConditionalFormattings().add();
// Gets the conditional format collection
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(2, 1, 2, 1);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.Expression);
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.setFormula1("=IF(SUM(B1:B2)>100,TRUE,FALSE)");
fc.getStyle().setBackgroundColor(AsposeCells.Color.Red);
sheet.getCells().get("B3").setFormula("=SUM(B1:B2)");
sheet.getCells().get("C4").putValue("If Sum of B1:B2 is greater than 100, B3 will have RED background");
// Saving the Excel file
workbook.save(path.join(dataDir, "output.out.xls"), AsposeCells.SaveFormat.Auto);

When the above code is executed, conditional formatting is applied to cell “B3” in the first worksheet of the output file (output.xls). The conditional formatting applied depends on the formula which calculates the value of “B3” as the sum of B1 & B2.