Datenvalidierung
Datenvalidierungstypen und Ausführung
Datenvalidierung ermöglicht das Festlegen von Regeln für auf einem Arbeitsblatt eingegebene Daten. Verwenden Sie beispielsweise Validierungen, um sicherzustellen, dass eine Spalte mit der Bezeichnung DATUM nur Datumsangaben enthält oder dass eine andere Spalte nur Zahlen enthält. Sie könnten sogar sicherstellen, dass eine Spalte mit der Bezeichnung DATUM nur Daten innerhalb eines bestimmten Bereichs enthält. Mit Datenvalidierungen können Sie steuern, was in Zellen im Arbeitsblatt eingegeben wird.
Microsoft Excel unterstützt verschiedene Arten von Datenvalidierungen. Jede Art wird verwendet, um zu steuern, welche Art von Daten in eine Zelle oder Zellbereich eingegeben wird. Nachfolgend werden Codeausschnitte illustriert, wie überprüft wird, dass:
- Zahlen ganzzahlig sind, d.h. keine Dezimalstellen haben.
- Dezimalzahlen die richtige Struktur aufweisen. Das Codebeispiel definiert, dass ein Zellbereich zwei Dezimalstellen haben sollte.
- Werte auf eine Liste von Werten beschränkt sind. Die Listenvalidierung definiert eine separate Liste von Werten, die auf eine Zelle oder einen Zellbereich angewendet werden können.
- Daten innerhalb eines bestimmten Bereichs liegen.
- Eine Uhrzeit innerhalb eines bestimmten Bereichs liegt.
- Ein Text eine bestimmte Zeichenlänge aufweist.
Datenvalidierung mit Microsoft Excel
Um Validierungen mit Microsoft Excel zu erstellen:
- Wählen Sie in einem Arbeitsblatt die Zellen aus, auf die Sie die Überprüfung anwenden möchten.
- Wählen Sie im Daten-Menü Validierung aus. Der Validierungsdialog wird angezeigt.
- Klicken Sie auf die Registerkarte Einstellungen und geben Sie die Einstellungen ein.
Datenvalidierung mit Aspose.Cells für Python Excel-Bibliothek
Die Datenvalidierung ist eine leistungsstarke Funktion zur Überprüfung der in Arbeitsblätter eingegebenen Informationen. Mit der Datenvalidierung können Entwickler Benutzern eine Auswahlliste bereitstellen, Daten eingaben auf einen bestimmten Typ oder eine bestimmte Größe beschränken usw. In Aspose.Cells für Python via .NET hat jede Worksheet-Klasse eine validations-Eigenschaft, die eine Sammlung von Validation-Objekten darstellt. Um die Validierung einzurichten, setzen Sie einige der Eigenschaften der Validation-Klasse wie folgt:
- Typ – stellt den Validierungstyp dar, der durch Verwendung eines der vordefinierten Werte in der ValidationType-Aufzählung angegeben werden kann.
- Operator – stellt den Operator dar, der bei der Validierung verwendet werden soll und durch Verwendung eines der vordefinierten Werte in der OperatorType-Aufzählung angegeben werden kann.
- Formel1 – stellt den Wert oder den Ausdruck dar, der mit dem ersten Teil der Datenvalidierung verbunden ist.
- Formel2 – stellt den Wert oder den Ausdruck dar, der mit dem zweiten Teil der Datenvalidierung verbunden ist.
Wenn die Eigenschaften des Validation-Objekts konfiguriert wurden, können Entwickler die CellArea-Struktur verwenden, um Informationen über den Zellenbereich zu speichern, der mithilfe der erstellten Validierung überprüft wird.
Arten der Datenvalidierung
Die ValidationType-Aufzählung hat die folgenden Elemente:
Member Name | Description |
---|---|
ANY_VALUE | Bezeichnet einen Wert beliebigen Typs. |
WHOLE_NUMBER | Bezeichnet Validierungstyp für ganze Zahlen. |
DECIMAL | Bezeichnet Validierungstyp für Dezimalzahlen. |
LIST | Bezeichnet Validierungstyp für Dropdown-Liste. |
DATE | Bezeichnet Validierungstyp für Datum. |
TIME | Bezeichnet Validierungstyp für Zeit. |
TEXT_LENGTH | Bezeichnet den Validierungstyp für die Länge des Textes. |
BENUTZERDEFINIERT | Bezeichnet benutzerdefinierten Validierungstyp. |
Ganze Zahl Datenvalidierung
Mit diesem Validierungstyp können Benutzer nur ganze Zahlen innerhalb eines bestimmten Bereichs in die validierten Zellen eingeben. Die folgenden Codebeispiele zeigen, wie der Validierungstyp WholeNumber implementiert wird. Das Beispiel erstellt dieselbe Datenvalidierung unter Verwendung von Aspose.Cells für Python via .NET, die wir oben mit Microsoft Excel erstellt haben.
from aspose.cells import CellArea, OperatorType, ValidationType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create a workbook object. | |
workbook = Workbook() | |
# Create a worksheet and get the first worksheet. | |
ExcelWorkSheet = workbook.worksheets[0] | |
# Accessing the Validations collection of the worksheet | |
validations = workbook.worksheets[0].validations | |
# Create Cell Area | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
# Creating a Validation object | |
validation = validations[validations.add(ca)] | |
# Setting the validation type to whole number | |
validation.type = ValidationType.WHOLE_NUMBER | |
# Setting the operator for validation to Between | |
validation.operator = OperatorType.BETWEEN | |
# Setting the minimum value for the validation | |
validation.formula1 = "10" | |
# Setting the maximum value for the validation | |
validation.formula2 = "1000" | |
# Applying the validation to a range of cells from A1 to B2 using the | |
# CellArea structure | |
area = CellArea() | |
area.start_row = 0 | |
area.end_row = 1 | |
area.start_column = 0 | |
area.end_column = 1 | |
# Adding the cell area to Validation | |
validation.add_area(area) | |
# Save the workbook. | |
workbook.save("output.out.xls") |
Listendatenvalidierung
Bei dieser Art der Validierung kann der Benutzer Werte aus einer Dropdown-Liste eingeben. Es bietet eine Liste: eine Reihe von Zeilen, die Daten enthalten. Im Beispiel wird ein zweites Arbeitsblatt hinzugefügt, um die Listenquelle zu speichern. Benutzer können nur Werte aus der Liste auswählen. Der Gültigkeitsbereich ist der Zellenbereich A1:A5 im ersten Arbeitsblatt.
Es ist wichtig, dass Sie die Validation.in_cell_drop_down-Eigenschaft auf true setzen.
from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create a workbook object. | |
workbook = Workbook() | |
# Get the first worksheet. | |
worksheet1 = workbook.worksheets[0] | |
# Add a new worksheet and access it. | |
i = workbook.worksheets.add() | |
worksheet2 = workbook.worksheets[i] | |
# Create a range in the second worksheet. | |
range = worksheet2.cells.create_range("E1", "E4") | |
# Name the range. | |
range.name = "MyRange" | |
# Fill different cells with data in the range. | |
worksheet2.cells.get("E1").value = "Blue" | |
worksheet2.cells.get("E2").value = "Red" | |
worksheet2.cells.get("E3").value = "Green" | |
worksheet2.cells.get("E4").value = "Yellow" | |
# Get the validations collection. | |
validations = worksheet1.validations | |
# Create Cell Area | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
# Create a new validation to the validations list. | |
validation = validations[validations.add(ca)] | |
# Set the validation type. | |
validation.type = ValidationType.LIST | |
# Set the operator. | |
validation.operator = OperatorType.NONE | |
# Set the in cell drop down. | |
validation.in_cell_drop_down = True | |
# Set the formula1. | |
validation.formula1 = "=MyRange" | |
# Enable it to show error. | |
validation.show_error = True | |
# Set the alert type severity level. | |
validation.alert_style = ValidationAlertType.STOP | |
# Set the error title. | |
validation.error_title = "Error" | |
# Set the error message. | |
validation.error_message = "Please select a color from the list" | |
# Specify the validation area. | |
area = CellArea() | |
area.start_row = 0 | |
area.end_row = 4 | |
area.start_column = 0 | |
area.end_column = 0 | |
# Add the validation area. | |
validation.add_area(area) | |
# Save the Excel file. | |
workbook.save("output.out.xls") |
Datum Datenvalidierung
Bei diesem Validierungstyp gibt der Benutzer Datumswerte innerhalb eines bestimmten Bereichs oder gemäß bestimmten Kriterien in die validierten Zellen ein. Im Beispiel ist der Benutzer darauf beschränkt, Daten zwischen 1970 und 1999 einzugeben. Der Gültigkeitsbereich ist die Zelle B1.
from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create a workbook. | |
workbook = Workbook() | |
# Obtain the cells of the first worksheet. | |
cells = workbook.worksheets[0].cells | |
# Put a string value into the A1 cell. | |
cells.get("A1").put_value("Please enter Date b/w 1/1/1970 and 12/31/1999") | |
# Set row height and column width for the cells. | |
cells.set_row_height(0, float(31)) | |
cells.set_column_width(0, float(35)) | |
# Get the validations collection. | |
validations = workbook.worksheets[0].validations | |
# Create Cell Area | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
# Add a new validation. | |
validation = validations[validations.add(ca)] | |
# Set the data validation type. | |
validation.type = ValidationType.DATE | |
# Set the operator for the data validation | |
validation.operator = OperatorType.BETWEEN | |
# Set the value or expression associated with the data validation. | |
validation.formula1 = "1/1/1970" | |
# The value or expression associated with the second part of the data validation. | |
validation.formula2 = "12/31/1999" | |
# Enable the error. | |
validation.show_error = True | |
# Set the validation alert style. | |
validation.alert_style = ValidationAlertType.STOP | |
# Set the title of the data-validation error dialog box | |
validation.error_title = "Date Error" | |
# Set the data validation error message. | |
validation.error_message = "Enter a Valid Date" | |
# Set and enable the data validation input message. | |
validation.input_message = "Date Validation Type" | |
validation.ignore_blank = True | |
validation.show_input = True | |
# Set a collection of CellArea which contains the data validation settings. | |
cellArea = CellArea() | |
cellArea.start_row = 0 | |
cellArea.end_row = 0 | |
cellArea.start_column = 1 | |
cellArea.end_column = 1 | |
# Add the validation area. | |
validation.add_area(cellArea) | |
# Save the Excel file. | |
workbook.save("output.out.xls") |
Zeit Datenvalidierung
Bei diesem Typ der Validierung können Benutzer Zeiten innerhalb eines bestimmten Bereichs oder gemäß bestimmter Kriterien in die validierten Zellen eingeben. Im Beispiel ist der Benutzer darauf beschränkt, Zeiten zwischen 09:00 und 11:30 Uhr einzugeben. Der Gültigkeitsbereich ist die Zelle B1.
from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create a workbook. | |
workbook = Workbook() | |
# Obtain the cells of the first worksheet. | |
cells = workbook.worksheets[0].cells | |
# Put a string value into A1 cell. | |
cells.get("A1").put_value("Please enter Time b/w 09:00 and 11:30 'o Clock") | |
# Set the row height and column width for the cells. | |
cells.set_row_height(0, float(31)) | |
cells.set_column_width(0, float(35)) | |
# Get the validations collection. | |
validations = workbook.worksheets[0].validations | |
# Create Cell Area | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
# Add a new validation. | |
validation = validations[validations.add(ca)] | |
# Set the data validation type. | |
validation.type = ValidationType.TIME | |
# Set the operator for the data validation. | |
validation.operator = OperatorType.BETWEEN | |
# Set the value or expression associated with the data validation. | |
validation.formula1 = "09:00" | |
# The value or expression associated with the second part of the data validation. | |
validation.formula2 = "11:30" | |
# Enable the error. | |
validation.show_error = True | |
# Set the validation alert style. | |
validation.alert_style = ValidationAlertType.INFORMATION | |
# Set the title of the data-validation error dialog box. | |
validation.error_title = "Time Error" | |
# Set the data validation error message. | |
validation.error_message = "Enter a Valid Time" | |
# Set and enable the data validation input message. | |
validation.input_message = "Time Validation Type" | |
validation.ignore_blank = True | |
validation.show_input = True | |
# Set a collection of CellArea which contains the data validation settings. | |
cellArea = CellArea() | |
cellArea.start_row = 0 | |
cellArea.end_row = 0 | |
cellArea.start_column = 1 | |
cellArea.end_column = 1 | |
# Add the validation area. | |
validation.add_area(cellArea) | |
# Save the Excel file. | |
workbook.save("output.out.xls") |
Textlängendatenvalidierung
Bei diesem Validierungstyp können Benutzer Textwerte einer bestimmten Länge in die validierten Zellen eingeben. Im Beispiel ist der Benutzer darauf beschränkt, Zeichenfolgenwerte mit nicht mehr als 5 Zeichen einzugeben. Der Gültigkeitsbereich ist die Zelle B1.
from aspose.cells import CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create a new workbook. | |
workbook = Workbook() | |
# Obtain the cells of the first worksheet. | |
cells = workbook.worksheets[0].cells | |
# Put a string value into A1 cell. | |
cells.get("A1").put_value("Please enter a string not more than 5 chars") | |
# Set row height and column width for the cell. | |
cells.set_row_height(0, float(31)) | |
cells.set_column_width(0, float(35)) | |
# Get the validations collection. | |
validations = workbook.worksheets[0].validations | |
# Create Cell Area | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
# Add a new validation. | |
validation = validations[validations.add(ca)] | |
# Set the data validation type. | |
validation.type = ValidationType.TEXT_LENGTH | |
# Set the operator for the data validation. | |
validation.operator = OperatorType.LESS_OR_EQUAL | |
# Set the value or expression associated with the data validation. | |
validation.formula1 = "5" | |
# Enable the error. | |
validation.show_error = True | |
# Set the validation alert style. | |
validation.alert_style = ValidationAlertType.WARNING | |
# Set the title of the data-validation error dialog box. | |
validation.error_title = "Text Length Error" | |
# Set the data validation error message. | |
validation.error_message = " Enter a Valid String" | |
# Set and enable the data validation input message. | |
validation.input_message = "TextLength Validation Type" | |
validation.ignore_blank = True | |
validation.show_input = True | |
# Set a collection of CellArea which contains the data validation settings. | |
cellArea = CellArea() | |
cellArea.start_row = 0 | |
cellArea.end_row = 0 | |
cellArea.start_column = 1 | |
cellArea.end_column = 1 | |
# Add the validation area. | |
validation.add_area(cellArea) | |
# Save the Excel file. | |
workbook.save("output.out.xls") |
Datenvalidierungsregeln
Wenn Datenvalidierungen implementiert sind, kann die Validierung überprüft werden, indem verschiedenen Werten in den Zellen zugewiesen werden. Cell.get_validation_value() kann verwendet werden, um das Validierungsergebnis abzurufen. Das folgende Beispiel demonstriert diese Funktion mit verschiedenen Werten. Die Beispieldatei kann über den folgenden Link heruntergeladen werden:
sampleDataValidationRules.xlsx
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Instantiate the workbook from sample Excel file | |
workbook = Workbook("sample.xlsx") | |
# Access the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Access Cell C1 | |
# Cell C1 has the Decimal Validation applied on it. | |
# It can take only the values Between 10 and 20 | |
cell = worksheet.cells.get("C1") | |
# Enter 3 inside this cell | |
# Since it is not between 10 and 20, it should fail the validation | |
cell.put_value(3) | |
# Check if number 3 satisfies the Data Validation rule applied on this cell | |
print("Is 3 a Valid Value for this Cell: " + str(cell.get_validation_value())) | |
# Enter 15 inside this cell | |
# Since it is between 10 and 20, it should succeed the validation | |
cell.put_value(15) | |
# Check if number 15 satisfies the Data Validation rule applied on this cell | |
print("Is 15 a Valid Value for this Cell: " + str(cell.get_validation_value())) | |
# Enter 30 inside this cell | |
# Since it is not between 10 and 20, it should fail the validation again | |
cell.put_value(30) | |
# Check if number 30 satisfies the Data Validation rule applied on this cell | |
print("Is 30 a Valid Value for this Cell: " + str(cell.get_validation_value())) |
Überprüfen Sie, ob die Validierung in der Zelle ein Dropdown ist
Wie wir gesehen haben, gibt es viele Arten von Validierungen, die innerhalb einer Zelle implementiert werden können. Wenn Sie überprüfen möchten, ob die Validierung ein Dropdown ist oder nicht, kann die Eigenschaft Validation.in_cell_drop_down verwendet werden, um dies zu testen. Der folgende Beispielcode demonstriert die Verwendung dieser Eigenschaft. Eine Beispieldatei für Tests kann über den folgenden Link heruntergeladen werden:
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
book = Workbook("sampleValidation.xlsx") | |
sheet = book.worksheets.get("Sheet1") | |
cells = sheet.cells | |
a2 = cells.get("A2") | |
va2 = a2.get_validation() | |
if va2.in_cell_drop_down: | |
print("A2 is a dropdown") | |
else: | |
print("A2 is NOT a dropdown") | |
b2 = cells.get("B2") | |
vb2 = b2.get_validation() | |
if vb2.in_cell_drop_down: | |
print("B2 is a dropdown") | |
else: | |
print("B2 is NOT a dropdown") | |
c2 = cells.get("C2") | |
vc2 = c2.get_validation() | |
if vc2.in_cell_drop_down: | |
print("C2 is a dropdown") | |
else: | |
print("C2 is NOT a dropdown") |
CellArea zur vorhandenen Validierung hinzufügen
Es gibt möglicherweise Fälle, in denen Sie CellArea zu vorhandenem Validation hinzufügen möchten. Wenn Sie CellArea mit Validation.add_area(cell_area) hinzufügen, überprüft Aspose.Cells alle vorhandenen Bereiche, um zu sehen, ob der neue Bereich bereits existiert. Wenn die Datei eine große Anzahl von Validierungen enthält, wirkt sich dies negativ auf die Leistung aus. Um dies zu überwinden, bietet die API die Methode Validation.add_area(cell_area, check_intersection, check_edge) an. Der Parameter checkIntersection gibt an, ob der Schnittpunkt eines bestimmten Bereichs mit vorhandenen Validierungsbereichen überprüft werden soll. Wenn Sie ihn auf false setzen, wird die Überprüfung anderer Bereiche deaktiviert. Der Parameter checkEdge gibt an, ob die angewendeten Bereiche überprüft werden sollen. Wenn der neue Bereich der obere linke Bereich wird, werden interne Einstellungen neu erstellt. Wenn Sie sicher sind, dass der neue Bereich nicht der obere linke Bereich ist, können Sie diesen Parameter auf false setzen.
Der folgende Code-Ausschnitt zeigt die Verwendung der Methode Validation.add_area(cell_area, check_intersection, check_edge) zum Hinzufügen neuer CellArea zu vorhandenem Validation.
from aspose.cells import CellArea, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
workbook = Workbook("ValidationsSample.xlsx") | |
# Access first worksheet. | |
worksheet = workbook.worksheets[0] | |
# Accessing the Validations collection of the worksheet | |
validation = worksheet.validations[0] | |
# Create your cell area. | |
cellArea = CellArea.create_cell_area("D5", "E7") | |
# Adding the cell area to Validation | |
validation.add_area(cellArea, False, False) | |
# Save the output workbook. | |
workbook.save("ValidationsSample_out.xlsx") |
Die Quell- und Ausgabedateien sind als Referenz angehängt.