Bedingte Formatierung in Arbeitsblättern anwenden

Verwendung von Aspose.Cells zur Anwendung bedingter Formatierung basierend auf Zellenwert

  1. Aspose.Cells herunterladen und installieren.
    1. Laden Sie Aspose.Cells for Node.js via C++ herunter.
  2. Installieren Sie es auf Ihrem Entwicklungscomputer. Alle Aspose-Komponenten arbeiten im Evaluierungsmodus, wenn sie installiert sind. Der Evaluierungsmodus hat kein Zeitlimit und fügt nur Wasserzeichen in erstellte Dokumente ein.
  3. Ein Projekt erstellen. Starten Sie Ihr Node.js-Projekt, indem Sie es initialisieren. Dieses Beispiel erstellt eine Node.js-Konsolenanwendung.
  4. Verweise hinzufügen. Fügen Sie eine Referenz zu Aspose.Cells in Ihr Projekt ein, indem Sie das Paket beispielsweise wie folgt require:
    const AsposeCells = require("aspose.cells.node");
    
  5. Bedingte Formatierung basierend auf Zellwert anwenden. Der unten stehende Code zeigt, wie die Aufgabe umgesetzt wird. Er wendet bedingte Formatierung auf eine Zelle an.
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);

Wenn der obige Code ausgeführt wird, wird die bedingte Formatierung auf die Zelle „A1“ im ersten Arbeitsblatt der Ausgabedatei (output.xls) angewendet. Die angewendete bedingte Formatierung auf A1 hängt vom Zellwert ab. Wenn der Zellwert von A1 zwischen 50 und 100 liegt, ist die Hintergrundfarbe aufgrund der angewendeten bedingten Formatierung rot.

Mit Aspose.Cells bedingte Formatierung basierend auf Formel anwenden

  1. Bedingte Formatierung abhängig von Formel anwenden (Code-Schnipsel) Im Folgenden finden Sie den Code zur Durchführung der Aufgabe. Er wendet bedingte Formatierung auf B3 an.
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);

Wenn der obige Code ausgeführt wird, wird die bedingte Formatierung auf die Zelle „B3“ im ersten Arbeitsblatt der Ausgabedatei (output.xls) angewendet. Die bedingte Formatierung hängt von der Formel ab, die den Wert von B3 als Summe von B1 & B2 berechnet.