Skip to content

Commit

Permalink
feat: add helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Oct 6, 2024
1 parent 72e74b0 commit 23f4c26
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ where
}
}

fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self {
match self {
Sheets::Xls(ref mut e) => {
e.with_header_row(header_row);
}
Sheets::Xlsx(ref mut e) => {
e.with_header_row(header_row);
}
Sheets::Xlsb(ref mut e) => {
e.with_header_row(header_row);
}
Sheets::Ods(ref mut e) => {
e.with_header_row(header_row);
}
}
self
}

/// Gets `VbaProject`
fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, Self::Error>> {
match self {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ where
self
}

/// Set current header row
fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self;

/// Gets `VbaProject`
fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, Self::Error>>;

Expand Down
5 changes: 5 additions & 0 deletions src/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ where
self.options = options;
}

fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self {
self.options.header_row = header_row;
self
}

/// Gets `VbaProject`
fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, OdsError>> {
None
Expand Down
5 changes: 5 additions & 0 deletions src/xls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ impl<RS: Read + Seek> Reader<RS> for Xls<RS> {
self.options = options;
}

fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self {
self.options.header_row = header_row;
self
}

fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, XlsError>> {
self.vba.as_ref().map(|vba| Ok(Cow::Borrowed(vba)))
}
Expand Down
5 changes: 5 additions & 0 deletions src/xlsb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ impl<RS: Read + Seek> Reader<RS> for Xlsb<RS> {
self.options = options;
}

fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self {
self.options.header_row = header_row;
self
}

fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, XlsbError>> {
self.zip.by_name("xl/vbaProject.bin").ok().map(|mut f| {
let len = f.size() as usize;
Expand Down
5 changes: 5 additions & 0 deletions src/xlsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,11 @@ impl<RS: Read + Seek> Reader<RS> for Xlsx<RS> {
self.options = options;
}

fn with_header_row(&mut self, header_row: Option<u32>) -> &mut Self {
self.options.header_row = header_row;
self
}

fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, XlsxError>> {
let mut f = self.zip.by_name("xl/vbaProject.bin").ok()?;
let len = f.size() as usize;
Expand Down
16 changes: 8 additions & 8 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use calamine::Data::{Bool, DateTime, DateTimeIso, DurationIso, Empty, Error, Float, Int, String};
use calamine::{
open_workbook, open_workbook_auto, DataRef, DataType, Dimensions, ExcelDateTime,
ExcelDateTimeType, Ods, OdsOptions, Range, Reader, ReaderOptions, ReaderRef, Sheet, SheetType,
SheetVisible, Xls, XlsOptions, Xlsb, XlsbOptions, Xlsx, XlsxOptions,
ExcelDateTimeType, Ods, Range, Reader, ReaderRef, Sheet, SheetType, SheetVisible, Xls, Xlsb,
Xlsx,
};
use calamine::{CellErrorType::*, Data};
use rstest::rstest;
Expand Down Expand Up @@ -1824,7 +1824,7 @@ fn test_header_row_xlsx(
);

let range = excel
.with_options(XlsxOptions { header_row })
.with_header_row(header_row)
.worksheet_range("Sheet1")
.unwrap();
assert_eq!(range.start(), Some(expected_start));
Expand All @@ -1837,11 +1837,11 @@ fn test_header_row_xlsx(
fn test_read_twice_with_different_header_rows() {
let mut xlsx: Xlsx<_> = wb("any_sheets.xlsx");
let _ = xlsx
.with_options(XlsxOptions::default().with_header_row(2))
.with_header_row(Some(2))
.worksheet_range("Visible")
.unwrap();
let _ = xlsx
.with_options(XlsxOptions::default().with_header_row(1))
.with_header_row(Some(1))
.worksheet_range("Visible")
.unwrap();
}
Expand Down Expand Up @@ -1882,7 +1882,7 @@ fn test_header_row_xlsb() {
assert_eq!(range.rows().nth(1).unwrap(), &second_line);

let range = xlsb
.with_options(XlsbOptions::default().with_header_row(1))
.with_header_row(Some(1))
.worksheet_range("Sheet1")
.unwrap();
assert_eq!(range.start(), Some((1, 0)));
Expand Down Expand Up @@ -1926,7 +1926,7 @@ fn test_header_row_xls() {
assert_eq!(range.rows().nth(1).unwrap(), &second_line);

let range = xls
.with_options(XlsOptions::default().with_header_row(1))
.with_header_row(Some(1))
.worksheet_range("Sheet1")
.unwrap();
assert_eq!(range.start(), Some((1, 0)));
Expand Down Expand Up @@ -1956,7 +1956,7 @@ fn test_header_row_ods() {
assert_eq!(range.rows().nth(2).unwrap(), &third_line);

let range = ods
.with_options(OdsOptions::default().with_header_row(2))
.with_header_row(Some(2))
.worksheet_range("Sheet1")
.unwrap();
assert_eq!(range.start(), Some((2, 0)));
Expand Down

0 comments on commit 23f4c26

Please sign in to comment.