Detect sheet creation/deletion with events

Posted by: ian.scriven on 1 May 2018, 6:23 pm EST

    • Post Options:
    • Link

    Posted 1 May 2018, 6:23 pm EST

    Why aren’t there events for sheet creation/deletion? It seems like a fairly obvious gap to me.

    I can see how to work-around the lack of a sheet creation event using active sheet changing/changed events, however that’s not possible for sheet deletion, as those events don’t get triggered when a user deletes a sheet (which is probably a bug?).

  • Posted 2 May 2018, 12:24 am EST

    Hello,

    To know when a sheet is added you can use the code as follows:

    spread.bind(GC.Spread.Sheets.Events.SheetTabClick, function (e, info) {

    if (info.sheet === null && info.sheetName === null) {

    //write your code here

    }

    });

    To delete sheet you must be using the Delete option available with context menu. You can handle the event for the contextmenu item.

    http://help.grapecity.com/spread/SpreadSheets11/webframe.html#UsingtheContextMenu.html

    I hope it helps.

    Thanks,

    Deepak Sharma

  • Posted 6 May 2018, 11:15 am EST

    Thanks Deepak for those work-arounds.

    You will also need to handle sheet insertion via the context menu. Is there a way to call the “default” command within a custom command? For example, if I replace the command for inserting a sheet, I want to still call the standard command, otherwise I’ll have to implement a sheet name generator that mimics the behaviour of adding a sheet using the “+” button.

    However, the following code doesn’t work:

    let commandManager = spread.commandManager();
    function insertSheet() {
        // some custom logic here
        commandManager.execute("gc.spread.contextMenu.insertSheet"); // this doesn't work
    }
    commandManager.register("insertSheet", {canUndo: false, execute: insertSheet});
    
    function CustomMenuView() {
    }
    CustomMenuView.prototype = new GC.Spread.Sheets.ContextMenu.MenuView();
    CustomMenuView.prototype.createMenuItemElement = function (menuItemData) {
        let menuItemView = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(this, menuItemData);
        if (menuItemData.name === "gc.spread.insertSheet") {
            menuItemData.command = "insertSheet";
        }
        return menuItemView;
    };
    spread.contextMenu.menuView = new CustomMenuView();
    
  • Posted 6 May 2018, 11:23 am EST

    Nevermind, I figured it out.

    For future readers:

    commandManager.execute({cmd: "gc.spread.contextMenu.insertSheet"});
    
  • Posted 6 May 2018, 4:02 pm EST

    Thanks for sharing the solution with us

  • Posted 2 October 2019, 12:18 pm EST

    Deepak,

    Can you drop the code for getting an event callback for sheet deletion/context menu click of sheet deletion.

    Jeff

  • Posted 2 October 2019, 8:30 pm EST

    Hi Jeff,

    You could handle the ActiveSheetChanged event and check if the name of the oldSheet is set for detecting sheet deletion. Please refer to the following sample which demonstrates the same:

    https://codesandbox.io/s/spread-js-starter-0e29l

    Further, we have asked the dev team to add events for delete/insert sheet and will let you know about any updates regarding the same. The internal tracking ID for the issue is SJS-1954.

    Regards

    Sharad

  • Posted 25 October 2019, 9:16 am EST

    Thanks Sharad.

  • Posted 24 April 2020, 12:23 am EST

    Hi Jeff,

    Support for detecting insert/delete/hide/unhide sheet events is now added in the latest version 13.1.0. We could use now handle the SheetChanging/SheetChanged event to handle the insert/delete/hide/unhide sheet events. Refer to the following code snippet:

    spread.bind(GC.Spread.Sheets.Events.SheetChanged, function (sender, args) {
         var sheetIndex = args.sheetIndex;
    });
    

    Please update to the latest version and let us know if you face any issues. You may get the latest build from npm or download using the following link:

    http://cdn.grapecity.com/spreadjs/13.1.0/Files/SpreadJS.Release.13.1.0.zip

    Regards

  • Posted 3 July 2020, 6:53 am EST

    Can you break this down into how I would detect:

    1. insert

      2 delete
    2. hide
    3. unhide
  • Posted 6 July 2020, 12:17 am EST

    Hi,

    You may use the propertyName field of the SheetsChanged Events Argument for the required functionality. Please refer to the following code snippet and the sample which demonstrates the same:

    spread.bind(GC.Spread.Sheets.Events.SheetChanged, function(sender, args) {
       var sheet = args.sheet;
       switch (args.propertyName) {
         case "insertSheet":
           alert("insert");
           break;
         case "deleteSheet":
           alert("Delete");
           break;
         case "isVisible":
           if (args.newValue) {
             alert("Unhide");
           } else {
             alert("hidden");
           }
           break;
         default:
           break;
       }
     });
    

    Sample: https://codesandbox.io/s/rough-wildflower-4qt10?file=/src/index.js

    For more information please refer to the following API References:

    SheetsChanged: https://www.grapecity.com/spreadjs/docs/v13/online/SpreadJS~GC.Spread.Sheets.Events~SheetChanged_EV.htm

    Regards

  • Posted 28 September 2020, 7:35 am EST

    Thank Sharad

Need extra support?

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

Learn More

Forum Channels