背景:
当某一列存在筛选的情况下时,删除整列操作会导致跟Excel结果不同,Excel中筛选掉的结果不会被删除,SpreadJS中会全部删除包括筛选掉的结果,这里在设计上会有稍许差异,如果想要保持跟Excel的行为一致可以参考下面方法
思路:
通过重写删除命令,在命令中来控制删除行为
核心代码:
- var oldclear = GC.Spread.Sheets.Commands.clear.execute;
- var myClear = {
- canUndo: true,
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- options.cmd = "myClear";
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- if(!sheet.isEditing()){
- Commands.startTransaction(context, options);
- spread.suspendPaint();
- var sheet = context.getActiveSheet();
- var filterRange = sheet.rowFilter().range;
- var filterRow = filterRange.row;
- var filterRowCount = filterRange.rowCount;
- var filterCol = filterRange.col;
- var filterColCount = filterRange.colCount;
- var selections = sheet.getSelections();
-
- for(var i = 0; i < selections.length; i++){
-
- var row = selections[i].row == -1 ? 0 : selections[i].row;
- var col = selections[i].col;
- var rowCount = selections[i].rowCount;
- var colCount = selections[i].colCount;
- //筛选标记
- var isfilteredFlag = false;
- for(var i = col; i < col + colCount; i++){
- if(sheet.rowFilter().isFiltered(i)){
- isfilteredFlag = true;
- }
- }
-
- if(col < filterCol || col+colCount > filterCol+filterColCount){
- //filter区域外
- sheet.clear(row,col,rowCount,colCount,GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
- }else{
- if(isfilteredFlag){
- if(rowCount > 1){
-
- for(var i=row; i < row + rowCount; i++){
- if( i < filterRow + filterRowCount && i >= filterRow){
- if(!sheet.rowFilter().isRowFilteredOut(i)){
- sheet.clear(i,col,1,colCount,GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
- }
- } else {
- sheet.clear(i,col,1,colCount,GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
- }
- }
-
- }else{
- sheet.clear(row,col,rowCount,colCount,GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
- }
- }else{
- sheet.clear(row,col,rowCount,colCount,GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data);
- }
-
- }
- }
-
- options.cmd = "myClear";
- spread.resumePaint();
- Commands.endTransaction(context, options);
- return true;
-
- } else {
- return false;
- }
-
- }
- }
- };
-
- spread.commandManager().register("myClear", myClear);
-
- spread.commandManager().setShortcutKey('myClear', GC.Spread.Commands.Key.del, false, false, false, false);
复制代码 完整demo参考附件
|
|