0

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;
        if (nameBefore !== nameAfter) {
            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.',
            );
        });
    });
};

Followed this documentation link to implement same.

Could see this error in console: Uncaught (in promise) 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

ActionIndex: Code: "AccessDenied" HttpStatusCode: 403 Location: "WorksheetCollection._RegisterEventNameChanged" Message: "You cannot perform the requested operation."

Can someone please let me know "onNameChanged" event supports desktop excel and its released to use? Please help if I am missing anything. Thanks in advance!

sagar
  • 464
  • 4
  • 13

1 Answers1

0

Please take a look at the code in this Script Lab code snippet. The onNamedChanged event API is only available for Excel Web.

To sample has the following code to register the event handler for the onNameChanged event:

async function registerOnNameChangedHandler() {
  await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets;
      sheet.onNameChanged.add(onWorksheetNameChanged);

    await context.sync();
    console.log("A handler has been registered for the OnNameChanged event.");
  });
}

This is the event handler code:

async function onWorksheetNameChanged(event) {
  await Excel.run(async (context) => {
    console.log(
      "Handler for worksheet onNameChanged event has been triggered. Name 
      changed for worksheet Id : " + event.worksheetId
    );
  });
}

In your code, it looks like you are adding the event handler for the onNamedChanged event in the callback function you are you are using as the event handler. That code will not get called until the event handler has been registered. Try to move the code to add the event handler out of the callback function.

  • Thanks Jakob Nielsen-MSFT, As you suggested removed event handler from callback, updated question with latest code changes But it giving same error. Only difference is I have implemented it using promise based approach. Could you please help on same? – sagar Nov 21 '22 at 06:49
  • code is working on desktop if I add event handler on active sheet like below: const activeSheet = context.workbook.worksheets.getActiveWorksheet(); activeSheet.onNameChanged.add(onWorksheetNameChanged); But on desktop excel its only working for active sheet, If I add new sheet then its not working on that sheet. And If I try to add event on "context.workbook.worksheets" Its not working on desktop excel giving error. Uncaught (in promise) RichApi.Error: You cannot perform the requested operation – sagar Dec 07 '22 at 12:44
  • Sagar, the event is only available for Excel Online as noted in the documentation of the Worksheet class https://learn.microsoft.com/en-us/javascript/api/excel/excel.worksheet?view=excel-js-preview#excel-excel-worksheet-onnamechanged-member. Please consider logging a request for an API that works cross-platform in the Microsoft 365 Developer Platform ideas list: https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/idb-p/Microsoft365DeveloperPlatform – Jakob Nielsen-MSFT Dec 13 '22 at 19:14