请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

jiankeyan

中级会员

116

主题

327

帖子

983

积分

中级会员

积分
983
jiankeyan
中级会员   /  发表于:2025-5-26 18:28  /   查看:141  /  回复:7
重写复制粘贴之后撤销和前进键不能使用,怎么能撤销我自定义粘贴的内容
  1. document.getElementById("gc-designer-container").addEventListener("copy", function (e) {
  2.         let sheetCopy = spread_subject.getActiveSheet();
  3.         let selectCopy = sheetCopy.getSelections();
  4.         copyAndpaste = ["1608943204a74eff9429a7b771d4e763"];

  5.         //是否选中图片
  6.         let picAll = sheetCopy.pictures.all();
  7.         let isDelPic = false;
  8.         for (let i = 0; i < picAll.length; i++) {
  9.             let isSelected = picAll[i].isSelected();
  10.             if (isSelected) {
  11.                 isDelPic = true;
  12.                 sheetCopy.pictures.remove(picAll[i].name());
  13.                 break
  14.             }
  15.         }
  16.         if (isDelPic) {

  17.             return;
  18.         }
  19.         if (!selectCopy.length) return;

  20.         selectCopy = selectCopy[0];
  21.         let rowCopy = selectCopy.row;
  22.         let colCopy = selectCopy.col;
  23.         let rowCountCopy = selectCopy.rowCount;
  24.         let colCountCopy = selectCopy.colCount;

  25.         copyAndpaste.push({
  26.             row: rowCopy,
  27.             col: colCopy,
  28.             rowCount: rowCountCopy,
  29.             colCount: colCountCopy
  30.         })

  31.         for (let i = rowCopy; i < rowCountCopy + rowCopy; i++) {
  32.             for (let j = colCopy; j < colCountCopy + colCopy; j++) {
  33.                 let cellCopy = sheetCopy.getSpan(i, j);
  34.                 if (!cellCopy) {
  35.                     cellCopy = sheetCopy.getCell(i, j);
  36.                 }

  37.                 if (cellCopy.row != i || cellCopy.col != j) continue;
  38.                 let textCopy = sheetCopy.getText(i, j);
  39.                 copyAndpaste.push({
  40.                     row: cellCopy.row,
  41.                     col: cellCopy.col,
  42.                     rowCount: cellCopy.rowCount,
  43.                     colCount: cellCopy.colCount,
  44.                     text: textCopy
  45.                 });
  46.             }
  47.         }

  48.         legacyCopy(JSON.stringify(copyAndpaste));
  49.     })
  50.     document.getElementById("gc-designer-container").addEventListener("paste", function (e) {
  51.         e.preventDefault();
  52.         if (subjectState || !isAllowEdit) return;

  53.         try {
  54.             if (e.clipboardData.items[0].kind == "file") {
  55.                 let itemFile = e.clipboardData.items[0].getAsFile();
  56.                 insertImageDrap(itemFile,spread_subject);
  57.                 return;
  58.             }
  59.         } catch (e) {
  60.             console.log(e)
  61.         }

  62.         let pasteData = "";
  63.         try {
  64.             pasteData = JSON.parse(e.clipboardData.getData('text/plain'));
  65.         } catch (error) {
  66.             pasteData = e.clipboardData.getData('text/plain')
  67.         }
  68.         let sheetCopy = spread_subject.getActiveSheet();
  69.         let selectPaste = sheetCopy.getSelections();
  70.         if (!selectPaste.length) return;
  71.         selectPaste = selectPaste[0];

  72.         let rowCopy = selectPaste.row;
  73.         let colCopy = selectPaste.col;

  74.         let indexTab = JSON.stringify(pasteData).indexOf("1608943204a74eff9429a7b771d4e763");

  75.         if (indexTab > -1) {
  76.             if (!copyAndpaste.length) return;

  77.             let rowDiff = rowCopy - pasteData[1].row;
  78.             let colDiff = colCopy - pasteData[1].col;

  79.             try {
  80.                 for (let i = (pasteData[1].row + rowDiff); i < pasteData[1].rowCount + pasteData[1].row + rowDiff; i++) {
  81.                     for (let j = (pasteData[1].col + colDiff); j < pasteData[1].colCount + pasteData[1].col + colDiff; j++) {
  82.                         let cellCopy = sheetCopy.getSpan(i, j);
  83.                         if (!cellCopy) sheetCopy.getCell(i, j);

  84.                         if (!cellCopy || (cellCopy.row != i || cellCopy.col != j)) continue;

  85.                         let formulaCell = sheetCopy.getFormula(i, j);
  86.                         if (formulaCell && !isLockFormal) {
  87.                             throw new Error("有公式,禁止粘贴");
  88.                         }
  89.                     }
  90.                 }
  91.             } catch (e) {
  92.                 console.log(e)
  93.                 return;
  94.             }

  95.             spread_subject.suspendPaint();
  96.             for (let i = (pasteData[1].row + rowDiff); i < pasteData[1].rowCount + pasteData[1].row + rowDiff; i++) {
  97.                 for (let j = (pasteData[1].col + colDiff); j < pasteData[1].colCount + pasteData[1].col + colDiff; j++) {
  98.                     let cellCopy = sheetCopy.getSpan(i, j);
  99.                     if (!cellCopy) continue;

  100.                     sheetCopy.removeSpan(i, j);
  101.                 }
  102.             }

  103.             for (let i = 2; i < pasteData.length; i++) {
  104.                 let nowPasteTab = pasteData[i];
  105.                 if (nowPasteTab.rowCount > 1 || nowPasteTab.colCount > 1) {
  106.                     sheetCopy.addSpan(nowPasteTab.row + rowDiff, nowPasteTab.col + colDiff, nowPasteTab.rowCount, nowPasteTab.colCount);
  107.                 }

  108.                 let tagCell = sheetCopy.getTag(nowPasteTab.row + rowDiff, nowPasteTab.col + colDiff);
  109.                 let formulaCell = sheetCopy.getFormula(nowPasteTab.row + rowDiff, nowPasteTab.col + colDiff);

  110.                 if (formulaCell) {
  111.                     continue;
  112.                 }
  113.                 if (tagCell) {
  114.                     if (tagCell.indexOf("cosign") != -1) {//签字标签
  115.                         continue;
  116.                     }
  117.                     sheetCopy.setText(nowPasteTab.row + rowDiff, nowPasteTab.col + colDiff, nowPasteTab.text);
  118.                 } else {
  119.                     sheetCopy.setText(nowPasteTab.row + rowDiff, nowPasteTab.col + colDiff, nowPasteTab.text);
  120.                 }
  121.             }
  122.             spread_subject.resumePaint();
  123.         } else {
  124.             let cellNow = sheetCopy.getSpan(rowCopy, colCopy);
  125.             if (!cellNow) {
  126.                 cellNow = sheetCopy.getCell(rowCopy, colCopy);
  127.             }
  128.             let excelRowArr = undefined;

  129.             try {
  130.                 excelRowArr = pasteData.split("\r\n");
  131.             } catch (e) {
  132.                 excelRowArr = [parseInt(pasteData).toString()];
  133.             }
  134.             let iNum = -1;

  135.             spread_subject.suspendPaint();
  136.             excelRowArr.forEach((item,i) => {
  137.                 let nowExcelColArr = item.split("\t");
  138.                 let afterCell = cellNow;

  139.                 try {
  140.                     nowExcelColArr.forEach(el => {
  141.                         if (el != "") {
  142.                             throw new Error("有内容");
  143.                         }
  144.                     })
  145.                 } catch (e) {
  146.                     iNum++;
  147.                 }
  148.                 for (let k = 0; k < iNum; k++) {
  149.                     afterCell = getBottomCell(afterCell, sheetCopy);
  150.                 }

  151.                 let jNum = -1;
  152.                 nowExcelColArr.forEach((el, j) => {
  153.                     if (el != "") {
  154.                         jNum++;
  155.                         for (let k = 0; k < jNum; k++) {
  156.                             afterCell = getAfterCell(afterCell, sheetCopy);
  157.                         }
  158.                         let tagCell = sheetCopy.getTag();
  159.                         let formulaCell = sheetCopy.getFormula(afterCell.row, afterCell.col);
  160.                         if (formulaCell && !isLockFormal) {
  161.                             return
  162.                         }
  163.                         if (tagCell) {
  164.                             if (tagCell.indexOf("cosign") != -1) { //签字标签
  165.                                 return;
  166.                             }
  167.                             sheetCopy.setText(afterCell.row, afterCell.col, el);
  168.                         } else {
  169.                             sheetCopy.setText(afterCell.row, afterCell.col, el);
  170.                         }
  171.                     }
  172.                 })
  173.             })
  174.             spread_subject.resumePaint();
  175.         }
  176.     })

  177.     function legacyCopy(text) {
  178.         const textarea = document.createElement('textarea');
  179.         textarea.value = text;
  180.         textarea.style.position = 'fixed';
  181.         document.body.appendChild(textarea);
  182.         textarea.select();

  183.         try {
  184.             const success = document.execCommand('copy');
  185.             if (!success) {
  186.                 throw new Error('复制失败');
  187.             }
  188.             console.log('复制成功');
  189.         } catch (err) {
  190.             console.error('复制失败:', err);
  191.         } finally {
  192.             document.body.removeChild(textarea);
  193.         }
  194.     }
