11package 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 ;
36import lombok .AllArgsConstructor ;
47import lombok .EqualsAndHashCode ;
58import 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