【问题标题】:How do I read the properties of a range in Office.js?如何读取 Office.js 中范围的属性?
【发布时间】:2020-04-01 11:08:53
【问题描述】:

我正在尝试读取范围 B2 的值并根据返回的内容编写一个 if 语句。我不断收到错误,我不知道为什么。请看下文。

function analyzeWorkbook() {
    // Run a batch operation against the Excel object model
    Excel.run(function (context) {
        // Create a proxy object for the selected range and load its properties
        var Sheet = context.workbook.worksheets.getActiveWorksheet();
        var Range = Sheet.getRange("B2");
        Range.load("values");
        // Run the queued-up command, and return a promise to indicate task completion
        return context.sync();

        if (Range.values[0][0] === "Business Unit:") {
            Sheet.getRange("B3").values = "You did it!";
        } else {
            Sheet.getRange("B3").values = "Youre still a rockstar!";
        }

    })
        .catch(errorHandler);
}

【问题讨论】:

  • 你遇到了什么错误?我试过你的代码,没有发现任何错误。这是我的要点:gist.github.com/lumine2008/1273a84c4ebb9bfe2dc4fc5c7af50bd5
  • 这是错误消息 Error PropertyNotLoaded: The property 'values' is not available。在读取属性值之前,在包含对象上调用 load 方法并在关联的请求上下文上调用“context.sync()”。
  • 我解决了。我在第一次返回context.sync()后打开了.then(function() 谢谢你的帮助!

标签: javascript office-js office-addins


【解决方案1】:
function analyzeWorkbook() {

    // Run a batch operation against the Excel object model
    Excel.run(function (context) {
        // Create a proxy object for the selected range and load its properties
        var Sheet = context.workbook.worksheets.getActiveWorksheet();
        var Range = Sheet.getRange("B2");
        Range.load("values");
        // Run the queued-up command, and return a promise to indicate task completion
        return context.sync()
            .then(function () {
                if (Range.values[0][0] === "Business Unit:") {
                    Sheet.getRange("B3").values = "You did it!"
                } else {
                    Sheet.getRange("B3").values = "Youre still a rockstar!"
                }
                return context.sync();
            });

    });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2019-05-01
    • 2020-11-21
    相关资源
    最近更新 更多