MathNya / umya-spreadsheet

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

Getting the background color when themed. #184

Open taylorh140 opened 3 months ago

taylorh140 commented 3 months ago

I have been finding that argb colors are sometimes empty when getting the background color:

i have this method to get them so far (extracting to json)

            if let Some(background_color) = cell.get_style().get_background_color() {
                if background_color.get_indexed() != &0  {
                    tmp_cell_data["fill_color"] = json!(INDEXED_COLORS.get(*background_color.get_indexed() as usize));
                } else if background_color.get_theme_index() != &0 {
                    // It would be more useful as argb
                    tmp_cell_data["theme_index"] = json!(*background_color.get_theme_index());
                } else {
                    tmp_cell_data["fill_color"] = json!(background_color.get_argb().to_string());
                }
            }

I have to make a copy of the INDEXED_COLORS to extract them at the moment. And im not sure how to decode a themed color.

MathNya commented 3 months ago

@taylorh140 Perhaps the following process can be used to obtain the color code.

    let book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
    let color = book.get_theme().get_theme_elements()
    .get_color_scheme()
    .get_color_map()
    .get(theme_index)

We will fix it so that arbg will always have a numerical value in the next version.

taylorh140 commented 3 months ago

@MathNya This is super helpful. I'll see if I can get it to work on my end but that looks sort of like the direction I was heading.

And yeah argb always having a numerical value would be super nice and very helpful in my case.

Just fyi I have been working on a typst (its similar to a latex) importing plugin for xlsx sheets. so far i can import values and merged cells and i should be able to handle the background colors now.

i think for the first release i would like to get (simple) text alignment and basic borders as well.

but your library is a key technology to make it possible.

Thanks!

taylorh140 commented 5 days ago

@MathNya

I noticed when i used the solution above i get the darkest version of the themed color or the first row color instead of the subsequent ones :

image

so here i expect : image but end up with: image

Note that my colors didn't quite turn out right here render wise but they do match the first row (see first image on this post)

MathNya commented 5 days ago

@taylorh140 Thank you for your report. Perhaps there is a bug somewhere. We will investigate.