jmcnamara / rust_xlsxwriter

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

feature request: manually flush contents to disk #13

Closed noonchen closed 1 year ago

noonchen commented 1 year ago

Feature Request

Thanks for this amazing crate!

Can we have an API that allows user to manually save current memory data into disk? For two reasons:

pseudo code is like:

let xpath = "/xx/xxx.xlsx"
let mut xlsx = Workbook::new().set_save_path(xpath);
// create a worksheet and write data in memory
let sheet = xlsx.add_worksheet();
sheet.set_name("A");
sheet.write_number_only(0, 0, 0)?;
// ...
// flush memory data into disk
xlsx.flush_to_disk();
// maybe we can still get sheet A?
let sheet = xlsx.worksheet_from_name("A")?;
sheet.write_number_only(1, 0, 1)?;
// save all to disk
xlsx.save_to_path(xx)?;

Wonder if it's implementable in current architecture?

jmcnamara commented 1 year ago

Can we have an API that allows user to manually save current memory data into disk?

You can so that with the current APIs. All the save*() methods allow you to resave a file. For example:

use rust_xlsxwriter::{Workbook, XlsxError};

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

    worksheet.write_string_only(0, 0, "Hello")?;
    worksheet.write_number_only(1, 0, 12345)?;

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

    // Get a handle to the worksheet and keep writing data:
    worksheet = workbook.worksheet_from_index(0)?;
    worksheet.write_number_only(2, 0, 2)?;
    worksheet.write_number_only(3, 0, 3)?;

    // Save again.
    workbook.save("hello.xlsx")?;

    Ok(())
}

Output:

screenshot

Do you mean something like that?

if there is a XlsxError occurred in between, all the data that has been written into memory have lost

Out of curiosity what are the errors that you are encountering? This is part of a more general question that I want to raise around rust_xlsxwriter errors which may, currently, be too restrictive.

noonchen commented 1 year ago

@jmcnamara I am not realized we can save workbook multiple times 😂, thanks for the info!

As for the XlsxError, I haven't encountered any errors from xlsxwriter APIs, but user code is likely to have some logic to abort from the middle, since you mentioned we can save xlsx anywhere we'd like, guess it won't be a problem though.

Just one more question, is the memory freed up when we call save* ?

jmcnamara commented 1 year ago

I am not realized we can save workbook multiple times 😂, thanks for the info!

Just remember that you are writing a zip file with 10+ files in it each time so there is a performance overhead. So it would be best not to over use this functionality.

I haven't encountered any errors from xlsxwriter APIs, but user code is likely to have some logic to abort from the middle

Yes that is a potential issue. I have asked a general question in that area here: #14

Just one more question, is the memory freed up when we call save* ?

No. Memory is only freed with the workbook object goes out of scope. The constant_memory mode, when it is implemented, will minimize the amount of data in memory by writing each completed row to a temp file until needed. However, that also created restrictions on some features.

jmcnamara commented 1 year ago

Closing because I think the initial request has been answered.