复制代码


7 个回复

倒序浏览
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2025-5-27 10:25:20
沙发
您好,在重写命令的时候,需要将对应的操作封装为命令,否则无法被commandManager控制,也就无法被撤销,具体您也可以参考这个帖子中的说明:https://gcdn.grapecity.com.cn/showtopic-156943-1-1.html
回复 使用道具 举报
jiankeyan
中级会员   /  发表于:2025-5-28 17:38:55
板凳
Joestar.Xu 发表于 2025-5-27 10:25
您好,在重写命令的时候,需要将对应的操作封装为命令,否则无法被commandManager控制,也就无法被撤销,具 ...

封装进入之后撤销可以了,但是撤销之后怎么实现恢复,也就是前进功能
回复 使用道具 举报
jiankeyan
中级会员   /  发表于:2025-5-28 17:47:36
地板
jiankeyan 发表于 2025-5-28 17:38
封装进入之后撤销可以了,但是撤销之后怎么实现恢复,也就是前进功能

使用这个方法之后不能前进

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
jiankeyan
中级会员   /  发表于:2025-5-28 17:59:11
5#
jiankeyan 发表于 2025-5-28 17:47
使用这个方法之后不能前进

重写的粘贴方法-----------

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2025-5-29 14:35:48
6#
命令的注册只需要一次,不建议您将命令封装在paste中执行多次,具体请参考:https://gcdn.grapecity.com.cn/showtopic-87326-1-1.html
回复 使用道具 举报
Matthew.Xue
超级版主   /  发表于:2025-5-30 11:36:24
7#
您好,请问您遇到的问题是否解决了呢,没有解决的话咱们可以继续讨论
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2025-6-4 14:05:50
8#
您好,由于本帖较长时间没有回复,本帖先做结贴处理了,后续如您有其他问题,欢迎您开新帖提问。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部