如何在Word JS中获取选定的行和列索引和计数
How to get selected rows and column index and count in Word JS
我有一个 Word 加载项 (API 1.3) 项目,我可以在其中插入 table 并使它们成为内容控件。我使用以下代码来识别用户是否在 table 内部单击或选择其任何单元格。
Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged,
function() {
Word.run(function(ctx) {
var ctrl = ctx.document.getSelection().parentContentControl;
return ctx.sync()
.then(function() {
if (!ctrl.isNull) { // found - clicked inside the control
// ... load some properties, ...
ctrl.load('tag'); // How to get startRow, startCol, rowCount, colCount?
ctx.sync()
.then(function() {
console.log(ctrl.tag);
}).catch(function(err) {
console.log(err);
});
}
}).catch(function(err) {
console.log(err);
});
});
});
有没有办法像 selectionChanged 的绑定事件处理程序一样从这里获取 startRow、startCol、rowCount、colCount?
感谢分享这个问题。您的代码有 2 个问题:
- 您正在订阅文档选择更改,但您确实想要进行绑定选择更改。请注意,坐标仅在 Table 绑定上返回。
- 一旦您拥有 table 绑定并订阅正确类型的事件,您需要在处理程序上添加事件参数以访问您需要的值。
查看以下代码以了解如何创建 table 绑定以及如何在处理程序上使用 eventArgs 来获取您需要的信息(另请注意,您将在行中获得 undefined如 headers,如果您在 table 中定义了 headers):
Office.context.document.bindings.addFromSelectionAsync(Office.BindingType.Table, function (result) {
if (result.status == Office.AsyncResultStatus.Succeeded) {
// after creating the binding i am adding the handler for the BindingSelectionChanged, check out eventArgs usage....
var binding = result.value;
binding.addHandlerAsync(Office.EventType.BindingSelectionChanged, function (eventArgs) {
app.showNotification('Selection Coordinates: ' + eventArgs.startColumn + " " + eventArgs.columnCount + " " + eventArgs.startRow + " " + eventArgs.rowCount);
});
}
});
希望这能让您朝着正确的方向前进。谢谢!
娟.
我有一个 Word 加载项 (API 1.3) 项目,我可以在其中插入 table 并使它们成为内容控件。我使用以下代码来识别用户是否在 table 内部单击或选择其任何单元格。
Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged,
function() {
Word.run(function(ctx) {
var ctrl = ctx.document.getSelection().parentContentControl;
return ctx.sync()
.then(function() {
if (!ctrl.isNull) { // found - clicked inside the control
// ... load some properties, ...
ctrl.load('tag'); // How to get startRow, startCol, rowCount, colCount?
ctx.sync()
.then(function() {
console.log(ctrl.tag);
}).catch(function(err) {
console.log(err);
});
}
}).catch(function(err) {
console.log(err);
});
});
});
有没有办法像 selectionChanged 的绑定事件处理程序一样从这里获取 startRow、startCol、rowCount、colCount?
感谢分享这个问题。您的代码有 2 个问题:
- 您正在订阅文档选择更改,但您确实想要进行绑定选择更改。请注意,坐标仅在 Table 绑定上返回。
- 一旦您拥有 table 绑定并订阅正确类型的事件,您需要在处理程序上添加事件参数以访问您需要的值。
查看以下代码以了解如何创建 table 绑定以及如何在处理程序上使用 eventArgs 来获取您需要的信息(另请注意,您将在行中获得 undefined如 headers,如果您在 table 中定义了 headers):
Office.context.document.bindings.addFromSelectionAsync(Office.BindingType.Table, function (result) {
if (result.status == Office.AsyncResultStatus.Succeeded) {
// after creating the binding i am adding the handler for the BindingSelectionChanged, check out eventArgs usage....
var binding = result.value;
binding.addHandlerAsync(Office.EventType.BindingSelectionChanged, function (eventArgs) {
app.showNotification('Selection Coordinates: ' + eventArgs.startColumn + " " + eventArgs.columnCount + " " + eventArgs.startRow + " " + eventArgs.rowCount);
});
}
});
希望这能让您朝着正确的方向前进。谢谢! 娟.