Class RangeObj

class ooodev.utils.data_type.range_obj.RangeObj(col_start, col_end, row_start, row_end, sheet_idx=-1)[source]

Range Parts

See also

Changed in version 0.32.0: Added support for __contains__ and __iter__ methods. If sheet_idx is set to -2 then no attempt is made to get the sheet index or name from spreadsheet.

New in version 0.8.2.

Parameters:
  • col_start (str) –

  • col_end (str) –

  • row_start (int) –

  • row_end (int) –

  • sheet_idx (int) –

__contains__(value)[source]

Gets if current instance contains a cell value.

Parameters:
  • value (str) – Cell object

  • value – Cell address

  • value – Cell Values

  • value – Cell name

Returns:

True if instance contains cell; Otherwise, False.

Return type:

bool

Note

If cell input contains sheet info the it is use in comparison. Otherwise sheet is ignored.

New in version 0.32.0.

__eq__(other)[source]

Compare if two ranges are equal

Parameters:

other (object) – Range Object, RangeValues or str in range format such as A1:C4

Returns:

True if equal; Otherwise, False

Return type:

bool

__getitem__(key)[source]

Get a cell object from range

Parameters:

key (int) – Zero-based index of cell.

Raises:
  • TypeError – If index is not a string.

  • IndexError – If index is out of range.

Returns:

Cell Object

Return type:

CellObj

Example

>>> rng = RangeObj.from_range("A1:C4")
>>> cell = rng["B3"]
>>> print(cell)
B3
__init__(col_start, col_end, row_start, row_end, sheet_idx=-1)
Parameters:
  • col_start (str) –

  • col_end (str) –

  • row_start (int) –

  • row_end (int) –

  • sheet_idx (int) –

Return type:

None

__iter__()[source]

Iterates over all cells in the range.

The iteration is done in a column-major order, meaning that the cells are iterated over by column, then by row.

Example

# each cell is an instance of CellObj
>>> rng = RangeObj.from_range("A1:C4")
>>> for cell in rng:
>>>     print(cell)
A1
B1
C1
A2
B2
C2
A3
B3
C3
A4
B4
C4
Yields:

Generator[mCellObj.CellObj, None, None] – Cell Object

Return type:

Generator[CellObj, None, None]

New in version 0.32.0.

Return type:

Generator[CellObj, None, None]

__str__()[source]

Convert range to string

Returns:

Inf the format of A1:C4

Return type:

str

contains(cell_obj: ooodev.utils.data_type.cell_obj.CellObj)[source]
contains(cell_addr: com.sun.star.table.CellAddress)
contains(cell_vals: ooodev.utils.data_type.cell_values.CellValues)
contains(cell_name: str)
contains(*args, **kwargs)

Gets if current instance contains a cell value.

Parameters:
  • cell_obj (CellObj) – Cell object

  • cell_addr (CellAddress) – Cell address

  • cell_vals (CellValues) – Cell Values

  • cell_name (str) – Cell name

Returns:

True if instance contains cell; Otherwise, False.

Return type:

bool

Note

If cell input contains sheet info the it is use in comparison. Otherwise sheet is ignored.

static from_range(range_val: str) ooodev.utils.data_type.range_obj.RangeObj[source]
static from_range(range_val: ooodev.utils.data_type.range_values.RangeValues) ooodev.utils.data_type.range_obj.RangeObj
static from_range(range_val: ooo.lo.table.cell_range_address.CellRangeAddress) ooodev.utils.data_type.range_obj.RangeObj
static from_range(range_val)

Gets a RangeObj from are range name.

Parameters:
  • range_name (str) – Range name such as A1:D34.

  • range_val (str | mRngValues.RangeValues | CellRangeAddress) –

Returns:

Object that represents the name range.

Return type:

RangeObj

get_cell_range_address()[source]

Gets a Cell Range Address

Returns:

Cell range Address

Return type:

CellRangeAddress

get_cells()[source]

Get a generator that can loop over all cells within current range.

Returns:

Nested Generator that returns CellObj for each cell in current range.

Return type:

Generator[Generator[CellObj, None, None], None, None]

Example

>>> rng = RangeObj.from_range("A1:C3")
>>> for row, cells in enumerate(rng.get_cells()):
>>>     print("Row:", row)
>>>     for cell in cells:
>>>         print(f"  {cell}: {repr(cell)}")
>>> print("Done")
Row: 0
  A1: CellObj(col='A', row=1, sheet_idx=0)
  B1: CellObj(col='B', row=1, sheet_idx=0)
  C1: CellObj(col='C', row=1, sheet_idx=0)
