Closed noonchen closed 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:
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.
@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* ?
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.
Closing because I think the initial request has been answered.
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:
XlsxError
occurred in between, all the data that has been written into memory have lost, some times we also like to access the half-complete file even if the process is aborted.constant memory
in the roadmap, user can flush the data into a file at any place they think is suitable. For instance,flush
once a worksheet is completed or number of rows have been reached.pseudo code is like:
Wonder if it's implementable in current architecture?