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 Webanwendung erstellt, bedingte Formatierungen auf Zellen mit wenigen einfachen Codezeilen unter Verwendung 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 JavaScript über 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 JavaScript-Projekt, indem Sie es initialisieren. Dieses Beispiel demonstriert die Verwendung in einer browserbasierten Webanwendung.
- Verweise hinzufügen.
Fügen Sie Ihrer Projekt eine Referenz zu Aspose.Cells hinzu, zum Beispiel durch Einbindung der Bibliothek wie folgt:
Apply Conditional Formatting Based on Cell Value
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, FormatConditionType, OperatorType, CellArea, Color } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Add conditional formatting collection
const cfCollection = worksheet.conditionalFormattings;
const idx = cfCollection.add();
const formatConditionCollection = cfCollection.get(idx);
// Define the cell area to apply conditional formatting (A1)
const area = CellArea.createCellArea(0, 0, 0, 0); // fromRow, fromCol, toRow, toCol
formatConditionCollection.addArea(area);
// Add a condition: Cell Value > 100
const conditionIndex = formatConditionCollection.addCondition(
FormatConditionType.CellValue,
OperatorType.Greater,
"100",
null
);
const formatCondition = formatConditionCollection.get(conditionIndex);
// Modify the style for the condition: bold and red font
const style = formatCondition.style;
if (!style.font) {
style.font = {};
}
style.font.bold = true;
style.font.color = Color.fromArgb(255, 255, 0, 0); // ARGB red
// Assign modified style back (property assignment pattern)
formatCondition.style = style;
// Save the modified workbook and provide download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'conditional_formatting_result.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Conditional formatting applied to cell A1 (value > 100). Click the download link to get the modified file.</p>';
});
</script>
```
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.