Skip to content

Commit

Permalink
add warning and return an empty range if not a worksheet
Browse files Browse the repository at this point in the history
  • Loading branch information
tafia committed Jul 8, 2024
1 parent 4dc9d36 commit 462ebdb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/xlsx/cells_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<'a> XlsxCellReader<'a> {
) -> Result<Self, XlsxError> {
let mut buf = Vec::with_capacity(1024);
let mut dimensions = Dimensions::default();
let mut sh_type = None;
'xml: loop {
buf.clear();
match xml.read_event_into(&mut buf).map_err(XlsxError::Xml)? {
Expand All @@ -57,9 +58,19 @@ impl<'a> XlsxCellReader<'a> {
return Err(XlsxError::UnexpectedNode("dimension"));
}
b"sheetData" => break,
_ => (),
typ => {
if sh_type.is_none() {
sh_type = Some(xml.decoder().decode(typ)?.to_string());
}
}
},
Event::Eof => return Err(XlsxError::XmlEof("sheetData")),
Event::Eof => {
if let Some(typ) = sh_type {
return Err(XlsxError::NotAWorksheet(typ));
} else {
return Err(XlsxError::XmlEof("worksheet"));
}
}
_ => (),
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/xlsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub enum XlsxError {
WorksheetNotFound(String),
/// Table not found
TableNotFound(String),
/// The specified sheet is not a worksheet
NotAWorksheet(String),
}

from_err!(std::io::Error, XlsxError, Io);
Expand Down Expand Up @@ -136,6 +138,7 @@ impl std::fmt::Display for XlsxError {
XlsxError::WorksheetNotFound(n) => write!(f, "Worksheet '{n}' not found"),
XlsxError::Password => write!(f, "Workbook is password protected"),
XlsxError::TableNotFound(n) => write!(f, "Table '{n}' not found"),
XlsxError::NotAWorksheet(typ) => write!(f, "Expecting a worksheet, got {typ}"),
}
}
}
Expand Down Expand Up @@ -854,7 +857,14 @@ impl<RS: Read + Seek> Xlsx<RS> {
&'a mut self,
name: &str,
) -> Result<Range<DataRef<'a>>, XlsxError> {
let mut cell_reader = self.worksheet_cells_reader(name)?;
let mut cell_reader = match self.worksheet_cells_reader(name) {
Ok(reader) => reader,
Err(XlsxError::NotAWorksheet(typ)) => {
log::warn!("'{typ}' not a valid worksheet");
return Ok(Range::default());
}
Err(e) => return Err(e),
};
let len = cell_reader.dimensions().len();
let mut cells = Vec::new();
if len < 100_000 {
Expand Down Expand Up @@ -928,7 +938,14 @@ impl<RS: Read + Seek> Reader<RS> for Xlsx<RS> {
}

fn worksheet_formula(&mut self, name: &str) -> Result<Range<String>, XlsxError> {
let mut cell_reader = self.worksheet_cells_reader(name)?;
let mut cell_reader = match self.worksheet_cells_reader(name) {
Ok(reader) => reader,
Err(XlsxError::NotAWorksheet(typ)) => {
warn!("'{typ}' not a worksheet");
return Ok(Range::default());
}
Err(e) => return Err(e),
};
let len = cell_reader.dimensions().len();
let mut cells = Vec::new();
if len < 100_000 {
Expand Down
Binary file added tests/issue438.xlsx
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,14 @@ fn issue_420_empty_s_attribute() {
);
}

#[test]
fn issue_438_charts() {
let mut excel: Xlsx<_> = wb("issue438.xlsx");
let _range = excel
.worksheet_range("Chart1")
.expect("could not open worksheet range");
}

#[test]
fn isssue_446_formulas() {
let mut excel: Xlsx<_> = wb("issue446.xlsx");
Expand Down

0 comments on commit 462ebdb

Please sign in to comment.