Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse column width and row height #347

Merged
merged 4 commits into from
Apr 30, 2024

Conversation

DavidPoetsch
Copy link
Contributor

@DavidPoetsch DavidPoetsch commented Apr 21, 2024

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
Copy link
Contributor

@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
Copy link
Contributor

FauconSpartiate commented Apr 24, 2024

@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
Copy link
Contributor Author

@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
Copy link
Contributor

FauconSpartiate commented Apr 25, 2024

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.

Copy link
Contributor

@FauconSpartiate FauconSpartiate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice work

@FauconSpartiate FauconSpartiate merged commit 6168585 into justkawal:main Apr 30, 2024
1 check passed
@DavidPoetsch DavidPoetsch deleted the parseColWidthRowHeight branch May 6, 2024 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong column width when Excel.save()
2 participants