1

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

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

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

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>

程序.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 />");
}

在此处输入图像描述

4

1 回答 1

1

我想你可能弄错了投标类型。在您的代码
Office.BindingType.Text, { id: 'tbl' } 中,您选择使用文本绑定。但是,如果您正在处理表格并想要使用表格功能,则可能需要改用表格绑定。您可以在https://msdn.microsoft.com/en-us/library/office/fp142273.aspx上找到有关绑定类型的更多信息

谢谢,天空

于 2016-01-08T21:36:09.090 回答