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: Line breaks "\n" behaves strangely #88

Closed Antonio225t closed 3 months ago

Antonio225t commented 3 months ago

Current behavior

I used worksheet.write_string_with_format(2, 2, "Hello\nWorld", &format)?; but the line break doesn't show and when I edit the cell in LibreOffice Calc to add the line break manually, it does add the line break but for some reason the last letter gets ereased, it shows like this:

HelloWorld

And after I edit manually:

Hello
Worl

(I'm sorry but I cannot test on Excel because I don't have it installed.)

Expected behavior

I expect that the xlsx cell that I want to add a line break looks like this:

Hello
World

Sample code to reproduce

use rust_xlsxwriter::{Workbook, XlsxError};

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

    worksheet.write_string(0, 0, "Hello\nWorld")?;

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

### Environment

```text
- `rust_xlsxwriter` version: **0.64.1**
- Cargo.toml dependency line for `rust_xlsxwriter`: **rust_xlsxwriter = "0.64.1"**
- rustc version: **1.75.0**
- LibreOffice version: **2.4.2.1.2 (X86_64)**
- OS: **Windows 10**
- If using wasm, which method/tool: **I didn't**

Any other information

No response

Antonio225t commented 3 months ago

I came to fix it. When writing a line break (at least for LibreOffice Calc works like this, no idea if it's the same for Excel) add Format::new().set_text_wrap(); or add .set_text_wrap() to your format. Hope this helps.

jmcnamara commented 3 months ago

I came to fix it.

Yes. That is the correct way (as required by Excel). See the docs for Format::new().set_text_wrap():

https://docs.rs/rust_xlsxwriter/latest/rust_xlsxwriter/struct.Format.html#method.set_text_wrap

Antonio225t commented 3 months ago

See the docs for Format::new().set_text_wrap()

Ahh there. Thanks for the information 😄