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 für Python via .NET 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 Visual Studio.NET und erstellen Sie eine neue Konsolenanwendung. Dieses Beispiel erstellt eine Python-Konsolenanwendung, aber Sie können auch VB.NET verwenden.
  4. Verweise hinzufügen. Fügen Sie eine Referenz zu Aspose.Cells in Ihr Projekt ein.
  5. *Bedingte Formatierung basierend auf Zellenwert anwenden. Im Folgenden finden Sie den Code zur Durchführung der Aufgabe. Diese wendet bedingte Formatierung auf eine Zelle an.
from aspose.cells import CellArea, FormatConditionType, OperatorType, SaveFormat, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
# Sets the background color.
fc = fcs[conditionIndex]
fc.style.background_color = Color.red
# Saving the Excel file
workbook.save(dataDir + "output.out.xls", SaveFormat.AUTO)

Wenn der obige Code ausgeführt wird, wird bedingte Formatierung auf Zelle “A1” im ersten Arbeitsblatt der Ausgabedatei (output.xls) angewendet. Die bedingte Formatierung, die auf A1 angewendet wird, hängt vom Zellwert ab. Wenn der Zellwert von A1 zwischen 50 und 100 liegt, ist die Hintergrundfarbe aufgrund der 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.
from aspose.cells import CellArea, FormatConditionType, SaveFormat, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca = CellArea()
ca.start_row = 2
ca.end_row = 2
ca.start_column = 1
ca.end_column = 1
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.EXPRESSION)
# Sets the background color.
fc = fcs[conditionIndex]
fc.formula1 = "=IF(SUM(B1:B2)>100,TRUE,FALSE)"
fc.style.background_color = Color.red
sheet.cells.get("B3").formula = "=SUM(B1:B2)"
sheet.cells.get("C4").put_value("If Sum of B1:B2 is greater than 100, B3 will have RED background")
# Saving the Excel file
workbook.save(dataDir + "output.out.xls", SaveFormat.AUTO)

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