如何删除 Office JS 加载项中的 headerReferences 和 footerReferences?

How to delete headerReferences and footerReferences in Office JS Add-in?

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

这就是我清除正文和部分的方式...

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

您可以在此处查看更多信息:https://github.com/OfficeDev/office-js/issues/1904