Skip to content

Commit 1fe333b

Browse files
committed
Added methods to be able to remove a sheet, row or cell. Added methods to be able to retrieve a row or cell based on an index. Fixed the getIndex() method of a sheet not being updated after deleting a sheet.
1 parent bca5fa9 commit 1fe333b

File tree

10 files changed

+300
-11
lines changed

10 files changed

+300
-11
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.mbenincasa.javaexcelutils.exceptions;
2+
3+
/**
4+
* This exception signals that the Excel cell was not found
5+
* @author Mirko Benincasa
6+
* @since 0.4.1
7+
*/
8+
public class CellNotFoundException extends Exception {
9+
10+
/**
11+
* Constructs an {@code CellNotFoundException} with {@code null}
12+
* as its error detail message.
13+
*/
14+
public CellNotFoundException() {
15+
super();
16+
}
17+
18+
/**
19+
* Constructs an {@code CellNotFoundException} with the specified detail message.
20+
*
21+
* @param message
22+
* The detail message (which is saved for later retrieval
23+
* by the {@link #getMessage()} method)
24+
*/
25+
public CellNotFoundException(String message) {
26+
super(message);
27+
}
28+
29+
/**
30+
* Constructs an {@code CellNotFoundException} with the specified detail message
31+
* and cause.
32+
*
33+
* <p> Note that the detail message associated with {@code cause} is
34+
* <i>not</i> automatically incorporated into this exception's detail
35+
* message.
36+
*
37+
* @param message
38+
* The detail message (which is saved for later retrieval
39+
* by the {@link #getMessage()} method)
40+
*
41+
* @param cause
42+
* The cause (which is saved for later retrieval by the
43+
* {@link #getCause()} method). (A null value is permitted,
44+
* and indicates that the cause is nonexistent or unknown.)
45+
*/
46+
public CellNotFoundException(String message, Throwable cause) {
47+
super(message, cause);
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.mbenincasa.javaexcelutils.exceptions;
2+
3+
/**
4+
* This exception signals that the Excel row was not found
5+
* @author Mirko Benincasa
6+
* @since 0.4.1
7+
*/
8+
public class RowNotFoundException extends Exception {
9+
10+
/**
11+
* Constructs an {@code RowNotFoundException} with {@code null}
12+
* as its error detail message.
13+
*/
14+
public RowNotFoundException() {
15+
super();
16+
}
17+
18+
/**
19+
* Constructs an {@code RowNotFoundException} with the specified detail message.
20+
*
21+
* @param message
22+
* The detail message (which is saved for later retrieval
23+
* by the {@link #getMessage()} method)
24+
*/
25+
public RowNotFoundException(String message) {
26+
super(message);
27+
}
28+
29+
/**
30+
* Constructs an {@code RowNotFoundException} with the specified detail message
31+
* and cause.
32+
*
33+
* <p> Note that the detail message associated with {@code cause} is
34+
* <i>not</i> automatically incorporated into this exception's detail
35+
* message.
36+
*
37+
* @param message
38+
* The detail message (which is saved for later retrieval
39+
* by the {@link #getMessage()} method)
40+
*
41+
* @param cause
42+
* The cause (which is saved for later retrieval by the
43+
* {@link #getCause()} method). (A null value is permitted,
44+
* and indicates that the cause is nonexistent or unknown.)
45+
*/
46+
public RowNotFoundException(String message, Throwable cause) {
47+
super(message, cause);
48+
}
49+
}

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelCell.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

3+
import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException;
34
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
45
import lombok.AllArgsConstructor;
56
import lombok.EqualsAndHashCode;
@@ -30,6 +31,17 @@ public class ExcelCell {
3031
*/
3132
private Integer index;
3233

34+
/**
35+
* Remove the selected Cell
36+
* @throws CellNotFoundException If the cell is not present or has not been created
37+
* @since 0.4.1
38+
*/
39+
public void remove() throws CellNotFoundException {
40+
getRow().removeCell(this.index);
41+
this.cell = null;
42+
this.index = null;
43+
}
44+
3345
/**
3446
* Returns the Row to which it belongs
3547
* @return A ExcelRow

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelRow.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

3+
import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException;
4+
import io.github.mbenincasa.javaexcelutils.exceptions.RowNotFoundException;
35
import lombok.AllArgsConstructor;
46
import lombok.EqualsAndHashCode;
57
import lombok.Getter;
@@ -31,6 +33,17 @@ public class ExcelRow {
3133
*/
3234
private Integer index;
3335

36+
/**
37+
* Remove the selected Row
38+
* @throws RowNotFoundException If the row is not present or has not been created
39+
* @since 0.4.1
40+
*/
41+
public void remove() throws RowNotFoundException {
42+
getSheet().removeRow(this.index);
43+
this.row = null;
44+
this.index = null;
45+
}
46+
3447
/**
3548
* The list of Cells related to the Row
3649
* @return A list of Cells
@@ -40,10 +53,35 @@ public List<ExcelCell> getCells() {
4053
for (Cell cell : this.row) {
4154
excelCells.add(new ExcelCell(cell, cell.getColumnIndex()));
4255
}
43-
4456
return excelCells;
4557
}
4658

59+
/**
60+
* Retrieve a cell by index
61+
* @param index The index of the cell requested
62+
* @return A ExcelCell
63+
* @throws CellNotFoundException If the cell is not present or has not been created
64+
* @since 0.4.1
65+
*/
66+
public ExcelCell getCell(Integer index) throws CellNotFoundException {
67+
Cell cell = this.row.getCell(index);
68+
if (cell == null) {
69+
throw new CellNotFoundException("There is not a cell in the index: " + index);
70+
}
71+
return new ExcelCell(cell, cell.getColumnIndex());
72+
}
73+
74+
/**
75+
* Removes a cell by index
76+
* @param index The index of the row to remove
77+
* @throws CellNotFoundException If the cell is not present or has not been created
78+
* @since 0.4.1
79+
*/
80+
public void removeCell(Integer index) throws CellNotFoundException {
81+
ExcelCell excelCell = getCell(index);
82+
this.row.removeCell(excelCell.getCell());
83+
}
84+
4785
/**
4886
* Returns the Sheet to which it belongs
4987
* @return A ExcelSheet

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelSheet.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

3+
import io.github.mbenincasa.javaexcelutils.exceptions.RowNotFoundException;
34
import lombok.AllArgsConstructor;
45
import lombok.EqualsAndHashCode;
56
import lombok.Getter;
7+
import lombok.SneakyThrows;
68
import org.apache.poi.ss.usermodel.Cell;
79
import org.apache.poi.ss.usermodel.Row;
810
import org.apache.poi.ss.usermodel.Sheet;
@@ -20,6 +22,17 @@
2022
@EqualsAndHashCode
2123
public class ExcelSheet {
2224

25+
/**
26+
* The Sheet index in the Workbook
27+
* @return The Sheet index
28+
*/
29+
@SneakyThrows
30+
public Integer getIndex() {
31+
if (this.sheet == null)
32+
return null;
33+
return getWorkbook().getWorkbook().getSheetIndex(this.name);
34+
}
35+
2336
/**
2437
* This object refers to the Apache POI Library {@code Sheet}
2538
*/
@@ -43,6 +56,17 @@ public ExcelWorkbook getWorkbook() {
4356
return new ExcelWorkbook(this.getSheet().getWorkbook());
4457
}
4558

59+
/**
60+
* Remove the selected Sheet
61+
* @since 0.4.1
62+
*/
63+
public void remove() {
64+
getWorkbook().removeSheet(getIndex());
65+
this.sheet = null;
66+
this.name = null;
67+
this.index = null;
68+
}
69+
4670
/**
4771
* The list of Rows related to the Sheet
4872
* @return A list of Rows
@@ -52,10 +76,36 @@ public List<ExcelRow> getRows() {
5276
for (Row row : this.sheet) {
5377
excelRows.add(new ExcelRow(row, row.getRowNum()));
5478
}
55-
79+
5680
return excelRows;
5781
}
5882

83+
/**
84+
* Retrieve a row by index
85+
* @param index The index of the row requested
86+
* @return A ExcelRow
87+
* @throws RowNotFoundException If the row is not present or has not been created
88+
* @since 0.4.1
89+
*/
90+
public ExcelRow getRow(Integer index) throws RowNotFoundException {
91+
Row row = this.sheet.getRow(index);
92+
if (row == null) {
93+
throw new RowNotFoundException("There is not a row in the index: " + index);
94+
}
95+
return new ExcelRow(row, index);
96+
}
97+
98+
/**
99+
* Removes a row by index
100+
* @param index The index of the row to remove
101+
* @throws RowNotFoundException If the row is not present or has not been created
102+
* @since 0.4.1
103+
*/
104+
public void removeRow(Integer index) throws RowNotFoundException {
105+
ExcelRow excelRow = getRow(index);
106+
this.sheet.removeRow(excelRow.getRow());
107+
}
108+
59109
/**
60110
* Create a new Row in the Sheet
61111
* @param index The index in the Sheet

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelWorkbook.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ public static ExcelWorkbook open(File file) throws ExtensionNotValidException, I
9696
* @param extension The file's extension
9797
* @return An ExcelWorkBook that is represented in the Excel file
9898
* @throws ExtensionNotValidException If the input file extension does not belong to an Excel file
99-
* @throws IOException If an I/O error has occurred
10099
* @throws OpenWorkbookException If an error occurred while opening the workbook
101100
*/
102-
public static ExcelWorkbook open(InputStream inputStream, String extension) throws ExtensionNotValidException, IOException, OpenWorkbookException {
101+
public static ExcelWorkbook open(InputStream inputStream, String extension) throws ExtensionNotValidException, OpenWorkbookException {
103102
/* Check the extension */
104103
if (!ExcelUtility.isValidExcelExtension(extension)) {
105104
throw new ExtensionNotValidException("Pass a file with the XLS or XLSX extension");
@@ -300,6 +299,15 @@ public ExcelSheet getSheetOrCreate(String sheetName) {
300299
}
301300
}
302301

302+
/**
303+
* Remove the Sheet
304+
* @param index The index of the Sheet in the workbook that will be removed
305+
* @since 0.4.1
306+
*/
307+
public void removeSheet(Integer index) {
308+
this.workbook.removeSheetAt(index);
309+
}
310+
303311
/**
304312
* Check if the sheet is present
305313
* @param sheetName The name of the sheet

src/test/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelCellTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

3-
import io.github.mbenincasa.javaexcelutils.exceptions.ExtensionNotValidException;
4-
import io.github.mbenincasa.javaexcelutils.exceptions.OpenWorkbookException;
5-
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
6-
import io.github.mbenincasa.javaexcelutils.exceptions.SheetNotFoundException;
3+
import io.github.mbenincasa.javaexcelutils.exceptions.*;
74
import org.junit.jupiter.api.Assertions;
85
import org.junit.jupiter.api.Test;
96

@@ -113,4 +110,15 @@ void readValueAsString() throws OpenWorkbookException, ExtensionNotValidExceptio
113110
Assertions.assertEquals("1/1/21 21:21", excelCell2.readValueAsString());
114111
Assertions.assertEquals("FALSE", excelCell3.readValueAsString());
115112
}
113+
114+
@Test
115+
void remove() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException, CellNotFoundException {
116+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
117+
ExcelSheet excelSheet = excelWorkbook.getSheet();
118+
ExcelRow excelRow = excelSheet.getRow(0);
119+
ExcelCell excelCell = excelRow.getCell(0);
120+
Assertions.assertDoesNotThrow(excelCell::remove);
121+
Assertions.assertNull(excelCell.getCell());
122+
Assertions.assertNull(excelCell.getIndex());
123+
}
116124
}

src/test/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelRowTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

3-
import io.github.mbenincasa.javaexcelutils.exceptions.ExtensionNotValidException;
4-
import io.github.mbenincasa.javaexcelutils.exceptions.OpenWorkbookException;
5-
import io.github.mbenincasa.javaexcelutils.exceptions.SheetNotFoundException;
3+
import io.github.mbenincasa.javaexcelutils.exceptions.*;
64
import org.junit.jupiter.api.Assertions;
75
import org.junit.jupiter.api.Test;
86

@@ -62,4 +60,33 @@ void countAllColumns() throws OpenWorkbookException, ExtensionNotValidException,
6260
Assertions.assertEquals(4, excelRows.get(0).countAllColumns(false));
6361
Assertions.assertEquals(3, excelRows.get(1).countAllColumns(true));
6462
}
63+
64+
@Test
65+
void remove() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException {
66+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
67+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
68+
ExcelRow excelRow = excelSheet.getRow(0);
69+
Assertions.assertDoesNotThrow(excelRow::remove);
70+
Assertions.assertNull(excelRow.getRow());
71+
Assertions.assertNull(excelRow.getIndex());
72+
}
73+
74+
@Test
75+
void getCell() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException, CellNotFoundException {
76+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
77+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
78+
ExcelRow excelRow = excelSheet.getRow(0);
79+
ExcelCell excelCell = excelRow.getCell(0);
80+
Assertions.assertEquals(0, excelCell.getIndex());
81+
Assertions.assertNotNull(excelCell.getCell());
82+
}
83+
84+
@Test
85+
void removeCell() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException, RowNotFoundException {
86+
ExcelWorkbook excelWorkbook = ExcelWorkbook.open(excelFile);
87+
ExcelSheet excelSheet = excelWorkbook.getSheet(0);
88+
ExcelRow excelRow = excelSheet.getRow(0);
89+
Assertions.assertDoesNotThrow(() -> excelRow.removeCell(0));
90+
Assertions.assertThrows(CellNotFoundException.class, () -> excelRow.getCell(0));
91+
}
6592
}

0 commit comments

Comments
 (0)