Office js 使用contentControl.track在Word Api Javascript的交叉上下文中更改属性
我已经完成了接下来的功能。 首先,选择内容控件并使用“跟踪”方法存储: 第二个,它将内容控制对象和一种颜色作为参数,以使用此颜色进行内容控制的高亮显示:Office js 使用contentControl.track在Word Api Javascript的交叉上下文中更改属性,office-js,office365api,word-addins,office365connectors,Office Js,Office365api,Word Addins,Office365connectors,我已经完成了接下来的功能。 首先,选择内容控件并使用“跟踪”方法存储: 第二个,它将内容控制对象和一种颜色作为参数,以使用此颜色进行内容控制的高亮显示: export async function highlightOneContentControl (currrentContentControl, color) { await window.Word.run(async context => { currrentContentControl.select() cons
export async function highlightOneContentControl (currrentContentControl, color) {
await window.Word.run(async context => {
currrentContentControl.select()
console.log('currrentContentControl', currrentContentControl)
currrentContentControl.font.highlightColor = color
currrentContentControl.untrack()
await context.sync()
})
}
前端的代码是接下来的两个按钮:
<Button
onClick={async () => {
const cc = await getContentControl()
console.log('in button', cc)
setMyCC(cc)
}}
>
get CC
</Button>
<Button
onClick={async () => {
await highlightOneContentControl(myCC, 'Yellow')
}}
>
highlight CC
</Button>
{
const cc=await getContentControl()
console.log('登录按钮',抄送)
setMyCC(cc)
}}
>
获取抄送
{
等待highlightOneContentControl(myCC,“黄色”)
}}
>
突出显示CC
下面是console.log消息:
如您所见,font.hightColor似乎已更改,但内容控件仍然没有突出显示:
已解决
我目前找到的解决方案是将内容控件作为参数传递给run方法:
export async function highlightOneContentControl (currentContentControl, color) {
await window.Word.run(currentContentControl, async context => {
currentContentControl.select()
console.log('currentContentControl', currentContentControl)
currentContentControl.font.highlightColor = color
currentContentControl.untrack()
await context.sync()
})
}
请问,你能告诉我答案是正确的还是错误的吗?
非常感谢您抽出时间 [虽然FranchoZGZ已经回答了他们自己的问题,但我认为添加更多细节可能会有所帮助,因为我为此苦苦挣扎了几个小时。] 我发现,当您跟踪一个实体(段落/…)时,虽然这意味着您可以在不同的Word.run上下文中使用它,但您必须使用该项的上下文,而不是Word.run中的上下文 希望这个例子有助于解释。这看起来非常微妙,而且很难找出你做错了什么。所以,希望这有助于节省其他人的时间
let selection = await Word.run(async ctx => {
let selectionInner = ctx.document.getSelection();
ctx.load(selectionInner);
await ctx.sync();
selectionInner.track();
await ctx.sync();
return selectionInner;
});
await Word.run(async ctx => {
// THIS IS WRONG (AS USES THE WRONG CONTEXT)
ctx.load(selection.paragraphs);
// THIS WORKS AS USES THE CONTEXT FOR THE TRACKED ITEM
selection.context.load(selection.paragraphs);
});
let selection = await Word.run(async ctx => {
let selectionInner = ctx.document.getSelection();
ctx.load(selectionInner);
await ctx.sync();
selectionInner.track();
await ctx.sync();
return selectionInner;
});
await Word.run(async ctx => {
// THIS IS WRONG (AS USES THE WRONG CONTEXT)
ctx.load(selection.paragraphs);
// THIS WORKS AS USES THE CONTEXT FOR THE TRACKED ITEM
selection.context.load(selection.paragraphs);
});