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

标签 javascript excel ms-office office365 office-js

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

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

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

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 个单元格的范围内尝试它时,我的异步回调根本没有被调用。

是否有更好、更有效的方法来做到这一点?

最佳答案

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

关于javascript - 使用 office.js 在 Excel 中获取单个单元格格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35647790/

相关文章:

javascript - 使用 lodash 对数组对象重新排序

excel - 针对单独的列表测试多个标准

excel - 获取 Excel 工作表名称并用作宏中的变量

com - 在 MS Office 64 位下使用 32 位 COM 插件

javascript - 正则表达式捕获 utf-8 行分隔符

php - 如何将数据从 Javascript 传递到 PHP,反之亦然?

javascript - 在 for 循环中使用 map api 进行反向地理编码期间城市名称乱序

excel - 从 excel 粘贴到 powerpoint 时 VBA 中的运行时错误

java - 如何在 Java Apache POI 库中使用嵌入式方程?

c# - Exchange 失去连接时不触发 Outlook 事件