Skip to content

Commit 9f9f1ec

Browse files
authored
Merge pull request #12 from MBenincasa/develop
Develop
2 parents 2b03717 + 6caa4f3 commit 9f9f1ec

File tree

14 files changed

+481
-29
lines changed

14 files changed

+481
-29
lines changed

CHANGELOG.MD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# v0.4.1
2+
### Features
3+
* There are getOrCreate methods for a Row or a Cell
4+
* There are methods to remove a Sheet, a Row or a Cell
5+
* There are methods to retrieve a Row or a Cell based on an index
6+
* There are methods for writing and reading a list of values from a Row
7+
### Fixes
8+
* ExcelSheet's getIndex() method did not return the correct index after deleting a Sheet
9+
### Changes
10+
* The static method that opens an ExcelWorkbook via InputStream no longer takes the extension as input and is no longer checked if it is correct
11+
112
# v0.4.0
213
### Deprecations
314
* All methods of the Converter class up to version 0.2.1 have been deprecated

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Java 17 or above.
7878
## Dependencies
7979
- org.apache.poi:poi:jar:5.2.3
8080
- org.apache.poi:poi-ooxml:jar:5.2.3
81-
- org.projectlombok:lombok:jar:1.18.24
81+
- org.projectlombok:lombok:jar:1.18.26
8282
- commons-beanutils:commons-beanutils:jar:1.9.4
8383
- com.opencsv:opencsv:jar:5.7.1
8484
- com.fasterxml.jackson.core:jackson-databind:jar:2.14.2
@@ -91,7 +91,7 @@ Java 17 or above.
9191
<dependency>
9292
<groupId>io.github.mbenincasa</groupId>
9393
<artifactId>java-excel-utils</artifactId>
94-
<version>0.4.0</version>
94+
<version>0.4.1</version>
9595
</dependency>
9696
```
9797

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.mbenincasa</groupId>
88
<artifactId>java-excel-utils</artifactId>
9-
<version>0.4.0</version>
9+
<version>0.4.1</version>
1010
<packaging>jar</packaging>
1111
<name>Java library with tools for Excel files</name>
1212
<description>Java library that collects tools and methods to speed up development with Excel sheets</description>
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: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.github.mbenincasa.javaexcelutils.model.excel;
22

3+
import io.github.mbenincasa.javaexcelutils.exceptions.CellNotFoundException;
4+
import io.github.mbenincasa.javaexcelutils.exceptions.ReadValueException;
5+
import io.github.mbenincasa.javaexcelutils.exceptions.RowNotFoundException;
36
import lombok.AllArgsConstructor;
47
import lombok.EqualsAndHashCode;
58
import lombok.Getter;
@@ -31,6 +34,17 @@ public class ExcelRow {
3134
*/
3235
private Integer index;
3336

37+
/**
38+
* Remove the selected Row
39+
* @throws RowNotFoundException If the row is not present or has not been created
40+
* @since 0.4.1
41+
*/
42+
public void remove() throws RowNotFoundException {
43+
getSheet().removeRow(this.index);
44+
this.row = null;
45+
this.index = null;
46+
}
47+
3448
/**
3549
* The list of Cells related to the Row
3650
* @return A list of Cells
@@ -40,10 +54,109 @@ public List<ExcelCell> getCells() {
4054
for (Cell cell : this.row) {
4155
excelCells.add(new ExcelCell(cell, cell.getColumnIndex()));
4256
}
43-
4457
return excelCells;
4558
}
4659

60+
/**
61+
* Retrieve a cell by index
62+
* @param index The index of the cell requested
63+
* @return A ExcelCell
64+
* @throws CellNotFoundException If the cell is not present or has not been created
65+
* @since 0.4.1
66+
*/
67+
public ExcelCell getCell(Integer index) throws CellNotFoundException {
68+
Cell cell = this.row.getCell(index);
69+
if (cell == null) {
70+
throw new CellNotFoundException("There is not a cell in the index: " + index);
71+
}
72+
return new ExcelCell(cell, cell.getColumnIndex());
73+
}
74+
75+
/**
76+
* Retrieve or create a cell by index
77+
* @param index The index of the cell requested
78+
* @return A ExcelCell
79+
* @since 0.4.1
80+
*/
81+
public ExcelCell getOrCreateCell(Integer index) {
82+
Cell cell = this.row.getCell(index);
83+
if (cell == null) {
84+
return createCell(index);
85+
}
86+
return new ExcelCell(cell, cell.getColumnIndex());
87+
}
88+
89+
/**
90+
* Removes a cell by index
91+
* @param index The index of the row to remove
92+
* @throws CellNotFoundException If the cell is not present or has not been created
93+
* @since 0.4.1
94+
*/
95+
public void removeCell(Integer index) throws CellNotFoundException {
96+
ExcelCell excelCell = getCell(index);
97+
this.row.removeCell(excelCell.getCell());
98+
}
99+
100+
/**
101+
* Write the values in the cells of the row
102+
* @param values The values to write in the cells of the row
103+
* @since 0.4.1
104+
*/
105+
public void writeValues(List<?> values) {
106+
for (int i = 0; i < values.size(); i++) {
107+
ExcelCell excelCell = getOrCreateCell(i);
108+
excelCell.writeValue(values.get(i));
109+
}
110+
}
111+
112+
/**
113+
* Reads the values of all cells in the row
114+
* @return The list of values written in the cells
115+
* @throws ReadValueException If an error occurs while reading
116+
* @since 0.4.1
117+
*/
118+
public List<?> readValues() throws ReadValueException {
119+
List<Object> values = new LinkedList<>();
120+
for (ExcelCell excelCell : getCells()) {
121+
values.add(excelCell.readValue());
122+
}
123+
return values;
124+
}
125+
126+
/**
127+
* Reads the values of all cells in the row
128+
* @param classes A list of Classes that is used to cast the results read
129+
* @return The list of values written in the cells
130+
* @throws ReadValueException If an error occurs while reading
131+
* @since 0.4.1
132+
*/
133+
public List<?> readValues(List<Class<?>> classes) throws ReadValueException {
134+
List<ExcelCell> excelCells = getCells();
135+
if(excelCells.size() != classes.size()) {
136+
throw new IllegalArgumentException("There are " + excelCells.size() + " items in the row and classlist has " + classes.size() + " values.");
137+
}
138+
139+
List<Object> values = new LinkedList<>();
140+
for (int i = 0; i < excelCells.size(); i++) {
141+
values.add(excelCells.get(i).readValue(classes.get(i)));
142+
}
143+
144+
return values;
145+
}
146+
147+
/**
148+
* Reads the values of all cells in the row as a String
149+
* @return The list of values, such as String, written in the cells
150+
* @since 0.4.1
151+
*/
152+
public List<String> readValuesAsString() {
153+
List<String> values = new LinkedList<>();
154+
for (ExcelCell excelCell : getCells()) {
155+
values.add(excelCell.readValueAsString());
156+
}
157+
return values;
158+
}
159+
47160
/**
48161
* Returns the Sheet to which it belongs
49162
* @return A ExcelSheet

0 commit comments

Comments
 (0)