SpreadJS Merge and Center value from the right

Posted by: kim.dok on 16 April 2023, 5:45 am EST

  • Posted 16 April 2023, 5:45 am EST - Updated 16 April 2023, 5:50 am EST

    Hi, when we are using merge and center action and the value is in the left cell it is changing like this:

    before:



    after:

    But when the value is in the right cell after merge and center it disappears

    before:



    after:

    can we have some guidance on how to achieve the required outcome?

    Thanks.

  • Posted 17 April 2023, 5:07 am EST

    Hello Kim

    I already asked this question. Avinash helped me. You can check this:

    https://www.grapecity.com/forums/spreadjs/missing-data-after-merging-cells-if-the-top-or-left-cell-is-empty

  • Posted 17 April 2023, 6:30 am EST

    Hi, Thank you for the quick response

    I saw the post but I need to implement my own merge instead of using your ribbon actions.

    I tried to do it by using the add span action as i saw in your documentation

    When using the add span action it makes the cell value Empty if the first cell is Empty.

    Is there any other way to implement the merge correctly?

  • Posted 18 April 2023, 12:23 am EST

    Hello,

    As per my understanding, when you click “Merge & Center” button from Designer ribbon, you want the cells to be merged as they are merged in MS Excel.

    When you click on “Merge & Center” button, the execute() method of “mergeCenter” command is executed. You can get this command using GC.Spread.Sheets.Designer.getCommand() method and override its execute() method to perform cell merging as you want. After modifying the “mergeCenter” command, you can add it to the command map of Designer config and set the modified designer config using designer.setConfig() method.

    Please refer to the code snippet and attached sample.

    // overrides the mergeCenter command
    let mergeCenterCommand = GC.Spread.Sheets.Designer.getCommand('mergeCenter');
    mergeCenterCommand.execute = function (context, info, value) {
        commandManager.execute({ cmd: 'customMergeCenter', merge: value });
    }
    
    config.commandMap = {
        mergeCenter: mergeCenterCommand,
    }
    
    designer.setConfig(config);

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

    Doc references:

    GC.Spread.Sheets.Designer.getCommand():https://www.grapecity.com/spreadjs/api/v16/designer/modules/GC.Spread.Sheets.Designer#getcommand

    ICommand.execute(): https://www.grapecity.com/spreadjs/api/designer/interfaces/GC.Spread.Sheets.Designer.ICommand#execute

    Regards,

    Ankit

  • Posted 18 April 2023, 4:12 am EST

    So if I understand correctly the fact that your merge command isn’t working the same way as Excel is a bug.

    Is it something you can fix on your end?

  • Posted 20 April 2023, 12:24 am EST

    Hello,

    Please note that the cell merge behavior in SpreadJS is by design. It is not a bug but the merge feature is different than the MS Excel merge. The merge behavior in SpreadJS is implemented to preserve the values of cells before they were merged when the merged cell is unmerged unlike MS Excel.

    In MS Excel, when more than one cells are merged then data of other cells except the first cell is cleared and their values can not be preserved when merged cell is unmerged again.

    However, in SpreadJS, when a merged cell is unmerged then all the cells that were merged still contain there original values before merging. That is why, when cells are merged in SpreadJS, if the first cell of the selection is empty then the merged cell will also be empty. This behavior is to preserve the original values of the cells that are merged.

    It is important to note that SpreadJS exposes various APIs through which you could implement the MS Excel’s merge behavior.

    Please let us know if you still have any doubts.

    Regards,

    Ankit

Need extra support?

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

Learn More

Forum Channels