Skip to content

Commit 51cc01e

Browse files
authored
Merge pull request #576 from lsergio/fix-blank-cells
Fixing row and column tracking when there are blank cells
2 parents 77fe4dc + dc80b07 commit 51cc01e

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/RowSpliterator.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515
*/
1616
package org.dhatim.fastexcel.reader;
1717

18-
import javax.xml.stream.XMLStreamException;
18+
import static org.dhatim.fastexcel.reader.DefaultXMLInputFactory.factory;
19+
1920
import java.io.InputStream;
2021
import java.math.BigDecimal;
21-
import java.util.*;
22+
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.NoSuchElementException;
27+
import java.util.Optional;
28+
import java.util.Spliterator;
2229
import java.util.function.Consumer;
2330
import java.util.function.Function;
2431

25-
import static org.dhatim.fastexcel.reader.DefaultXMLInputFactory.factory;
32+
import javax.xml.stream.XMLStreamException;
2633

2734
class RowSpliterator implements Spliterator<Row> {
2835

@@ -85,7 +92,7 @@ private Row next() throws XMLStreamException {
8592
}
8693

8794
int trackedColIndex = 0;
88-
int rowIndex = getRowIndexWithFallback(++trackedRowIndex);
95+
int rowIndex = getRowIndexWithFallback(trackedRowIndex);
8996
boolean isHidden = "1".equals(r.getAttribute("hidden"));
9097

9198
List<Cell> cells = new ArrayList<>(rowCapacity);
@@ -98,11 +105,14 @@ private Row next() throws XMLStreamException {
98105

99106
Cell cell = parseCell(trackedColIndex++);
100107
CellAddress addr = cell.getAddress();
108+
// we may have to adjust because we may have skipped blanks
109+
trackedColIndex = addr.getColumn() + 1;
101110
ensureSize(cells, addr.getColumn() + 1);
102111

103112
cells.set(addr.getColumn(), cell);
104113
physicalCellCount++;
105114
}
115+
trackedRowIndex++;
106116
rowCapacity = Math.max(rowCapacity, cells.size());
107117
return new Row(rowIndex, physicalCellCount, cells, isHidden);
108118
}
@@ -112,15 +122,15 @@ private int getRowIndexWithFallback(int fallbackRowIndex) {
112122
return rowIndexOrNull != null ? rowIndexOrNull : fallbackRowIndex;
113123
}
114124

115-
private CellAddress getCellAddressWithFallback(int trackedColIndex) {
125+
private CellAddress getCellAddressWithFallback(int trackedColIndex, int trackedRowIndex) {
116126
String cellRefOrNull = r.getAttribute("r");
117127
return cellRefOrNull != null ?
118128
new CellAddress(cellRefOrNull) :
119129
new CellAddress(trackedRowIndex, trackedColIndex);
120130
}
121131

122132
private Cell parseCell(int trackedColIndex) throws XMLStreamException {
123-
CellAddress addr = getCellAddressWithFallback(trackedColIndex);
133+
CellAddress addr = getCellAddressWithFallback(trackedColIndex, trackedRowIndex);
124134
String type = r.getOptionalAttribute("t").orElse("n");
125135
String styleString = r.getAttribute("s");
126136
String formatId = null;
@@ -192,7 +202,7 @@ private Cell parseOther(CellAddress addr, String type, String dataFormatId, Stri
192202
if (formula == null && value == null && definedType == CellType.NUMBER) {
193203
return new Cell(workbook, CellType.EMPTY, null, addr, null, rawValue);
194204
} else {
195-
CellType cellType = (formula != null) ? CellType.FORMULA : definedType;
205+
CellType cellType = formula != null ? CellType.FORMULA : definedType;
196206
return new Cell(workbook, cellType, value, addr, formula, rawValue, dataFormatId, dataFormatString);
197207
}
198208
}
@@ -209,7 +219,7 @@ private String parseSharedFormula(Integer si, CellAddress addr) {
209219
* @see <a href="https://github.com/qax-os/excelize/blob/master/cell.go">here</a>
210220
*/
211221
private String parseSharedFormula(Integer dCol, Integer dRow, String baseFormula) {
212-
String res = "";
222+
StringBuilder res = new StringBuilder();
213223
int start = 0;
214224
boolean stringLiteral = false;
215225
for (int end = 0; end < baseFormula.length(); end++) {
@@ -222,7 +232,7 @@ private String parseSharedFormula(Integer dCol, Integer dRow, String baseFormula
222232
}
223233
if (c >= 'A' && c <= 'Z' || c == '$') {
224234

225-
res += baseFormula.substring(start, end);
235+
res.append(baseFormula.substring(start, end));
226236
start = end;
227237
end++;
228238
boolean foundNum = false;
@@ -240,17 +250,17 @@ private String parseSharedFormula(Integer dCol, Integer dRow, String baseFormula
240250
}
241251
if (foundNum) {
242252
String cellID = baseFormula.substring(start, end);
243-
res += shiftCell(cellID, dCol, dRow);
253+
res.append(shiftCell(cellID, dCol, dRow));
244254
start = end;
245255
}
246256
}
247257
}
248258

249259
if (start < baseFormula.length()) {
250-
res += baseFormula.substring(start);
260+
res.append(baseFormula.substring(start));
251261
}
252262

253-
return res;
263+
return res.toString();
254264
}
255265

256266
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.dhatim.fastexcel.reader;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.InputStream;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class RowSpliteratorTest {
10+
11+
@Test
12+
void testBlankCells() throws Exception {
13+
InputStream is = Resources.open("/xml/blank_cells.xml");
14+
RowSpliterator it = new RowSpliterator(null, is);
15+
it.tryAdvance(row -> {
16+
assertEquals(8, row.getCellCount());
17+
Cell cell = row.getCell(7);
18+
assertEquals("H1", cell.getAddress().toString());
19+
});
20+
it.tryAdvance(row -> {
21+
assertEquals(8, row.getCellCount());
22+
Cell cell = row.getCell(7);
23+
assertEquals("H2", cell.getAddress().toString());
24+
});
25+
}
26+
27+
}
28+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<sheetData>
2+
<row>
3+
<c t="b">
4+
<v>1</v>
5+
</c>
6+
<c r="C1" t="b">
7+
<v>1</v>
8+
</c>
9+
<c t="b">
10+
<v>1</v>
11+
</c>
12+
<c t="b">
13+
<v>1</v>
14+
</c>
15+
<c t="b">
16+
<v>1</v>
17+
</c>
18+
<c t="b">
19+
<v>1</v>
20+
</c>
21+
<c t="b">
22+
<v>1</v>
23+
</c>
24+
</row>
25+
<row>
26+
<c t="b">
27+
<v>1</v>
28+
</c>
29+
<c r="C2" t="b">
30+
<v>1</v>
31+
</c>
32+
<c t="b">
33+
<v>1</v>
34+
</c>
35+
<c t="b">
36+
<v>1</v>
37+
</c>
38+
<c t="b">
39+
<v>1</v>
40+
</c>
41+
<c t="b">
42+
<v>1</v>
43+
</c>
44+
<c t="b">
45+
<v>1</v>
46+
</c>
47+
</row>
48+
</sheetData>

0 commit comments

Comments
 (0)