Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sheet): add range merge util #1615

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions packages/sheets/src/basics/rangeMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading