rust_xlsxwriter
rust_xlsxwriter copied to clipboard
Feature request: set a cell 's background color
Question
As the title says , when I want to set a cell's background color , which method should I can use ?
write_with_format
must set a data for a cell , but the cell that I want to change it's background color already had a data .
worksheet.write_with_format(4, 0, 3.00, &decimal_format)?;
when I use the method write_blank
, I just got a blank cell without the data .
Please help me ,Thanks!
when I want to set a cell's background color , which method should I can use ?
There isn't currently a way to do that. All of the current methods require that you set the data and format at the same time with one of the write_*_with_format()
methods. However, it was/is planned to add methods that write the formatting separately from the data.
For the simple case of just adding cell data I can add that very soon. However, there is also the case where the user might want to merge the new format with the existing cell format. That will need a little bit more work/time.
If you are interested in the first/simpler use case I can add it and you can try it out.
when I want to set a cell's background color , which method should I can use ?
There isn't currently a way to do that. All of the current methods require that you set the data and format at the same time with one of the
write_*_with_format()
methods. However, it was/is planned to add methods that write the formatting separately from the data.For the simple case of just adding cell data I can add that very soon. However, there is also the case where the user might want to merge the new format with the existing cell format. That will need a little bit more work/time.
If you are interested in the first/simpler use case I can add it and you can try it out.
That's great! I like to try it . In some case, I also need to read xlsx file and modify it. But in rust I seems have to use two different crates rather then in Python that use openpyxl lib. em..... what ever , it's a good crate to use , and faster!
It is now possible (on main) to set a cell format separately from the data. Something like this:
use rust_xlsxwriter::{Format, Workbook, XlsxError};
fn main() -> Result<(), XlsxError> {
// Create a new Excel file object.
let mut workbook = Workbook::new();
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();
// Add a format.
let decimal_format = Format::new()
.set_num_format("0.000")
.set_background_color("#6495ED");
// Write some data.
worksheet.write(4, 0, 3)?;
// Set the format.
worksheet.set_cell_format(4, 0, &decimal_format)?;
workbook.save("worksheet.xlsx")?;
Ok(())
}
Output:
I'll roll this into a release in the next few days.
This is now upstream in v0.74.0.