Row: 1
  A2: CellObj(col='A', row=2, sheet_idx=0)
  B2: CellObj(col='B', row=2, sheet_idx=0)
  C2: CellObj(col='C', row=2, sheet_idx=0)
Row: 2
  A3: CellObj(col='A', row=3, sheet_idx=0)
  B3: CellObj(col='B', row=3, sheet_idx=0)
  C3: CellObj(col='C', row=3, sheet_idx=0)
Done
get_col(col: int) ooodev.utils.data_type.range_obj.RangeObj[source]
get_col(col: str) ooodev.utils.data_type.range_obj.RangeObj
get_col(col)

Gets a Range object that represents only the column range.

Parameters:

col (int | str) – Zero-based column index or column letter such as A.

Raises:

IndexError – If column index is out of range.

Returns:

Range Object

Return type:

RangeObj

New in version 0.15.1.

get_end_col()[source]

Gets a Range object that represents only the end column range.

Returns:

Range Object

Return type:

RangeObj

get_end_row()[source]

Gets a Range object that represents only the end row range.

Returns:

Range Object

Return type:

RangeObj

get_range_values()[source]

Gets RangeValues

Returns:

Range Values.

Return type:

RangeValues

get_row(row)[source]

Gets a Range object that represents a row in range.

Parameters:

row (int) – Zero-based row index.

Raises:

IndexError – If row index is out of range.

Returns:

Range Object

Return type:

RangeObj

New in version 0.15.1.

get_start_col()[source]

Gets a Range object that represents only the start column range.

Returns:

Range Object

Return type:

RangeObj

get_start_row()[source]

Gets a Range object that represents only the start row range.

Returns:

Range Object

Return type:

RangeObj

is_single_cell()[source]

Gets if a instance is a single cell or a range

Returns:

True if single cell; Otherwise, False

Return type:

bool

is_single_col()[source]

Gets if instance is a single column or multi-column

Returns:

True if single column; Otherwise, False

Return type:

bool

Note

If instance is a single cell address then True is returned.

is_single_row()[source]

Gets if instance is a single row or multi-row

Returns:

True if single row; Otherwise, False

Return type:

bool

Note

If instance is a single cell address then True is returned.

set_sheet_index(idx=None)[source]

Set the sheet index for the range.

If idx is None then the active sheet index is used.

Setting the sheet index to -2 will cause the sheet name to always return an empty string.

Changing the sheet index will cause the sheet name to be re-evaluated.

Parameters:

idx (int, optional) – Sheet index, Default None.

Returns:

Self

Return type:

RangeObj

New in version 0.32.0.

to_string(include_sheet_name=False)[source]

Get a string representation of range

Parameters:

include_sheet_name (bool, optional) – If True and there is a sheet name then it is included in format of Sheet1.A2.G3; Otherwise format of A2:G3.Defaults to False.

Returns:

Gets a string representation such as A1:T12/

Return type:

str

property cell_count: int

Gets the number of cell in the current range

Returns:

Number of cells

Return type:

int

property cell_end: CellObj

Gets the End Cell object for Range

Return type:

CellObj

property cell_start: CellObj

Gets the Start Cell object for Range

Return type:

CellObj

property col_count: int

Gets the number of columns in the current range

Returns:

Number of columns

Return type:

int

col_end: str

Column end such as C

col_start: str

Column start such as A

end: CellObj

End Cell Object

property end_col_index: int

Gets end column zero-based index

Return type:

int

property end_row_index: int

Gets end row zero-based index

Return type:

int

property row_count: int

Gets the number of rows in the current range

Returns:

Number of rows

Return type:

int

row_end: int

Row end such as 125

row_start: int

Row start such as 1

sheet_idx: int = -1

Sheet index that this cell value belongs to. If value is -1 then the active spreadsheet, if available, is used to get the sheet index. If the value is -2 then no sheet index is applied and sheet name will always return and empty string.

property sheet_name: str

Gets sheet name

Return type:

str

start: CellObj

Start Cell Object

property start_col_index: int

Gets start column zero-based index

Return type:

int

property start_row_index: int

Gets start row zero-based index

Return type:

int