我刚刚开始研究新的 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/