jmcnamara / rust_xlsxwriter

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

feature request: `autofit_height()` #60

Closed Expurple closed 10 months ago

Expurple commented 10 months ago

Feature Request

autofit() is only concerned with cell width, but I have a use case with multiline text cells which I would like to display fully. Are you interested in having a method that adjusts height? I can take a shot at implementing it

jmcnamara commented 10 months ago

In general Excel auto-adjusts the height of the cell to take the wrapped length into account if you apply a text wrap format to the cell or column. For example:

use rust_xlsxwriter::{Format, Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    // Create a new Excel file object.
    let mut workbook = Workbook::new();
    let worksheet = workbook.add_worksheet();

    // Add a text wrap format.
    let text_wrap_format = Format::new().set_text_wrap();

    // This autofits and wraps
    worksheet.write_with_format(0, 1, "Some text\nwrapped\nat newlines", &text_wrap_format)?;

    // This autofits but doesn't wrap (due to autofit())
    worksheet.write_with_format(0, 3, "Some long text that isn't wrapped", &text_wrap_format)?;

    // This wraps without newlines due to a fixed column width below.
    worksheet.write_with_format(0, 5, "Some long text that is wrapped", &text_wrap_format)?;

    worksheet.autofit();

    worksheet.set_column_width(5, 10)?;

    // Save the file to disk.
    workbook.save("gh60.xlsx")?;

    Ok(())
}

Output:

screenshot

See if you can make one of these options work to suits your needs.

Are you interested in having a method that adjusts height?

This isn't a frequently requested feature (this is the first time) even in the Python version (which is used a lot) so this is probably a "won't fix/add" for me.

Expurple commented 10 months ago

Oh, thank you, I didn't notice the solution with set_text_wrap(). This solves the issue for me