Skip to content

Commit

Permalink
feat: impl sheet to be able to delete whole rows & cols
Browse files Browse the repository at this point in the history
  • Loading branch information
iyxan23 committed Oct 18, 2024
1 parent ab2108a commit 499462e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/sheet/sheet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,25 @@ describe("sheet tests", () => {
expect(sheet.getWholeRow({ row: 2 })).toEqual([7, 14, 15, 10]);
});

it("deletes whole row and fill from row", () => {
const sheet = new Sheet<number>([
[1, 2, 3, 8],
[4, 5, null, null],
[7, null, 9, 10],
[10, 11, 12, 3],
[null, 14, 15, 2],
]);

sheet.deleteBlock({
start: { row: 1, col: 0 },
end: { row: 1 },
fillFrom: "row",
});

expect(sheet.getWholeRow({ row: 1 })).toEqual([7, null, 9, 10]);
expect(sheet.getWholeRow({ row: 2 })).toEqual([10, 11, 12, 3]);
});

it("deletes a block without filling", () => {
const sheet = new Sheet<number>([
[1, 2, 3, 8],
Expand Down
15 changes: 10 additions & 5 deletions src/sheet/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,19 @@ export class Sheet<T> {
fillFrom,
}: {
start: { col: number; row: number };
end: { col: number; row: number };
end: { col?: number; row?: number };
fillFrom: "col" | "row" | null;
}) {
const width = end.col - start.col + 1;
const height = end.row - start.row + 1;
const bounds = this.getBounds();

const endCol = end.col ?? bounds.colBound + 1;
const endRow = end.row ?? bounds.rowBound + 1;

const width = endCol - start.col + 1;
const height = endRow - start.row + 1;

for (let row = start.row; row <= end.row; row++) {
for (let col = start.col; col <= end.col; col++) {
for (let row = start.row; row <= endRow; row++) {
for (let col = start.col; col <= endCol; col++) {
this.setCell(col, row, null);
}
}
Expand Down

0 comments on commit 499462e

Please sign in to comment.