简体   繁体   English

在 MS Word Office.js 插件中实现键盘快捷键

[英]Implement keyboard shortcuts in MS word Office.js add-in

I'm trying to implement a feature in a Word add-in where I track what the user typing in MS Word and then perform automatic replacements.我正在尝试在 Word 插件中实现一项功能,我可以在其中跟踪用户在 MS Word 中键入的内容,然后执行自动替换。 For example, the user could type例如,用户可以输入

:smile: :frown:

and I would automatically replace them with我会自动将它们替换为

:) :(

I've implemented a handler to receive documentSelectionChanged events, and I'm currently using the following event handler:我已经实现了一个处理程序来接收documentSelectionChanged事件,我目前正在使用以下事件处理程序:

function refLabelShortcut() {
  var range, results, par;
  Word.run(function (context) {
    range = context.document.getSelection();
    range.paragraphs.load('items');
    return context.sync()
    .then(function() {
      par = range.paragraphs.items[0];
      console.log(par.text); // THIS WORKS!
      results = par.search('the*', {matchWildCards: true});
      //results = par.search('the');
      results.load('items'); // SOMETHING WRONG HERE?
      console.log('1');
    })
    .then(context.sync)
    .then(function() {
      console.log('2a'); // DOESN'T GET TO THIS LINE :(
      console.log(results.items.length);
      console.log('2b');
    });
  });
}

The above code works when the search isn't a wildcard one.当搜索不是通配符时,上面的代码有效。 I've commented out the non-wildcard search so you can see what does work.我已经注释掉了非通配符搜索,所以你可以看到什么是有效的。

When you do the wildcard search, the console shows:进行通配符搜索时,控制台显示:

[text of the matching paragraph]
1

and that's it.就是这样。 So the code doesn't get to the last then function.所以代码没有到达最后一个then函数。

Any idea why this doesn't work with a wildcard search?知道为什么这不适用于通配符搜索吗? Is there a mistake with the results.load('items') line? results.load('items')行是否有错误?

Range.paragraphs is a collection object, you can't load a collection object, so range.load('paragraphs') doesn't do anything. Range.paragraphs 是一个集合对象,你不能加载一个集合对象,所以range.load('paragraphs')不做任何事情。 In order to read the items of the paragraph collection, you have to load the items (or some property of the paragraph object).为了读取段落集合的项目,您必须加载项目(或段落对象的某些属性)。 Try:尝试:

range.paragraphs.load('items');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM