在SpreadJS中实现Range.End 属性 (Excel)
关于Range.End属性可以参考下面的微软官方的文档https://docs.microsoft.com/zh-cn/office/vba/api/excel.range.end
简而言之就是从获取当前指定单元格开始,按照指定方向,获取跳过空单元格后第一个有值的单元格的位置的操作。
SpreadJS源生没有直接包含这样的方法,不过可以通过一些API的组合来达到这样的目的,并且我们可以再做一个二次封装,封装成一个常用方法来供我们使用。
下面的示例实现了这样的方法,并将该方法设置成了快捷键,alt+上下左右方向键来快捷实现(SpreadJS不支持end+上下左右的组合键位)。
首先,我们二次封装一个这样的方法
function ExcelEnd(sheet,cell,direction){
var endCell = null;
if(direction == "up"){
for(var i=cell.row-1;i>=0;i--){
if(sheet.getValue(i,cell.col)!=null){
endCell = sheet.getCell(i,cell.col);
break;
}
if(i==0){
endCell = sheet.getCell(0,cell.col);
}
}
}else if(direction == "down"){该方法中,通过循环判断,按方向获取到了最近有值得cell。并且将该cell返回
之后我们通过commandmanager将构造4个命令,分别对应该方法的4个方向(这里目前没有特别简单的写法,如果有知道更简单的写法可以在下方讨论)。
var endUp = {
canUndo: true,
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
var sheet = context.getSheetFromName(options.sheetName);
var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
var endCell = ExcelEnd(sheet,cell,"up");
sheet.setActiveCell(endCell.row,endCell.col);
Commands.endTransaction(context, options);
return true;
}
}
};
var commandManager = spread.commandManager();
commandManager.register("endUp", endUp);
var endDown = {
canUndo: true,
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
var sheet = context.getSheetFromName(options.sheetName);
var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
var endCell = ExcelEnd(sheet,cell,"down");
sheet.setActiveCell(endCell.row,endCell.col);
Commands.endTransaction(context, options);
return true;
}
}
};
var commandManager = spread.commandManager();
commandManager.register("endDown", endDown);
var endLeft = {
canUndo: true,
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
var sheet = context.getSheetFromName(options.sheetName);
var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
var endCell = ExcelEnd(sheet,cell,"left");
sheet.setActiveCell(endCell.row,endCell.col);
Commands.endTransaction(context, options);
return true;
}
}
};
var commandManager = spread.commandManager();
commandManager.register("endLeft", endLeft);
var endRight = {
canUndo: true,
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
var sheet = context.getSheetFromName(options.sheetName);
var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
var endCell = ExcelEnd(sheet,cell,"right");
sheet.setActiveCell(endCell.row,endCell.col);
Commands.endTransaction(context, options);
return true;
}
}
};
var commandManager = spread.commandManager();
commandManager.register("endRight", endRight);最后,通过setShortcutKey分别将不同的命令绑到对应的键位上
spread.commandManager().setShortcutKey('endUp', GC.Spread.Commands.Key.up, false, false, true, false);
spread.commandManager().setShortcutKey('endDown', GC.Spread.Commands.Key.down, false, false, true, false);
spread.commandManager().setShortcutKey('endLeft', GC.Spread.Commands.Key.left, false, false, true, false);
spread.commandManager().setShortcutKey('endRight', GC.Spread.Commands.Key.right, false, false, true, false);完整的示例可以参考附件,也可以下载后自己在demo中用alt+上下左右方向键尝试一下效果
页:
[1]