我刚刚开始研究关于转换现有 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 () { …Run Code Online (Sandbox Code Playgroud)