获取一系列单元格的填充颜色officejs

Get the fill color of a range of cells officejs

我是 office.js 的新手,正在制作插件,我正在尝试为 Excel 制作插件。我 运行 遇到了一件看起来应该很容易的事情,但事实并非如此。我只是想获取所选单元格的背景颜色。据我所知,我需要循环遍历每个选定的单元格并单独检查 fill.color 值,这很好,除非我在尝试读取此 属性.[= 时不断收到错误消息13=]

Error PropertyNotLoaded: The property 'color' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.

我不太明白为什么我必须 运行 context.sync() 为此,当它已经是 运行 并且我正在尝试使用代码已由 Visual Studio 为加载项生成。

错误令人困惑,因为我可以毫无问题地设置这样的颜色。这是我添加的代码,试图获取填充颜色。第一行被注释掉,但向选定的单元格添加橙色填充没有问题。我添加这个只是为了看看我是否可以读出一个我知道已经设置的值。不过,我正在尝试为选定范围获取用户定义的填充。第二行是抛出错误的地方。

//sourceRange.getCell(i, j).format.fill.color = "orange"; // this sets the color no problem when uncommented
$('#fa-output').append("color: " + sourceRange.getCell(i,j).format.fill.color + "<br>"); //this is where it can't get the fill color

我正在使用 Visual Studio 生成的示例,它将随机生成 9 个随机数单元格并突出显示所选范围内的最高数字。这是此方法的完整代码:

// Run a batch operation against the Excel object model
    Excel.run(function (ctx) {
        // Create a proxy object for the selected range and load its properties
        var sourceRange = ctx.workbook.getSelectedRange().load("values, rowCount, columnCount, format");

        // Run the queued-up command, and return a promise to indicate task completion
        return ctx.sync()
            .then(function () {
                var highestRow = 0;
                var highestCol = 0;
                var highestValue = sourceRange.values[0][0];

                // Find the cell to highlight
                for (var i = 0; i < sourceRange.rowCount; i++) {
                    for (var j = 0; j < sourceRange.columnCount; j++) {

                        //sourceRange.getCell(i, j).format.fill.color = "orange"; // this sets the color no problem when uncommented
                        $('#fa-output').append("color: " + sourceRange.getCell(i,j).format.fill.color + "<br>"); //this is where it can't get the fill color

                        if (!isNaN(sourceRange.values[i][j]) && sourceRange.values[i][j] > highestValue) {
                            highestRow = i;
                            highestCol = j;
                            highestValue = sourceRange.values[i][j];
                        }
                    }
                }

                cellToHighlight = sourceRange.getCell(highestRow, highestCol);
                sourceRange.worksheet.getUsedRange().format.font.bold = false;

                // Highlight the cell
                cellToHighlight.format.font.bold = true;

                $('#fa-output').append("<br>The highest value is " + highestValue);

            })
            .then(ctx.sync);
    })
    .catch(errorHandler);

您的代码中有很多注释掉的代码,难以阅读。

无论如何,这是预期的行为。当你想读取工作簿中对象的 属性 时,你必须加载()然后同步()。加载和同步将 属性 的值从工作簿带到加载项中的 JavaScript,以便您可以阅读它。您的代码正在尝试读取尚未首先加载的 属性。下面是一个简单的例子:

const cell = context.workbook.getActiveCell();
cell.load('format/fill/color');
await context.sync();
console.log(cell.format.fill.color);

ES5 版本:

const cell = context.workbook.getActiveCell();
cell.load('format/fill/color');
return context.sync()
.then(function () {
    console.log(cell.format.fill.color);
});    

您还应该看看 Range.getCellProperties() 方法,它是一种加载包装器。