Bedingte Formatierung in Arbeitsblättern anwenden
Dieser Artikel soll ein detailliertes Verständnis dafür vermitteln, wie bedingte Formatierung auf eine Zellenreihe in einem Arbeitsblatt angewendet wird.
Bedingte Formatierung ist eine fortgeschrittene Funktion in Microsoft Excel, die es ermöglicht, Formate auf eine Zellenreihe anzuwenden und diese Formatierung je nach dem Wert der Zelle oder dem Wert einer Formel zu ändern. Zum Beispiel kann der Hintergrund einer Zelle rot sein, um einen negativen Wert hervorzuheben, oder die Textfarbe könnte grün sein, um einen positiven Wert hervorzuheben. Wenn der Wert der Zelle die Formatbedingung erfüllt, wird das Format angewendet. Erfüllt der Wert der Zelle die Formatbedingung nicht, wird das Standardformat der Zelle verwendet.
Es ist möglich, bedingte Formatierungen mit Microsoft Office Automation anzuwenden, aber das hat seine Nachteile. Es gibt mehrere Gründe und Probleme, die damit verbunden sind: zum Beispiel Sicherheit, Stabilität, Skalierbarkeit und Geschwindigkeit. Der Hauptgrund, nach einer anderen Lösung zu suchen, ist, dass Microsoft selbst die Nutzung von Office Automation für Softwarelösungen nachdrücklich ablehnt.
Dieser Artikel zeigt, wie man eine Konsolenanwendung erstellt, bedingte Formatierung auf Zellen mit einigen einfachsten Zeilen Code mithilfe der Aspose.Cells API hinzufügt.
Verwendung von Aspose.Cells zur Anwendung bedingter Formatierung basierend auf Zellenwert
- Aspose.Cells herunterladen und installieren.
- Laden Sie Aspose.Cells for Node.js via C++ herunter.
- 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.
- Ein Projekt erstellen. Starten Sie Ihr Node.js-Projekt, indem Sie es initialisieren. Dieses Beispiel erstellt eine Node.js-Konsolenanwendung.
- 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");
- 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
- 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.