我想跟踪的SelectionChanged上述解决办法是聪明,但我相信它可以让你在事件之间的竞争条件射击。我测试了上述解决方案,并且看起来SelectionChange在DataChanged事件之前触发,这意味着您将抓取当前选定的单元格而不是先前的单元格。我不认为你能避免这种竞争状态,因为事件是异步但可能你可以跟踪过去和当前的选择是这样的:
myBinding.addHandlerAsync(Office.EventType.BindingSelectionChanged, onSelectionChange);
var startRow, rowCount, startColumn, columnCount;
var previousStartRow, previousRowCount, previousStartColumn, previousColumnCount;
function onSelectionChange(eventArgs){
// save "previous" selected cell into previous variables
previousStartRow = startRow;
previousRowCount = rowCount;
previousStartColumn = startColumn;
previousColumnCount = columnCount;
// re-assign the current selected to the eventArgs
startRow = eventArgs.startRow;
rowCount = eventArgs.rowCount;
startColumn = eventArgs.startColumn;
columnCount = eventArgs.columnCount;
}
function onBindingDataChange(eventArgs){
eventArgs.binding.getDataAsync({
startRow: previousStartRow,
rowCount: previousRowCount,
startCol: previousStartColumn,
columnCount: previousColumnCount
}, function(result){
// Do whatever you need with result.value.
// You might want to compare and update your in-memory representation of the data.
});
}
我测试了它和它的作品......不是很好,并可能有更好的跟踪多个层面的方式,但它似乎工作。理想情况下,DataChanged事件可以在像VSTO这样的EventArgs中实现。手指穿过它的到来很快:)
注 - 它似乎有复制&粘贴工作,但这种解决方案将需要加强,以处理以下情况:
- 细胞拖动ñ”降带“+”号。
- 多个小区中选择,然后更新单个细胞用键盘和输入
更新1 - 另一种情况
- 当更新绑定的边缘细胞,并按下回车键就会造成选择在绑定范围之外。在选择更改事件没有揭开序幕,并在上述解决方案将失败
1.用户拖放操作如何工作?这些事件是否总是一个接一个地出现?此外,它看起来像多个用户在工作表上时,所有用户都将获得'BindingSelectionChanged'事件。如何区分在这种情况下更改的选择。 – renil
您有两种选择:或者等到用户在拖放操作之后更改其选择(这适用于上面的解决方案),或者修改解决方案以检查*当前*选定范围的更改。 –
您能否详细解释选项2?我们如何跟踪大型Excel表中的变化?任何推荐的方法?第一个选项对用户有很大的依赖性,除非办公室api触发拖动结束事件。 – renil