使用Office JS预先填充具有多个值的单元格

时间:2016-11-16 18:37:38

标签: excel office-js excel-addins

我正在使用Office JS为Office 365构建Excel Add In。我热衷于提供Excel工作表中的一些单元格,以便在Excel工作表中输入数据时,为用户提供预先填充的值下拉值。 Excel工作表的模板是在用户第一次加载“添加”时生成的。是否有允许相同的Office -JS API?

2 个答案:

答案 0 :(得分:0)

此时我们没有API来添加数据验证(将下拉列表添加到Excel单元格)。 请随时在https://officespdev.uservoice.com/处记录功能请求,我们会考虑在将来为此添加API。

-Philip,Office可扩展性团队的软件开发人员

答案 1 :(得分:0)

在这里,我已将下拉列表添加到excel单元格中。

 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 Selected value are allowed",
    //    showAlert: true,
    //    style: "Stop",
    //    title: "Other value 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));
    }
});