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(core): merge range util error (#2776)

Gpound.liu7a41b9cd7151
diff --git a/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts b/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
index 4d6c2d71a1..05e8e379e2 100644
--- a/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
+++ b/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
@@ -183,4 +183,33 @@ describe('test rangeMerge', () => {
]);
expect(new RangeMergeUtil().add(...testRange).subtract(...testRange).merge()).toEqual([]);
});
+
+ it('should be no overlapping regions', () => {
+ const arr = [
+ {
+ startRow: 4,
+ endRow: 12,
+ startColumn: 1,
+ endColumn: 6,
+ },
+ {
+ startRow: 12,
+ endRow: 13,
+ startColumn: 2,
+ endColumn: 5,
+ },
+ {
+ startRow: 3,
+ endRow: 3,
+ startColumn: 2,
+ endColumn: 5,
+ },
+ ];
+ const res = new RangeMergeUtil().add(...arr).merge();
+ expect(res).toEqual([
+ { startRow: 4, endRow: 12, startColumn: 1, endColumn: 6 },
+ { startRow: 3, endRow: 3, startColumn: 2, endColumn: 5 },
+ { startRow: 13, endRow: 13, startColumn: 2, endColumn: 5 },
+ ]);
+ });
});
diff --git a/packages/sheets/src/basics/rangeMerge.ts b/packages/sheets/src/basics/rangeMerge.ts
index aec4befe83..9daaa369ad 100644
--- a/packages/sheets/src/basics/rangeMerge.ts
+++ b/packages/sheets/src/basics/rangeMerge.ts
@@ -92,14 +92,20 @@ const findMaximalRectangle = (topMatrix: ObjectMatrix<number>) => {
const filterLeftMatrix = (topMatrix: ObjectMatrix<number>, range: IRange) => {
Range.foreach(range, (row, col) => {
topMatrix.realDeleteValue(row, col);
- let theNextRow = row + 1;
- let theNextRowValue = topMatrix.getValue(theNextRow, col) || 0;
- while (theNextRowValue > 1) {
- topMatrix.setValue(theNextRow, col, theNextRowValue - 1);
- theNextRow += 1;
- theNextRowValue = topMatrix.getValue(theNextRow, col) || 0;
- }
});
+
+ for (let col = range.startColumn; col <= range.endColumn; col++) {
+ const row = range.endRow + 1;
+ const v = topMatrix.getValue(row, col);
+ if (v > 0) {
+ topMatrix.setValue(row, col, 1);
+ let nextRow = row + 1;
+ while (topMatrix.getValue(nextRow, col) > 0) {
+ topMatrix.setValue(nextRow, col, topMatrix.getValue(nextRow - 1, col) + 1);
+ nextRow++;
+ }
+ }
+ }
return topMatrix;
};