Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(sheet): fix expand range performance (#2882)

VicKun0de348867714
diff --git a/packages/sheets-filter-ui/src/commands/commands/sheets-filter.command.ts b/packages/sheets-filter-ui/src/commands/commands/sheets-filter.command.ts
index 6db56426e5..8241327dcd 100644
--- a/packages/sheets-filter-ui/src/commands/commands/sheets-filter.command.ts
+++ b/packages/sheets-filter-ui/src/commands/commands/sheets-filter.command.ts
@@ -18,10 +18,9 @@ import type { IAccessor, ICommand, IMutationInfo, Nullable, Workbook } from '@un
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, LocaleService, Quantity, sequenceExecute, UniverInstanceType } from '@univerjs/core';
import { MessageType } from '@univerjs/design';
import type { ISheetCommandSharedParams } from '@univerjs/sheets';
-import { isSingleCellSelection, SheetsSelectionsService } from '@univerjs/sheets';
+import { expandToContinuousRange, isSingleCellSelection, SheetsSelectionsService } from '@univerjs/sheets';
import type { FilterColumn, IAutoFilter, IFilterColumn, IReCalcSheetsFilterMutationParams, ISetSheetsFilterCriteriaMutationParams, ISetSheetsFilterRangeMutationParams } from '@univerjs/sheets-filter';
import { ReCalcSheetsFilterMutation, RemoveSheetsFilterMutation, SetSheetsFilterCriteriaMutation, SetSheetsFilterRangeMutation, SheetsFilterService } from '@univerjs/sheets-filter';
-import { expandToContinuousRange } from '@univerjs/sheets-ui';
import { IMessageService } from '@univerjs/ui';
/**
diff --git a/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx b/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx
index 38dac303d1..36e0688788 100644
--- a/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx
+++ b/packages/sheets-sort-ui/src/services/sheets-sort-ui.service.tsx
@@ -29,9 +29,8 @@ import {
UniverInstanceType,
} from '@univerjs/core';
-import { getPrimaryForRange, SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
+import { expandToContinuousRange, getPrimaryForRange, SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
import type { ISheetRangeLocation } from '@univerjs/sheets';
-import { expandToContinuousRange } from '@univerjs/sheets-ui';
import type { ISortOption } from '@univerjs/sheets-sort';
import { SheetsSortService, SortType } from '@univerjs/sheets-sort';
diff --git a/packages/sheets-ui/src/commands/commands/set-selection.command.ts b/packages/sheets-ui/src/commands/commands/set-selection.command.ts
index 096c98d825..1182b80c86 100644
--- a/packages/sheets-ui/src/commands/commands/set-selection.command.ts
+++ b/packages/sheets-ui/src/commands/commands/set-selection.command.ts
@@ -17,6 +17,7 @@
import type { Direction, ICommand, IRange } from '@univerjs/core';
import { CommandType, ICommandService, IUniverInstanceService, RANGE_TYPE, Rectangle, Tools } from '@univerjs/core';
import {
+ expandToContinuousRange,
getCellAtRowCol,
getSelectionsService,
getSheetCommandTarget,
@@ -27,7 +28,6 @@ import { KeyCode } from '@univerjs/ui';
import { ShortcutExperienceService } from '../../services/shortcut-experience.service';
import {
checkIfShrink,
- expandToContinuousRange,
expandToNextCell,
expandToNextGapRange,
expandToWholeSheet,
@@ -131,6 +131,7 @@ export interface IMoveSelectionEnterAndTabCommandParams {
export const MoveSelectionEnterAndTabCommand: ICommand<IMoveSelectionEnterAndTabCommandParams> = {
id: 'sheet.command.move-selection-enter-tab',
type: CommandType.COMMAND,
+ // eslint-disable-next-line max-lines-per-function
handler: async (accessor, params) => {
if (!params) {
return false;
diff --git a/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts b/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts
index 06ebb98198..c278fbf146 100644
--- a/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts
+++ b/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts
@@ -409,108 +409,6 @@ export function shrinkToNextCell(startRange: IRange, direction: Direction, works
return alignToMergedCellsBorders(Rectangle.union(otherEdge, next), worksheet, false);
}
-// eslint-disable-next-line max-lines-per-function
-export function expandToContinuousRange(startRange: IRange, directions: IExpandParams, worksheet: Worksheet): IRange {
- const { left, right, up, down } = directions;
- const maxRow = worksheet.getMaxRows();
- const maxColumn = worksheet.getMaxColumns();
-
- let changed = true;
- const destRange: IRange = { ...startRange }; // startRange should not be used below
-
- while (changed) {
- changed = false;
-
- if (up && destRange.startRow !== 0) {
- // see if there are value in the upper row of contents
- // set `changed` to true if `startRow` really changes
- const destRow = destRange.startRow - 1; // it may decrease if there are merged cell
- const matrixFromLastRow = worksheet.getMatrixWithMergedCells(
- destRow,
- destRange.startColumn,
- destRow,
- destRange.endColumn
- );
-
- // we should check if there are value in the upper row of contents, if it does
- // we should update the `destRange` and set `changed` to true
- matrixFromLastRow.forValue((row, col, value) => {
- if (cellHasValue(value)) {
- destRange.startRow = Math.min(row, destRange.startRow);
- destRange.startColumn = Math.min(col, destRange.startColumn);
- destRange.endColumn = Math.max(col, destRange.endColumn);
- changed = true;
- }
- });
- }
-
- if (down && destRange.endRow !== maxRow - 1) {
- const destRow = destRange.endRow + 1;
- const matrixFromLastRow = worksheet.getMatrixWithMergedCells(
- destRow,
- destRange.startColumn,
- destRow,
- destRange.endColumn
- );
-
- matrixFromLastRow.forValue((row, col, value) => {
- if (cellHasValue(value)) {
- destRange.endRow = Math.max(
- row + (value.rowSpan !== undefined ? value.rowSpan - 1 : 0),
- destRange.endRow
- );
- destRange.startColumn = Math.min(col, destRange.startColumn);
- destRange.endColumn = Math.max(col, destRange.endColumn);
- changed = true;
- }
- });
- }
-
- if (left && destRange.startColumn !== 0) {
- const destCol = destRange.startColumn - 1;
- const matrixFromLastCol = worksheet.getMatrixWithMergedCells(
- destRange.startRow,
- destCol,
- destRange.endRow,
- destCol
- );
-
- matrixFromLastCol.forValue((row, col, value) => {
- if (cellHasValue(value)) {
- destRange.startColumn = Math.min(col, destRange.startColumn);
- destRange.startRow = Math.min(row, destRange.startRow);
- destRange.endRow = Math.max(row, destRange.endRow);
- changed = true;
- }
- });
- }
-
- if (right && destRange.endColumn !== maxColumn - 1) {
- const destCol = destRange.endColumn + 1;
- const matrixFromLastCol = worksheet.getMatrixWithMergedCells(
- destRange.startRow,
- destCol,
- destRange.endRow,
- destCol
- );
-
- matrixFromLastCol.forValue((row, col, value) => {
- if (cellHasValue(value)) {
- destRange.endColumn = Math.max(
- col + (value.colSpan !== undefined ? value.colSpan - 1 : 0),
- destRange.endColumn
- );
- destRange.startRow = Math.min(row, destRange.startRow);
- destRange.endRow = Math.max(row, destRange.endRow);
- changed = true;
- }
- });
- }
- }
-
- return destRange;
-}
-
export function expandToWholeSheet(worksheet: Worksheet): IRange {
// DEBT: RANGE_TYPE.ALL should not use startRow, startColumn, endRow, endColumn
return {
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 99fe541bd7..c23845d1e2 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -91,7 +91,6 @@ export type { IRangeProtectionRenderCellData } from './views/permission/extensio
export { SheetPrintInterceptorService } from './services/print-interceptor.service';
export { UniverSheetsMobileUIPlugin } from './mobile-sheets-ui-plugin';
export { MobileSheetBar } from './views/mobile/sheet-bar/MobileSheetBar';
-export { expandToContinuousRange } from './commands/commands/utils/selection-utils';
export { SheetPermissionInitController } from './controllers/permission/sheet-permission-init.controller';
// #region - all commands
diff --git a/packages/sheets/src/basics/utils.ts b/packages/sheets/src/basics/utils.ts
index 769fdc91ab..5d0714496c 100644
--- a/packages/sheets/src/basics/utils.ts
+++ b/packages/sheets/src/basics/utils.ts
@@ -52,7 +52,10 @@ export const createUniqueKey = (initValue = 0) => {
};
};
-function cellHasValue(cell: ICellData): boolean {
+function cellHasValue(cell: ICellData | undefined): boolean {
+ if (cell === undefined || cell === null) {
+ return false;
+ }
return (cell.v !== undefined && cell.v !== null && cell.v !== '') || cell.p !== undefined;
}
@@ -65,6 +68,7 @@ export function expandToContinuousRange(startRange: IRange, directions: IExpandP
let changed = true;
const destRange: IRange = { ...startRange }; // startRange should not be used below
+ const allMatrix = worksheet.getMatrixWithMergedCells(0, 0, maxRow - 1, maxColumn - 1);
while (changed) {
changed = false;
@@ -72,86 +76,84 @@ export function expandToContinuousRange(startRange: IRange, directions: IExpandP
// see if there are value in the upper row of contents
// set `changed` to true if `startRow` really changes
const destRow = destRange.startRow - 1; // it may decrease if there are merged cell
- const matrixFromLastRow = worksheet.getMatrixWithMergedCells(
- destRow,
- destRange.startColumn,
- destRow,
- destRange.endColumn
- );
-
- // we should check if there are value in the upper row of contents, if it does
- // we should update the `destRange` and set `changed` to true
- matrixFromLastRow.forValue((row, col, value) => {
- if (cellHasValue(value)) {
- destRange.startRow = Math.min(row, destRange.startRow);
+
+ const checkRange = {
+ startRow: destRow,
+ startColumn: destRange.startColumn,
+ endRow: destRow,
+ endColumn: destRange.endColumn,
+ };
+ for (let col = checkRange.startColumn; col <= checkRange.endColumn; col++) {
+ const cell = allMatrix.getValue(checkRange.startRow, col);
+ if (cellHasValue(cell)) {
+ destRange.startRow = Math.min(checkRange.startRow, destRange.startRow);
destRange.startColumn = Math.min(col, destRange.startColumn);
destRange.endColumn = Math.max(col, destRange.endColumn);
changed = true;
}
- });
+ }
}
if (down && destRange.endRow !== maxRow - 1) {
const destRow = destRange.endRow + 1;
- const matrixFromLastRow = worksheet.getMatrixWithMergedCells(
- destRow,
- destRange.startColumn,
- destRow,
- destRange.endColumn
- );
-
- matrixFromLastRow.forValue((row, col, value) => {
- if (cellHasValue(value)) {
+ const checkRange = {
+ startRow: destRow,
+ startColumn: destRange.startColumn,
+ endRow: destRow,
+ endColumn: destRange.endColumn,
+ };
+ for (let col = checkRange.startColumn; col <= checkRange.endColumn; col++) {
+ const cellValue = allMatrix.getValue(checkRange.startRow, col);
+ if (cellHasValue(cellValue)) {
+ destRange.endRow = Math.max(checkRange.endRow, destRange.endRow);
destRange.endRow = Math.max(
- row + (value.rowSpan !== undefined ? value.rowSpan - 1 : 0),
+ checkRange.endRow + (cellValue.rowSpan !== undefined ? cellValue.rowSpan - 1 : 0),
destRange.endRow
);
destRange.startColumn = Math.min(col, destRange.startColumn);
destRange.endColumn = Math.max(col, destRange.endColumn);
changed = true;
}
- });
+ }
}
if (left && destRange.startColumn !== 0) {
const destCol = destRange.startColumn - 1;
- const matrixFromLastCol = worksheet.getMatrixWithMergedCells(
- destRange.startRow,
- destCol,
- destRange.endRow,
- destCol
- );
-
- matrixFromLastCol.forValue((row, col, value) => {
- if (cellHasValue(value)) {
- destRange.startColumn = Math.min(col, destRange.startColumn);
+ const checkRange = {
+ startRow: destRange.startRow,
+ startColumn: destCol,
+ endRow: destRange.endRow,
+ endColumn: destCol,
+ };
+ for (let row = checkRange.startRow; row <= checkRange.endRow; row++) {
+ const cell = allMatrix.getValue(row, checkRange.startColumn);
+ if (cellHasValue(cell)) {
+ destRange.startColumn = Math.min(checkRange.startColumn, destRange.startColumn);
destRange.startRow = Math.min(row, destRange.startRow);
destRange.endRow = Math.max(row, destRange.endRow);
changed = true;
}
- });
+ }
}
if (right && destRange.endColumn !== maxColumn - 1) {
const destCol = destRange.endColumn + 1;
- const matrixFromLastCol = worksheet.getMatrixWithMergedCells(
- destRange.startRow,
- destCol,
- destRange.endRow,
- destCol
- );
-
- matrixFromLastCol.forValue((row, col, value) => {
- if (cellHasValue(value)) {
+ const checkRange = {
+ startRow: destRange.startRow,
+ startColumn: destCol,
+ endRow: destRange.endRow,
+ endColumn: destCol,
+ };
+ for (let row = checkRange.startRow; row <= checkRange.endRow; row++) {
+ const cellValue = allMatrix.getValue(row, checkRange.endColumn);
+ if (cellHasValue(cellValue)) {
destRange.endColumn = Math.max(
- col + (value.colSpan !== undefined ? value.colSpan - 1 : 0),
+ checkRange.endColumn + (cellValue.colSpan !== undefined ? cellValue.colSpan - 1 : 0),
destRange.endColumn
);
- destRange.startRow = Math.min(row, destRange.startRow);
- destRange.endRow = Math.max(row, destRange.endRow);
changed = true;
}
- });
+ }
}
}