Microsoft Word JavaScript API - 文档中文本选择的事件处理程序 [英] Microsoft Word JavaScript API - event handler for text selection in document

查看:46
本文介绍了Microsoft Word JavaScript API - 文档中文本选择的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 JavaScript API for Office 并且我找不到在用户选择 Word 文档中的文本时通知的处理程序.

I read JavaScript API for Office and I couldn't find a handler to notify when user select a text in the word document.

我知道我们可以从文档中复制选定/突出显示的文本,如下所示:

I am aware that we can copy the selected/highlighted text from document as follow:

Word.run(function(context) {
    let body = context.document.body;

    // ask for the user selected text
    let range = context.document.getSelection();
});

这种方法不是注册的回调或事件.使用这种方法我必须请求更新或检查用户是否选择了任何内容.

This approach is not a registered callback or an event. With this approach I have to request update or to check if user selected anything.

是否有现有的功能可以注册以获取用户与文档交互的通知?

Is there an existing function that I can register for getting notified for user interaction with document?

提前感谢您的帮助

推荐答案

getSelection() 方法实际上并未在文档中进行选择.它为您提供当前选择的范围.为了获得订阅文档选择事件所需的事件,您可以通过以下方式实现:

the getSelection() method does not actually make a selection in the document. it gives you the range that its currently selected. in order to get the events you need to subscribe to the document selection event, you can achieve that fairly simple just with:

function subscribeToEvent() {
    Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, handler);
}

function handler(evtArgs) { 
  // here you can handle the event. 
    console.log("select");
}

另一方面,如果您想以编程方式执行,range.select() 方法将触发选择更改事件.请查看这个脚本实验室片段,它基本上订阅了加载时的事件,然后如果你单击 RUN 按钮,您将看到最后一段被选中并触发了事件.

On the other hand the range.select() method WILL trigger the selection changed event if you want todo it programmatically. Please check out this Script Lab snippet, it basically subscribe to the event on load, then if you click the RUN button you will see that the last paragraph gets selected and the event triggered.

这篇关于Microsoft Word JavaScript API - 文档中文本选择的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