首页 > 解决方案 > 检查文本选择是否在页眉/页脚中

问题描述

我们创建的 Word 插件允许向文本选择添加自定义注释。Word 不允许在页眉/页脚中添加注释。Because of that, users should get warned when text in a header/footer is selected.

如何以编程方式发现在页眉/页脚中选择了文本?

问题:https ://github.com/OfficeDev/office-js/issues/341

标签: ms-wordoffice-js

解决方案


您可以通过查看parentBody选择范围的属性来实现这一点。上的type属性parentBody将显示选择是在“标题”中还是在其他地方(请参阅文档)。

例子

function determineSelectionInHeader() {
    Word.run(function (context) {
        const HEADER_TYPE = "Header";

        // Retrieve and load 'type' of selection.
        var selection = context.document.getSelection();
        var parentBody = selection.parentBody;
        parentBody.load("type");

        context
            .sync()
            .then(function () {
                if (parentBody.type === HEADER_TYPE) {
                    console.log("This is the header");
                }
            });
    });
}

推荐阅读