From d5265b67f63bf8838c067ff78b6b5e871eddf8e2 Mon Sep 17 00:00:00 2001 From: Christoph Deil Date: Fri, 16 Oct 2026 13:40:35 +0200 Subject: [PATCH] fix: read the cells of a row that omits its index A without r is valid (the attribute is optional in ECMA-376 18.3.1.73) and means the row after the previous one, yet the SAX reader left the row index at -1 and dropped every cell in it, even one placed by its own r="A1". An unindexed row after an indexed one reused the previous row's index, so positional cells overwrote it. The index is now inferred when absent, a cell with its own reference is always placed, and the row's ht/hidden/outline attributes are applied after the index is known rather than in attribute order. Co-Authored-By: Claude Fable 5.1 --- lib/src/reader/excel_parser.dart | 59 ++++++++++++++++++-------------- test/reader_test.dart | 44 ++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 26 deletions(-) diff --git a/lib/src/reader/excel_parser.dart b/lib/src/reader/excel_parser.dart index fbed58b..22e0c78 100644 --- a/lib/src/reader/excel_parser.dart +++ b/lib/src/reader/excel_parser.dart @@ -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': @@ -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 { @@ -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, @@ -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, diff --git a/test/reader_test.dart b/test/reader_test.dart index 371993d..1a5c3ff 100644 --- a/test/reader_test.dart +++ b/test/reader_test.dart @@ -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('42'), + ); + expect(at(excel, 'A1'), const IntCellValue(42)); + }); + + test('places an unindexed row after the previous indexed row', () { + final excel = Excel.decodeBytes( + buildXlsx( + '1' + '23', + ), + ); + 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('78'), + ); + 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( + '', + ), + )['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'));