Hide Unprotect Sheet options

Posted by: mhill on 6 July 2023, 4:30 am EST

    • Post Options:
    • Link

    Posted 6 July 2023, 4:30 am EST - Updated 6 July 2023, 4:35 am EST

    Hi. I am using SpreadJS 14 with the Designer Component.

    I am presenting the user with the a spreadsheet that has a protected sheet, and I am trying to stop the user from unprotecting the sheet.

    I have found three places where they can do this:

    1. On the Settings/SheetSettings dialog. I have hidden this tab to avoid them clicking that option.
    2. On the Cell Format submenu. I have hidden this option by traversing the designerConfig.ribbon dom
    3. The last place I have found is on the context menu by right-clicking on the tab itself. This shows the option to “Move or Copy…” the sheet, for example, but also the option to Unprotect the sheet - but I have not found a way to stop this menu option from showing up.

      So my question is, either
    4. Is there a global setting to stop a sheet from being unprotected (or some event I can trap and cancel) OR
    5. Is there a way for me to redefine the tab context menu and hide the Unprotect sheet option somehow.

    Thanks,

    Mark

  • Posted 6 July 2023, 8:29 am EST - Updated 6 July 2023, 8:34 am EST

    Hi Mark,

    Currently, there is no global option or event to prevent the user from unprotecting the sheet.

    However, you could override the onOpenMenu method, check if the sheet is protected, and then remove the context menu option from the list of available options.

    Kindly refer to the following code snippet and the attached sample:

        // Override the onOpenMenu method
        var oldOpenMenu = spread.contextMenu.onOpenMenu;
        spread.contextMenu.onOpenMenu = function (menuData, itemsDataForShown, hitTest, spread) {
            oldOpenMenu.apply(this, arguments);
            if (hitTest.tabStripHitInfo && hitTest.tabStripHitInfo.sheetTab) {
                var sheet = spread.getSheetFromName(hitTest.tabStripHitInfo.sheetTab.sheetName);
                if (sheet.options.isProtected) {
                    let index = itemsDataForShown.findIndex((item) => item.name === "unprotectSheet");
                    itemsDataForShown.splice(index, 1);
                }
            }
        }

    References:

    onOpenMenu method: https://www.grapecity.com/spreadjs/api/v15/classes/GC.Spread.Sheets.ContextMenu.ContextMenu#onopenmenu

    Regards,

    Ankit

    purejs.zip

  • Posted 6 July 2023, 11:13 am EST

    Wow! That seemed to work. Thank you so much. I’ll get back to you if I have any other problems (or find another Unprotect Sheet option somewhere!)

    Cheers

  • Posted 11 April 2025, 3:24 am EST

    It would be helpful to have a disableProtection method on the designer config it would save everyone hours and hours of repeating the same code by figuring it out themselves . Just the adding code here for someone else to use.

    If developers wrote it , we will rely on ids and labels that you may change and it might break our application

    variable **spreadSheet ** is instance of GC.Spread.Sheets.Workbook

    variable designerConfigis GC.Spread.Sheets.Designer.DefaultConfig

    Removing protect and unprotect options from the CellsFormat button group pf the homeRibbon

            const homeMenu = designerConfig.ribbon.find(s => s.id === "home");  
            const cellsButtonGroup = homeRibbon.buttonGroups.find(s => s.label === "Cells");
            const cellsFormatButtonGroup = cellsButtonGroup.commandGroup.children.find(s => s.command === "cellsFormat");
            const unprotectIndex = cellsFormatButtonGroup.children.indexOf('unProtectSheet');
            if (unprotectIndex > -1) {
                cellsFormatButtonGroup.children.splice(unprotectIndex, 1);
            }
            const protectIndex = cellsFormatButtonGroup.children.indexOf('"cellFormatProtectSheet"');
            if(protectIndex > -1) {
                cellsFormatButtonGroup.children.splice(protectIndex, 1);
            }        

    **Removing Sheet settings from the Settings Ribbon **,

             const settingsRibbon = config.ribbon.find(s => s.id === "settings");
            const sheetSettingsIndex = settingsRibbon.buttonGroups.findIndex(s => s.label === "Sheet Settings");
            if (sheetSettingsIndex > -1) {
                settingsRibbon.buttonGroups.splice(sheetSettingsIndex, 1);
            }        

    May be on the workbook it is for developers to do as they might want to do other things on open menu of the context menu

    Removing unprotect option from the context menu if sheet is set to be protected

     spreadSheet.contextMenu.onOpenMenu = function (menuData, itemsDataForShown, hitTest) {
            oldOpenMenu.apply(this, arguments);
            if (hitTest.tabStripHitInfo && hitTest.tabStripHitInfo.sheetTab) {
                spreadSheet.sheets.forEach(sheet => {
                    if (sheet.options.isProtected) {
                        let index = itemsDataForShown.findIndex((item) => item.name === "unprotectSheet");
                        itemsDataForShown.splice(index, 1);
                    }
                });
            }
        }
  • Posted 14 April 2025, 4:59 am EST

    Hi,

    Thank you for the feedback.

    SpreadJS is designed to closely align with Excel’s behavior, and Excel does not provide a disableProtection property to disable protection-related options. Instead, Excel offers a “Protect Workbook” feature that restricts changes to the workbook structure. This functionality is already on our product backlog.

    Introducing a disableProtection option in SpreadJS could potentially affect import/export compatibility, as Excel does not support such a feature. However, if you still wish to implement similar behavior, you may consider using the previously suggested and your mentioned approaches to achieve it.

    Regards,

    Priyam

  • Posted 14 April 2025, 5:24 am EST

    Hi Priyam

    For correctness , I was suggesting the disable option on the configuration of the SpreadJs designer default config or a method on the designer that does what is mentioned above, I was not implying the change in the spreadJs library itself, It is merely a shorter way of configuring the designer ribbon

    Eitherway we are good , so thank you

  • Posted 14 April 2025, 6:40 am EST

    Hi,

    Currently, SpreadJS does not offer a built-in option in the default Designer configuration or a specific method to disable the mentioned functionality. However, this can be achieved using the approaches shared above.

    If Excel introduces a similar feature in the future or if we receive more requests for this capability, we will consider forwarding it to the development team for possible implementation.

    Regards,

    Priyam

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels