代码之家  ›  专栏  ›  技术社区  ›  hasnayn

获取Word文档所有部分的标题文本

  •  1
  • hasnayn  · 技术社区  · 9 年前

    我想使用 office.js . 我无法从节中获取标题文本。在section object中有一个方法返回body object。如何从body对象获取标题文本?

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

    我尝试了一种方法:从body对象获取“段落集合”。然后得到第一段文字。这实际上是标题文本。在这种情况下,我必须加载并同步段落集合以获取项目。现在,如果我加载集合并将其同步到一个循环中,循环只进行一次,这样我就只能获得一个部分的标题。但我需要所有章节的标题。代码如下:

    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”添加到数组中,并一次加载所有。但加载集合数组会引发错误:无法在不同的请求上下文中使用该对象。以下是代码:

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

    感谢阅读。欢迎提供任何类型的提示/帮助!

    1 回复  |  直到 9 年前
        1
  •  1
  •   Marc LaFleur    9 年前

    在标题文本可用之前,您需要 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);
        });
    }
    

    从性能角度来看,这将产生一系列不必要的 电话。更好的选择是利用队列,这样您可以请求头标头,但可以在一个队列中获取所有头标头 同步

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