3

Is there way to implement data validation programmatically using Office.js API ?

enter image description here

2 Answers 2

2

While you can absolutely implement your own data validation within an add-in, it would be distinct from the built-in data validation tool. There is currently no API for configuring Excel's data validation tool programmatically.

Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to get the "drop-down list" functionality in a cell without Excel data validation through Office JS? In other words, I want the drop-down not so much for data validation purposes as for making data entry easier for users.
That being said, it is on the team's radar for the not-too-distant future.
@MichaelZlatkovsky-Microsoft are there any updates on this?
Its been 7 years since the last post, and yes you can now add data validation programmatically using OfficeJS. learn.microsoft.com/en-us/office/dev/add-ins/excel/…
0

Here I have try to add data validation on excel cell within range.

 Excel.run(function (context) {
    var currentWorksheet = context.workbook.worksheets.getActiveWorksheet();
    var expensesTable = currentWorksheet.tables.add("A1:D1", true /*hasHeaders*/);
    expensesTable.name = "ExpensesTable";
    expensesTable.getHeaderRowRange().values = [["Date", "Merchant", "Category", "Amount"]];
    expensesTable.rows.add(null /*add at the end*/, [
        ["1/1/2017", "The Phone Company", "Communications", "120"],
        ["1/2/2017", "Northwind Electric Cars", "Transportation", "142.33"],
        ["1/5/2017", "Best For You Organics Company", "Groceries", "27.9"],
        ["1/10/2017", "Coho Vineyard", "Restaurant", "33"],
        ["1/11/2017", "Bellows College", "Education", "350.1"],
        ["1/15/2017", "Trey Research", "Other", "135"],
        ["1/15/2017", "Best For You Organics Company", "Groceries", "97.88"]
    ]);
    var range = currentWorksheet.getRange("C2:C200");
    range.dataValidation.clear();
    range.dataValidation.rule = {
        list: {
            inCellDropDown: true,
            source: "Groceries, Education, Other,Transportation",
            autofitColumns: true
        }
    };
    //range.dataValidation.errorAlert = {
    //    message: "Sorry, only positive numbers are allowed",
    //    showAlert: true,
    //    style: "Stop",
    //    title: "Negative Number Entered"
    //};
    list.find();

    return context.sync();
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.