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.