thscharler / spreadsheet-ods

Apache License 2.0
29 stars 6 forks source link

Does it support text formatting? #51

Open darksylinc opened 3 months ago

darksylinc commented 3 months ago

Hi!

First, wonderful crate, although documentation could be improved.

My question is the following:

I have a spreadsheet generated in LibreOffice with the number 5 with a custom style that adds extra 0 padding. So when read in LibreOffice it reads as: 00005.

For reference, the format is the following: image

I tried the following:

let my_num = my_sheet
                .cell(row, 1)
                .unwrap()
                .value
                .as_str_opt()
                .unwrap();

However it fails. Ok, I guess there i no auto conversion. Of course the following works:

let my_num: u32 = my_sheet
                .cell(row, 1)
                .unwrap()
                .value
                .as_u32_opt()
                .unwrap();

However the problem is that I have no idea how to retrieve the formatting.

Does spreadsheet-ods have a way for me to somewhat easily retrieve the formatting and print the value the way the user seeing it? (i.e. retrieve the fact that there are four 0 of padding so I can print 0005 instead of 5).

thscharler commented 3 months ago

That's a tricky question.

You can:

    if let Some(s) = sheet.cellstyle(row, 1) {
        if let Some(style) = wb.cellstyle(s) {
            if let Some(format) = style.value_format() {
                if let Some(format) = wb.number_format(format) {
                   for part in format.parts() {
                       part.attr_def("attr-name", "");
                       // ...
                   }
                }
            }
        }
    }

... and work through those attributes to make something up.

But that's admittedly not very user-friendly.

And now that I tried it, I saw that wb.number_format() is broken.

So, short answer: No, sorry. Doesn't work.

A bit longer answer: I once had half of a solution for this, but I threw it away, because there is no number formating crate that comes even close to the requirements for ODS. For dates there is chrono, which should work, but they added localization only recently, so at the time there was nothing. And the same again for numbers. For those I recently wrote format_num_pattern which should be sufficient.

And even then there is all the different attributes for formatting. Calc can really do a LOT of variations of the theme.

Which means the answer is still: No, sorry.

I will keep this issue open, and after I finished my current project (which may take a while) I'll read this again, and maybe find the enthusiasm to start this.

PS: Is it really necessary to use the defined format from the spreadsheet, or can you just guess an appropriate format for your usecase. In that case I would try format_num_pattern or something similar, and chronos formatting for dates.

darksylinc commented 3 months ago

Thanks for the detailed response!

Indeed my needs are different from spreadsheet-ods. Whereas I am just fine with 0-padding on the left you need something generic & flexible that works with every possible formatting rule ODS supports.

For the time being I'm managing with just hardcoding it for the specific needs. If I get some time, I'll take a look at your snippet to see if I can get the formatting out.

Cheers

thscharler commented 3 months ago

You can always contact me, if you want to start something. At least I can give you a few hints.