使用 office.js 获取 Excel 中的单个单元格格式?

Get individual cell formats in Excel using office.js?

我刚开始研究新的 office js API 将现有的 Excel 加载项转换为使用这项新技术。

我可以通过在上下文中对单个加载进行排队,轻松地从整个范围中获取值数组,但似乎没有获取单元格格式的等效方法。除非该范围内的所有单元格的格式都相同,否则为该范围返回的值为 'undefined'.

我想出的解决方案是对范围内的每个单元格进行加载操作排队。例如,此函数获取范围内每个单元格的填充颜色:

function readFormats() {
    Excel.run(function (ctx) {
        var cells = [];

        //First get the size of the range for use in the loop below
        var myRange = ctx.workbook.getSelectedRange().load(["rowCount", "columnCount"]);

        return ctx.sync()
        .then(function () {
            //Loop though every cell and queue a load on the context for fill colour
            for (var r = 0; r < myRange.rowCount; ++r)
                for (var c = 0; c < myRange.columnCount; ++c)
                    cells.push(myRange.getCell(r, c).load("format/fill"));
        })
        .then(ctx.sync)
        .then(function () {
            //Do something useful with the fill color of cells in array here
        })
    })
    .then(function () {
        console.log("Formats done");
    })
    .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
}

此代码按预期工作,但速度极慢。例如,10,000 个单元格范围大约需要 12 秒,而 20k 单元格范围大约需要 45 秒到 运行。当我在包含 50k 个单元格的范围内尝试它时,我的异步回调根本没有被调用。

有没有更好更有效的方法?

目前没有更好的方法,但我们的积压工作中确实有它来公开单元级属性。我一定会与团队分享您的问题。