MathNya / umya-spreadsheet

A pure rust library for reading and writing spreadsheet files
MIT License
276 stars 44 forks source link

`Style` #50

Closed KyGost closed 2 years ago

KyGost commented 2 years ago

Is there a better way than this?:

let mut color = Color::default();
color.set_tint(0.2);
let mut pattern_fill = PatternFill::default();
pattern_fill.set_background_color(color);
let mut fill = Fill::default();
fill.set_pattern_fill(pattern_fill);
let mut style = Style::default();
style.set_fill(fill);
KyGost commented 2 years ago

Oh.... Does Cell::set_style even work?

KyGost commented 2 years ago

Ah... PatternFill's default is None

KyGost commented 2 years ago

Ah, I'm silly and didn't read: https://docs.rs/umya-spreadsheet/latest/umya_spreadsheet/structs/struct.Style.html#

KyGost commented 2 years ago

Hmm.... Using PatternValues::Solid I can't make it anything but pure black.

I suspect that styles need some work and currently don't respond to background color.

MathNya commented 2 years ago

Hmmm, there may be something wrong with the style settings. We will investigate.

MathNya commented 2 years ago

Use set_foreground_color() instead of set_background_color(). This is also implemented in accordance with open xml, but it is confusing. We will take some countermeasures in the future.

This implementation worked well.

    let mut color = umya_spreadsheet::Color::default();
    color.set_argb(umya_spreadsheet::Color::COLOR_BLUE);
    let mut pattern_fill = umya_spreadsheet::PatternFill::default();
    pattern_fill.set_foreground_color(color);
    let mut fill = umya_spreadsheet::Fill::default();
    fill.set_pattern_fill(pattern_fill);
    let mut style = umya_spreadsheet::Style::default();
    style.set_fill(fill);
    book.get_sheet_by_name_mut("Sheet2")
        .unwrap()
        .set_style("A2", style);

Here is an example of a smarter implementation.

    // change background color.
    book.get_sheet_by_name_mut("Sheet2")
        .unwrap()
        .get_style_mut("A1")
        .get_fill_mut()
        .get_pattern_fill_mut()
        .get_foreground_color_mut()
        .set_argb(umya_spreadsheet::Color::COLOR_BLUE);
KyGost commented 2 years ago

Thanks, I'll try that. I had thought foreground color meant text color.

MathNya commented 2 years ago

The following implementation has been added for changing the background color of cells. Please check after obtaining the latest version.

use umya_spreadsheet::*;
let mut book = new_file();
let mut style = book.get_sheet_by_name_mut("Sheet1").unwrap().get_style_mut("A1");
style.set_background_color(Color::COLOR_RED);
KyGost commented 2 years ago

Looks nicer!