Currently, I use the following code to listen to the change of Sheet1!A1:B2
:
function addEventHandler() {
Office.context.document.bindings.addFromNamedItemAsync("Sheet1!A1:B2", "matrix", { id: "myBind" }, function (asyncResult) {
Office.select("binding#myBind").addHandlerAsync(Office.EventType.BindingDataChanged, onBindingDataChanged2016);
})
}
function onBindingDataChanged2016(eventArgs) {
Excel.run(function (ctx) {
var foundBinding = ctx.workbook.bindings.getItem(eventArgs.binding.id);
var myRange = foundBinding.getRange();
myRange.load(["address", 'values']);
return ctx.sync().then(function () {
console.log(JSON.stringify({ "address": myRange.address, "value": myRange.values }));
// costly reaction
})
})
}
Because my reaction to the change is quite costly, I want to undertake it only when it is really necessary. I have two questions:
1) If I want to listen to multi ranges, is it possible to define only ONE listener for "Sheet1!A1:B2, Sheet1!A9:B10, Sheet1!A100:B120"
? Do I have to add ONE handler for EACH range?
2) Is it possible to express I listen only to the change of VALUES
, rather than formats, etc.?
Optional question:
Is it possible to specify a debounce somewhere? For example,
we initialise a clock with
0
if a listener is triggered, we record the
binding id
of the change, and set the clock to0
when the clock reaches
1 second
(ie, it has been quiet for 1 second), we react to all the recorded changes (ie, load all the changed ranges and undertake the costly reaction)