找回密码
 立即注册

QQ登录

只需一步,快速开始

Clark.Pan 讲师达人认证 悬赏达人认证 SpreadJS 开发认证

超级版主

200

主题

9899

帖子

1万

积分

超级版主

Rank: 8Rank: 8

积分
15533

讲师达人悬赏达人微信认证勋章SpreadJS 认证SpreadJS 高级认证元老葡萄

Clark.Pan 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2021-5-5 22:15  /   查看:1912  /  回复:0
关于Range.End属性可以参考下面的微软官方的文档
https://docs.microsoft.com/zh-cn/office/vba/api/excel.range.end

简而言之就是从获取当前指定单元格开始,按照指定方向,获取跳过空单元格后第一个有值的单元格的位置的操作。

SpreadJS源生没有直接包含这样的方法,不过可以通过一些API的组合来达到这样的目的,并且我们可以再做一个二次封装,封装成一个常用方法来供我们使用。

下面的示例实现了这样的方法,并将该方法设置成了快捷键,alt+上下左右方向键来快捷实现(SpreadJS不支持end+上下左右的组合键位)。

首先,我们二次封装一个这样的方法
  1. function ExcelEnd(sheet,cell,direction){
  2.   var endCell = null;
  3.   if(direction == "up"){
  4.     for(var i=cell.row-1;i>=0;i--){
  5.       if(sheet.getValue(i,cell.col)!=null){
  6.         endCell = sheet.getCell(i,cell.col);
  7.         break;
  8.       }
  9.       if(i==0){
  10.         endCell = sheet.getCell(0,cell.col);
  11.       }
  12.     }
  13.   }else if(direction == "down"){
复制代码
该方法中,通过循环判断,按方向获取到了最近有值得cell。并且将该cell返回

之后我们通过commandmanager将构造4个命令,分别对应该方法的4个方向(这里目前没有特别简单的写法,如果有知道更简单的写法可以在下方讨论)。
  1. var endUp = {
  2.                                 canUndo: true,
  3.                                 execute: function (context, options, isUndo) {
  4.                                         var Commands = GC.Spread.Sheets.Commands;
  5.                                         if (isUndo) {
  6.                                                 Commands.undoTransaction(context, options);
  7.                                                 return true;
  8.                                         } else {
  9.                                                 Commands.startTransaction(context, options);
  10.                                                 var sheet = context.getSheetFromName(options.sheetName);
  11.                                                 var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
  12.                                                 var endCell = ExcelEnd(sheet,cell,"up");
  13.                                                 sheet.setActiveCell(endCell.row,endCell.col);
  14.                                                 Commands.endTransaction(context, options);
  15.                                                 return true;
  16.                                         }
  17.                                 }
  18.                         };
  19.                         var commandManager = spread.commandManager();
  20.                         commandManager.register("endUp", endUp);
  21.                        
  22.                         var endDown = {
  23.                                 canUndo: true,
  24.                                 execute: function (context, options, isUndo) {
  25.                                         var Commands = GC.Spread.Sheets.Commands;
  26.                                         if (isUndo) {
  27.                                                 Commands.undoTransaction(context, options);
  28.                                                 return true;
  29.                                         } else {
  30.                                                 Commands.startTransaction(context, options);
  31.                                                 var sheet = context.getSheetFromName(options.sheetName);
  32.                                                 var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
  33.                                                 var endCell = ExcelEnd(sheet,cell,"down");
  34.                                                 sheet.setActiveCell(endCell.row,endCell.col);
  35.                                                 Commands.endTransaction(context, options);
  36.                                                 return true;
  37.                                         }
  38.                                 }
  39.                         };
  40.                         var commandManager = spread.commandManager();
  41.                         commandManager.register("endDown", endDown);
  42.                        
  43.                         var endLeft = {
  44.                                 canUndo: true,
  45.                                 execute: function (context, options, isUndo) {
  46.                                         var Commands = GC.Spread.Sheets.Commands;
  47.                                         if (isUndo) {
  48.                                                 Commands.undoTransaction(context, options);
  49.                                                 return true;
  50.                                         } else {
  51.                                                 Commands.startTransaction(context, options);
  52.                                                 var sheet = context.getSheetFromName(options.sheetName);
  53.                                                 var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
  54.                                                 var endCell = ExcelEnd(sheet,cell,"left");
  55.                                                 sheet.setActiveCell(endCell.row,endCell.col);
  56.                                                 Commands.endTransaction(context, options);
  57.                                                 return true;
  58.                                         }
  59.                                 }
  60.                         };
  61.                         var commandManager = spread.commandManager();
  62.                         commandManager.register("endLeft", endLeft);
  63.                        
  64.                         var endRight = {
  65.                                 canUndo: true,
  66.                                 execute: function (context, options, isUndo) {
  67.                                         var Commands = GC.Spread.Sheets.Commands;
  68.                                         if (isUndo) {
  69.                                                 Commands.undoTransaction(context, options);
  70.                                                 return true;
  71.                                         } else {
  72.                                                 Commands.startTransaction(context, options);
  73.                                                 var sheet = context.getSheetFromName(options.sheetName);
  74.                                                 var cell = sheet.getCell(sheet.getActiveRowIndex(),sheet.getActiveColumnIndex());
  75.                                                 var endCell = ExcelEnd(sheet,cell,"right");
  76.                                                 sheet.setActiveCell(endCell.row,endCell.col);
  77.                                                 Commands.endTransaction(context, options);
  78.                                                 return true;
  79.                                         }
  80.                                 }
  81.                         };
  82.                         var commandManager = spread.commandManager();
  83.                         commandManager.register("endRight", endRight);
复制代码
最后,通过setShortcutKey分别将不同的命令绑到对应的键位上
  1. spread.commandManager().setShortcutKey('endUp', GC.Spread.Commands.Key.up, false, false, true, false);
  2.                         spread.commandManager().setShortcutKey('endDown', GC.Spread.Commands.Key.down, false, false, true, false);
  3.                         spread.commandManager().setShortcutKey('endLeft', GC.Spread.Commands.Key.left, false, false, true, false);
  4.                         spread.commandManager().setShortcutKey('endRight', GC.Spread.Commands.Key.right, false, false, true, false);
复制代码
完整的示例可以参考附件,也可以下载后自己在demo中用alt+上下左右方向键尝试一下效果


Range.End.html

5.46 KB, 下载次数: 18

0 个回复

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