简体   繁体   English

如何在Word中将一个处理程序绑定到多个ContentControl(具有相同标题)? 将Javascript API用于Office

[英]How to Bind one handler to multiple ContentControls (with same title) in Word? Use Javascript API for Office

I'm developing with JavaScript API for Office, MS Word 2016, VisualStudio 2015. There are multiple Rich Text ContentContols with a same title in the document. 我正在使用Office,MS Word 2016,VisualStudio 2015的JavaScript API进行开发。文档中有多个具有相同标题的Rich Text ContentContols。 I'm trying to bind these ContentControls to a handler so that I can get onBindingDataChanged notification. 我试图将这些ContentControls绑定到处理程序,以便可以获取onBindingDataChanged通知。

Is there a way to bind the ContentControls to one handler with their own ID? 有没有一种方法可以将ContentControl绑定到具有自己ID的处理程序? or pass ContentControls' id as one parameter? 或通过ContentControls的ID作为一个参数?

My current code is like: 我当前的代码是这样的:

function bindNamedItem() {

    Office.context.document.bindings.addFromNamedItemAsync("CCTitle", Office.BindingType.Text, { id: 'ccbind' }, function (result) {
        if (result.status == 'succeeded') {
            console.log('Added new binding with type: ' + result.value.type + ' and id: ' + result.value.id);
        }
        else
            console.log('Error: ' + result.error.message);
    });

}
 function addEventHandlerToBinding() {
    Office.select("bindings#ccbind").addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged);
}

 var onBindingDataChanged = function (result) {
        console.log(result);     
    }

Since there are multiple contentcontrols in the document with title "CCTitle", addFromNamedItemAsync in function bindNamedItem will give error: Multiple objects with the same name were found. 由于文档中有多个标题为“ CCTitle”的addFromNamedItemAsync ,因此函数bindNamedItem将给出错误:找到Multiple objects with the same name were found.

What I'm trying to achieve is to get the ContentControls' id and content whenever the user make some change to any of them. 我要实现的目标是,只要用户对其进行任何更改,都将获取ContentControls的ID和内容。 Is there any idea to help? 有什么需要帮助的想法吗? Thanks in advance. 提前致谢。

As you've discovered, the naming of the content controls prevents you from binding based on the name. 如您所知,内容控件的命名使您无法基于名称进行绑定。 However, there is a workaround you can use to bind to every content control: 但是您可以使用绑定到每一个内容控件的解决方法:

  1. First retrieve Document .contentControls, which returns an array of all the content controls in the doc called a ContentControlCollection . 首先检索Document .contentControls,它返回文档中称为ContentControlCollection的所有内容控件的数组。
  2. Each element in the array is a ContentControl object . 数组中的每个元素都是一个ContentControl对象 Perform steps 3-6 in sequence for each ContentControl: 对每个ContentControl依次执行步骤3-6:
  3. Check the name of the ContentControl with contentControl.title. 使用contentControl.title检查ContentControl的名称。 If it matches the name you're looking for (CCTitle) then proceed with the following steps. 如果名称与您要查找的名称(CCTitle)相匹配,请继续执行以下步骤。 Otherwise start back at step 3 for the next ContentControl. 否则,请从步骤3重新开始下一个ContentControl。
  4. Call the ContentControl's select() method with the default parameters to cause word to select it. 使用默认参数调用ContentControl的select()方法 ,以使word对其进行选择。
  5. Once you've received confirmation in the callback that the ContentControl was selected, call Bindings.addFromSelectionAsync() with the Text bindingType. 在回调函数中收到确认已选择ContentControl的确认后,请使用Text bindingType调用Bindings.addFromSelectionAsync()
  6. Once you've received confirmation in the callback that the Binding was created successfully, call Binding.addHandlerAsync with the BindingDataChanged EventType . 在回调中收到确认已成功创建绑定的确认后,请使用BindingDataChanged EventType调用Binding.addHandlerAsync Use can use the same handler function for all of these bindings if you'd like. 如果需要,可以对所有这些绑定使用相同的处理函数。

One of the disadvantages of this workaround is that there are many chained asynchronous calls, so the performance may not be as fast as you'd like. 这种解决方法的缺点之一是有许多链接的异步调用,因此性能可能不如您想要的快。 As a result, I would recommend tying this operation to some user action and/or adding loading UI in the task pane in order to avoid confusing the user. 因此,我建议将此操作绑定到某些用户操作和/或在任务窗格中添加加载UI,以避免使用户感到困惑。

-Michael (PM for Office add-ins) -Michael(Office加载项的PM)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM