我正在尝试使用 Excel Javascript APi 根据删除线对文本进行排序。我想做的是检测范围内的所有删除线文本并设置为红色然后排序。这就是我所拥有的,但出现错误..
ebugInfo:对象 代码:“属性未加载” 消息:“属性‘删除线’不可用。在读取属性值之前,请在包含对象上调用 load 方法,并在关联的请求上下文上调用“context.sync()”。”
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
// Add values to the range
const range = sheet.getRange("A5:M500");
let dataRange = sheet.getRange("A5:M500").load(["rowCount", "format", "values","font","strikethrough"]);
console.log(dataRange.toJSON());
await context.sync();
for (var row = 0; row < dataRange.rowCount; row++) {
dataRange.getRow(row).format.fill.clear();
if (dataRange.getCell(row, 0).format.font.strikethrough == true) {
dataRange.getRow(row).format.font.color = "Red";
}
}
// Sort the range
const sortFields = [
{
key: 1,
sortOn: Excel.SortOn.fontColor,
ascending: false
},
{
key: 0,
ascending: true
}
];
range.sort.apply(sortFields);
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
您没有正确使用
load()
方法。尝试更换:
let dataRange = sheet.getRange("A5:M500").load(["rowCount", "format", "values","font","strikethrough"]);
与
let dataRange = sheet.getRange("A5:M500").load(["rowCount", "format/font/strikethrough", "values"]);
此外,请参阅 load() 了解如何使用该方法。