libxlsxwriter
|
Data validation is a feature of Excel which allows restrictions to be placed on the data that a user enters in a cell and to display associated help and warning messages. It can also be used to restrict input to values in a drop down list.
A typical use case might be to restrict data in a cell to integer values in a certain range, to provide a help message to indicate the required value and to issue a warning if the input data doesn't meet the stated criteria. In libxlsxwriter
this can be done as follows:
If the user inputs a value that doesn't match the specified criteria an error message is displayed:
For more information on data validation see the Microsoft support article Description and examples of data validation in Excel.
The following sections describe how to use the worksheet_data_validation_cell()
and worksheet_data_validation_range()
functions and the various options of lxw_data_validation.
The worksheet_data_validation_cell()
and worksheet_data_validation_range()
functions are used to construct an Excel data validation.
The data validation can be applied to a single cell or a range of cells:
The lxw_data_validation struct used in these functions is explained below.
The lxw_data_validation struct is used to set the properties of a data validation. A typical usage would look something like this:
This example shows the main properties of lxw_data_validation and Excel data validations:
validate
: The type of data to restrict the validation to. In this case it is whole numbers.criteria
: The criteria by which the data will be evaluated. In this case whether the input is greater than a value.value
: The value that the criteria applies to. This has several different versions for different types of data. This could also be a minimum
and maximum
value if the criteria used is a "BETWEEN" criteria. This is explained in the value, minimum, maximum section below.show_input
or input_message
. These parameters are explained in the Data validation options section.Note, in the examples in this document the data_validation
variable is shown as dynamically allocated, however an address of a static or automatic variable could also be used. In these cases make sure that the struct members are initialized to zero before setting other parameters.
The validate
parameter is used to set the type of data that you wish to validate:
It is always required and it has no default value. In Excel the validate parameters are:
The equivalent parameters in libxlsxwriter are defined in lxw_validation_types:
TRUE/FALSE
.The criteria
parameter is used to set the criteria by which the data in the cell is validated. It is almost always required except for the list
, custom
and any
validate options. It has no default value:
Allowable values are defined in lxw_validation_criteria:
The list
, custom
and any
validate options don't require a criteria
. If you specify one it will be ignored.
The value
parameters are used to set the limiting value to which the criteria
is applied. It is always required and it has no default value. There are different types of value
parameter associated with different types of data. They are:
value_number
:
The value_number
parameter is used to set the limiting value to which the criteria is applied using a whole or decimal number. It is typically used with LXW_VALIDATION_TYPE_INTEGER and LXW_VALIDATION_TYPE_DECIMAL and LXW_VALIDATION_TYPE_LENGTH.
value_formula
:
The value_formula
parameter is used to set the limiting value to which the criteria is applied using a cell reference. It is valid for any of the lxw_validation_types types that end in _FORMULA
:
value_list
:
The value_list
parameter is used to set a list of strings for a dropdown list. The list should be a NULL
terminated array of char* strings:
Note, when using the LXW_VALIDATION_TYPE_LIST validation with a list of strings, like in the last example above, Excel stores the strings internally as a Comma Separated Variable string. The total length for this string, including commas, cannot exceed the Excel limit of 255 characters. For longer sets of data you should use a range reference like the previous example above.
value_datetime
:
The value_datetime
parameter is used to set the limiting value to which the LXW_VALIDATION_TYPE_DATE or LXW_VALIDATION_TYPE_TIME criteria is applied using a lxw_datetime struct:
The minimum
and maximum
parameters are used to set the lower and upper limiting values when the criteria
is either LXW_VALIDATION_CRITERIA_BETWEEN or LXW_VALIDATION_CRITERIA_NOT_BETWEEN. The parameters are:
minimum_number
minimum_formula
minimum_datetime
maximum_number
maximum_formula
maximum_datetime
They are similar to the value
parameters described above. For example:
The ignore_blank
parameter is used to toggle on and off the 'Ignore blank' option in the Excel data validation dialog. When the option is on the data validation is not applied to blank data in the cell. It is on by default:
The dropdown
parameter is used to toggle on and off the 'In-cell dropdown' option in the Excel data validation dialog. When the option is on a dropdown list will be shown for list
validations. It is on by default.
The input_title
parameter is used to set the title of the input message that is displayed when a cell is entered. It has no default value and is only displayed if the input message is also displayed. See the input_message
parameter below.
The maximum title length is 32 characters.
The input_message
parameter is used to set the input message that is displayed when a cell is entered. It has no default value:
The input message generated from the above example is:
The message can be split over several lines using newlines. The maximum message length is 255 characters.
The show_input
parameter is used to toggle on and off the 'Show input message when cell is selected' option in the Excel data validation dialog. When the option is off an input message is not displayed even if it has been set using input_message
. It is on by default.
The error_title
parameter is used to set the title of the error message that is displayed when the data validation criteria is not met. The default error title is 'Microsoft Excel'. The maximum title length is 32 characters.
The error_message
parameter is used to set the error message that is displayed when a cell is entered. The default error message is "The value you
entered is not valid. A user has restricted values that can be entered into
the cell". A non-default error message can be displayed as follows:
Which give the following message:
The message can be split over several lines using newlines. The maximum message length is 255 characters.
The error_type
parameter is used to specify the type of error dialog that is displayed. There are 3 lxw_validation_error_types options:
The show_error
parameter is used to toggle on and off the 'Show error alert after invalid data is entered' option in the Excel data validation dialog. When the option is off an error message is not displayed even if it has been set using error_message
. It is on by default.
For a full example see data_validate.c.