Skip to content

Commit

Permalink
Parse column width and row height (#347)
Browse files Browse the repository at this point in the history
Co-authored-by: David Pötsch <[email protected]>
  • Loading branch information
DavidPoetsch and David Pötsch authored Apr 30, 2024
1 parent ea06d23 commit 6168585
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
80 changes: 80 additions & 0 deletions lib/src/parser/parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ class Parser {
});

_parseHeaderFooter(worksheet, sheetObject);
_parseColWidthsRowHeights(worksheet, sheetObject);

_excel._sheets[name] = sheet;

Expand Down Expand Up @@ -811,4 +812,83 @@ class Parser {

sheetObject.headerFooter = HeaderFooter.fromXmlElement(headerFooterElement);
}

void _parseColWidthsRowHeights(XmlElement worksheet, Sheet sheetObject) {
/* parse default column width and default row height
example XML content
<sheetFormatPr baseColWidth="10" defaultColWidth="26.33203125" defaultRowHeight="13" x14ac:dyDescent="0.15" />
*/
Iterable<XmlElement> results;
results = worksheet.findAllElements("sheetFormatPr");
if (results.isNotEmpty) {
results.forEach((element) {
double? defaultColWidth;
double? defaultRowHeight;
// default column width
String? widthAttribute = element.getAttribute("defaultColWidth");
if (widthAttribute != null) {
defaultColWidth = double.tryParse(widthAttribute);
}
// default row height
String? rowHeightAttribute = element.getAttribute("defaultRowHeight");
if (rowHeightAttribute != null) {
defaultRowHeight = double.tryParse(rowHeightAttribute);
}

// both values valid ?
if (defaultColWidth != null && defaultRowHeight != null) {
sheetObject._defaultColumnWidth = defaultColWidth;
sheetObject._defaultRowHeight = defaultRowHeight;
}
});
}

/* parse custom column height
example XML content
<col min="2" max="2" width="71.83203125" customWidth="1"/>,
<col min="4" max="4" width="26.5" customWidth="1"/>,
<col min="6" max="6" width="31.33203125" customWidth="1"/>
*/
results = worksheet.findAllElements("col");
if (results.isNotEmpty) {
results.forEach((element) {
String? colAttribute =
element.getAttribute("min"); // i think min refers to the column
String? widthAttribute = element.getAttribute("width");
if (colAttribute != null && widthAttribute != null) {
int? col = int.tryParse(colAttribute);
double? width = double.tryParse(widthAttribute);
if (col != null && width != null) {
col -= 1; // first col in _columnWidths is index 0
if (col >= 0) {
sheetObject._columnWidths[col] = width;
}
}
}
});
}

/* parse custom row height
example XML content
<row r="1" spans="1:2" ht="44" customHeight="1" x14ac:dyDescent="0.15">
*/
results = worksheet.findAllElements("row");
if (results.isNotEmpty) {
results.forEach((element) {
String? rowAttribute =
element.getAttribute("r"); // i think min refers to the column
String? heightAttribute = element.getAttribute("ht");
if (rowAttribute != null && heightAttribute != null) {
int? row = int.tryParse(rowAttribute);
double? height = double.tryParse(heightAttribute);
if (row != null && height != null) {
row -= 1; // first col in _rowHeights is index 0
if (row >= 0) {
sheetObject._rowHeights[row] = height;
}
}
}
});
}
}
}
26 changes: 25 additions & 1 deletion test/excel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ void main() {
new Directory('./tmp').delete(recursive: true);
expect(newExcel.sheets.entries.length, equals(1));
expect(newExcel.tables['Sheet1']!.maxColumns, equals(5));
expect(newExcel.tables['Sheet1']!.rows[0][0]!.value, equals(IntCellValue(8)));
expect(
newExcel.tables['Sheet1']!.rows[0][0]!.value, equals(IntCellValue(8)));
expect(
newExcel.tables['Sheet1']!.rows[0][0]!.cellStyle?.numberFormat
.toString(),
Expand Down Expand Up @@ -1028,4 +1029,27 @@ void main() {
testSpannedItemsSheetValues(newSheet);
});
});

test('Parse column width row height', () {
var file = './test/test_resources/columnWidthRowHeight.xlsx';
var bytes = File(file).readAsBytesSync();
var excel = Excel.decodeBytes(bytes);
Sheet? sheetObject = excel.tables['Sheet1']!;

// should 20 with a litle bit of tolerance.
expect(sheetObject.defaultColumnWidth, greaterThan(18));
expect(sheetObject.defaultColumnWidth, lessThan(22));

// should 20 with a litle bit of tolerance.
expect(sheetObject.defaultRowHeight, greaterThan(18));
expect(sheetObject.defaultRowHeight, lessThan(22));

// should 40 with a litle bit of tolerance.
expect(sheetObject.getColumnWidth(1), greaterThan(38));
expect(sheetObject.getColumnWidth(1), lessThan(42));

// should 40 with a litle bit of tolerance.
expect(sheetObject.getRowHeight(1), greaterThan(38));
expect(sheetObject.getRowHeight(1), lessThan(42));
});
}
Binary file added test/test_resources/columnWidthRowHeight.xlsx
Binary file not shown.

0 comments on commit 6168585

Please sign in to comment.