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
40 changes: 31 additions & 9 deletions lib/src/reader/excel_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<int> _storedKeysWithin(
Map<int, Object> 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')];
Expand Down
40 changes: 40 additions & 0 deletions test/reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
'<mergeCells count="1"><mergeCell ref="$ref"/></mergeCells>',
),
)['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(
'<row r="1"><c r="A1"><v>42</v></c></row>',
'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(
'<row r="1"><c r="A1"><v>1</v></c><c r="B1" s="1"><v>2</v></c>'
'<c r="C1"><v>3</v></c></row>'
'<row r="3"><c r="A3"><v>4</v></c><c r="D3"><v>5</v></c></row>',
'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));
});
});
}
Loading