Ms word 在MS word Office.js加载项中实现键盘快捷键
我试图在Word加载项中实现一个功能,跟踪用户在MS Word中键入的内容,然后执行自动替换。例如,用户可以键入Ms word 在MS word Office.js加载项中实现键盘快捷键,ms-word,office-js,Ms Word,Office Js,我试图在Word加载项中实现一个功能,跟踪用户在MS Word中键入的内容,然后执行自动替换。例如,用户可以键入 :smile: :frown: 我会自动将它们替换为 :) :( 我已经实现了一个处理程序来接收documentSelectionChanged事件,目前我正在使用以下事件处理程序: function refLabelShortcut() { var range, results, par; Word.run(function (context) { range
:smile: :frown:
我会自动将它们替换为
:) :(
我已经实现了一个处理程序来接收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');
});
});
}
当搜索不是通配符时,上面的代码可以工作。我已经注释掉了非通配符搜索,所以您可以看到什么是有效的
执行通配符搜索时,控制台显示:
[text of the matching paragraph]
1
就这样。因此,代码不会到达最后一个then
函数
知道为什么通配符搜索不起作用吗?
results.load('items')
行有错误吗?范围。段落是一个集合对象,不能加载集合对象,因此Range.load('parations')
不起任何作用。为了读取段落集合的项目,必须加载这些项目(或段落对象的某些属性)。尝试:
范围.段落.加载('items')代码>您能将其插入函数吗range.insertText(“:smile:”,“:”)
@Mech,根据,它不是那样工作的。Hi Rich,我在第二个示例中又添加了两个load
语句,但是项在最后一个log语句中仍然没有加载。还有什么想法吗?删除行range.load('段落')代码>也执行行console.log(par.text);现在run?Rich,console.log(par.text)
在你提出建议之前就一直对我有效。不起作用的部分是搜索段落<代码>结果。未加载项目
。我还删除了context.load(结果)代码>行。然后(context.sync())
应该是。然后(context.sync)
。当我和我推荐的其他人一起进行更改时,您的代码对我来说是有效的。谢谢Rick,我已经解决了这个问题,但它仍然不能用于通配符搜索。我简化了我的问题,把重点放在最新的代码上。当使用通配符搜索运行当前代码时,是否在最后一个then
函数中获得console.log
语句?