テーブルを 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 />");
}