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(sheet): add range merge util (#1615)

Gpound.liue2118b5deb08
diff --git a/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts b/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
index a5f34fd722..264664e28b 100644
--- a/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
+++ b/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
@@ -17,7 +17,7 @@
import type { IRange } from '@univerjs/core';
import { describe, expect, it } from 'vitest';
-import { rangeMerge } from '../rangeMerge';
+import { rangeMerge, RangeMergeUtil } from '../rangeMerge';
const cellToRange = (row: number, col: number) =>
({ startColumn: col, endColumn: col, endRow: row, startRow: row }) as IRange;
@@ -171,31 +171,18 @@ describe('test rangeMerge', () => {
]);
});
- // it('performance 10 rows and 250 columns', () => {
- // // column sparse matrices have better performance
- // const now = performance.now();
- // rangeMerge([{ startRow: 0, endRow: 10, startColumn: 0, endColumn: 250 }]);
- // const end = performance.now();
- // const result = end - now;
- // console.log(result, ':ms');
- // expect(result).toBeLessThanOrEqual(5000); // 4002
- // });
-
- // it('performance 100 rows and 100 columns', () => {
- // const now = performance.now();
- // rangeMerge([{ startRow: 0, endRow: 100, startColumn: 0, endColumn: 100 }]);
- // const end = performance.now();
- // const result = end - now;
- // console.log(result, ':ms');
- // expect(result).toBeLessThanOrEqual(5000); // 4640
- // });
-
- // it('performance 1000 rows and 10 columns', () => {
- // const now = performance.now();
- // rangeMerge([{ startRow: 0, endRow: 1000, startColumn: 0, endColumn: 10 }]);
- // const end = performance.now();
- // const result = end - now;
- // console.log(result, ':ms');
- // expect(result).toBeLessThanOrEqual(4000); // 3483
- // });
+ it('test rangeMergeUtil', () => {
+ const testRange: IRange[] = [cellToRange(2, 2), cellToRange(3, 2), cellToRange(4, 2)];
+ expect(new RangeMergeUtil().add(...testRange).merge()).toEqual([
+ { startColumn: 2, endColumn: 2, startRow: 2, endRow: 4 },
+ ]);
+ expect(new RangeMergeUtil().add(...testRange).subtract(cellToRange(4, 2)).merge()).toEqual([
+ { startColumn: 2, endColumn: 2, startRow: 2, endRow: 3 },
+ ]);
+ expect(new RangeMergeUtil().add(...testRange).subtract(cellToRange(3, 2)).merge()).toEqual([
+ { startColumn: 2, endColumn: 2, startRow: 2, endRow: 2 },
+ { startColumn: 2, endColumn: 2, startRow: 4, endRow: 4 },
+ ]);
+ expect(new RangeMergeUtil().add(...testRange).subtract(...testRange).merge()).toEqual([]);
+ });
});
diff --git a/packages/sheets/src/basics/rangeMerge.ts b/packages/sheets/src/basics/rangeMerge.ts
index a85cf512a4..eb5a834afc 100644
--- a/packages/sheets/src/basics/rangeMerge.ts
+++ b/packages/sheets/src/basics/rangeMerge.ts
@@ -17,7 +17,7 @@
import type { IRange } from '@univerjs/core';
import { ObjectMatrix, Range } from '@univerjs/core';
-const createTopMatrix = (ranges: IRange[]) => {
+export const createTopMatrixFromRanges = (ranges: IRange[]) => {
const matrix = new ObjectMatrix<number>();
ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
@@ -32,6 +32,16 @@ const createTopMatrix = (ranges: IRange[]) => {
});
return matrix;
};
+export const createTopMatrixFromMatrix = (matrix: ObjectMatrix<1>) => {
+ const _matrix = matrix as ObjectMatrix<number>;
+ _matrix.forValue((row, col) => {
+ const theLastRowValue = matrix.getValue(row - 1, col);
+ if (theLastRowValue) {
+ _matrix.setValue(row, col, theLastRowValue + 1);
+ }
+ });
+ return _matrix;
+};
const findMaximalRectangle = (topMatrix: ObjectMatrix<number>) => {
const res: {
area: number;
@@ -92,7 +102,7 @@ const filterLeftMatrix = (topMatrix: ObjectMatrix<number>, range: IRange) => {
return topMatrix;
};
-const findAllRectangle = (topMatrix: ObjectMatrix<number>) => {
+export const findAllRectangle = (topMatrix: ObjectMatrix<number>) => {
const resultList = [];
let result = findMaximalRectangle(topMatrix);
while (result.area > 0) {
@@ -113,6 +123,33 @@ const findAllRectangle = (topMatrix: ObjectMatrix<number>) => {
* @returns {IRange[]}
*/
export const rangeMerge = (ranges: IRange[]) => {
- const topMatrix = createTopMatrix(ranges);
+ const topMatrix = createTopMatrixFromRanges(ranges);
return findAllRectangle(topMatrix);
};
+
+export class RangeMergeUtil {
+ private _matrix = new ObjectMatrix<1>();
+ add(...ranges: IRange[]) {
+ ranges.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ this._matrix.setValue(row, col, 1);
+ });
+ });
+ return this;
+ }
+
+ subtract(...ranges: IRange[]) {
+ ranges.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ this._matrix.realDeleteValue(row, col);
+ });
+ });
+ return this;
+ }
+
+ merge() {
+ const topMatrix = createTopMatrixFromMatrix(this._matrix);
+ const ranges = findAllRectangle(topMatrix);
+ return ranges;
+ }
+}
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index 7d82051980..054c4f47eb 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -54,7 +54,7 @@ export {
// #region commands
-export { rangeMerge } from './basics/rangeMerge';
+export { rangeMerge, createTopMatrixFromRanges, createTopMatrixFromMatrix, findAllRectangle, RangeMergeUtil } from './basics/rangeMerge';
export { ClearSelectionAllCommand } from './commands/commands/clear-selection-all.command';
export { ClearSelectionContentCommand } from './commands/commands/clear-selection-content.command';
export { ClearSelectionFormatCommand } from './commands/commands/clear-selection-format.command';