tafia / calamine

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

[panic] Tables must be loaded before they are referenced #373

Closed anzhi0708 closed 10 months ago

anzhi0708 commented 11 months ago

Hello, sorry if this's a noob question. I was trying to read data from a file:

Code

extern crate calamine;
use calamine::{DataType, Xlsx};
use std::env;

fn read_sheet<'a>(
    book_name: &'a str,
    table_name: &'a str,
) -> Result<calamine::Table<DataType>, calamine::Error> {
    let mut wb: Xlsx<_> = calamine::open_workbook(book_name).expect("Cannot read workbook");
    if let Some(Ok(result)) = wb.table_by_name(table_name) {
        return Ok(result);
    }
    Err(calamine::Error::from("!"))
}

fn main() {
    if let Some(file_name) = env::args().nth(1) {
        let table = read_sheet(&file_name, "Sheet1").expect("Failed to read columns names");
        let columns = table.columns();
        println!("Hello, world! {:#?}", columns);
    }
}

Command line

.\target\debug\out.exe "C:\Users\USER\Desktop\sample.xlsx"

Error

thread 'main' panicked at C:\Users\USER\.cargo\registry\src\index.crates.io-6f17d22bba15001f\calamine-0.22.1\src\xlsx.rs:676:14:
Tables must be loaded before they are referenced
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library\std\src\panicking.rs:595
   1: core::panicking::panic_fmt
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library\core\src\panicking.rs:67
   2: core::panicking::panic_display
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library\core\src\panicking.rs:150
   3: core::panicking::panic_str
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library\core\src\panicking.rs:134
   4: core::option::expect_failed
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library\core\src\option.rs:1988
   5: enum2$<core::option::Option<ref$<alloc::vec::Vec<tuple$<alloc::string::String,alloc::string::String,alloc::vec::Vec<alloc::string::String,alloc::alloc::Global>,calamine::xlsx::Dimensions>,alloc::alloc::Global> > > >::expect<ref$<alloc::vec::Vec<tuple$<all
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33\library\core\src\option.rs:898
   6: calamine::xlsx::Xlsx<std::io::buffered::bufreader::BufReader<std::fs::File> >::table_by_name<std::io::buffered::bufreader::BufReader<std::fs::File> >
             at C:\Users\USER\.cargo\registry\src\index.crates.io-6f17d22bba15001f\calamine-0.22.1\src\xlsx.rs:673
   7: brox_pt::read_sheet
             at .\src\main.rs:10
   8: brox_pt::main
             at .\src\main.rs:18
   9: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

The code compiled successfully and I'm too dumb to figure it out myself. Send help plz

tafia commented 11 months ago

It seems like you're trying to read a table when you want to read a whole sheet. These are different.

Try

 if let Some(Ok(result)) = wb.worksheet_range(sheet_name) {
}

To load tables, you must first run the load_tables function. We should probably update and return an error instead of panicking though.

t1hq commented 9 months ago

same panic here, later found that it is sheet name, not table name:

let mut workbook: Xlsx<_> = open_workbook(&file_path).unwrap();
let sheet_name = workbook.sheet_names()[0].to_owned();

if let Ok(range) = workbook.worksheet_range(&sheet_name) {
  ...
}