Enquiry regarding remove row command in command Manager

Posted by: sbhadane on 7 March 2025, 1:14 pm EST

  • Posted 7 March 2025, 1:14 pm EST

    We have a command remove colum

    spread.commandManager().execute({cmd: “RemoveColumn”, sheetName: “Sheet1”, { col: 1 }});

    But don’t we have a command to remove rows ?

    I want to give remove column and remove row on a button click and be able to undo it to .

  • Posted 10 March 2025, 8:14 am EST - Updated 11 March 2025, 12:19 am EST

    Hi,

    You can create a custom command in SpreadJS to remove rows and columns and execute them on button clicks. Please refer to the following code snippet:

    // Define the custom command for removing a row
    var removeSelectedRow = {
        canUndo: true,
        execute: function (context, options, isUndo) {
            var sheet = context.getSheetFromName(options.sheetName);
            var Commands = GC.Spread.Sheets.Commands;
    
            if (isUndo) {
                Commands.undoTransaction(context, options);
                return true;
            } else {
                Commands.startTransaction(context, options);
                var rowIndex = sheet.getActiveRowIndex(); // Get the selected row index
                if (rowIndex < 0) return false; // Ensure a row is selected
                sheet.deleteRows(rowIndex, 1); // Delete the selected row
                Commands.endTransaction(context, options);
                return true;
            }
        }
    };
    
    // Define the custom command for removing a column
    var removeSelectedColumn = {
        canUndo: true,
        execute: function (context, options, isUndo) {
            var sheet = context.getSheetFromName(options.sheetName);
            var Commands = GC.Spread.Sheets.Commands;
    
            if (isUndo) {
                Commands.undoTransaction(context, options);
                return true;
            } else {
                Commands.startTransaction(context, options);
                var colIndex = sheet.getActiveColumnIndex(); // Get the selected column index
                if (colIndex < 0) return false; // Ensure a column is selected
                sheet.deleteColumns(colIndex, 1); // Delete the selected column
                Commands.endTransaction(context, options);
                return true;
            }
        }
    };
    
    // Register the custom commands
    var commandManager = spread.commandManager();
    commandManager.register("removeSelectedRow", removeSelectedRow);
    commandManager.register("removeSelectedColumn", removeSelectedColumn);
    
    // Attach commands to button click events
    document.getElementById("removeRowBtn").addEventListener("click", function () {
        spread.commandManager().execute({ cmd: "removeSelectedRow", sheetName: spread.getActiveSheet().name() });
    });
    
    document.getElementById("removeColBtn").addEventListener("click", function () {
        spread.commandManager().execute({ cmd: "removeSelectedColumn", sheetName: spread.getActiveSheet().name() });
    });
    

    Observation on Predefined RemoveColumn Command

    We have noticed that the predefined RemoveColumn command is not functioning as expected. This issue has been escalated to the development team, and the internal tracking id is SJS-28525.

    Regards,

    Ankit

    remove_row_column_command (3).zip

  • Posted 10 March 2025, 12:44 pm EST

    [code]*******
    [/code]Hi Ankit ,

    Thank you for response , but I think you might have mistakenly shared the wrong zip file.

    Kindly request you to share the correct file.

    Also wanted to know with the approach shared above , can it be undone and redone with the keyboard shortcuts ?

    Thanks & Regards,

    Saurabh Bhadane

  • Posted 12 March 2025, 1:51 am EST - Updated 12 March 2025, 1:56 am EST

    Hi,

    The zip file shared with you is correct and works according to the attached screenshot above. In case you are not able to run the attached sample, please follow these steps:

    • Extract the files.
    • Open the folder containing the files in Visual Studio Code.
    • Serve the folder on the localhost.

    You can also refer to the screenshot below that follows the above steps and runs the sample as expected:

    In addition, the commands to remove the row and column can be undone and redone using the keyboard shortcuts Ctrl + Z and Ctrl + Y, respectively, as illustrated in the above screenshot.

    Please feel free to reach out if you encounter any further issues or require additional guidance.

    Kind Regards,

Need extra support?

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

Learn More

Forum Channels