justkawal / excel

Excel Library for Flutter and Dart - https://pub.dev/packages/excel
MIT License
416 stars 229 forks source link

Parse column width and row height #347

Closed DavidPoetsch closed 5 months ago

DavidPoetsch commented 5 months ago

Changed the parse.dart to solve the issue where column widths and row heights are not parsed.

Fixes #277, #238

I solved it by adding a new function to the Parser class.

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;
            }
          }
        }
      });
    }
  }

and i called the function in the _parseTable function right after _parseHeaderFooter:

    _parseHeaderFooter(worksheet, sheetObject);
    _parseColWidthsRowHeights(worksheet, sheetObject);
FauconSpartiate commented 5 months ago

@DavidPoetsch For the tests, you can look at what's already in the test/ folder, and also the Flutter documentation.

Tests ensure everything works as expected, and does not get broken in future updates.

FauconSpartiate commented 5 months ago

@DavidPoetsch Make sure to run dart run . to not push formatting changes to other code.

Edit: The command is dart format ., not dart run .

DavidPoetsch commented 5 months ago

@DavidPoetsch Make sure to run dart run . to not push formatting changes to other code.

Sry i do not understand. How will it change the formatting? Where can i run this?

Furthermore, why are checks failing? When i run it on my machine it is fine. image

FauconSpartiate commented 5 months ago

Sorry the right command is dart format ., to be run in the terminal. My bad.

The online checks are sometimes broken, I reran them and now they're passing.