[]
Use IRange.getValidation() to obtain an IValidation instance for a range and define the allowed data type, comparison rules, formulas, input messages, error messages, and drop-down behavior for that range. A cell can have only one validation rule applied at a time.
worksheet.getRange("A1:A3").setValue(new Object[][] {{"Low"}, {"Medium"}, {"High"}});
IValidation validation = worksheet.getRange("B2:B5").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "=$A$1:$A$3", null);
validation.setInCellDropdown(true);
voidadd(ValidationType type) voidadd(ValidationType type,
ValidationAlertStyle alertStyle,
ValidationOperator validationOperator,
Object formula1,
Object formula2) voiddelete()voidValidationAlertStyle value that specifies the data validation alert style.booleanbooleanbooleanbooleangetType()booleangetValue()voidValidationAlertStyle value that specifies the data validation alert style.voidsetErrorMessage(String value) voidsetErrorTitle(String value) voidsetFormula1(Object value) voidsetFormula2(Object value) voidsetIgnoreBlank(boolean value) voidsetIMEMode(IMEModeType value) voidsetInCellDropdown(boolean value) voidsetInputMessage(String value) voidsetInputTitle(String value) voidsetOperator(ValidationOperator value) voidsetShowError(boolean value) voidsetShowInputMessage(boolean value) voidsetType(ValidationType value) toJson()Uses the specified ValidationType to define the validation rule for the range associated with this IValidation object.
IValidation validation = worksheet.getRange("A1:A3").getValidation();
validation.add(ValidationType.None);
type - The validation type to add. Must not be null.Uses the specified ValidationType, ValidationAlertStyle, and ValidationOperator to define the validation rule for the range associated with this IValidation object.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
type - The validation type. Must not be null.alertStyle - The validation alert style.validationOperator - The data validation operator.formula1 - The first part of the data validation formula.formula2 - The second part of the data validation formula when validationOperator is ValidationOperator.Between or ValidationOperator.NotBetween; otherwise, this argument is ignored.IllegalArgumentException - if formula1 or formula2 is not supported for the specified validation rule, or if type is ValidationType.Custom and formula1 is null.Removes the existing data validation rule applied to the associated cell or range.
IValidation validation = worksheet.getRange("A1:A2").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.delete();
ValidationAlertStyle value that specifies the data validation alert style.Returns the ValidationAlertStyle that specifies which icon is shown in the validation error message box when invalid data is entered for the range.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
ValidationAlertStyle alertStyle = validation.getAlertStyle();
ValidationAlertStyle value that specifies the data validation alert style.ValidationAlertStyle value that specifies the data validation alert style.The specified ValidationAlertStyle controls the icon shown in the validation error message box when invalid data is entered for the range.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setAlertStyle(ValidationAlertStyle.Warning);
value - The ValidationAlertStyle value that specifies the data validation alert style. Must not be null.Use this method to get the message associated with the validation rule for the range. The message can be configured with setErrorMessage(String) and is used by the data validation error alert.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setErrorMessage("Input a value between 1 and 10.");
String errorMessage = validation.getErrorMessage();
Use this property to configure the message associated with the validation rule for the range.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setErrorMessage("Input a value between 1 and 10.");
String errorMessage = validation.getErrorMessage();
value - The data validation error message.This title is shown in the error alert when a user enters invalid data and error alerts are enabled. Use setErrorTitle(String) to change the title text displayed in the dialog box.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setErrorTitle("Invalid Number");
String errorTitle = validation.getErrorTitle();
null if no title has been set.Use this property to configure the title shown in the error alert when a user enters invalid data.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setErrorTitle("Invalid Number");
String errorTitle = validation.getErrorTitle();
value - The title of the data-validation error dialog box, or null if no title has been set.The returned object represents the first part of the validation criteria and can be a constant value, a string value, a cell reference, or a formula.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "Yes,No,Maybe", null);
Object formula1 = validation.getFormula1();
The assigned value represents the first part of the validation criteria and can be a constant value, a string value, a cell reference, or a formula.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "Yes,No,Maybe", null);
validation.setFormula1(100);
value - The value or expression associated with the conditional format or data validation.The returned object represents the second comparison value for rules that use two operands, such as data validation rules that use ValidationOperator.Between or ValidationOperator.NotBetween. It can be a constant value, a string value, a cell reference, or a formula.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
Object formula2 = validation.getFormula2();
The assigned value represents the second comparison value for rules that use two operands, such as data validation rules that use ValidationOperator.Between or ValidationOperator.NotBetween. It can be a constant value, a string value, a cell reference, or a formula.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setFormula2(100);
value - The value or expression associated with the second part of the conditional format or data validation.When this property returns true, empty cells in the validated range are allowed without violating the validation rule.
IValidation validation = worksheet.getRange("A1:A3").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setIgnoreBlank(true);
boolean ignoreBlank = validation.getIgnoreBlank();
true if blank values are permitted by the data range validation; otherwise, false.When this property is set to true, empty cells in the validated range are allowed without violating the validation rule.
IValidation validation = worksheet.getRange("A1:A3").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setIgnoreBlank(true);
value - true if blank values are permitted by the data range validation; otherwise, false.This value describes the Japanese input rules applied to validated cells.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setIMEMode(IMEModeType.Hiragana);
IMEModeType imeMode = validation.getIMEMode();
Use this property to specify the IMEModeType for a data validation rule.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setIMEMode(IMEModeType.Hiragana);
value - The IME mode to apply to the data validation rule.Use this property to determine whether the validation rule shows an in-cell list for selecting allowed values. This setting is typically used with list validation.
worksheet.getRange("A1").setValue("Open");
worksheet.getRange("A2").setValue("Closed");
IValidation validation = worksheet.getRange("C1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "=$A$1:$A$2", null);
boolean inCellDropdown = validation.getInCellDropdown();
true if data validation displays a drop-down list of acceptable values; otherwise, false.Use this property to set whether the validation rule shows an in-cell list for selecting allowed values.
worksheet.getRange("A1").setValue("Open");
worksheet.getRange("A2").setValue("Closed");
IValidation validation = worksheet.getRange("C1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "=$A$1:$A$2", null);
validation.setInCellDropdown(true);
value - true if data validation displays a drop-down list of acceptable values; otherwise, false.The input message is displayed when the user selects a cell in the validated range if input messages are enabled for the validation.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setInputMessage("Enter a whole number from 1 to 10.");
String inputMessage = validation.getInputMessage();
null if no input message has been set.The input message is displayed when the user selects a cell in the validated range if input messages are enabled for the validation.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setInputMessage("Enter a whole number from 1 to 10.");
String inputMessage = validation.getInputMessage();
value - The text of the data validation input message. Sets null if no input message has been set.Use this method to retrieve the title configured for the input prompt of a validated cell or range.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setInputTitle("Tips");
String title = validation.getInputTitle();
Use this property to configure the title shown in the input prompt for a validated cell or range.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setInputTitle("Tips");
String inputTitle = validation.getInputTitle();
value - The title of the data-validation input dialog box.The returned ValidationOperator determines how the validation compares the cell value with getFormula1() and, when applicable, getFormula2(). For example, ValidationOperator.Between and ValidationOperator.NotBetween compare the cell value against two formulas.
IRange range = worksheet.getRange("A1");
range.getValidation().add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
ValidationOperator operator = range.getValidation().getOperator();
The ValidationOperator determines how the validation compares the cell value with getFormula1() and, when applicable, getFormula2(). ValidationOperator.Between and ValidationOperator.NotBetween require two formulas.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
validation.setOperator(ValidationOperator.NotBetween);
value - The operator for the conditional format or data validation. Use ValidationOperator.Between or ValidationOperator.NotBetween only when both validation formulas are defined.Use setShowError(boolean) to control whether invalid input displays the validation error alert for the range.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "1,2,3", null);
validation.setShowError(false);
boolean showError = validation.getShowError();
true if the validation error message is displayed when invalid input is entered; otherwise, false.IllegalArgumentException - if no data validation is applied to the range.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "1,2,3", null);
validation.setShowError(false);
boolean showError = validation.getShowError();
value - true if the validation error message is displayed when invalid input is entered; otherwise, false.IllegalArgumentException - if no data validation is applied to the range.Use setShowInputMessage(boolean) to control whether the input message is shown for cells covered by this validation.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "1,2,3", null);
validation.setShowInputMessage(true);
boolean showInputMessage = validation.getShowInputMessage();
true if the data validation input message is displayed when a user selects a cell in the data validation range; otherwise, false.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.List, ValidationAlertStyle.Stop, ValidationOperator.Between, "1,2,3", null);
validation.setShowInputMessage(true);
value - true if the data validation input message is displayed when a user selects a cell in the data validation range; otherwise, false.Returns the current ValidationType that defines how values in the range are validated.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
ValidationType type = validation.getType();
Use this method to change the validation type of an existing IValidation rule. To fully modify a validation rule, you can use this method together with setAlertStyle(ValidationAlertStyle) and setOperator(ValidationOperator).
IValidation validation = worksheet.getRange("A1:A2").getValidation();
validation.add(ValidationType.Date, ValidationAlertStyle.Stop, ValidationOperator.Between, "13:30:00", "18:30:00");
validation.setType(ValidationType.Time);
value - The ValidationType to apply to the range. Must not be null.Returns true when the current value in the validated range satisfies the configured validation settings. If no data validation is applied to the range, this method returns true.
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
worksheet.getRange("A1").setValue(5);
boolean isValid = validation.getValue();
true if the current value meets all validation criteria; false otherwise.Calling this method clears the existing data validation in the current range and then applies the data validation defined by the specified JSON string. If both the current API call and the JSON content provide range information, the current range is applied and the range information in the JSON string is ignored.
IValidation validation = worksheet.getRange("A1:B2").getValidation();
validation.fromJson("{\"inputTitle\":\"Tip\",\"inputMessage\":\"Value must be between 5 and 20.\"," +
"\"type\":1,\"condition\":{\"conType\":0,\"compareType\":1,\"item1\":{\"conType\":1," +
"\"compareType\":3,\"expected\":\"5\",\"integerValue\":true},\"item2\":{\"conType\":1," +
"\"compareType\":5,\"expected\":\"20\",\"integerValue\":true},\"ignoreBlank\":true},\"ranges\":\"C3:D4\"}");
json - The JSON string that describes the data validation to apply.Use the returned JSON string to serialize the current validation settings for reuse with fromJson(String).
IValidation validation = worksheet.getRange("A1").getValidation();
validation.add(ValidationType.Whole, ValidationAlertStyle.Stop, ValidationOperator.Between, 1, 10);
String json = validation.toJson();