繁体   English   中英

如何在Word JS中获取选定的行和列索引并计数

[英]How to get selected rows and column index and count in Word JS

我有一个Word加载项(API 1.3)项目,可以在其中插入表并使它们成为内容控件。 我使用以下代码来识别用户是否单击表内部或选择其任何单元格。

 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个问题:

  1. 您正在订阅文档选择更改,但是您确实要进行“绑定选择更改”。 请注意,仅在表绑定上返回坐标。
  2. 一旦有了表绑定并订阅了正确的事件类型,就需要在处理程序上添加事件args,以访问所需的值。

请查看以下代码,以了解如何创建表绑定以及如何在处理程序上使用eventArgs获取所需的信息(还请注意,如果您在桌子):

  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); }); } }); 

希望这能为您指明正确的方向。 谢谢! 胡安

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM