如何在office js api中获取段落的列表样式

时间:2017-08-09 17:02:38

标签: office-js

每当我尝试获取office app中段落的list属性时,我都会收到以下错误:

{
    "name": "OfficeExtension.Error",
    "code": "ItemNotFound",
    "message": "We couldn’ t find the item you requested. Check the OfficeExtension.Error.debugInfo for more information.",
    "traceMessages": [],
    "innerError": null,
    "debugInfo": {
        "code": "ItemNotFound ",
        "message": "We couldn’ t find the item you requested.Check the OfficeExtension.Error.debugInfo for more information.",
        "errorLocation ": "Paragraph._onAccess "
    }
}

以下是我使用的代码:

var paragraphs = context.document.body.paragraphs;
context.load(paragraphs, 'style');
return context.sync().then(function () {
            var list = paragraphs.items[0].listOrNullObject;
            context.load(list);
            return context.sync().then(function () {
                var item = list.levelTypes;
                return context.sync().then(function () {
                    console.log("text" + item);
                });

            })

如何获取段落的列表样式?

1 个答案:

答案 0 :(得分:0)

您未在Word.run上下文中运行。所有内容都应该在此上下文中确定范围,否则您的对象将无法填充。

要填充段落集合,请尝试以下操作:

Word.run(function (context) {
        var paragraphs = context.document.body.paragraphs;
        context.load(paragraphs);
        return context.sync().then(function () {
             // Process paragraphs here
        });
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });