找回密码
 立即注册

QQ登录

只需一步,快速开始

Richard.Ma 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-6-2 10:23  /   查看:1247  /  回复:0
“粘贴链接”是excel提供的剪切板粘贴的一个特色功能,会把复制区域的单元格的地址引用粘贴到目标单元格

举例,在A1单元格使用Ctrl+C进行复制,然后在A5单元格右键点击,选择“粘贴链接”图标,效果如下
image.png325326842.png
image.png672769469.png

可以看到A5被设置了一个公式,引用A1单元格的绝对地址

目前SpreadJS暂不支持此功能,但是我们可以通过自定义命令,来实现这个功能,下面是具体的实现代码

  1. function addPasteLink(config) {

  2.       let contextcopycommand = GC.Spread.Sheets.Designer.getCommand("contextMenuCopy");
  3.       let oldExecute=contextcopycommand.execute;
  4.       
  5.       contextcopycommand.execute=function(context, propertyName, args){
  6.             oldExecute.call(this, context, propertyName, args);
  7.             let range=context.Spread.getActiveSheet().getSelections()[0];
  8.             console.log(range);
  9.             let formulastr="";
  10.   
  11.             formulastr=GC.Spread.Sheets.CalcEngine.rangeToFormula(range);
  12.             
  13.             context.setData("copyLink",formulastr);
  14.             console.log(context.getData("copyLink"));
  15.       }

  16.       let copycommand = GC.Spread.Sheets.Designer.getCommand("copy");
  17.       oldcopyExecute=copycommand.execute;
  18.       
  19.       copycommand.execute=function(context, propertyName, args){
  20.             oldcopyExecute.call(this, context, propertyName, args);
  21.             let range=context.Spread.getActiveSheet().getSelections()[0];
  22.             console.log(range);
  23.             let formulastr="";
  24.   
  25.             formulastr=GC.Spread.Sheets.CalcEngine.rangeToFormula(range);
  26.             
  27.             context.setData("copyLink",formulastr);
  28.             console.log(context.getData("copyLink"));
  29.       }

  30.       

  31.       

  32.       config.commandMap = {
  33.             "pasteLink": {
  34.                   text: "粘贴链接",
  35.                   group: "contextMenuPaste",
  36.                   iconClass: "gc-spread-pasteAll",
  37.                   commandName: "pasteLink",
  38.                   execute: async (context) => {
  39.                         setTimeout(() => {

  40.                               if(context.getData("copyLink")){
  41.                                     let sheet = context.Spread.getActiveSheet();
  42.                                     var referenceCellRange=GC.Spread.Sheets.CalcEngine.formulaToRange(sheet,context.getData("copyLink"));
  43.                                     console.log(referenceCellRange);
  44.                                     var isRelative=referenceCellRange.rowCount==1&&referenceCellRange.colCount==1?GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allRelative:GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allAbsolute;
  45.                                     for(var r=0;r<referenceCellRange.rowCount;r++){
  46.                                           for(var c=0;c<referenceCellRange.colCount;c++){
  47.                                                 var formulastr=GC.Spread.Sheets.CalcEngine.rangeToFormula(sheet.getRange(referenceCellRange.row+r,referenceCellRange.col+c),null,null,isRelative);
  48.                                                 sheet.setFormula(sheet.getActiveRowIndex()+r, sheet.getActiveColumnIndex()+c, formulastr);
  49.                                           }
  50.                                     }
  51.                                     
  52.                               }

  53.                         }, 0)
  54.                   },
  55.             },
  56.       };
  57.       config.commandMap[GC.Spread.Sheets.Designer.CommandNames.ContextMenuCopy] = contextcopycommand;
  58.       config.commandMap[GC.Spread.Sheets.Designer.CommandNames.RibbonCopy] = copycommand;

  59.       config.contextMenu.splice(9, 0, "pasteLink");
  60. }
复制代码



在创建designer时调用
  1.       var config = GC.Spread.Sheets.Designer.DefaultConfig;
  2. addPasteLink(config);
  3. var designer = new GC.Spread.Sheets.Designer.Designer(document.getElementById("gc-designer-container"), GC.Spread.Sheets.Designer.DefaultConfig);
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部