MathNya / umya-spreadsheet

A pure rust library for reading and writing spreadsheet files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Number format is wrong after saving

Andrew-Lyu opened this issue · comments

When I do a cell to cell copy from one excel to another, cells of some columns got the wrong number format.

My code is like below,
for row_num in 1..max_row {
for col_num in 1..max_col {
let source_cell = from_sheet.get_cell((col_num, row_num)).unwrap();
let mut target_cell = sheet_cell.clone();
to_sheet.set_cell(target_cell);
}
}

after saving, cell's number format goes wrong. format code in from_sheet is Some(NumberingFormat { number_format_id: 176, format_code: "0.00_ ", is_build_in: false }), but format code in to_sheet is Some(NumberingFormat { number_format_id: 178, format_code: "[$-F800]dddd\,\ mmmm\ dd\,\ yyyy", is_build_in: false })

@Andrew-Lyu
Thank you for your report.
It is probably a glitch in the program.
We will address this issue in the next version update.

@Andrew-Lyu
I tried to verify the operation here, but could not reproduce it.
(The code we verified is here: f54342a)
We are modifying some code for the new version.
Maybe the problem was solved in the process.
Version 1.1.0 will be released soon.
Please check again there.

Hi Math,

Actually the problem still exists, I attach full example code and excel file here, you can run and check column O and AH, the number format were changed.

Regards
excel_number.zip

@MathNya
I think I found the root cause, not sure if my fix is totally correct (it pass my test case though), so I paste it here, for your reference.
numbering_formats.rs, from line 57. In my test case, cell B1's format id is 178, format is "[$-F800]dddd,\ mmmm\ dd,\ yyyy", but the current code will write format id 176, this makes the cells with format 176 display the wrong format.

pub(crate) fn set_style(&mut self, style: &Style) -> u32 {
    match style.get_numbering_format() {
        Some(v) => {
            let number_format_id = v.get_number_format_id();
            if v.get_is_build_in() == &true {
                return *number_format_id;
            }
            if self.numbering_format.get(number_format_id).is_some() {
                return *number_format_id;
            }
            let hash_code = v.get_hash_code();
            // let mut id = 175;
            for (index, numbering_format) in &self.numbering_format {
                if numbering_format.get_hash_code() == hash_code {
                    return *index;
                }
                // if &id < index {
                //     id = *index;
                // }
            }
            // id += 1;
            let mut num = v.clone();
            // num.set_number_format_id_crate(id);
            self.set_numbering_format(num);
            // id
            *number_format_id
        }
        None => 0,
    }
}

@Andrew-Lyu
Thanks for the information.
We have reproduced the problem here as well.
We will fix it soon.

I think this can be closed