我正在使用Office JS API嘗試在文檔中的表格綁定周圍添加內容控件。Office Word JS - 從表格選擇內容控件
我遇到的問題是在選擇表格的綁定後,使用goToByIdAsync()
,內容控件僅在表格的最後一行周圍創建,而不是選擇。返回ctx.document.getSelection()
的值,我可以看到選定的範圍只是選定表格中的最後一行。我需要使用Binding對象來知道Table的選定單元格範圍。有什麼我做錯了嗎?
var bindingId = '123';
var doc = Office.context.document;
// Create table in document.
doc.setSelectedDataAsync(tableValue, { coercionType: Office.CoercionType.Table }, function (asyncResult) {
// Add Binding to table
doc.bindings.addFromSelectionAsync(Office.BindingType.Table, { id: bindingId }, function (asyncResult) {
// Select the table object
doc.goToByIdAsync(bindingId, Office.GoToType.Binding, { selectionMode: 'selected' }, function (asyncResult) {
// Create Content Control
createCC();
});
});
});
function createCC(){
Word.run(function (ctx) {
var range = ctx.document.getSelection();
var html = range.getHtml();
return ctx.sync().then(function() {
console.log('Selected Range', html.value); // Only displays last row in the table.
var myContentControl = range.insertContentControl();
myContentControl.tag = bindingId;
myContentControl.title = 'My Content Control';
return ctx.sync().then(function() {
//Content control for table created
});
}).catch(function (err) {
errorHandler(err);
});
});
}
非常感謝您的答覆胡安!有道理,這是有效的。但是,無論如何要使用CC的不同屬性而不是標題(即標籤)?在我的應用程序中有可能標題不會是唯一的。 – user35202
不幸的不是。它必須是標題。如果許多ccs共享相同的標題,則無法創建bindingByName,因此請確保分配不同的標題。 –