获取 Word 文档所有部分的 header 个文本

Get header text of all sections of a Word document

我想使用 office.js 创建一个 table 的 header 个所有部分。我无法从某个部分获取 header 文本。 object 部分有一个方法 returns body object。如何从 body object 中获取 header 文本?

sectionObject.getHeader(type);

这是我的代码:

Word.run(function (context) {
        //Create table here            
        var sections = context.document.sections;
        context.load(sections);
        return context.sync()
            .then(function () {
                if (sections != null) {
                    var items = sections.items;
                    var itemsCount = items.length;                        
                    for (var i = 0; i < itemsCount; i++) {
                        var currentSection = items[i];
                        var headerBody =currentSection.getHeader('primary');
                        //Get headerText here
                        //??


                        //Write the text into table
                    }                        
                }

            }).catch(function (myError) {                    
                showNotification("Error", myError.message);
            });

    }).catch(errorHandler);

请忽略 table create/write 评论。

我试过一种方法:从 body object 中获取`段落 collection。然后得到第一段文字。那实际上是 header 文本。在这种情况下,我必须加载并同步段落 collection 才能获取项目。现在,如果我将 collection 加载并同步到一个循环中,则循环只执行一次,这样我只得到一个部分的 header。但我需要所有部分的 headers。这是代码:

Word.run(function (context) {
        //Create table here  
        var sections = context.document.sections;
        context.load(sections);
        return context.sync()
            .then(function () {
                if (sections != null) {
                    var items = sections.items;
                    var itemsCount = items.length;                        
                    for (var i = 0; i < itemsCount; i++) {
                        var currentSection = items[i];
                        var headerBody = currentSection.getHeader('primary');
                        var headerBodyParagraps = headerBody.paragraphs;

                        context.load(headerBodyParagraps);
                        return context.sync()
                        .then(function () {
                            if (headerBodyParagraps != null) {
                                var headerBodyParaItems = headerBodyParagraps.items;
                                var headerText = headerBodyParagraps.items[0].text;

                                //Write the headerText into table
                                //
                            }

                        });
                    }                        
                }

            }).catch(function (myError) {
                //otherwise we handle the exception here!
                showNotification("Error", myError.message);
            });

    }).catch(errorHandler);

我尝试了另一种方式。不是为每个部分加载一次 "headerBodyParagraps",而是将 "headerBodyParagraps" 添加到数组中并一次加载所有这些。但是加载 collection 的数组会抛出错误:无法在不同的请求上下文中使用 object。这是代码:

Word.run(function (context) {
        //Add table here 
        var sections = context.document.sections;
        context.load(sections);
        return context.sync()
            .then(function () {
                if (sections != null) {
                    var items = sections.items;
                    var itemsCount = items.length;
                    var paraList = [];
                    for (var i = 0; i < itemsCount; i++) {
                        var currentSection = items[i];
                        var headerBody = currentSection.getHeader('primary');                            
                        var headerBodyParagraps = headerBody.paragraphs;
                        paraList[i] = headerBodyParagraps;                           
                    }
                    context.load(paraList); // Throws exception("Cannot use the object across different request contexts ") here.

                    context.sync()
                    .then(function () {
                        if (paraList != null) {
                            for (var j = 0; j < itemsCount; j++) {

                                var headerBodyParaItems = paraList[j].items;
                                var headerText = paraList[j].items[0].text;

                                //Add headertext to table here 
                            }
                        }

                    });

                }

            }).catch(function (myError) {
                //otherwise we handle the exception here!
                showNotification("Error", myError.message);
            });

    }).catch(errorHandler);

感谢阅读。欢迎任何类型的tip/help!

在 header 文本可用之前,您需要 load & sync 来检索它。例如:

for (var i = 0; i < itemsCount; i++) {
    var currentSection = items[i];
    var headerBody = currentSection.getHeader('primary');
    context.load(headerBody);
    context.sync().then(function () {
        console.log(headerBody.text);
    });
}

从性能的角度来看,这会产生一堆不必要的 sync 调用。一个更好的选择是利用 queue 所以你请求 head header 但在一个 sync 调用中获取所有它们:

Word.run(function (context) {
    var sections = context.document.sections;
    context.load(sections);
    return context.sync().then(function () {
        if (sections != null) {
            // Collection of headers we'll populate later
            var headers = [];

            // Iterate through the sections 
            for (var i = 0; i < sections.items.length; i++) {

                // Grab the header from the current section
                var header = sections.items[i].getHeader('primary');

                // Add loading this header to the  queue 
                context.load(header);

                // Push this header into the headers collection
                headers.push(header);
            }

            // Sync/Exectute the queued actions
            context.sync().then(function () {
                // Iterate through the headers collection
                for (var i = 0; i < headers.length; i++) {
                    // Output each header's text to the console
                    console.log(headers[i].text);
                }
            });

        }
    });
})