MathNya / umya-spreadsheet

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

Number format is wrong after saving #147

Closed Andrew-Lyu closed 6 months ago

Andrew-Lyu commented 7 months ago

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, formatcode: "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 })

MathNya commented 7 months ago

@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.

MathNya commented 7 months ago

@Andrew-Lyu I tried to verify the operation here, but could not reproduce it. (The code we verified is here: https://github.com/MathNya/umya-spreadsheet/commit/f54342acd85d321a250eab66e72d2b7811d907b2) 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.

Andrew-Lyu commented 7 months ago

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

Andrew-Lyu commented 7 months ago

@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,
    }
}
MathNya commented 7 months ago

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

Andrew-Lyu commented 6 months ago

I think this can be closed