Conditionally displaying delete sheet menu option

Posted by: datta on 4 June 2019, 3:35 am EST

    • Post Options:
    • Link

    Posted 4 June 2019, 3:35 am EST

    Hi,

    In the default context menu there is ‘Delete sheet’ option. I want this option to be visible only for certain sheets. For other sheets this option should not be visible. I went through the documentation for context menu but it is not very clear. Can you please tell me how to do it

  • Posted 6 June 2019, 4:18 am EST

    Hi,

    You could handle the contextMenu event and update the menuData for the context menu to customize the options shown. Please refer to the following code snippet and the attached same demonstrating the same:

    spread.getHost().addEventListener('contextmenu', (e) => {
                    var offset = getCumulativeOffset(spread.getHost());
                    var htInfo = spread.hitTest(e.pageX - offset.left, e.pageY - offset.top);
                    
                    if(!htInfo.tabStripHitInfo){
                        // not on tab strip
                        return;
                    }
                    if(htInfo.tabStripHitInfo.sheetTab.sheetName == "Sheet1"){
                        // disable delete option for Sheet1
                        setMenuDataDisable(spread.contextMenu.menuData, "gc.spread.deleteSheet", true);
                    }else{
                        // enable delete option
                        setMenuDataDisable(spread.contextMenu.menuData, "gc.spread.deleteSheet", false);
                    }
    
                }, true);
    
    function setMenuDataDisable(menuData, optionName, value){
                // here we are only disabling the menu item, you may also remove the item from the menuData array to remove the item from the context menu
    
                Array.prototype.forEach.call(menuData, function(data){
                    if(data.name == optionName){
                        data.disable = value;
                    }
                });
            }
    

    ~sharad

    spreadSample.zip

Need extra support?

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

Learn More

Forum Channels