Your add-in may react to changing the data in a worksheet. To detect these changes, you can register an event handler for the onChanged
event of a worksheet
. Event handlers for the onChanged
event receive a WorksheetChangedEventArgs
object when the event fires.
The WorksheetChangedEventArgs
object provides information about the changes and the source. Since onChanged
fires when either the format or value of the data changes, it can be useful to have your add-in check if the values have actually changed. The details property encapsulates this information as a ChangedEventDetail
. The following code sample shows how to display the before and after values and types of a cell that has been changed.
// This function would be used as an event handler for the Worksheet.onChanged event.
function onWorksheetChanged(eventArgs) {
Excel.run(function (context) {
let details = eventArgs.details;
let address = eventArgs.address;
// Print the before and after types and values to the console.
console.log(`Change at ${address}: was ${details.valueBefore}(${details.valueTypeBefore}),`
+ ` now is ${details.valueAfter}(${details.valueTypeAfter})`);
return context.sync();
});
}
See Work with worksheets using the Excel JavaScript API for more information.
Note, you can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team goes through the planning process.