Office js Office.js是否存在自定义属性
我正在尝试使用office js查看自定义属性的存在。我很难了解它的存在Office js Office.js是否存在自定义属性,office-js,Office Js,我正在尝试使用office js查看自定义属性的存在。我很难了解它的存在 Excel.run(async (context) => { let customDocProperties = context.workbook.properties.custom; let customProperty = customDocProperties.getItem("prop"); customPro
Excel.run(async (context) => {
let customDocProperties = context.workbook.properties.custom;
let customProperty = customDocProperties.getItem("prop");
customProperty.load("key, value");
await context.sync();
console.log(customProperty);
}
在上述情况下,如果属性“prop”可用,代码可以正常工作,没有任何问题。如果没有“prop”或任何customproperties可用,则代码不会继续执行
如果没有此类属性或没有可用的自定义属性,如何中断代码
此外,我还尝试了以下代码。在这种情况下,当有任何自定义属性可用时,代码将挂起,excel内存将出现峰值(以GB为单位)
customDocProperties.load('items');
await context.sync();
console.log(customDocProperties.items.length);
谢谢尝试使用getItemOrNullObject()而不是getItem()。以下是一个例子:
Excel.run(async (context) => {
let customDocProperties = context.workbook.properties.custom;
let customProperty = customDocProperties.getItemOrNullObject("prop");
customProperty.load("key, value");
await context.sync();
if (customProperty.isNullObject) {
//Handle case where the custom property does not exist.
}
else
console.log(customProperty);
})
}
有关更多信息,请参见。此方法的优点也是,您可以同时检查多个属性的存在性(在同一
context.sync()上),只需为不同的属性创建多个变量即可。和/或与其他代码一起批处理。反之,如果依赖于抛出行为,则必须对每个属性执行context.sync()
调用,这将大大降低性能,并且更加繁琐。FWIW,我的书中有一章介绍了这一点。这一章叫做“9:检查对象是否存在”。本书以电子书形式提供,网址为。