Excel.DataValidationRule interface

Data validation rule contains different types of data validation. You can only use one of them at a time according the Excel.DataValidationType.

[ API set: ExcelApi 1.8 ]

Properties

custom

Custom data validation criteria.

[ API set: ExcelApi 1.8 ]

date

Date data validation criteria.

[ API set: ExcelApi 1.8 ]

decimal

Decimal data validation criteria.

[ API set: ExcelApi 1.8 ]

list

List data validation criteria.

[ API set: ExcelApi 1.8 ]

textLength

TextLength data validation criteria.

[ API set: ExcelApi 1.8 ]

time

Time data validation criteria.

[ API set: ExcelApi 1.8 ]

wholeNumber

WholeNumber data validation criteria.

[ API set: ExcelApi 1.8 ]

Property Details

custom

Custom data validation criteria.

[ API set: ExcelApi 1.8 ]

custom?: Excel.CustomDataValidation;

Property Value

date

Date data validation criteria.

[ API set: ExcelApi 1.8 ]

date?: Excel.DateTimeDataValidation;

Property Value

decimal

Decimal data validation criteria.

[ API set: ExcelApi 1.8 ]

decimal?: Excel.BasicDataValidation;

Property Value

list

List data validation criteria.

[ API set: ExcelApi 1.8 ]

list?: Excel.ListDataValidation;

Property Value

Examples

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Decision");
    const nameRange = 
        sheet.tables.getItem("NameOptionsTable").columns.getItem("Baby Name").getDataBodyRange();

    // When you are developing, it is a good practice to
    // clear the dataValidation object with each run of your code.
    nameRange.dataValidation.clear();

    const nameSourceRange = context.workbook.worksheets.getItem("Names").getRange("A1:A3");

    let approvedListRule = {
        list: {
            inCellDropDown: true,
            source: nameSourceRange
        }
    };
    nameRange.dataValidation.rule = approvedListRule;

    await context.sync();
});

textLength

TextLength data validation criteria.

[ API set: ExcelApi 1.8 ]

textLength?: Excel.BasicDataValidation;

Property Value

time

Time data validation criteria.

[ API set: ExcelApi 1.8 ]

time?: Excel.DateTimeDataValidation;

Property Value

wholeNumber

WholeNumber data validation criteria.

[ API set: ExcelApi 1.8 ]

wholeNumber?: Excel.BasicDataValidation;

Property Value

Examples

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Decision");
    const rankingRange = sheet.tables.getItem("NameOptionsTable").columns.getItem("Ranking").getDataBodyRange();

    // When you are developing, it is a good practice to
    // clear the dataValidation object with each run of your code.
    rankingRange.dataValidation.clear();

    let greaterThanZeroRule = {
        wholeNumber: {
            formula1: 0,
            operator: Excel.DataValidationOperator.greaterThan
        }
    };
    rankingRange.dataValidation.rule = greaterThanZeroRule;

    rankingRange.dataValidation.prompt = {
        message: "Please enter a positive number.",
        showPrompt: true,
        title: "Positive numbers only."
    };

    rankingRange.dataValidation.errorAlert = {
        message: "Sorry, only positive numbers are allowed",
        showAlert: true,
        style: "Stop",
        title: "Negative Number Entered"
    };

    await context.sync();
});