本帖最后由 zxx 于 2020-2-20 17:53 编辑
思路来源帖子https://gcdn.grapecity.com.cn/showtopic-44077-1-193.html
根据这个帖子还是遇到了一些问题,下面把实现代码贴一下
1.因为重写ctrl+v需要commandName,所以需要先注册自定义命令。(可以参考https://www.grapecity.com.cn/blogs/how-to-use-spreadjs-built-in-commands-and-custom-commands)
在里面记录锁定单元格的位置和值,将单元格设置为可编辑,然后在调用粘贴- spread.commandManager().register("uf_ClipboardPaste",{
- canUndo: true,
- execute: function (context, options, isUndo) {
- var activeSheet = spread.getActiveSheet();
- var Commands = GC.Spread.Sheets.Commands;
- // 在此加cmd名称
- options.cmd = "uf_ClipboardPaste";
- if (isUndo) {
- // isUndo 为true时,调用undoTransaction
- Commands.undoTransaction(context, options);
- return true;
- } else {
- // 开始事务
- Commands.startTransaction(context, options);
- lockedCells = [];
- activeSheet.suspendPaint();
- var rowCount = activeSheet.getRowCount();
- var columnCount = activeSheet.getColumnCount();
- for (var r = 0; r < rowCount; r++) {
- for (var c = 0; c < columnCount; c++) {
- var cell = activeSheet.getCell(r, c);
- if (cell == null) {
- continue;
- }
- if(cell.locked()){
- lockedCells.push({
- row:cell.row,
- col:cell.col,
- value:cell.value(),
- });
- cell.locked(false);
- }
- }
- }
- spread.commandManager().execute({
- cmd: "paste",
- sheetName: activeSheet.name()
- });
- activeSheet.resumePaint();
- // 结束事务
- Commands.endTransaction(context, options);
- return true;
- }
- }
- });
复制代码
2.重写ctrl+v,并指向自定义命令
- spread.commandManager().setShortcutKey('uf_ClipboardPaste', GC.Spread.Commands.Key.v, true, false, false, false);
复制代码 3.在粘贴后的事件里进行还原单元格值。
- activeSheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, function (sender, args) {
- var sheet = args.sheet;
- sheet.suspendPaint();
- for (var i = 0; i < lockedCells.length; i++) {
- var lockedCell = lockedCells[i];
- sheet.setValue(lockedCell.row, lockedCell.col, lockedCell.value);
- sheet.getCell(lockedCell.row, lockedCell.col).locked(true);
- }
- sheet.resumePaint();
- });
复制代码 4.右键粘贴需要系统内置command重新指向自定义命令即可
|