From 499462e10aaf383bb74d7cf02f5c389de8304e50 Mon Sep 17 00:00:00 2001 From: iyxan23 Date: Fri, 18 Oct 2024 21:53:57 +0700 Subject: [PATCH] feat: impl sheet to be able to delete whole rows & cols --- src/sheet/sheet.test.ts | 19 +++++++++++++++++++ src/sheet/sheet.ts | 15 ++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/sheet/sheet.test.ts b/src/sheet/sheet.test.ts index a402318..3fa89c2 100644 --- a/src/sheet/sheet.test.ts +++ b/src/sheet/sheet.test.ts @@ -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([ + [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([ [1, 2, 3, 8], diff --git a/src/sheet/sheet.ts b/src/sheet/sheet.ts index ec85cba..6b9b7bf 100644 --- a/src/sheet/sheet.ts +++ b/src/sheet/sheet.ts @@ -261,14 +261,19 @@ export class Sheet { 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); } }