SpreadJS实现Excel的“粘贴链接”
“粘贴链接”是excel提供的剪切板粘贴的一个特色功能,会把复制区域的单元格的地址引用粘贴到目标单元格举例,在A1单元格使用Ctrl+C进行复制,然后在A5单元格右键点击,选择“粘贴链接”图标,效果如下
可以看到A5被设置了一个公式,引用A1单元格的绝对地址
目前SpreadJS暂不支持此功能,但是我们可以通过自定义命令,来实现这个功能,下面是具体的实现代码
function addPasteLink(config) {
let contextcopycommand = GC.Spread.Sheets.Designer.getCommand("contextMenuCopy");
let oldExecute=contextcopycommand.execute;
contextcopycommand.execute=function(context, propertyName, args){
oldExecute.call(this, context, propertyName, args);
let range=context.Spread.getActiveSheet().getSelections();
console.log(range);
let formulastr="";
formulastr=GC.Spread.Sheets.CalcEngine.rangeToFormula(range);
context.setData("copyLink",formulastr);
console.log(context.getData("copyLink"));
}
let copycommand = GC.Spread.Sheets.Designer.getCommand("copy");
oldcopyExecute=copycommand.execute;
copycommand.execute=function(context, propertyName, args){
oldcopyExecute.call(this, context, propertyName, args);
let range=context.Spread.getActiveSheet().getSelections();
console.log(range);
let formulastr="";
formulastr=GC.Spread.Sheets.CalcEngine.rangeToFormula(range);
context.setData("copyLink",formulastr);
console.log(context.getData("copyLink"));
}
config.commandMap = {
"pasteLink": {
text: "粘贴链接",
group: "contextMenuPaste",
iconClass: "gc-spread-pasteAll",
commandName: "pasteLink",
execute: async (context) => {
setTimeout(() => {
if(context.getData("copyLink")){
let sheet = context.Spread.getActiveSheet();
var referenceCellRange=GC.Spread.Sheets.CalcEngine.formulaToRange(sheet,context.getData("copyLink"));
console.log(referenceCellRange);
var isRelative=referenceCellRange.rowCount==1&&referenceCellRange.colCount==1?GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allRelative:GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allAbsolute;
for(var r=0;r<referenceCellRange.rowCount;r++){
for(var c=0;c<referenceCellRange.colCount;c++){
var formulastr=GC.Spread.Sheets.CalcEngine.rangeToFormula(sheet.getRange(referenceCellRange.row+r,referenceCellRange.col+c),null,null,isRelative);
sheet.setFormula(sheet.getActiveRowIndex()+r, sheet.getActiveColumnIndex()+c, formulastr);
}
}
}
}, 0)
},
},
};
config.commandMap = contextcopycommand;
config.commandMap = copycommand;
config.contextMenu.splice(9, 0, "pasteLink");
}
在创建designer时调用
var config = GC.Spread.Sheets.Designer.DefaultConfig;
addPasteLink(config);
var designer = new GC.Spread.Sheets.Designer.Designer(document.getElementById("gc-designer-container"), GC.Spread.Sheets.Designer.DefaultConfig);
页:
[1]