jmcnamara / rust_xlsxwriter

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

Bug: header_format and table_default are incompatible #90

Closed claudiofsr closed 7 months ago

claudiofsr commented 7 months ago

Current behavior

I use rust_xlsxwriter in find-identical-files.

If

I comment out line 98 of the excel.rs file [.set_row_format(0, fmt_header)?]

and

I uncomment line 17 of the path_info.rs file [header_format = get_xlsx_format("header")],

the header format is changed.

To verify:

cd /tmp
git clone https://github.com/claudiofsr/find-identical-files.git
cd find-identical-files
cargo run -- -tx .

See the XLSX file "fif.xlsx" with and without the changes.

Expected behavior

#[derive(XlsxSerialize, Serialize)]
#[xlsx(
    header_format = get_xlsx_format("header"),
    table_default
)]
pub struct PathInfo {
...
}

I hope

the header_format and table_default options are compatible and .set_row_format(0, fmt_header)? is not necessary.

jmcnamara commented 7 months ago

I ran the example with and without the changes and got the following:

Without changes.

screenshot

With changes:

screenshot

I see that one version has some alignment properties and the other doesn't.

That is probably correct in terms of what is specified in the code. The row format and the table header format are separate. In rust_xlsxwriter and Excel. The rust_xlsxwriter library doesn't attempt to apply the row format to the table header because of this separation. (It does however apply the format to cells outside the table.)

In this particular case I think you should be setting the table header and not the row format but it is hard to tell from the description alone.

Let me know what you think.

claudiofsr commented 7 months ago

.set_text_wrap() doesn't work either.

You could implement the set_header_format(some_format) method in Worksheet and differentiate .set_row_format(row_num, some_format) from .set_header_format(some_format) [use excluded for header].

set_header_format would be fully compatible with

[xlsx(

header_format = some_format, table_default )].

I don't know if this makes sense to you.

jmcnamara commented 7 months ago

I don't know if this makes sense to you.

Not really. I'm still not sure what the issue is. Do you think you could break this down into a small runnable example that demonstrates the issue.

claudiofsr commented 7 months ago

Instead of just one PathInfo structure, there would be PathInfo1 and PathInfo2, and PathInfo1 would have the format FFF1 and PathInfo2 would have the format FFF2.

Using

    worksheet
        .set_name(sheet_name)?
        .set_row_height(0, 32)?
        .set_row_format(0, fmt_header)? // <---
        .set_freeze_panes(1, 0)?;

would not be the solution!

jmcnamara commented 7 months ago

Sorry. I still don’t understand what the issue is. Try explain with a reproducible example.

claudiofsr commented 7 months ago

I wanted to create an "excel.rs" file that could be used generically for several different possible structures (PathInfo01, PathInfo02, ... PathInfo0N), with possible different formats.

However, I see that you understand otherwise.

You can close this topic.

Thank you for your attention.