Working with Data Validation

Working with Data Validation

Working with Data Validation

Aspose.Cells FOSS for C++ supports adding cell-level data validation rules to worksheets via the Validation and ValidationCollection classes. The WorkbookValidator class provides a pre-save consistency check.


Adding a Validation Rule

Access the validation collection for a worksheet and add a new rule using ValidationCollection::Add(). Set the type, formula, and target area on the returned Validation object:

#include "aspose/cells_foss/Workbook.h"
#include "aspose/cells_foss/Worksheet.h"
#include "aspose/cells_foss/ValidationCollection.h"
#include "aspose/cells_foss/Validation.h"
#include "aspose/cells_foss/CellArea.h"

using namespace Aspose::Cells_FOSS;

int main() {
    Workbook workbook;
    Worksheet& sheet = workbook.GetWorksheets()[0];
    ValidationCollection& validations = sheet.GetValidations();

    int idx = validations.Add();
    Validation& v = validations[idx];

    v.SetType(ValidationType::WholeNumber);
    v.SetFormula1("1");
    v.SetFormula2("100");
    v.SetIgnoreBlank(true);
    v.SetInCellDropDown(false);
    v.SetInputTitle("Enter Value");
    v.SetInputMessage("Value must be between 1 and 100.");
    v.SetErrorTitle("Invalid Input");
    v.SetErrorMessage("Value is out of range.");
    v.SetShowInput(true);
    v.SetShowError(true);

    CellArea area = CellArea::CreateCellArea("B2", "B10");
    v.AddArea(area);

    workbook.Save("validated.xlsx");
    return 0;
}

Validation Types

The ValidationType enumeration defines the kinds of constraints that can be applied:

ValidationTypeDescription
WholeNumberInteger value constraint
DecimalFloating-point constraint
ListDrop-down list from a formula or range
DateDate value constraint
TextLengthString length constraint
CustomFormula-based custom rule

Alert Style

The ValidationAlertType enumeration controls the behavior shown when the validation fails:

AlertStyleBehavior
StopBlocks entry and shows an error dialog
WarningShows a warning; user can accept or reject
InformationShows an informational tooltip

Set the alert style with Validation::SetAlertStyle(ValidationAlertType).


Querying Validation in a Cell

Use ValidationCollection::GetValidationInCell(row, col) to find which validation rule applies to a specific cell. Returns nullptr if no rule applies.


Removing Validation Rules

Remove a validation from a specific cell or range area:

CellArea area = CellArea::CreateCellArea("B2", "B10");
validations.RemoveArea(area);
// Or remove a single cell:
validations.RemoveACell(1, 1); // row=1, col=1 (0-based)

Pre-Save Workbook Validation

Call WorkbookValidator::ValidateForSave to run a consistency check on the workbook model before saving. This is called automatically by Workbook::Save but can be invoked independently during development:

WorkbookValidator validator;
validator.ValidateForSave(workbook);

Tips and Best Practices

  • Always set both SetFormula1 and SetFormula2 for range-type validations (WholeNumber, Decimal, Date) to define the lower and upper bounds.
  • Use SetIgnoreBlank(true) to allow empty cells to pass validation without triggering an error.
  • Call ValidationCollection::AreasOverlap before adding a new rule to check for overlapping validation areas on the same sheet.
  • WorkbookValidator::ValidateForSave is called internally on save; invoke it during development to catch issues early.

Common Issues

IssueCauseFix
Validation not applied to cellArea not addedCall Validation::AddArea(area) with the target range
Drop-down not showingSetInCellDropDown(false)Set SetInCellDropDown(true) for list validations
Rules overlapTwo rules cover the same cellsUse AreasOverlap to detect and resolve conflicts
ValidateForSave failsModel inconsistencyReview and fix the reported inconsistency before saving

API Reference Summary

Class/MethodDescription
ValidationCollection::Add()Add a new empty validation rule
ValidationCollection::GetValidationInCell(row, col)Find validation for a cell
ValidationCollection::RemoveArea(area)Remove validation from a range
ValidationCollection::RemoveACell(row, col)Remove validation from a cell
ValidationCollection::AreasOverlapCheck for overlapping validation areas
Validation::SetType(ValidationType)Set the validation type
Validation::SetFormula1(expr)Set lower bound or list source
Validation::SetFormula2(expr)Set upper bound
Validation::AddArea(area)Apply the rule to a range
Validation::RemoveArea(area)Remove the rule from a range
Validation::SetAlertStyle(style)Set stop/warning/information behavior
WorkbookValidator::ValidateForSavePre-save consistency check

See Also