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

Jam*_*can 5 javascript excel ms-office office365 office-js

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

通过在上下文中排队单个加载,我可以轻松地从整个范围中获取一组值,但似乎没有等效的方法来获取单元格格式。除非该范围内的所有单元格的格式都相同,否则该范围的返回值为“未定义”。

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

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));
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

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

有没有更好更有效的方法来做到这一点?

Mic*_*oft 4

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