无法在绑定时插入表

时间:2016-01-01 15:47:19

标签: ms-word ms-office office-addins office-js

我正在尝试将表插入RichText内容控件(我发现文档声明绑定仅适用于RichText控件)但它失败了:

  

指定数据对象的类型与当前选择不兼容。

我能够使用coercionType“html”将HTML插入到绑定中。 我还能够在选择光标处插入一个表,但如果有人必须单击他们想要的表,那么这会严重降低文档自动化体验。 我也可以通过编程导航到绑定然后执行插入来插入表,但这似乎有点hacky。 我的语法是错误的还是不能在没有当前选择的情况下在绑定中插入表?

CSHTML:

@{
    ViewBag.Title = "Home Page";
}

<div class="row">
    <button onclick="return addRowsWithoutSelection();"> Insert Table At Binding </button><br />
    <button onclick="return navigateToBinding();"> Navigate then insert table </button><br />
    <button onclick="return addRowsAtSelection();"> Insert Table At Selection </button><br />
    <button onclick="return addHTML();"> Insert HTML </button><br />
</div>
    Results: <div id="results"></div>
<div id="trace"></div>

Program.js:

var myTable;

// The initialize function is required for all apps.
Office.initialize = function (reason) {
    $(document).ready(function () {

        myTable = new Office.TableData();
        myTable.headers = ["First Name", "Last Name", "Grade"];
        myTable.rows = [["Brittney", "Booker", "A"], ["Sanjit", "Pandit", "C"], ["Naomi", "Peacock", "B"]];

        try {
            Office.context.document.bindings.addFromNamedItemAsync('TheTable',
                Office.BindingType.Text, { id: 'tbl' },
                    function (result) {
                        if (result.status === Office.AsyncResultStatus.Succeeded) {
                            trace('Control bound. Binding.id: ' + result.value.id + ' Binding.type: ' + result.value.type);
                        } else {
                            trace('Error:', result.error.message);
                        }
                    });

        } catch (e) { trace("Exception: " + e.message); }
    });
}

//THIS WORKS!
function addHTML() {
    try {
        Office.select("bindings#tbl").setDataAsync("<table border='1' width='100%'><thead><tr><th style='background-color:#ff00ff'>Description</th><th>From</th><th>To</th></tr></thead></table>", { coercionType: "html" },
            function (asyncResult) {
                if (asyncResult.status == "failed") {
                    trace('Error with addHTML : ' + asyncResult.error.message);
                } else { trace("Success calling addHTML()"); }
            });

    } catch (e) { trace("Exception: " + e.message); }
}
//THIS WORKS!
function addRowsAtSelection() {

    try {
        Office.context.document.setSelectedDataAsync(myTable, { coercionType: Office.CoercionType.Table },
            function (result) {
                var error = result.error
                if (result.status === Office.AsyncResultStatus.Failed) {
                    trace(error.name + ": " + error.message);
                } else { trace("Success calling addRowsAtSelection"); }
            });
    } catch (e) { trace("Exception: " + e.message); }
}

//FAIL!
function addRowsWithoutSelection() {
    try {
        Office.select("bindings#tbl").setDataAsync(myTable, { coercionType: Office.CoercionType.Table },
            function (asyncResult) {
                if (asyncResult.status == "failed") {
                    trace("Action failed with error: " + asyncResult.error.message);
                } else { trace("Success with addRowsWithoutSelection.");}
            });
    } catch (e) {trace("Exception: " + e.message);}
}

//WORKS!
function navigateToBinding() {
    Office.context.document.goToByIdAsync("tbl", Office.GoToType.Binding, function (asyncResult) {
        if (asyncResult.status == "failed") {
            trace("Go To Binding failed with error: " + asyncResult.error.message);
        }
        else {
            trace("Navigation successful");
            try {
                Office.context.document.setSelectedDataAsync(myTable, { coercionType: Office.CoercionType.Table },
                   function (asyncResult) {
                       if (asyncResult.status == "failed") {
                           trace("Action failed with error: " + asyncResult.error.message);
                       } else { trace("Success with addRowsWithoutSelection."); }
                   });
            } catch (e) { trace("Exception: " + e.message); }
        }
    });
}
function trace(msg) {
    $("#trace").append(msg + "<br />");
}

enter image description here

1 个答案:

答案 0 :(得分:1)

我想你可能有错误的招架类型。在你的代码中     Office.BindingType.Text,{id:&#39; tbl&#39; }, 您选择使用文本绑定。但是,如果您正在处理表并希望使用表功能,则可能需要使用表绑定。您可以在https://msdn.microsoft.com/en-us/library/office/fp142273.aspx

上找到有关绑定类型的更多信息

感谢, 天空