jmcnamara / rust_xlsxwriter

A Rust library for creating Excel XLSX files.
https://crates.io/crates/rust_xlsxwriter
Apache License 2.0
335 stars 26 forks source link

`worksheet.autofit()` is not aware of merged cells #34

Closed LeoniePhiline closed 1 year ago

LeoniePhiline commented 1 year ago

Current behavior

Situation:

Merging multiple cells with merge_range to form a "title" area, setting string content and a format (e.g. a large font size).

Adding data columns, then calling autofit on the worksheet.

Result:

The string content of the multi-cell title is used to autofit the first column, although the string content spans multiple columns (merged range).

The increased font size is not taken into account.

The width of the first column is calculated

image

Note how the first column could be as narrow as "Manager" & "cargo" but is as wide as the title would be if it was written into the first cell in default font size.

Expected behavior

The expected behavior would be closer to the un-merged situation:

image

The first column is only calculated by the actual contents.

Note that I am using "actual" here not in a technical sense (since technically the content of a merged range is in fact written to the first cell) but in the sense of what a viewer perceives as the "visual reality".

Sample code to reproduce

// Write full project name with namespace into first row, merged across 4 columns. (big & bold)
        worksheet.set_row_height(0, 25)?;
        worksheet.merge_range(
            0,
            0,
            0,
            3,
            &self.project.name_with_namespace,
            &Format::new().set_bold().set_font_size(20),
        )?;

Environment

- rust_xlsxwriter version: 0.36.1
- rustc version: 1.69.0 (84c898d65 2023-04-16)
- Excel version: LibreOffice 7.5.2.2 (X86_64)
- OS: OpenSUSE Tumbleweed Linux

Any other information

No response

LeoniePhiline commented 1 year ago

Observation:

autofit is generally unaware of font size. This means that the merged-cells awareness and the font size awareness are two distinct issues.

image

jmcnamara commented 1 year ago

The documentation tries to make clear that autofit() isn't part of the file format and as such the autofit in rust_xlsxwriter is just a simulation/approximation. It works for simpler cases but not in all cases.

Specifically font and number formatting aren't taken into account and it may not be possible (or very easy) to handle that in the future.

Merged cells should possibly be handled but aren't. There are some other limitations listed here: https://github.com/jmcnamara/XlsxWriter/issues/936

For now you will just need to work around these by manually setting the column widths.

Closing as won't/can't fix for now.

LeoniePhiline commented 1 year ago

Hi @jmcnamara , thanks for your response!

I'm absolutely fine with working around this, and did not expect a quick fix. (I am not "expecting" anything at all from open source developers and I thank you very much for making this awesome crate available!)

I was wondering if maybe we should leave this issue open as reference / "TODO" item?

Font size might be handled by multiplying the estimated width by the font scaling factor compared to the base font size.

Merged cells should also be possible, albeit complex to implement.

What do you think - should this better be kept open to be eventually PRed by someone (e.g. possibly me)?

jmcnamara commented 1 year ago

What do you think - should this better be kept open to be eventually PRed by someone (e.g. possibly me)?

I prefer not to leave too many TODO issues open. I have it on my own TODO list to make it more robust and to add some enhancements. And it is probably not the type of feature I'd take a PR for. But thanks for the offer.