Reputation: 464
Using below code to identify worksheet rename event and perform activity after rename. Same is working fine in online version of excel(online office365) on Chrome/edge browser but its not working on desktop version of excel(Microsoft 365 MSO (16.0.14326.21170) - 32-bit).
export const onSheetNameChange = event => {
Excel.run(context => {
return context.sync().then(() => {
const { nameAfter, nameBefore } = event;
console.log('nameBefore=>', nameBefore);
console.log('nameAfter=>', nameAfter);
});
});
};
export const onSheetRenameHandler = () => {
Excel.run(context => {
const sheets = context.workbook.worksheets;
sheets.onNameChanged.add(onSheetNameChange);
return context
.sync()
.then(() => {
console.log(
'A handler has been registered for the OnNameChanged event.',
);
})
.catch(error => {
console.log('error=>', error);
});
});
};
Its giving below ERROR: error=> RichApi.Error: You cannot perform the requested operation. at new n (excel-win32-16.01.js:25:241192) at i.processRequestExecutorResponseMessage (excel-win32-16.01.js:25:305358) at excel-win32-16.01.js:25:303421
If I change event handler code like below:
const sheets= context.workbook.worksheets.getActiveWorksheet(); sheets.onNameChanged.add(onSheetNameChange);
Then its working on desktop excel but only for active sheet. If I add new sheet and tries to rename the sheet onSheetNameChange event handler is not getting called.
I am using promise based approach. Even tried same code as per mentioned in THIS LINK. and getting same error.
Can someone please help to understand if there is any issue in above mentioned "onSheetRenameHandler " method? why its not working if I tries to load the sheet like "const sheets = context.workbook.worksheets" and then add the handler on it.
Upvotes: 1
Views: 187
Reputation: 464
Fixed by using below code. As it requires to add event handler on each sheet.
const onSheetRenameAddHandler = async callBack => {
await Excel.run(async context => {
const sheets = context.workbook.worksheets;
sheets.load('items/name');
sheets.load('name');
await context.sync();
for (let i = 0; i < sheets.items.length; i += 1) {
sheets.items[i].onNameChanged.add(callBack);
}
await context.sync();
console.log(
'A handler has been registered for the OnNameChanged event.',
);
});
}
One thing observed if we add new sheet in workbook then that sheet wont be added this onNameChanged handler method, working on that part as well as removing of this event else it will push the events on running stack which causes the duplicate execution of event handler.
Upvotes: 1