MortalreminderPT / edit-xlsx

A quick and easy-to-use Rust library for Excel file editing.
https://crates.io/crates/edit-xlsx
25 stars 4 forks source link

Rich text: first word does not get the color specification #22

Closed mschnell1 closed 5 months ago

mschnell1 commented 5 months ago

In my tests I found that when I get a cell with rich text content (containing a string ABCDEFG) with e.g. one character underlined and all characters in the same color (e.g. blue) and decode the "words", the first word gets the color "Default".

When the words have different colors, also the first is correctly denoted,.

MortalreminderPT commented 5 months ago

I think that when rich text in a cell is styled in the same way, it's probably styled content, not rich text.

MortalreminderPT commented 5 months ago

I will test it later.

MortalreminderPT commented 5 months ago

I tested it using <span style="color: #ffff00;"><b>AB<u>C</u>DEFG</b></span> and it printed out the following results:

// cell.format.unwrap().font.color
RGB(
    255,
    255,
    0,
)
// cell.rich_text.unwrap()
RichText {
    words: [
        Word {
            text: "AB",
            font: FormatFont {
                ...
                color: Default,
                ...
            },
        },
        Word {
            text: "C",
            font: Some(
                FormatFont {
                    bold: true,
                    italic: false,
                    underline: true,
                    size: 14.0,
                    color: RGB(
                        255,
                        255,
                        0,
                    ),
                    name: "Helvetica",
                },
            ),
        },
        Word {
            text: "DEFG",
            font: Some(
                FormatFont {
                    bold: true,
                    italic: false,
                    underline: false,
                    size: 14.0,
                    color: RGB(
                        255,
                        255,
                        0,
                    ),
                    name: "Helvetica",
                },
            ),
        },
    ],
}
MortalreminderPT commented 5 months ago

This seems to be an Excel feature, and I think the FormatFont in Word needs to be adjusted to Optional to remove the ambiguity

mschnell1 commented 5 months ago

// cell.format.unwrap().font.color: Yep. I get exactly the same with pretty printing the rich text.

Hmm. .... From my "outside" perspective, the outcome seems to be hard to digest:

In the excel sheet the colors of the parts of the text are the same. The words of the rich text feature different colors, the first being "Default". Up to now the "Default" color seemed to mean a default for the complete table. Here the "Default" might mean the "overall" color of the Cell. But with the "text" being rich text, I up to now fail to find the "Cell Text Color" (which I successfully use with non-Rich-Text cells).

If there is a "Default cell text color" for rich text cells, it seems weird, that same is not used for the other words.

mschnell1 commented 5 months ago

Got it working:

    for word in rt.words.iter() {
        let text_color = match word.font.color {
            FormatColor::Default => cell.text_color,
            text_color => text_color,
        };
       ...

Thanks for listening !

mschnell1 commented 5 months ago

Seems to work OK this way