在使用Office js进行初始加载时,我无法从excel加载自定义属性。我们对Office js使用react。首次打开excel(Office.AutoShowTaskpaneWithDocument设置为true)时,我们需要从excel加载自定义属性。
window.Office.initialize = (reason) => {
ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));
};
window.Excel.run(async (context) => {
var docProps;
try {
var contextProps = await this.loadCustomProperties(context);
docProps = await this.getDocumentProperties(context);
}
catch(error) {
console.error("Error getting document properties");
}
});
async loadCustomProperties (context) {
console.log('context-getProperties');
try {
context.workbook.load('properties');
await context.sync();
}
catch(error) {
console.log('error loading properties of workbook');
}
context.workbook.properties.load('custom');
await context.sync();
try {
var customProps = context.workbook.properties.custom;
customProps.load('items');
await context.sync();
}
catch(error) {
console.log('error loading custom properties items');
}
return context.sync().then(function(){
console.log('context.workbook.properties.custom loaded successfully')
});
}
var DOCUMENT_SINGLEPROPS = ['DOCUMENT_PROP1', 'DOCUMENT_PROP2']
async getDocumentProperties (context) {
let customProps = new documentProperty();
const customDocProps = context.workbook.properties.custom;
for(var i = 0; i < DOCUMENT_SINGLEPROPS.length; i++) {
const custProp = DOCUMENT_SINGLEPROPS[i];
let customProperty = customDocProps.getItemOrNullObject(custProp);
customProperty.load("key, value");
await context.sync();
if (!customProperty.isNullObject) {
customProps[custProp] = customProperty.value;
}
else {
console.log("UNABLE TO FIND PROPERTY - " + custProp);
}
}
console.log('------- custom document properties -------');
console.log(customProps);
return customProps;
}
应用始终在初始加载时引发错误。当我们尝试从外接程序显式重新加载时,以上代码可以正常工作。看起来在到达代码时(第一次)也没有加载文档属性。如果是这种情况,请告诉我。有什么方法可以等待Office js完全加载吗?