Office 脚本错误 - 属性 不可用
Office Scripts error - the property is not available
我用新 Office Scripts in Excel on the web 创建了一个脚本。该脚本只是为当前工作表中的某个范围设置填充颜色,获取已使用的范围,然后尝试将该范围的地址写入控制台。
async function main(context: Excel.RequestContext) {
let workbook = context.workbook;
let worksheets = workbook.worksheets;
let selectedSheet = worksheets.getActiveWorksheet();
selectedSheet.getRange("B3:E6").format.fill.color = "E2EFDA";
// write used range to console
let usedRange = selectedSheet.getUsedRange();
usedRange.load("address");
context.sync();
console.log("used range = " + usedRange.address);
}
运行 此脚本生成以下错误消息:
The property 'address' 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.
似乎我已经按照错误消息指南的建议进行操作了——即调用 load
方法加载 address
属性 然后调用 context.sync()
加载后。我在这里错过了什么?
您需要在 context.sync();
之前添加 await
(因此它变成 await context.sync();
)。
context.sync()
是一个异步操作,这意味着脚本将在 sync
完成之前继续 运行。由于脚本的下一行需要工作簿中的信息,因此 sync
需要在继续之前完成。将行更改为 await context.sync();
确保工作簿和脚本之间的同步在继续之前完成并成功。
文章 Scripting fundamentals for Office Scripts in Excel on the web 中有更多相关内容。
我用新 Office Scripts in Excel on the web 创建了一个脚本。该脚本只是为当前工作表中的某个范围设置填充颜色,获取已使用的范围,然后尝试将该范围的地址写入控制台。
async function main(context: Excel.RequestContext) {
let workbook = context.workbook;
let worksheets = workbook.worksheets;
let selectedSheet = worksheets.getActiveWorksheet();
selectedSheet.getRange("B3:E6").format.fill.color = "E2EFDA";
// write used range to console
let usedRange = selectedSheet.getUsedRange();
usedRange.load("address");
context.sync();
console.log("used range = " + usedRange.address);
}
运行 此脚本生成以下错误消息:
The property 'address' 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.
似乎我已经按照错误消息指南的建议进行操作了——即调用 load
方法加载 address
属性 然后调用 context.sync()
加载后。我在这里错过了什么?
您需要在 context.sync();
之前添加 await
(因此它变成 await context.sync();
)。
context.sync()
是一个异步操作,这意味着脚本将在 sync
完成之前继续 运行。由于脚本的下一行需要工作簿中的信息,因此 sync
需要在继续之前完成。将行更改为 await context.sync();
确保工作簿和脚本之间的同步在继续之前完成并成功。
文章 Scripting fundamentals for Office Scripts in Excel on the web 中有更多相关内容。