Table of Contents

Data validation

XlsxDocumentSheet.Validations lists a sheet's data validation rules, each with the cells it covers, and adds, changes and removes them as Excel's Data Validation dialog does. Validate says whether a value would pass, as Excel judges it. A rule is written the way Excel writes its own, measured against Excel: Excel opens what DocWright writes without repair and lists its rules as written.

dotnet add package DocWright.Spreadsheet
using DocWright.Spreadsheet;

// Items given in the rule: B2:B100 get Excel's in-cell dropdown.
XlsxDataValidation region = orders.Validations.AddList(new XlsxCellRange(2, 1, 100, 1), "North", "South", "East", "West");
region.InputTitle = "Region";
region.InputMessage = "Pick one of the four.";

// Items from cells on another sheet: stored in the sheet's extension, where Excel stores such a rule.
orders.Validations.AddListFrom(new XlsxCellRange(2, 3, 100, 3), "Lists!$A$1:$A$3");

AddList takes the items; none may hold a comma or a double quote, and together they are at most 4,095 characters with their commas (Excel refuses a longer list when it opens the file). AddListFrom takes a range or a defined name as a formula. A rule whose formula names another sheet is stored in the sheet's extension, as Excel stores it; every other rule in the sheet's dataValidations.

Numbers, dates, times, text lengths and formulas

using DocWright.Spreadsheet;

XlsxDataValidation quantity = orders.Validations.Add(
    new XlsxCellRange(2, 2, 100, 2), XlsxValidationType.WholeNumber, XlsxValidationOperator.Between, "1", "500");
quantity.ErrorStyle = XlsxValidationErrorStyle.Warning;          // Excel asks instead of refusing
quantity.ErrorTitle = "Quantity";
quantity.ErrorMessage = "Orders are from 1 to 500.\nLarger ones go to the desk.";

orders.Validations.Add(new XlsxCellRange(2, 4, 100, 4), XlsxValidationType.TextLength, XlsxValidationOperator.Equal, "6");
orders.Validations.Add(new XlsxCellRange(2, 5, 100, 5), XlsxValidationType.Custom, formula1: "=E2<=B2*10");

try
{
    orders.Validations.Add(new XlsxCellRange(2, 6, 9, 6), XlsxValidationType.WholeNumber, XlsxValidationOperator.GreaterThan, "1.5");
}
catch (ArgumentException error)
{
    Console.WriteLine(error.Message);
}

A formula is a number or a formula, with or without its =. A date is given as its serial number (46023 is 1 January 2026) and a time as a fraction of a day (0.5 is noon), as Excel stores them. Between and NotBetween take two formulas, every other operator one. What Excel refuses is refused at the call, with its reason: a formula that does not parse or names a sheet or a name the workbook does not have, a whole-number bound that is not whole, reversed bounds, a title over 32 characters, an input message over 254, an error message over 225. A refused call changes nothing.

Would a value pass?

using DocWright.Spreadsheet;

Console.WriteLine($"A2 'North': {orders.Validations.Validate("A2", XlsxCellValue.Text("North"))}, 'north': {orders.Validations.Validate("A2", XlsxCellValue.Text("north"))}");
Console.WriteLine($"B2 250: {orders.Validations.Validate("B2", XlsxCellValue.Number(250m))}, 2.5: {orders.Validations.Validate("B2", XlsxCellValue.Number(2.5m))}, blank: {orders.Validations.Validate("B2", XlsxCellValue.Blank)}");
Console.WriteLine($"D2 123456: {orders.Validations.Validate("D2", XlsxCellValue.Number(123456m))}; C2 'Shipped': {orders.Validations.Validate("C2", XlsxCellValue.Text("Shipped"))?.ToString() ?? "needs calculating"}");

The answer is Excel's, measured case by case with Excel's own check: a blank cell passes when the rule allows blanks; a list is case-sensitive and compares a number with an item that is the same number; a text length counts the value's own text, not as its number format shows it. The result is bool?: null where a formula would have to be calculated (a list or a bound given by a reference or a name, a custom rule). A cell no rule covers passes anything.

A rule over a rule

using DocWright.Spreadsheet;

orders.Validations.Add(new XlsxCellRange(50, 2, 100, 2), XlsxValidationType.Decimal, XlsxValidationOperator.GreaterThan, "0");
foreach (XlsxDataValidation rule in orders.Validations)
{
    Console.WriteLine(rule);
}

Adding a rule over cells that have one replaces it there, as the dialog does: the old rule keeps the cells to the left, to the right, above and below the new range, and goes when it has none left. That is also how a rule's criteria are changed; its settings (AllowBlank, InCellDropdown, the titles, the messages, ErrorStyle) are set on the rule itself.

Remove rules

using DocWright.Spreadsheet;

Console.WriteLine($"Rules that gave up cells: {orders.Validations.Remove(new XlsxCellRange(90, 1, 100, 5))}");
orders.Validations.Remove(orders.Validations.For("E2")!);

Remove(range) is Excel's Clear All over a selection: every rule gives up the cells in the range, and says how many did. Remove(rule) takes one rule with all its cells. The last rule removed takes the sheet's list of rules with it, since Excel refuses an empty one. Download the workbook: orders-validated.xlsx.

Excel, measured
In-cell dropdown The file's showDropDown="1" hides the dropdown; InCellDropdown says what the dialog says.
Two rules on a cell A file can give a cell two rules; Excel uses the first, and so does For.
Messages A line break is kept (written _x000a_, as Excel writes it).
Rows and columns Inserting and deleting rows and columns, sorting and copying a sheet move rules and their formulas with their cells.

Rules are held in memory while you edit them and written into the workbook once, when the sheet is next read or the workbook is saved, and they are found by cell through an index: 32,000 rules are added one row at a time in about a tenth of a second.

The whole run prints:

A whole-number rule's bound is a whole number; Excel refuses 1.5. (Parameter 'formula1')
A2 'North': True, 'north': False
B2 250: True, 2.5: False, blank: True
D2 123456: True; C2 'Shipped': needs calculating
A2:A100 list "North,South,East,West"
B2:B49 whole between 1 and 500
D2:D100 textLength equal 6
E2:E100 custom E2<=B2*10
B50:B100 decimal greaterThan 0
C2:C100 list Lists!$A$1:$A$3
Rules that gave up cells: 5
5 rules; B10: WholeNumber 1..500, Warning: Orders are from 1 to 500. / Larger ones go to the desk.