如何在不同的上下文中更新相同的内容控件



我正在使用word javascript Api来开发单词加载项,我需要在单击按钮时插入内容控件并发送ajax请求。在 ajax 响应中,我需要更新相同的内容控件。

我正在尝试使用以下方法:

1). 在文档集中将 cc 插入为"临时"标签并在获得 ajax 响应后,使用"

contentControls.getByTag"搜索 CC,但使用多个内容控件无法更新正确的 cc,因为 ajax 响应可能需要一些时间,因此多个 cc 将具有"临时"标签。

2). 在文档中插入 cc 后,我尝试使用以下方法加载 cc 'ID'

var range2 = context.document.getSelection().parentContentControlOrNullObject;
context.load(range2);

但它返回不确定。

请指导我如何达到上述要求。这是执行此操作的正确方法,或者我可以将相同的范围对象用于另一个单词运行并更新该范围的 cc。

应该非常简单。使用 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");
}

我认为这将在您的场景中为您提供帮助。谢谢!

最新更新