我正在使用word javascript Api开发一个word外接程序,我需要插入一个内容控件按钮点击并发送一个ajax请求。在ajax响应中,我需要更新相同的内容控件。
我试图采用以下方法:
1)。在document标记中插入cc作为‘临时’并在获得ajax响应之后,使用搜索CC,但使用多个内容控件无法将正确的cc更新为ajax响应可能需要时间,因此多个cc将具有‘临时’标记。
2)。在文档中插入cc之后,我尝试使用以下方法加载cc 'ID':
var range2 = context.document.getSelection().parentContentControlOrNullObject;
context.load(range2);
但它返回未定义。
请指导我如何达到上述要求。这是正确的方法,或者我可以在另一个词中使用相同的range对象,运行并更新该范围的cc。
发布于 2017-02-07 10:11:53
这样做应该很简单。当您在API中插入内容控件时,将返回一个内容控件对象。这实际上是该内容控件的句柄。加载之后,您可以使用它执行任何操作,包括添加、修改内容。查看下面的示例,说明如何做到这一点:
function InsertCCandUpdate() {
Word.run(function(context) {
// we first insert a content control, on this case on the selection!
//notice we'll hold a reference to the CC in the myCC var:
var myCC = context.document.getSelection().insertContentControl();
context.load(myCC);
return context.sync()
.then(function(){
// myCC holds a handle to the contentt control.... then we can update its content
myCC.insertText(getSomeContent(),"replace");
})
});
}
function getSomeContent(){
//this method is just to simulate your AJAX call.
return("some text from your AJAX call");
}
我想这会对你的情节有所帮助。谢谢!
https://stackoverflow.com/questions/41935230
复制相似问题