jmcnamara / rust_xlsxwriter

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

Bug: Incorrect adjustment of column widths in autofit method with autofilter headers #92

Closed RMOREANOV closed 2 months ago

RMOREANOV commented 2 months ago

Current behavior

The autofit method in the Worksheet struct incorrectly identifies autofilter header cells, causing improper adjustment of column widths. When autofilter is applied and date headers are added, the method does not consider the additional width occupied by these headers.

image

Expected behavior

The autofit method should accurately identify autofilter header cells, including the additional width occupied by date headers, and adjust the column width accordingly.

image

Sample code to reproduce

use rust_xlsxwriter::{Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    let mut workbook = Workbook::new();
    let worksheet = workbook.add_worksheet();

    // Set autofilter
    worksheet.autofilter(0, 0, 3, 3).unwrap();

    // Add data
    worksheet.write_string(0, 0, "Header 1")?;
    worksheet.write_string(0, 1, "Header 2")?;
    worksheet.write_string(0, 2, "Header 3")?;
    worksheet.write_string(0, 3, "Header 4")?;

    // Add content
    worksheet.write_string(1, 0, "Data 1")?;
    worksheet.write_string(1, 1, "Data 2")?;
    worksheet.write_string(1, 2, "Data 3")?;
    worksheet.write_string(1, 3, "Data 4")?;

    worksheet.write_string(2, 0, "Data 5")?;
    worksheet.write_string(2, 1, "STRATEGIC MANAGEMENT WITH A MENTION IN INNOVATION AND MANAGEMENT IN HIGHER EDUCATION")?;
    worksheet.write_string(2, 2, "Data 7")?;
    worksheet.write_string(2, 3, "Data 8")?;

    worksheet.write_string(3, 0, "Data 9")?;
    worksheet.write_string(3, 1, "Data 10")?;
    worksheet.write_string(3, 2, "Data 11")?;
    worksheet.write_string(3, 3, "Data 12")?;

    // Autofit
    worksheet.autofit();

    // Save file
    workbook.save("test.xlsx")?;
    Ok(())
}

Environment

- `rust_xlsxwriter` version: 0.64.2
- Cargo.toml dependency line for `rust_xlsxwriter`: rust_xlsxwriter = "0.64.2"
- rustc version: rustc 1.77.2 (25ef9e3d8 2024-04-09)
- Excel version: -
- OS: Windows 10
- If using wasm, which method/tool: -

Any other information

In the autofit function (pub fn autofit(&mut self) -> &mut Worksheet {...}) of the Worksheet struct (worksheet.rs), the condition for identifying autofilter header cells is incorrect. It currently uses the row_num variable instead of the first_row property of the Worksheet dimensions. This causes the method to improperly adjust column widths.


// Incorrect condition
if pixel_width > 0 && self.cells_with_autofilter.contains(&(row_num, col_num)) {
    pixel_width += 16;
}

// Corrected condition
if pixel_width > 0 && self.cells_with_autofilter.contains(&(self.dimensions.first_row, col_num)) {
    pixel_width += 16;
}```
jmcnamara commented 2 months ago

The autofit method in the Worksheet struct incorrectly identifies autofilter header cells, causing improper adjustment of column widths.

I don't think that is the case. In the first example the column is autofitted correctly to the text width.

Also, the additional 16 pixel allowance for header cells is to prevent the dropdown arrow from obscuring the fitted text, like this:

CleanShot 2024-05-07 at 20 04 31

I'm guessing that the "expected behaviour" example is an example of Excel's autofit. One thing to note about that is that the autofit width is the same regardless of whether the autofilter is on or off. The difference between the rust_xlsxwriter and Excel output is explained in Notes on the XlsxWriter implementation of autofit(). Particularly in the section on "Fidelity with Excel".

The bug report mentions "dates" a few times but the example doesn't show any. In order to save time I'll point out that dates aren't currently handled properly since they have a separate number format that dictates the width of the cell. From the limitations section in the docs on autofit() it says:

It doesn’t take number or date formatting into account, although it may try to in a later version.

That is still the case.

jmcnamara commented 2 months ago

Closing as won't/can't fix.