Losing copy selection after setting formula/value on cells

Posted by: pdoxakis on 1 February 2024, 3:40 am EST

    • Post Options:
    • Link

    Posted 1 February 2024, 3:40 am EST

    Reproduction steps:

    • use the demo in attachment
    • unzip and open test.html
    • select cell C3
    • press on ctrl and c to set the copy selection
    • select cell E4
    • press on link “set value” or “set formula” in the bottom
    • we lose the copy selection on the cell C3 (by using: workbook.getActiveSheet().getCell(1, 1).value(‘789’)) or workbook.getActiveSheet().getCell(1, 1).formula(null)

    Is it possible to get/set the copy selection so I can workaround that behavior for my app ?

    demo.zip

  • Posted 2 February 2024, 9:35 am EST

    Hi,

    This behavior is expected, and Excel exhibits the same behavior.

    To set or get the copy selection, you can follow these steps:

    1. Use the “GC.Spread.Sheets.Events.ClipboardChanging” event, which triggers when the copy selection changes. Use this event to store the copy selection. Refer to the snippet below:
    let selections = null;
    spread.bind(GC.Spread.Sheets.Events.ClipboardChanging, function (sender, args) {
        console.log("ClipboardChanging", args);
        selections = args.sheet.getSelections();
    });
    1. After setting value, clear the selection using sheet.clearSelection, then add the stored selection, and finally execute the copy command. Refer to the snippet below:
    spread.getActiveSheet().getCell(1, 1).value('789');
    spread.getActiveSheet().clearSelection();
    spread.getActiveSheet().addSelection(selections[0].row, selections[0].col, selections[0].rowCount, selections[0].colCount);
    spread.commandManager().execute({ cmd: "copy", sheetName: spread.getActiveSheet().name() });
    1. If you want to get back the active selection, first store the active selection, then follow the above 2 step. After that, clear the selection again and add back the active selection. Refer to the snippet below:
    spread.getActiveSheet().getCell(1, 1).value('789');
    const activeSelection = spread.getActiveSheet().getSelections();
    spread.getActiveSheet().clearSelection();
    spread.getActiveSheet().addSelection(selections[0].row, selections[0].col, selections[0].rowCount, selections[0].colCount);
    spread.commandManager().execute({ cmd: "copy", sheetName: spread.getActiveSheet().name() });
    spread.getActiveSheet().clearSelection();
    activeSelection.forEach(range => {
        spread.getActiveSheet().addSelection(range.row, range.col, range.rowCount, range.colCount);
    });

    I have attached a sample to refer.

    References:

    ClipboardChanging Event: https://developer.mescius.com/spreadjs/api/classes/GC.Spread.Sheets.Events#clipboardchanging

    Copy command: https://developer.mescius.com/spreadjs/api/modules/GC.Spread.Sheets.Commands#copy

    Best regards,

    Ankit

    copySelection.zip

  • Posted 6 February 2024, 4:27 am EST

    Great, thank you!

Need extra support?

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

Learn More

Forum Channels