本帖最后由 Richard.Ma 于 2023-10-30 17:42 编辑
SpreadJS和Excel由于设计的差异,在一些行为上存在不同。当选中区域后,使用快捷键【Ctrl+Shift+↓】来进行向下扩展选中区域的时候,
- SpreadJS会直接将当前选中区域的尾行直接扩展到sheet最下方
在使用spreadjs的过程中,如果想要spreadJS能实现同样的效果,我们可以通过重写spreadJS给【Ctrl+Shift+↓】绑定的命令selectionBottom来实现
首先定义一个command,在其中通过spreadJS提供的search方法,设置选项通配符匹配,通过"?" 来匹配选中区域下方的非空单元格。然后修改选中区域范围
- var selectionBottomCmd = {
- canUndo: true,
- execute: function(spread, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- if (isUndo) {
- Commands.undoTransaction(spread, options);
- return true;
- } else {
- Commands.startTransaction(spread, options);
- console.log("start");
- spread.suspendPaint();
- var sheet = spread.getActiveSheet();
- var selections = sheet.getSelections();
- var newselection=[];
- selections.forEach((selection)=>{
- var searchCondition = new GC.Spread.Sheets.Search.SearchCondition();
- searchCondition.searchString = "?";
- searchCondition.startSheetIndex = spread.getActiveSheetIndex();
- searchCondition.endSheetIndex = spread.getActiveSheetIndex();
- searchCondition.columnStart = selection.col;
- searchCondition.columnEnd = selection.col+selection.colCount-1;
- searchCondition.rowStart = selection.row+selection.rowCount;
- searchCondition.rowEnd = sheet.getRowCount()-1;
- searchCondition.searchFlags = GC.Spread.Sheets.Search.SearchFlags.ignoreCase| GC.Spread.Sheets.Search.SearchFlags.useWildCards|GC.Spread.Sheets.Search.SearchFlags.blockRange;
- var searchresult= sheet.search(searchCondition);
- var newendrowindx=searchresult.foundRowIndex!=-1?searchresult.foundRowIndex:sheet.getRowCount()-1;
- newselection.push(new GC.Spread.Sheets.Range(selection.row,selection.col,newendrowindx-selection.row+1,selection.colCount))
-
- });
- sheet.clearSelection();
- newselection.forEach((selection)=>{
- console.log(selection);
- if(selection)
- sheet.addSelection(selection.row,selection.col,selection.rowCount,selection.colCount)
- })
- spread.resumePaint();
- Commands.endTransaction(spread, options);
- return true;
- }
- }
- };
复制代码
将这个创建的command 注册为名称为"selectionBottom"的command来覆盖原有的同名命令,注册的同时也给设置了相同的快捷键【Ctrl+Shift+↓】 这里的40是向下箭头对应的KeyCode。
- var commandManager = spread.commandManager();
- commandManager.register('selectionBottom', selectionBottomCmd,40,true,true);
复制代码
接下来,你可以看到在SpreadJS中点击上述快捷键。也实现了和Excel相同的向下扩展选中你的效果
最后,附上上述代码的完整demo
|