データ検証
データ検証の種類と実行
データ検証は、ワークシートに入力されたデータに関するルールを設定する機能です。たとえば、「日付」と記載された列には日付のみが含まれるようにし、他の列には数字のみが含まれるようにすることができます。あるいは、「日付」と記載された列には特定の範囲内の日付のみが含まれるようにすることもできます。データ検証を使用すると、ワークシートのセルに入力される内容を制御することができます。
Microsoft Excel はさまざまな種類のデータ検証をサポートしています。それぞれの種類は、セルまたはセル範囲に入力されるデータの種類を制御するために使用されます。以下は、それぞれの種類を確認するためのコードスニペットの例です。
- 数値が整数で、つまり小数部を持たないこと。
- 小数点以下の桁に構造が正しいこと。コード例では、セルの範囲に2つの小数点以下があることを定義しています。
- 一覧からの値に制限されていること。一覧の検証では、セルやセル範囲に適用する別々の値の一覧が定義されます。
- 特定の範囲内の日付であること。
- 特定の範囲内の時間であること。
- 指定された文字長内のテキストであること。
Microsoft Excel でデータ検証
Microsoft Excel を使用して検証を作成するには:
- ワークシートで、検証を適用したいセルを選択します。
- データ メニューから 検証 を選択します。 検証のダイアログが表示されます。
- 設定 タブをクリックし、設定を入力します。
Aspose.Cells for Python Excel Library でのデータ検証
データ検証は、ワークシートに入力された情報を検証するための強力な機能です。データ検証を使用すると、開発者はユーザーに選択肢のリストを提供したり、データの入力を特定のタイプやサイズに制限したりすることができます。 Aspose.Cells for Python via .NET では、各 Worksheet クラスには validations プロパティがあり、これは Validation オブジェクトのコレクションを表します。検証を設定するには、Validation クラスのいくつかのプロパティを次のように設定します:
- type – 検証のタイプを表し、ValidationType 列挙型で定義された事前定義の値のいずれかを使用して指定できます。
- Operator – 検証で使用される演算子を表し、OperatorType 列挙型で定義された事前定義の値のいずれかを使用して指定できます。
- formula1 – データ検証の最初の部分に関連付けられた値または式を表します。
- formula2 – データ検証の2番目の部分に関連付けられた値または式を表します。
{0} オブジェクトのプロパティが構成されたら、開発者は作成された検証を使用して、{@1} 構造を使用して検証を行う予定のセル範囲に関する情報を保存できます。
データ検証の種類
ValidationType 列挙型には、次のメンバーがあります:
メンバー名 | 説明 |
---|---|
ANY_VALUE | 任意のタイプの値を示します。 |
WHOLE_NUMBER | 整数の検証タイプを示します。 |
DECIMAL数値の検証タイプを示します。 | |
LISTドロップダウンリストの検証タイプを示します。 | |
DATE | 日付の検証タイプを示します。 |
TIME時間の検証タイプを示します。 | |
TEXT_LENGTH | テキストの長さの検証タイプを示します。 |
CUSTOMカスタム検証タイプを示します。 |
整数データの検証
この種類の検証では、ユーザーは検証されたセルに指定された範囲内の整数のみを入力できます。以下のコード例では、WholeNumber検証タイプを実装する方法が示されます。この例では、マイクロソフトエクセルで作成したものと同じデータ検証を Aspose.Cells for Python via .NET を使用して作成します。
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") |
リストデータの検証
この種類の検証では、ユーザーはドロップダウンリストから値を入力できます。リストにはデータを含む一連の行があります。この例では、リストのソースを保持するために2番目のワークシートが追加されます。ユーザーはリストからのみ値を選択できます。検証エリアは最初のワークシートのセル範囲 A1:A5 です。
ここで重要な点は、Validation.in_cell_drop_downプロパティを true に設定することです。
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") |
日付データの検証
このタイプの検証では、ユーザーは検証済みのセルに特定の範囲内の日付値、または特定の条件を満たす日付値を入力できます。例では、ユーザーは1970年から1999年の間の日付を入力することに制限されます。ここでは、検証領域は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") |
時間データの検証
このタイプの検証では、ユーザーは検証済みのセルに特定の範囲内の時刻、または特定の条件を満たす時刻を入力できます。例では、ユーザーは午前09:00から11:30の間の時間のみを入力するように制限されます。ここでは、検証領域は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") |
テキスト長のデータ検証
このタイプの検証を使用すると、ユーザーは検証されたセルに指定された長さのテキスト値を入力できます。例では、ユーザーは5文字を超えない文字列値を入力することが制限されています。検証エリアは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") |
データ検証ルール
データ検証が実装されている場合、検証はセルに異なる値を割り当てて確認できます。Cell.get_validation_value() を使用して検証結果を取得できます。次の例は、異なる値を使用してこの機能をカバーしています。テスト用のサンプルファイルは次のリンクからダウンロードできます。
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())) |
セルの検証がドロップダウンであるかどうかをチェック
検証がドロップダウンかどうかを確認する
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を追加
既存の検証に CellArea を追加する場合があります。Validation.add_area(cell_area) を使用して CellArea を追加する場合、Aspose.Cells は新しいエリアがすでに存在するかどうかをチェックします。ファイルに検証が多数ある場合は、これにパフォーマンスの影響があります。これを克服するために、APIは Validation.add_area(cell_area, check_intersection, check_edge) メソッドを提供します。 checkIntersection パラメーターは、指定されたエリアと既存の検証エリアとの交差をチェックするかどうかを示します。これを false に設定すると、他のエリアをチェックしないようになります。 checkEdge パラメーターは、適用エリアをチェックするかどうかを示します。新しいエリアが左上のエリアになる場合、内部設定が再構築されます。新しいエリアが左上のエリアでないことを確信している場合、このパラメーターを false に設定できます。
新しいCellAreaを既存のValidationに追加するValidation.add_area(cell_area, check_intersection, check_edge)メソッドの使用を示すコードスニペット。
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") |
ソースエクセルファイルと出力エクセルファイルが添付されています。