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

Bug: clone #68

Closed han1548772930 closed 9 months ago

han1548772930 commented 9 months ago

Current behavior

   let res: Result<(), XlsxError> = export_excel(uf_map_list, tgt_path, false, true).await;
        return match res {
            Ok(_) => {
                Ok(())
            }
            Err(e) => {
                log::error!("err:{}", e);

                return Err(e.to_string().as_str());
            }
        };

### Expected behavior

Currently I get XlsxError but I can't get the information inside

### Environment

```markdown
- `rust_xlsxwriter` version:0.60.0
- Cargo.toml dependency line for `rust_xlsxwriter`: rust_xlsxwriter = { version = "0.60.0" }
- rustc version:rustc 1.75.0 (82e1608df 2023-12-21)
- Excel version:16
- OS:
- If using wasm, which method/tool:

Any other information

No response

jmcnamara commented 9 months ago

It should work as expected.

Here is a complete working example that raises and outputs an error:

use rust_xlsxwriter::{Workbook, XlsxError};

fn main() -> Result<(), String> {
    let res: Result<(), XlsxError> = export_excel();
    return match res {
        Ok(_) => Ok(()),
        Err(e) => {
            return Err(e.to_string());
        }
    };
}

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

    // This will raise an error.
    worksheet.write(0, 16_384, "Hello")?;

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

    Ok(())
}

Output:

$ cargo run --example gh68
   Compiling rust_xlsxwriter v0.60.0 (/path/rust_xlsxwriter)
    Finished dev [unoptimized + debuginfo] target(s) in 0.47s
     Running `target/debug/examples/gh68`

Error: "Row or column exceeds Excel's allowed limits (1,048,576 x 16,384)."
han1548772930 commented 9 months ago

tokio::spawn(async move {
   let res: Result<(), XlsxError> = export_excel(uf_map_list, tgt_path, false, true).await;
        return match res {
            Ok(_) => {
                Ok(())
            }
            Err(e) => {
                log::error!("err:{}", e);

                return Err(e.to_string().as_str());
            }
        };
        });

My approximate code is like this
jmcnamara commented 9 months ago

And what is the error that you get?

han1548772930 commented 9 months ago
error[E0515]: cannot return value referencing temporary value
   --> src\ufmap\TSKMapping.rs:569:17
    |
569 |                 Err(e.to_string().as_str())
    |                 ^^^^-------------^^^^^^^^^^
    |                 |   |
    |                 |   temporary value created here
    |                 returns a value referencing data owned by the current function
jmcnamara commented 9 months ago

That is just a general rust issue where you can't return a &str from a function without specifying some lifetime. See for example:

https://stackoverflow.com/questions/29781331/why-cant-i-return-an-str-value-generated-from-a-string

It doesn't have anything to do with rust_xlsxwriter.

han1548772930 commented 9 months ago

I changed the way it works now


  let res: Result<(), XlsxError> = export_excel(uf_map_list, tgt_path, false, true).await;
        return match res {
            Ok(_) => {
                "".to_string()
            }
            Err(e) => {
                log::error!("err:{}", e);
                e.to_string()
            }
        };
han1548772930 commented 9 months ago

That is just a general rust issue where you can't return a &str from a function without specifying some lifetime. See for example:

https://stackoverflow.com/questions/29781331/why-cant-i-return-an-str-value-generated-from-a-string

It doesn't have anything to do with rust_xlsxwriter.

I'm really attracted to the potential and capabilities of this framework. It has earned my utmost admiration and support, and I've shown a little heartfelt support by sponsoring $10

jmcnamara commented 9 months ago

I'm really attracted to the potential and capabilities of this framework. It has earned my utmost admiration and support, and I've shown a little heartfelt support by sponsoring $10

Thank you for the kind words and the donation. :-)