diff --git a/packages/core/src/shared/__tests__/rectangle.spec.ts b/packages/core/src/shared/__tests__/rectangle.spec.ts index 1289620917f..f22760531f0 100644 --- a/packages/core/src/shared/__tests__/rectangle.spec.ts +++ b/packages/core/src/shared/__tests__/rectangle.spec.ts @@ -135,7 +135,7 @@ describe('multiSubtractMulti', () => { { startColumn: 2, endColumn: 4, startRow: 2, endRow: 4 }, ]; const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2); - expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); + expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); }); it('should handle subtracting multiple ranges from a single range', () => { @@ -147,7 +147,7 @@ describe('multiSubtractMulti', () => { { startColumn: 6, endColumn: 8, startRow: 6, endRow: 8 }, ]; const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2); - expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); + expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); }); it('should handle non-overlapping subtraction ranges', () => { @@ -158,7 +158,7 @@ describe('multiSubtractMulti', () => { { startColumn: 6, endColumn: 8, startRow: 6, endRow: 8 }, ]; const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2); - expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); + expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); }); it('should handle subtraction ranges that completely overlap', () => { @@ -169,7 +169,7 @@ describe('multiSubtractMulti', () => { { startColumn: 1, endColumn: 5, startRow: 1, endRow: 5 }, ]; const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2); - expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); + expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); }); it('should handle empty ranges', () => { @@ -178,7 +178,7 @@ describe('multiSubtractMulti', () => { { startColumn: 2, endColumn: 4, startRow: 2, endRow: 4 }, ]; const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2); - expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); + expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); }); it('should handle empty subtraction ranges', () => { @@ -187,6 +187,6 @@ describe('multiSubtractMulti', () => { ]; const ranges2: IRange[] = []; const expected: IRange[] = multiSubtractMultiRanges(ranges1, ranges2); - expect(rangesToMatrix(Rectangle.multiSubtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); + expect(rangesToMatrix(Rectangle.subtractMulti(ranges1, ranges2))).toEqual(rangesToMatrix(expected)); }); }); diff --git a/packages/core/src/shared/range.ts b/packages/core/src/shared/range.ts index d700ce6ffb7..1c5d2ec1142 100644 --- a/packages/core/src/shared/range.ts +++ b/packages/core/src/shared/range.ts @@ -216,3 +216,10 @@ export function mergeRanges(ranges: IRange[]): IRange[] { // return horizontalMerged; } +export function multiSubtractSingleRange(ranges: IRange[], toDelete: IRange) { + const res: IRange[] = []; + ranges.forEach((range) => { + res.push(...Rectangle.subtract(range, toDelete)); + }); + return Rectangle.mergeRanges(res); +}; diff --git a/packages/core/src/shared/rectangle.ts b/packages/core/src/shared/rectangle.ts index 56eb104f886..395f2ab7cdf 100644 --- a/packages/core/src/shared/rectangle.ts +++ b/packages/core/src/shared/rectangle.ts @@ -15,7 +15,7 @@ */ import { AbsoluteRefType, type IRange, type IRectLTRB, RANGE_TYPE } from '../sheets/typedef'; -import { mergeRanges } from './range'; +import { mergeRanges, multiSubtractSingleRange } from './range'; import type { Nullable } from './types'; /** @@ -323,22 +323,14 @@ export class Rectangle { return mergeRanges(ranges); } - static multiSubtractSingle(ranges: IRange[], toDelete: IRange) { - const res: IRange[] = []; - ranges.forEach((range) => { - res.push(...Rectangle.subtract(range, toDelete)); - }); - return Rectangle.mergeRanges(res); - }; - - static multiSubtractMulti(ranges1: IRange[], ranges2: IRange[]): IRange[] { + static subtractMulti(ranges1: IRange[], ranges2: IRange[]): IRange[] { if (!ranges2.length) { return ranges1; } let res: IRange[] = ranges1; ranges2.forEach((range) => { - res = Rectangle.multiSubtractSingle(res, range); + res = multiSubtractSingleRange(res, range); }); return Rectangle.mergeRanges(res); diff --git a/packages/sheets-data-validation/src/models/rule-matrix.ts b/packages/sheets-data-validation/src/models/rule-matrix.ts index d1e11d1e7f0..2814e76b8fe 100644 --- a/packages/sheets-data-validation/src/models/rule-matrix.ts +++ b/packages/sheets-data-validation/src/models/rule-matrix.ts @@ -46,7 +46,7 @@ export class RuleMatrix { const ranges = rule.ranges.map((range) => Range.transformRange(range, this._worksheet)); this._map.forEach((value, key) => { - const newRanges = Rectangle.multiSubtractMulti(value, ranges); + const newRanges = Rectangle.subtractMulti(value, ranges); if (newRanges.length === 0) { this._map.delete(key); } else { @@ -60,7 +60,7 @@ export class RuleMatrix { removeRange(_ranges: IRange[]) { const ranges = _ranges.map((range) => Range.transformRange(range, this._worksheet)); this._map.forEach((value, key) => { - const newRanges = Rectangle.multiSubtractMulti(value, ranges); + const newRanges = Rectangle.subtractMulti(value, ranges); if (newRanges.length === 0) { this._map.delete(key); } else { @@ -77,7 +77,7 @@ export class RuleMatrix { this._map.delete(ruleId); const ranges = _newRanges.map((range) => Range.transformRange(range, this._worksheet)); this._map.forEach((value, key) => { - const newRanges = Rectangle.multiSubtractMulti(value, ranges); + const newRanges = Rectangle.subtractMulti(value, ranges); if (newRanges.length === 0) { this._map.delete(key); } else { @@ -190,7 +190,7 @@ export class RuleMatrix { if (key === ruleId) { return; } - const newRanges = Rectangle.multiSubtractMulti(value, ranges); + const newRanges = Rectangle.subtractMulti(value, ranges); if (newRanges.length === 0) { this._map.delete(key); } else {