diff --git a/lib/src/parser/parse.dart b/lib/src/parser/parse.dart index 3c0f7295..0fefe5ba 100644 --- a/lib/src/parser/parse.dart +++ b/lib/src/parser/parse.dart @@ -561,6 +561,7 @@ class Parser { }); _parseHeaderFooter(worksheet, sheetObject); + _parseColWidthsRowHeights(worksheet, sheetObject); _excel._sheets[name] = sheet; @@ -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 + + */ + Iterable 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 + , + , + + */ + 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 + + */ + 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; + } + } + } + }); + } + } } diff --git a/test/excel_test.dart b/test/excel_test.dart index 493e00bf..f94c4a63 100644 --- a/test/excel_test.dart +++ b/test/excel_test.dart @@ -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(), @@ -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)); + }); } diff --git a/test/test_resources/columnWidthRowHeight.xlsx b/test/test_resources/columnWidthRowHeight.xlsx new file mode 100644 index 00000000..da7c2b82 Binary files /dev/null and b/test/test_resources/columnWidthRowHeight.xlsx differ