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

我刚开始看到新的办公室JS API转换现有的Excel加载项使用这种新技术。

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

我提出的解决scheme是在范围内的每个单独的单元格上对加载操作进行排队。 例如,这个函数为一个范围内的每个单元格获取填充颜色:

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)); } }); } 

此代码按预期工作,但速度非常慢。 例如,10000个单元格范围大约需要12秒,而在45秒左右的时间内需要20k个单元格范围才能运行。 当我在包含50k个单元格的范围上尝试时,我的asynchronouscallback从来没有被调用过。

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

目前还没有更好的方法,但是我们确实已经在我们的待办事项上公开了单元级属性。 我一定会和球队分享你的问题。