Add rows by context menu command

Posted by: pttrung811 on 22 March 2023, 5:24 am EST

    • Post Options:
    • Link

    Posted 22 March 2023, 5:24 am EST

    Hi,

    I have a requirement to add new rows by button and context menu insert rows.

    At add new row button, I’m using addRows() method.

    But when using addRows() method it can’t be undo. I know that I don’t use it in Commands, so I can’t do undo.

    Currently, i don’t want to create another custom command. Is there a way to execute the context menu command instead of registering a new command?

  • Posted 23 March 2023, 11:23 pm EST

    Hello,

    As I can understand, you want to add rows by clicking on a button and executing the context menu command for inserting rows. You can achieve this by executing the command “gc.spread.contextMenu.insertRows”. To manually execute the command, you can use the execute() method of the command manager. You can perform undo operation as this command is undoable.

    Please refer to the code snippet and attached sample which explains the same.

    
    let commandManager = spread.commandManager();
    
    document.getElementById('button').addEventListener('click', (e) => {
        // gets the last selection from the sheet
        let selections = sheet.getSelections();
        let selection = selections[selections.length - 1];
        // executes the context menu command for inserting rows
        commandManager.execute({
            cmd: 'gc.spread.contextMenu.insertRows',
            sheetName: sheet.name(),
            selections: [selection],
        });
    });

    Sample: https://jscodemine.grapecity.com/share/a4DnOb-OSUOrZQ7cH8W7jg/?defaultOpen={"OpenedFileName"%3A["%2Findex.html"%2C"%2Fsrc%2Fapp.js"]%2C"ActiveFile"%3A"%2Findex.html"}

    Please let us know if you face any difficulties and don’t hesitate to ask if you have any doubts.

    Doc reference

    CommandManager class: https://www.grapecity.com/spreadjs/api/classes/GC.Spread.Commands.CommandManager#class-commandmanager

    commandManager.execute(): https://www.grapecity.com/spreadjs/api/classes/GC.Spread.Commands.CommandManager#execute

    Regards,

    Ankit

Need extra support?

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

Learn More

Forum Channels