tafia / calamine

A pure Rust Excel/OpenDocument SpreadSheets file reader: rust on metal sheets
MIT License
1.6k stars 155 forks source link

Parse Integer Error from Excel files with empty `s` attributes #420

Closed jlondonobo closed 2 months ago

jlondonobo commented 2 months ago

Description

Reading an Excel file that contains empty s attributes (s="") results in a ParseIntError.

Steps to reproduce

  1. Download an example file from Colombia's National Tax and Customs Department: https://www.dian.gov.co/dian/cifras/Basesestadisticasimportaciones/01_Importaciones_2018_Enero.zip

  2. Attempt to parse the file:

    use calamine::{open_workbook, Reader, Xlsx};
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        let path = "PATH_TO_FILE";
        let mut workbook: Xlsx<_> = open_workbook(path)?;
        dbg!(workbook.worksheet_range("Sheet1"));
        Ok(())
    }
    • Note: Directly opening and saving the file in Excel may auto-correct the s attributes, thus altering the test conditions.

Possible Cause

The issue likely stems from the read_v function, which attempts to convert style attributes into integers. When an s attribute is an empty string, the conversion fails, leading to the error.

https://github.com/tafia/calamine/blob/953d80ee66d80ecbeabf14670841ea6cc0e1b006/src/xlsx/cells_reader.rs#L236-L241

Suggested fix

A potential fix could involve modifying the read_v function to handle empty s attributes gracefully, either by ignoring them or by assigning a default style value.