【问题标题】:Check if text selection is in header/footer检查文本选择是否在页眉/页脚中
【发布时间】:2019-01-03 20:49:47
【问题描述】:

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

  • 正文中的文本和标题中的文本的选择的 OOXML 结构是相同的。
  • 选择UI单词本身在选择页脚/标头文本时禁用了评论CMETS部分。
  • 将文本选择对象转储到控制台时,没有对象字段指向页眉/页脚中的选择。

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

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

【问题讨论】:

    标签: ms-word office-js


    【解决方案1】:

    您可以通过查看选择范围的parentBody 属性来实现此目的。 parentBody 上的 type 属性将显示选择是在“标题”中还是在其他位置(请参阅documentation)。

    示例

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

    【讨论】:

    • 完美,parentBody.type 确实做到了。不知道页眉/页脚有自己的正文。
    猜你喜欢
    • 2020-01-13
    • 2012-09-03
    • 1970-01-01
    • 2012-03-28
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多