Ms word 如何在Office JS加载项中删除headerReferences和footerReferences?

Ms word 如何在Office JS加载项中删除headerReferences和footerReferences?,ms-word,office-js,openxml,word-addins,Ms Word,Office Js,Openxml,Word Addins,当我清除正文和部分时,我希望页眉和页脚的引用将被自动删除,但不幸的是,如果我查看文件,我看到引用仍然存在 这是我清理身体和部分的方式 Word.run(async context => { context.document.body.clear(); return context.sync().then(r => { const sections = context.document.sections; sections.load();

当我清除正文和部分时,我希望页眉和页脚的引用将被自动删除,但不幸的是,如果我查看文件,我看到引用仍然存在

这是我清理身体和部分的方式

Word.run(async context => {
    context.document.body.clear();
    return context.sync().then(r => {
        const sections = context.document.sections;
        sections.load();
        return context.sync().then(function () {
            sections.items.forEach(function (section) {
                // Clear the Body
                section.body.clear();

                // Clear any Headers
                section.getHeader("Primary").clear();
                section.getHeader("FirstPage").clear();
                section.getHeader("EvenPages").clear();

                // Clear any Footers
                section.getFooter("Primary").clear();
                section.getFooter("FirstPage").clear();
                section.getFooter("EvenPages").clear();
            });
        });
    });
}).catch(handleError);
这是我在清除所有项目后在.docx文件中看到的

<w:headerReference r:id='rId9'
                   w:type='even'/>
<w:headerReference r:id='rId10'
                   w:type='default'/>
<w:footerReference r:id='rId11'
                   w:type='even'/>
<w:footerReference r:id='rId12'
                   w:type='default'/>
<w:headerReference r:id='rId13'
                   w:type='first'/>
<w:footerReference r:id='rId14'
                   w:type='first'/>

预期成果:

<w:headerReference r:id='rId8'
                   w:type='default'/>
<w:footerReference r:id='rId9'
                   w:type='default'/>


有办法删除它们吗?

现在,据我所知,可以通过调用
body.select()
方法删除未使用的引用

Word.run(async context => {
    context.document.body.clear();
    return context.sync().then(r => {
        const sections = context.document.sections;
        sections.load();
        return context.sync().then(function () {
            sections.items.forEach(function (section) {
                // Clear the Body
                section.body.clear();

                // Clear any Headers
                section.getHeader("Primary").clear();
                section.getHeader("FirstPage").clear();
                section.getHeader("EvenPages").clear();

                // Clear any Footers
                section.getFooter("Primary").clear();
                section.getFooter("FirstPage").clear();
                section.getFooter("EvenPages").clear();
            });

            context.document.body.select(); //This line is needed
            
            return context.sync().then(function(){
                console.log("Done");
            });
        });
    });
}).catch(handleError);
更多信息,请参见此处: