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

问题描述 投票:0回答:2

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

excel office-js excel-addins
2个回答
0
投票

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

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


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));
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.