diff --git a/lib/src/reader/excel_parser.dart b/lib/src/reader/excel_parser.dart index fbed58b..359c149 100644 --- a/lib/src/reader/excel_parser.dart +++ b/lib/src/reader/excel_parser.dart @@ -166,19 +166,21 @@ class Parser extends _ParserBase final rowSpanStart = spanObj.rowSpanStart; final rowSpanEnd = spanObj.rowSpanEnd; - for (var columnI = columnSpanStart; columnI <= columnSpanEnd; columnI++) { - for (var rowI = rowSpanStart; rowI <= rowSpanEnd; rowI++) { - bool isTopLeftCellThatShouldNotBeDeleted = - columnI == columnSpanStart && rowI == rowSpanStart; - - if (isTopLeftCellThatShouldNotBeDeleted) { - continue; - } + // The range comes from the file and may cover the whole grid while the + // sheet stores a few cells, so the walk visits the stored cells inside + // it rather than every position it advertises. + final rows = _storedKeysWithin(sheet._sheetData, rowSpanStart, rowSpanEnd); + for (final rowI in rows) { + final row = sheet._sheetData[rowI]; + if (row == null) continue; + final columns = _storedKeysWithin(row, columnSpanStart, columnSpanEnd); + for (final columnI in columns) { + if (columnI == columnSpanStart && rowI == rowSpanStart) continue; // Excel draws a merged region's border from the covered cells' own // borders, so keep a covered cell's style and clear only its value // rather than removing the cell (which would lose the border). Cells // with no style are removed as before. - final covered = sheet._sheetData[rowI]?[columnI]; + final covered = row[columnI]; if (covered?._cellStyle != null) { covered!._value = null; } else { @@ -188,6 +190,26 @@ class Parser extends _ParserBase } } + /// The keys of [map] within [first]..[last], as a list so the caller can + /// change [map] while walking them. Costs the smaller of the range's width + /// and the map's size. + static List _storedKeysWithin( + Map map, + int first, + int last, + ) { + if (last - first < map.length) { + return [ + for (var key = first; key <= last; key++) + if (map.containsKey(key)) key, + ]; + } + return [ + for (final key in map.keys) + if (key >= first && key <= last) key, + ]; + } + void _parseTable(XmlElement node) { var name = node.getAttribute('name')!; var target = _worksheetTargets[node.getAttribute('r:id')]; diff --git a/test/reader_test.dart b/test/reader_test.dart index 371993d..80525f6 100644 --- a/test/reader_test.dart +++ b/test/reader_test.dart @@ -211,4 +211,44 @@ void main() { expect(thin?.topBorder.borderStyle, BorderStyle.Thin); }); }); + + group('Merged Range Parsing', () { + Sheet read(String inner, String ref) => Excel.decodeBytes( + buildXlsx( + inner, + afterSheetData: + '', + ), + )['Sheet1']; + + Data cell(Sheet sheet, String ref) => + sheet.cell(CellIndex.indexByString(ref)); + + test('a merge over the whole grid with one stored cell reads promptly', () { + // Clearing the covered cells must cost the cells the sheet holds, not + // the 17 billion positions the range advertises. + final sheet = read( + '42', + 'A1:XFD1048576', + ); + expect(cell(sheet, 'A1').value, const IntCellValue(42)); + expect(sheet.spannedItems, ['A1:XFD1048576']); + expect(sheet.maxRows, 1); + }); + + test('covered cells lose their value and keep their style', () { + final sheet = read( + '12' + '3' + '45', + 'A1:C3', + ); + expect(cell(sheet, 'A1').value, const IntCellValue(1)); + expect(cell(sheet, 'B1').value, isNull); + expect(cell(sheet, 'B1').cellStyle?.underline, Underline.Single); + expect(cell(sheet, 'C1').value, isNull); + expect(cell(sheet, 'A3').value, isNull); + expect(cell(sheet, 'D3').value, const IntCellValue(5)); + }); + }); }