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:
- 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();
});
- 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() });
- 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