rust_xlsxwriter icon indicating copy to clipboard operation
rust_xlsxwriter copied to clipboard

Feature request: set a cell 's background color

Open tmacychen opened this issue 1 year ago • 2 comments

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!

tmacychen avatar Feb 04 '24 08:02 tmacychen

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.

jmcnamara avatar Feb 04 '24 18:02 jmcnamara

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!

tmacychen avatar Feb 05 '24 00:02 tmacychen

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:

screenshot

I'll roll this into a release in the next few days.

jmcnamara avatar Aug 15 '24 19:08 jmcnamara

This is now upstream in v0.74.0.

jmcnamara avatar Aug 24 '24 16:08 jmcnamara