Struct calamine::Range

source ·
pub struct Range<T> { /* private fields */ }
Expand description

A struct which represents a squared selection of cells

Implementations§

source§

impl<T: CellType> Range<T>

source

pub fn new(start: (u32, u32), end: (u32, u32)) -> Range<T>

Creates a new non-empty Range

When possible, prefer the more efficient Range::from_sparse

§Panics

Panics if start.0 > end.0 or start.1 > end.1

source

pub fn empty() -> Range<T>

Creates a new empty range

source

pub fn start(&self) -> Option<(u32, u32)>

Get top left cell position (row, column)

source

pub fn end(&self) -> Option<(u32, u32)>

Get bottom right cell position (row, column)

source

pub fn width(&self) -> usize

Get column width

source

pub fn height(&self) -> usize

Get column height

source

pub fn get_size(&self) -> (usize, usize)

Get size in (height, width) format

source

pub fn is_empty(&self) -> bool

Is range empty

source

pub fn from_sparse(cells: Vec<Cell<T>>) -> Range<T>

Creates a Range from a coo sparse vector of Cells.

Coordinate list (COO) is the natural way cells are stored Inner size is defined only by non empty.

cells: Vec of non empty Cells, sorted by row

§Panics

panics when a Cell row is lower than the first Cell row or bigger than the last Cell row.

source

pub fn set_value(&mut self, absolute_position: (u32, u32), value: T)

Set inner value from absolute position

§Remarks

Will try to resize inner structure if the value is out of bounds. For relative positions, use Index trait

Try to avoid this method as much as possible and prefer initializing the Range with from_sparse constructor.

§Panics

If absolute_position > Cell start

§Examples
use calamine::{Range, Data};

let mut range = Range::new((0, 0), (5, 2));
assert_eq!(range.get_value((2, 1)), Some(&Data::Empty));
range.set_value((2, 1), Data::Float(1.0));
assert_eq!(range.get_value((2, 1)), Some(&Data::Float(1.0)));
source

pub fn get_value(&self, absolute_position: (u32, u32)) -> Option<&T>

Get cell value from absolute position.

If the absolute_position is out of range, returns None, else returns the cell value. The coordinate format is (row, column).

§Warnings

For relative positions, use Index trait

§Remarks

Absolute position is in sheet referential while relative position is in range referential.

For instance if we consider range C2:H38:

  • (0, 0) absolute is “A1” and thus this function returns None
  • (0, 0) relative is “C2” and is returned by the Index trait (i.e my_range[(0, 0)])
§Examples
use calamine::{Range, Data};

let range: Range<usize> = Range::new((1, 0), (5, 2));
assert_eq!(range.get_value((0, 0)), None);
assert_eq!(range[(0, 0)], 0);
source

pub fn get(&self, relative_position: (usize, usize)) -> Option<&T>

Get cell value from relative position.

Unlike using the Index trait, this will not panic but rather yield None if out of range. Otherwise, returns the cell value. The coordinate format is (row, column).

source

pub fn rows(&self) -> Rows<'_, T>

Get an iterator over inner rows

§Examples
use calamine::{Range, Data};

let range: Range<Data> = Range::new((0, 0), (5, 2));
// with rows item row: &[Data]
assert_eq!(range.rows().map(|r| r.len()).sum::<usize>(), 18);
source

pub fn used_cells(&self) -> UsedCells<'_, T>

Get an iterator over used cells only

source

pub fn cells(&self) -> Cells<'_, T>

Get an iterator over all cells in this range

source

pub fn deserialize<'a, D>( &'a self ) -> Result<RangeDeserializer<'a, T, D>, DeError>

Build a RangeDeserializer from this configuration.

§Example
fn main() -> Result<(), Error> {
    let path = format!("{}/tests/temperature.xlsx", env!("CARGO_MANIFEST_DIR"));
    let mut workbook: Xlsx<_> = open_workbook(path)?;
    let mut sheet = workbook.worksheet_range("Sheet1")?;
    let mut iter = sheet.deserialize()?;

    if let Some(result) = iter.next() {
        let (label, value): (String, f64) = result?;
        assert_eq!(label, "celsius");
        assert_eq!(value, 22.2222);

        Ok(())
    } else {
        return Err(From::from("expected at least one record but got none"));
    }
}
source

pub fn range(&self, start: (u32, u32), end: (u32, u32)) -> Range<T>

Build a new Range out of this range

§Remarks

Cells within this range will be cloned, cells out of it will be set to Empty

§Example
let mut a = Range::new((1, 1), (3, 3));
a.set_value((1, 1), Data::Bool(true));
a.set_value((2, 2), Data::Bool(true));

let b = a.range((2, 2), (5, 5));
assert_eq!(b.get_value((2, 2)), Some(&Data::Bool(true)));
assert_eq!(b.get_value((3, 3)), Some(&Data::Empty));

let c = a.range((0, 0), (2, 2));
assert_eq!(c.get_value((0, 0)), Some(&Data::Empty));
assert_eq!(c.get_value((1, 1)), Some(&Data::Bool(true)));
assert_eq!(c.get_value((2, 2)), Some(&Data::Bool(true)));

Trait Implementations§

source§

impl<T: Clone> Clone for Range<T>

source§

fn clone(&self) -> Range<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Range<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for Range<T>

source§

fn default() -> Range<T>

Returns the “default value” for a type. Read more
source§

impl<T: CellType> Index<(usize, usize)> for Range<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: (usize, usize)) -> &T

Performs the indexing (container[index]) operation. Read more
source§

impl<T: CellType> Index<usize> for Range<T>

§

type Output = [T]

The returned type after indexing.
source§

fn index(&self, index: usize) -> &[T]

Performs the indexing (container[index]) operation. Read more
source§

impl<T: CellType> IndexMut<(usize, usize)> for Range<T>

source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<T: CellType> IndexMut<usize> for Range<T>

source§

fn index_mut(&mut self, index: usize) -> &mut [T]

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Range<T>
where T: RefUnwindSafe,

§

impl<T> Send for Range<T>
where T: Send,

§

impl<T> Sync for Range<T>
where T: Sync,

§

impl<T> Unpin for Range<T>
where T: Unpin,

§

impl<T> UnwindSafe for Range<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.