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.

feat(core): optimize split into grid function (#3565)

zhangw4e8d9a59c010
diff --git a/packages/core/src/shared/__tests__/range.spec.ts b/packages/core/src/shared/__tests__/range.spec.ts
index 65ad69b4a6..09a5dd3b74 100644
--- a/packages/core/src/shared/__tests__/range.spec.ts
+++ b/packages/core/src/shared/__tests__/range.spec.ts
@@ -20,6 +20,10 @@ import { AbsoluteRefType } from '../../sheets/typedef';
import { mergeHorizontalRanges, mergeRanges, mergeVerticalRanges, moveRangeByOffset, splitIntoGrid } from '../range';
import type { IRange } from '../../sheets/typedef';
+const stringifyRanges = (ranges: IRange[]) => {
+ return ranges.sort((a, b) => a.startRow - b.startRow || a.startColumn - b.startColumn).map((range) => `${range.startRow},${range.startColumn},${range.endRow},${range.endColumn}`);
+};
+
describe('test moveRangeByOffset', () => {
it('test normal', () => {
const range = {
@@ -92,13 +96,13 @@ describe('splitIntoGrid', () => {
{ startColumn: 6, endColumn: 8, startRow: 2, endRow: 2 },
];
- expect(splitIntoGrid(input)).toEqual(expected);
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
});
it('should handle no ranges', () => {
const input: IRange[] = [];
const expected: IRange[] = [];
- expect(splitIntoGrid(input)).toEqual(expected);
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
});
it('should handle single range', () => {
@@ -108,7 +112,18 @@ describe('splitIntoGrid', () => {
const expected: IRange[] = [
{ startColumn: 1, endColumn: 3, startRow: 1, endRow: 2 },
];
- expect(splitIntoGrid(input)).toEqual(expected);
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
+ });
+
+ it('should handle full overlap range', () => {
+ const input: IRange[] = [
+ { startColumn: 1, endColumn: 3, startRow: 1, endRow: 2 },
+ { startColumn: 1, endColumn: 3, startRow: 1, endRow: 2 },
+ ];
+ const expected: IRange[] = [
+ { startColumn: 1, endColumn: 3, startRow: 1, endRow: 2 },
+ ];
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
});
it('should handle simple case', () => {
@@ -119,7 +134,7 @@ describe('splitIntoGrid', () => {
{ startColumn: 6, endColumn: 8, startRow: 2, endRow: 2 },
];
- expect(splitIntoGrid(input)).toEqual(expected);
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
});
it('should handle this case', () => {
@@ -144,7 +159,48 @@ describe('splitIntoGrid', () => {
{ startColumn: 6, endColumn: 7, startRow: 8, endRow: 8 },
{ startColumn: 0, endColumn: 5, startRow: 9, endRow: 10 },
];
- expect(JSON.stringify(splitIntoGrid(input))).toEqual(JSON.stringify(expected));
+
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
+ });
+
+ it('should handle small range', () => {
+ const input = [{ startColumn: 1, endColumn: 1, startRow: 1, endRow: 1 }, { startColumn: 1, endColumn: 1, startRow: 2, endRow: 2 }, { startColumn: 2, endColumn: 2, startRow: 1, endRow: 1 }, { startColumn: 2, endColumn: 2, startRow: 2, endRow: 2 }, { startColumn: 1, endColumn: 1, startRow: 2, endRow: 2 }];
+ const expected = [{ startColumn: 1, endColumn: 1, startRow: 1, endRow: 1 }, { startColumn: 1, endColumn: 1, startRow: 2, endRow: 2 }, { startColumn: 2, endColumn: 2, startRow: 1, endRow: 1 }, { startColumn: 2, endColumn: 2, startRow: 2, endRow: 2 }];
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
+ });
+
+ it('should handle overlapping ranges correctly', () => {
+ const input = [
+ {
+ startColumn: 1,
+ endColumn: 4,
+ startRow: 1,
+ endRow: 4,
+ },
+ {
+ startColumn: 1,
+ startRow: 1,
+ endColumn: 5,
+ endRow: 4,
+ },
+ ];
+
+ const expected: IRange[] = [
+ {
+ startColumn: 1,
+ endColumn: 4,
+ startRow: 1,
+ endRow: 4,
+ },
+ {
+ startColumn: 5,
+ startRow: 1,
+ endColumn: 5,
+ endRow: 4,
+ },
+ ];
+
+ expect(stringifyRanges(splitIntoGrid(input))).toEqual(stringifyRanges(expected));
});
});
@@ -159,7 +215,7 @@ describe('mergeHorizontalRanges', () => {
{ startColumn: 1, endColumn: 8, startRow: 1, endRow: 1 },
];
- expect(mergeHorizontalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeHorizontalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle ranges with gaps', () => {
@@ -173,7 +229,7 @@ describe('mergeHorizontalRanges', () => {
{ startColumn: 5, endColumn: 7, startRow: 1, endRow: 1 },
];
- expect(mergeHorizontalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeHorizontalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle overlapping ranges', () => {
@@ -186,7 +242,7 @@ describe('mergeHorizontalRanges', () => {
{ startColumn: 1, endColumn: 8, startRow: 1, endRow: 1 },
];
- expect(mergeHorizontalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeHorizontalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle multiple rows independently', () => {
@@ -202,7 +258,7 @@ describe('mergeHorizontalRanges', () => {
{ startColumn: 1, endColumn: 7, startRow: 2, endRow: 2 },
];
- expect(mergeHorizontalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeHorizontalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle single range', () => {
@@ -214,7 +270,7 @@ describe('mergeHorizontalRanges', () => {
{ startColumn: 1, endColumn: 3, startRow: 1, endRow: 1 },
];
- expect(mergeHorizontalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeHorizontalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle no ranges', () => {
@@ -234,7 +290,7 @@ describe('mergeHorizontalRanges', () => {
{ startColumn: 7, endColumn: 7, startRow: 1, endRow: 2 },
];
const expected = [{ startColumn: 1, endColumn: 7, startRow: 1, endRow: 2 }];
- expect(JSON.stringify(mergeHorizontalRanges(input))).toEqual(JSON.stringify(expected));
+ expect(stringifyRanges(mergeHorizontalRanges(input))).toEqual(stringifyRanges(expected));
});
});
@@ -264,7 +320,7 @@ describe('mergeVerticalRanges', () => {
{ startColumn: 1, endColumn: 1, startRow: 5, endRow: 7 },
];
- expect(mergeVerticalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeVerticalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle overlapping ranges', () => {
@@ -277,7 +333,7 @@ describe('mergeVerticalRanges', () => {
{ startColumn: 1, endColumn: 1, startRow: 1, endRow: 8 },
];
- expect(mergeVerticalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeVerticalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle multiple columns independently', () => {
@@ -293,7 +349,7 @@ describe('mergeVerticalRanges', () => {
{ startColumn: 2, endColumn: 2, startRow: 1, endRow: 8 },
];
- expect(mergeVerticalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeVerticalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle single range', () => {
@@ -305,7 +361,7 @@ describe('mergeVerticalRanges', () => {
{ startColumn: 1, endColumn: 8, startRow: 1, endRow: 2 },
];
- expect(mergeVerticalRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeVerticalRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle no ranges', () => {
@@ -321,7 +377,7 @@ describe('mergeVerticalRanges', () => {
{ startColumn: 0, endColumn: 5, startRow: 9, endRow: 10 },
];
- expect(JSON.stringify(mergeVerticalRanges(input))).toEqual(JSON.stringify(input));
+ expect(stringifyRanges(mergeVerticalRanges(input))).toEqual(stringifyRanges(input));
});
});
@@ -336,13 +392,13 @@ describe('mergeRanges', () => {
{ startColumn: 1, endColumn: 8, startRow: 1, endRow: 2 },
];
- expect(JSON.stringify(mergeRanges(input))).toEqual(JSON.stringify(expected));
+ expect(stringifyRanges(mergeRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle no ranges', () => {
const input: IRange[] = [];
const expected: IRange[] = [];
- expect(mergeRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle single range', () => {
@@ -354,7 +410,7 @@ describe('mergeRanges', () => {
{ startColumn: 1, endColumn: 3, startRow: 1, endRow: 3 },
];
- expect(mergeRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle ranges with gaps', () => {
@@ -368,7 +424,7 @@ describe('mergeRanges', () => {
{ startColumn: 5, endColumn: 7, startRow: 1, endRow: 2 },
];
- expect(mergeRanges(input)).toEqual(expected);
+ expect(stringifyRanges(mergeRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle multiple ranges in multiple rows and columns', () => {
@@ -382,7 +438,7 @@ describe('mergeRanges', () => {
{ startColumn: 1, endColumn: 5, startRow: 1, endRow: 5 },
];
- expect(JSON.stringify(mergeRanges(input))).toEqual(JSON.stringify(expected));
+ expect(stringifyRanges(mergeRanges(input))).toEqual(stringifyRanges(expected));
});
it('should handle this case', () => {
@@ -406,6 +462,6 @@ describe('mergeRanges', () => {
{ startColumn: 0, endColumn: 7, startRow: 8, endRow: 8 },
{ startColumn: 0, endColumn: 5, startRow: 9, endRow: 10 },
];
- expect(JSON.stringify(mergeRanges(input))).toEqual(JSON.stringify(expected));
+ expect(stringifyRanges(mergeRanges(input))).toEqual(stringifyRanges(expected));
});
});
diff --git a/packages/core/src/shared/object-matrix-query.ts b/packages/core/src/shared/object-matrix-query.ts
index 9afe6ff280..9136d7ae32 100644
--- a/packages/core/src/shared/object-matrix-query.ts
+++ b/packages/core/src/shared/object-matrix-query.ts
@@ -85,6 +85,9 @@ function resetMatrix<T>(matrix: Nullable<T>[][], range: IRange) {
});
}
+/**
+ * @deprecated this function could cause memory out of use in large range.
+ */
export function queryObjectMatrix<T>(matrix: ObjectMatrix<T>, match: (value: T) => boolean) {
const arrayMatrix = matrix.toFullArray();
const results: IRange[] = [];
diff --git a/packages/core/src/shared/range.ts b/packages/core/src/shared/range.ts
index 1c5d2ec114..3dec90bfbd 100644
--- a/packages/core/src/shared/range.ts
+++ b/packages/core/src/shared/range.ts
@@ -72,32 +72,37 @@ export function splitIntoGrid(ranges: IRange[]): IRange[] {
const sortedColumns = Array.from(columns).sort((a, b) => a - b);
const sortedRows = Array.from(rows).sort((a, b) => a - b);
- // Create grid cells based on unique boundaries
- const gridCells: IRange[] = [];
- for (let i = 0; i < sortedRows.length - 1; i++) {
- for (let j = 0; j < sortedColumns.length - 1; j++) {
- gridCells.push({
- startColumn: sortedColumns[j],
- endColumn: sortedColumns[j + 1] - 1,
- startRow: sortedRows[i],
- endRow: sortedRows[i + 1] - 1,
- });
- }
- }
+ // Sort ranges by startRow and startColumn
+ ranges.sort((a, b) => a.startRow - b.startRow || a.startColumn - b.startColumn);
- // Assign original ranges to the grid cells
+ // Assign original ranges to the grid cells
const result: IRange[] = [];
- for (const gridCell of gridCells) {
- for (const range of ranges) {
- if (range.startRow <= gridCell.endRow && range.endRow >= gridCell.startRow &&
- range.startColumn <= gridCell.endColumn && range.endColumn >= gridCell.startColumn) {
- result.push({
- startColumn: Math.max(gridCell.startColumn, range.startColumn),
- endColumn: Math.min(gridCell.endColumn, range.endColumn),
- startRow: Math.max(gridCell.startRow, range.startRow),
- endRow: Math.min(gridCell.endRow, range.endRow),
- });
- break; // No need to check other ranges for this grid cell
+ for (let i = 0; i < sortedRows.length - 1; i++) {
+ for (let j = 0; j < sortedColumns.length - 1; j++) {
+ // grid cell
+ const startColumn = sortedColumns[j];
+ const endColumn = sortedColumns[j + 1] - 1;
+ const startRow = sortedRows[i];
+ const endRow = sortedRows[i + 1] - 1;
+
+ for (const range of ranges) {
+ if (range.startRow > endRow) {
+ // Since ranges are sorted, we can break early
+ break;
+ }
+
+ // grid cell must be in some range
+ // just need to check range is contain grid cell
+ if (range.startRow <= startRow && range.endRow >= endRow &&
+ range.startColumn <= startColumn && range.endColumn >= endColumn) {
+ result.push({
+ startColumn,
+ endColumn,
+ startRow,
+ endRow,
+ });
+ break; // No need to check other ranges for this grid cell
+ }
}
}
}
@@ -213,7 +218,6 @@ export function mergeRanges(ranges: IRange[]): IRange[] {
const split = splitIntoGrid(ranges);
const horizontalMerged = mergeHorizontalRanges(split);
return mergeVerticalRanges(horizontalMerged);
- // return horizontalMerged;
}
export function multiSubtractSingleRange(ranges: IRange[], toDelete: IRange) {
diff --git a/packages/core/src/shared/rectangle.ts b/packages/core/src/shared/rectangle.ts
index 9382005c56..5928c1dabf 100644
--- a/packages/core/src/shared/rectangle.ts
+++ b/packages/core/src/shared/rectangle.ts
@@ -350,7 +350,7 @@ export class Rectangle {
res = multiSubtractSingleRange(res, range);
});
- return Rectangle.mergeRanges(res);
+ return res;
}
static hasIntersectionBetweenTwoRect(rect1: IRectLTRB, rect2: IRectLTRB) {
diff --git a/packages/sheets/src/services/ref-range/util.ts b/packages/sheets/src/services/ref-range/util.ts
index 03231044a5..ff3035851a 100644
--- a/packages/sheets/src/services/ref-range/util.ts
+++ b/packages/sheets/src/services/ref-range/util.ts
@@ -14,22 +14,23 @@
* limitations under the License.
*/
-import type { ICommandInfo, IMutationInfo, IRange, Nullable } from '@univerjs/core';
import { ObjectMatrix, queryObjectMatrix, Range, RANGE_TYPE, Rectangle } from '@univerjs/core';
+import type { ICommandInfo, IMutationInfo, IRange, Nullable } from '@univerjs/core';
-import { type IMoveColumnsMutationParams, type IMoveRowsMutationParams, MoveColsMutation, MoveRowsMutation } from '../../commands/mutations/move-rows-cols.mutation';
-import type { IInsertColMutationParams, IInsertRowMutationParams, IRemoveColMutationParams, IRemoveRowsMutationParams, IRemoveSheetMutationParams } from '../../basics';
-import { type IMoveRangeMutationParams, MoveRangeMutation } from '../../commands/mutations/move-range.mutation';
-import { RemoveSheetMutation } from '../../commands/mutations/remove-sheet.mutation';
-import { RemoveColMutation, RemoveRowMutation } from '../../commands/mutations/remove-row-col.mutation';
-import { InsertColMutation, InsertRowMutation } from '../../commands/mutations/insert-row-col.mutation';
-import type { ISheetCommandSharedParams } from '../../commands/utils/interface';
-import type { SheetsSelectionsService } from '../selections/selection-manager.service';
import { DeleteRangeMoveLeftCommand } from '../../commands/commands/delete-range-move-left.command';
import { DeleteRangeMoveUpCommand } from '../../commands/commands/delete-range-move-up.command';
-import type { IRemoveRowColCommandInterceptParams } from '../../commands/commands/remove-row-col.command';
import { InsertRangeMoveDownCommand } from '../../commands/commands/insert-range-move-down.command';
import { InsertRangeMoveRightCommandId } from '../../commands/commands/insert-range-move-right.command';
+import { InsertColMutation, InsertRowMutation } from '../../commands/mutations/insert-row-col.mutation';
+import { type IMoveRangeMutationParams, MoveRangeMutation } from '../../commands/mutations/move-range.mutation';
+import { type IMoveColumnsMutationParams, type IMoveRowsMutationParams, MoveColsMutation, MoveRowsMutation } from '../../commands/mutations/move-rows-cols.mutation';
+import { RemoveColMutation, RemoveRowMutation } from '../../commands/mutations/remove-row-col.mutation';
+import { RemoveSheetMutation } from '../../commands/mutations/remove-sheet.mutation';
+import { EffectRefRangId, OperatorType } from './type';
+import type { IInsertColMutationParams, IInsertRowMutationParams, IRemoveColMutationParams, IRemoveRowsMutationParams, IRemoveSheetMutationParams } from '../../basics';
+import type { IRemoveRowColCommandInterceptParams } from '../../commands/commands/remove-row-col.command';
+import type { ISheetCommandSharedParams } from '../../commands/utils/interface';
+import type { SheetsSelectionsService } from '../selections/selection-manager.service';
import type {
EffectRefRangeParams,
IDeleteRangeMoveLeftCommand,
@@ -45,7 +46,6 @@ import type {
IRemoveRowColCommand,
IReorderRangeCommand,
} from './type';
-import { EffectRefRangId, OperatorType } from './type';
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
export const handleRangeTypeInput = (range: IRange) => {
@@ -275,6 +275,7 @@ export const handleMoveRowsCommon = (params: IMoveRowsCommand, targetRange: IRan
matrix.moveRows(fromRow, count, toRow);
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
const res = queryObjectMatrix(matrix, (value) => value === 1);
return res;
};
@@ -301,6 +302,7 @@ export const handleReorderRangeCommon = (param: IReorderRangeCommand, targetRang
cacheMatrix.forValue((row, col, cellData) => {
matrix.setValue(row, col, cellData);
});
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
const res = queryObjectMatrix(matrix, (value) => value === 1);
return res;
};
@@ -351,7 +353,7 @@ export const handleMoveColsCommon = (params: IMoveColsCommand, targetRange: IRan
});
matrix.moveColumns(fromCol, count, toCol);
-
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
return queryObjectMatrix(matrix, (value) => value === 1);
};
@@ -435,6 +437,7 @@ export const handleMoveRangeCommon = (param: IMoveRangeCommand, targetRange: IRa
matrix.setValue(targetRow, targetCol, fromMatrix.getValue(row, col) ?? 0);
});
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
const res = queryObjectMatrix(matrix, (value) => value === 1);
return res;
};
@@ -694,6 +697,7 @@ export const handleInsertRangeMoveDownCommon = (param: IInsertRangeMoveDownComma
matrix.setValue(row + moveCount, col, 1);
});
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
return queryObjectMatrix(matrix, (v) => v === 1);
};
@@ -744,6 +748,7 @@ export const handleInsertRangeMoveRightCommon = (param: IInsertRangeMoveRightCom
matrix.setValue(row, col + moveCount, 1);
});
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
return queryObjectMatrix(matrix, (v) => v === 1);
};
@@ -810,6 +815,7 @@ export const handleDeleteRangeMoveLeftCommon = (param: IDeleteRangeMoveLeftComma
});
});
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
return queryObjectMatrix(matrix, (v) => v === 1);
};
@@ -872,6 +878,7 @@ export const handleDeleteRangeMoveUpCommon = (param: IDeleteRangeMoveUpCommand,
});
});
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
return queryObjectMatrix(matrix, (v) => v === 1);
};
@@ -890,6 +897,7 @@ export const handleRemoveRowCommon = (param: IRemoveRowColCommandInterceptParams
matrix.removeRows(startRow, count);
});
+ // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.
return queryObjectMatrix(matrix, (value) => value === 1);
};