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
32 changes: 18 additions & 14 deletions lib/src/reader/sheet_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,12 @@ extension SheetStream on Excel {
if (file == null) return;
file.decompress();

final xmlStr = utf8.decode(file.content);
final start = xmlStr.indexOf('<sheetData');
if (start == -1) return;
final openEnd = xmlStr.indexOf('>', start);
if (openEnd == -1) return;
// <sheetData/> carries no rows at all.
if (xmlStr[openEnd - 1] == '/') return;
final end = xmlStr.indexOf('</sheetData>', openEnd);
if (end == -1) return;

yield* _streamSheetData(xmlStr.substring(openEnd + 1, end));
yield* _streamWorksheet(utf8.decode(file.content));
}

/// SAX-walks the inner content of `<sheetData>` and yields one list per row.
Iterable<List<CellValue?>> _streamSheetData(String inner) sync* {
/// Lazily walks XML events inside `sheetData`, without building a tree or
/// matching tag-like text in comments. Stops at the end of the cell data.
Iterable<List<CellValue?>> _streamWorksheet(String xml) sync* {
var row = <CellValue?>[];
var col = -1;
String? cellRef;
Expand All @@ -161,8 +152,21 @@ extension SheetStream on Excel {
final valueBuf = StringBuffer();
StringBuffer? formulaBuf;
var inRow = false;
var inSheetData = false;

for (final event in parseEvents('<sheetData>$inner</sheetData>')) {
for (final event in parseEvents(xml)) {
if (!inSheetData) {
if (event is XmlStartElementEvent &&
_localName(event.name) == 'sheetData') {
if (event.isSelfClosing) return;
inSheetData = true;
}
continue;
}
if (event is XmlEndElementEvent &&
_localName(event.name) == 'sheetData') {
return;
}
if (event is XmlStartElementEvent) {
switch (_localName(event.name)) {
case 'row':
Expand Down
56 changes: 56 additions & 0 deletions test/sheet_stream_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,62 @@ List<List<CellValue?>> _streamedRows(Excel excel, String sheet) =>
excel.streamRows(sheet).toList();

void main() {
group('Streaming Namespace Parity', () {
test('accepts whitespace in a prefixed closing tag', () {
final bytes = buildXlsx(
'',
extraParts: {
'xl/worksheets/sheet1.xml':
'<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
'<x:sheetData><x:row r="1"><x:c r="A1"><x:v>42</x:v></x:c>'
'</x:row></x:sheetData ></x:worksheet>',
},
);
expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), [
[const IntCellValue(42)],
]);
});

test('ignores a prefixed sheetData tag inside a comment', () {
final bytes = buildXlsx(
'<row r="1"><c r="A1"><v>42</v></c></row>',
beforeSheetData: '<!-- <x:sheetData/> -->',
);
expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), [
[const IntCellValue(42)],
]);
});

test('reads a worksheet with every element namespace prefixed', () {
const ns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
final bytes = buildXlsx(
'',
extraParts: {
'xl/worksheets/sheet1.xml':
'<x:worksheet xmlns:x="$ns"><x:sheetData><x:row r="1">'
'<x:c r="A1"><x:v>42</x:v></x:c>'
'</x:row></x:sheetData></x:worksheet>',
},
);
final excel = Excel.decodeBytes(bytes);
expect(excel.streamRows('Sheet1').toList(), [
[const IntCellValue(42)],
]);
});

test('yields no rows for a prefixed worksheet with empty sheetData', () {
const ns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
final bytes = buildXlsx(
'',
extraParts: {
'xl/worksheets/sheet1.xml':
'<x:worksheet xmlns:x="$ns"><x:sheetData/></x:worksheet>',
},
);
expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), isEmpty);
});
});

group('Streaming Row Read', () {
test('agrees with the eager reader on a generated workbook', () {
final excel = Excel.createExcel();
Expand Down
Loading