Office-JS:初始加载时加载Excel自定义属性

时间:2018-09-05 18:11:21

标签: excel office-js

在使用Office js进行初始加载时,我无法从excel加载自定义属性。我们对Office js使用react。首次打开excel(Office.AutoShowTaskpaneWithDocument设置为true)时,我们需要从excel加载自定义属性。

这是index.js中的代码,

window.Office.initialize = (reason) => {
        ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));
};

在App.js中

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");
            }
        });

loadCustomProperties方法:

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')
    }); 
}

getDocumentProperties方法:

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完全加载吗?

0 个答案:

没有答案