MathNya / umya-spreadsheet

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

Getting the background color when themed. #184

Open taylorh140 opened 8 months ago

taylorh140 commented 8 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 8 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 8 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 months 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 months ago

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

MathNya commented 3 months ago

The latest version has been released. Please check it out.

taylorh140 commented 3 months ago

Ok, so i did and its looking much better but it still has some issues.

Input: ThemeColors.xlsx

image

Output: image

I have attached my input file.

Some look correct but others are off by a bit.

The associated code to grab this is:


  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 {
          tmp_cell_data["fill_color"] = json!(background_color.get_argb_with_theme(book.get_theme()));
      } else {
          tmp_cell_data["fill_color"] = json!(background_color.get_argb().to_string());
      }
  }
MathNya commented 3 months ago

@taylorh140 Thank you for your report. We will investigate the cause.

MathNya commented 3 months ago

@taylorh140 Sorry, we have considered correcting the problem, but it is difficult to respond. It will be some time before the response is complete.

taylorh140 commented 3 months ago

I am patient. Thank you for your work. It looks like a difficult problem.

MathNya commented 3 months ago

@taylorh140 Fixed issue. Please get the latest version and check it.

I happened to find a good document and was able to correct it. However, a special calculation is used to calculate the colors. Therefore, there are slight deviations between spreadsheets (Excel, LibreOffice, etc.).