如何使用保存到React状态的范围-Microsoft Word javascript API? [英] How to use ranges saved to React state - Microsoft Word javascript API?
问题描述
我正在使用Microsoft Word Javascript API.我已经使用.search()函数检索范围数组,然后将它们保存到App.js React组件状态下的state.definitions中.这部分有效.当我尝试使用console.log(JSON.stringify(this.state.definitions))打印状态时,我看到了刚刚保存的范围.
I am using the Microsoft Word Javascript API. I have used the .search() function to retrieve an array of ranges and then have saved them to state.definitions in my App.js React component state. This part works. When I try to print out the state using console.log(JSON.stringify(this.state.definitions)), I see the ranges that I just saved.
在一个单独的函数中,我想检索这些范围并以新的颜色突出显示它们.这部分不起作用.我没有任何错误,但是我没有看到文档中的任何突出显示更改.
In a separate function, I want to retrieve those ranges and highlight them in a new color. This part does not work. I don't get any errors, but I don't see any highlight changes in the document.
有趣的是,如果在保存状态之前尝试突出显示范围,它会起作用.这使我认为我从状态检索的范围实际上不是Word可以理解的范围.
Interestingly, if I try to highlight the ranges BEFORE saving them to state, it works. This makes me think that the ranges that I am retrieving from state are not actually the ranges understood by Word.
任何帮助将不胜感激.
var flattenedTerms包含一系列从Word上面几行中检索到的范围项.该代码成功更改了字体
var flattenedTerms contains an array of range items that were retrieved from Word a few lines above. This code successfully changes the font
for (var i = 0; i < flattenedTerms.length; i++) {
console.log('flattenedTerms: ', flattenedTerms[i]);
flattenedTerms[i].font.color = 'purple';
flattenedTerms[i].font.highlightColor = 'pink';
flattenedTerms[i].font.bold = true;
}
return context.sync().then(function () {
return resolve(flattenedTerms);
})
})
现在,包含范围项目的flattenedTerms数组已使用this.setState保存到state.definitions.这无法更改字体.所有console.logs都可以打印.
Now the flattenedTerms array, which contains the range items, has been saved to state.definitions using this.setState. This fails to change the font. All of the console.logs do print.
highlightDefinedTerms = () => {
var self = this;
return Word.run(
function (context) {
var definitions = self.state.definitions;
console.log('Highlighting ', definitions.length, ' terms.');
for (var i = 0; i < definitions.length; i++) {
console.log('Highlighting definition: ', JSON.stringify(definitions[i]));
definitions[i].font.color = 'blue';
definitions[i].font.highlightColor = 'red';
definitions[i].font.bold = true;
}
return context.sync();
}
)
}
推荐答案
您需要将第一个参数传递给"Word.run",以指定要恢复其上下文的对象.
You need to pass a first parameter to "Word.run" to specify the object whose context you want to resume.
Word.run(self.state.definitions,function(context)...)
Word.run(self.state.definitions, function(context) ...)
这篇关于如何使用保存到React状态的范围-Microsoft Word javascript API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!