Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions lib/src/reader/excel_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,36 @@ class Parser extends _ParserBase
switch (_localName(event.name)) {
case 'row':
currentCol = -1;
// `r` is optional (ECMA-376 18.3.1.73): a row that omits it
// follows the previous row. It is resolved before the other
// attributes, which key off the row index in whatever order the
// file wrote them.
String? rowRef;
for (final attr in event.attributes) {
if (attr.localName == 'r') {
currentRow = (int.tryParse(attr.value) ?? 0) - 1;
} else if (attr.localName == 'ht') {
final height = double.tryParse(attr.value);
if (height != null && currentRow >= 0) {
sheetObject._rowHeights[currentRow] = height;
}
} else if (attr.localName == 'outlineLevel') {
final level = int.tryParse(attr.value);
if (level != null && level > 0 && currentRow >= 0) {
sheetObject._rowOutlineLevel[currentRow] = level;
}
} else if (attr.localName == 'hidden') {
if (attr.value == '1' && currentRow >= 0) {
sheetObject._rowHidden.add(currentRow);
}
} else if (attr.localName == 'collapsed') {
if (attr.value == '1' && currentRow >= 0) {
sheetObject._rowCollapsed.add(currentRow);
}
if (attr.localName == 'r') rowRef = attr.value;
}
currentRow = rowRef == null
? currentRow + 1
: (int.tryParse(rowRef) ?? 0) - 1;
if (currentRow < 0) break;
for (final attr in event.attributes) {
switch (attr.localName) {
case 'ht':
final height = double.tryParse(attr.value);
if (height != null) {
sheetObject._rowHeights[currentRow] = height;
}
case 'outlineLevel':
final level = int.tryParse(attr.value);
if (level != null && level > 0) {
sheetObject._rowOutlineLevel[currentRow] = level;
}
case 'hidden':
if (attr.value == '1') sheetObject._rowHidden.add(currentRow);
case 'collapsed':
if (attr.value == '1') {
sheetObject._rowCollapsed.add(currentRow);
}
}
}
case 'c':
Expand All @@ -341,7 +350,8 @@ class Parser extends _ParserBase
}
}
// Cells may legally omit the `r` coordinate; in that case the
// column is the next one after the previous cell in this row.
// column is the next one after the previous cell in this row. A
// cell that carries `r` is placed by it, whatever its row says.
if (cellRef != null) {
currentCol = _cellCoordsFromCellId(cellRef).$2;
} else {
Expand All @@ -354,10 +364,7 @@ class Parser extends _ParserBase
// styled cell matters (there is no value in a self-closing cell);
// this keeps a bordered-but-empty cell from being dropped along with
// its style.
if (event.isSelfClosing &&
cellStyle > 0 &&
cellRef != null &&
currentRow >= 0) {
if (event.isSelfClosing && cellStyle > 0 && cellRef != null) {
_processSaxCell(
sheetObject,
sheetName,
Expand Down Expand Up @@ -397,7 +404,7 @@ class Parser extends _ParserBase
} else if (event is XmlEndElementEvent) {
switch (_localName(event.name)) {
case 'c':
if (cellRef != null && currentRow >= 0) {
if (cellRef != null) {
_processSaxCell(
sheetObject,
sheetName,
Expand Down
44 changes: 44 additions & 0 deletions test/reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,50 @@ import 'package:test/test.dart';
import 'test_helper.dart';

void main() {
group('Rows Without Explicit Indices', () {
CellValue? at(Excel excel, String ref) =>
excel['Sheet1'].cell(CellIndex.indexByString(ref)).value;

test('reads cells in rows whose r attribute is omitted', () {
final excel = Excel.decodeBytes(
buildXlsx('<row><c r="A1"><v>42</v></c></row>'),
);
expect(at(excel, 'A1'), const IntCellValue(42));
});

test('places an unindexed row after the previous indexed row', () {
final excel = Excel.decodeBytes(
buildXlsx(
'<row r="3"><c r="A3"><v>1</v></c></row>'
'<row><c><v>2</v></c><c><v>3</v></c></row>',
),
);
expect(at(excel, 'A3'), const IntCellValue(1));
expect(at(excel, 'A4'), const IntCellValue(2));
expect(at(excel, 'B4'), const IntCellValue(3));
});

test('counts rows from the first one when none carries an index', () {
final excel = Excel.decodeBytes(
buildXlsx('<row><c><v>7</v></c></row><row><c><v>8</v></c></row>'),
);
expect(at(excel, 'A1'), const IntCellValue(7));
expect(at(excel, 'A2'), const IntCellValue(8));
});

test('applies row attributes written before the row index', () {
final sheet = Excel.decodeBytes(
buildXlsx(
'<row ht="30" customHeight="1" hidden="1" r="2">'
'<c r="A2"><v>1</v></c></row>',
),
)['Sheet1'];
expect(sheet.getRowHeight(1), 30);
expect(sheet.isRowHidden(1), isTrue);
expect(sheet.getRowHeights.containsKey(-1), isFalse);
});
});

group('Reading existing files', () {
test('reads cells and dimensions from example.xlsx', () {
var excel = Excel.decodeBytes(loadResource('example.xlsx'));
Expand Down
Loading