jmcnamara/rust_xlsxwriter

Bug: Line breaks "\n" behaves strangely

Closed this issue · 3 comments

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

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.

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

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

Ahh there. Thanks for the information 😄