Office js (Word) : addFromNamedItemAsync 函数的参数不正确

问题描述

我目前正在创建 Word 插件,并想对当前 Word 文件中的搜索词创建评论。 为了插入绑定,我调用了 setBinding 方法。我收到一条错误消息,告诉我 addFromNamedItemAsync 方法的参数不正确,但我不明白为什么我在文档中被检查:https://docs.microsoft.com/en-us/javascript/api/office/office.bindings?view=excel-js-preview#addFromNamedItemAsync_itemName__bindingType__options__callback_addFromNamedItemAsync_itemName__bindingType__options__callback_ 但我找不到问题所在。

我的函数 find():

function find() {
        Word.run(function (context) {
            var searchResult = context.document.body.search('Salut',{ ignorePunct: true,matchWholeWord: true });

            searchResult.load('items');

            return context.sync().then(function () {
                console.log('Found count: ' + searchResult.items.length);
                var cpt = 0;
                var id = 0;
                searchResult.items.forEach(function (range) {
                    cpt = cpt + 1;
                    id = id + 1;
                    console.log('cpt: ' + cpt);
                    const contentControlledItem = range.insertContentControl();
                    contentControlledItem.appearance = "BoundingBox";
                    contentControlledItem.title = 'titleOk'+id;

                    setBinding(id,contentControlledItem);
                });

                // Synchronize the document state by executing the queued commands,// and return a promise to indicate task completion.
                return context.sync();
            });
        })
    } 

我的函数 setBindings():

function setBinding(bindingId,contentControlledItem) {
        console.log(bindingId + contentControlledItem.title);
        Office.context.document.bindings.addFromNamedItemAsync(contentControlledItem.title,//-------
            Office.BindingType.Text,//Problem is here
            { id: bindingId },//-------
            function(result) {
                if (result.error) {
                    console.log(`add binding error: ${result.error.message}`);
            } else {
                    console.log(`Added new binding with type: ${result.value.type} and id: ${result.value.id}`);
            }
        });
    }

先谢谢你!

解决方法

Id 必须是字符串:

function find() {
        Word.run(function (context) {
            var searchResult = context.document.body.search('Salut',{ ignorePunct: true,matchWholeWord: true });

            searchResult.load('items');

            return context.sync().then(function () {
                console.log('Found count: ' + searchResult.items.length);
                var cpt = 0;                
                var id = 0;
                searchResult.items.forEach(function (range) {
                    cpt = cpt + 1;
                    id = 'id'+cpt;                          //-----JUST HERE------
                    const contentControlledItem = range.insertContentControl();
                    contentControlledItem.appearance = "BoundingBox";
                    contentControlledItem.title = 'titleOk'+cpt;
                    setBinding(id,contentControlledItem);
                });

                // Synchronize the document state by executing the queued commands,// and return a promise to indicate task completion.
                return context.sync();
            });
        })
    }