Cell Objects
Working with the ooodev.utils.data_type.cell_obj.CellObj
class.
Comparison
Comparison is done using sheet index, row and then column.
If the sheet indexes for the two CellObj
instances less than 0
then they are ignored when comparing.
Rules
==
,!=
,>
,>=
,<
and<=
comparisons are supportedCellObj
can be compared toCellObj
CellObj
can be compared tostr
Example
>>> from ooodev.utils.data_type.cell_obj import CellObj
>>>
>>> b2 = CellObj.from_cell("B2")
>>> b2
CellObj(col='B', row=2, sheet_idx=-1)
>>> b4 = CellObj("b", 4)
>>> b4 == b2
False
>>> b4 != b2
True
>>> b4 == "b4"
True
>>> b4 == "B4"
True
>>> b4 > b2
True
>>> b2 < b4
True
>>> c2 = CellObj.from_cell("C2")
>>> c2 > b2
True
Right, Left, Up, Down
Rules
Going out of range results in
IndexError
Example
>>> from ooodev.utils.data_type.cell_obj import CellObj
>>>
>>> b4 = CellObj("B", 4, 0)
>>> b4.right # gets cell to the right
CellObj(col='C', row=4, sheet_idx=0)
>>> b4.left # get cell to the left
CellObj(col='A', row=4, sheet_idx=0)
>>> b4.up # get cell above
CellObj(col='B', row=3, sheet_idx=0)
>>> b4.down # get cell below
CellObj(col='B', row=5, sheet_idx=0)
>>> b4.down.right
CellObj(col='C', row=5, sheet_idx=0)
>>> b4.down.right.right
CellObj(col='D', row=5, sheet_idx=0)
>>> b4.left.left # out of range
IndexError
Addition and Subtraction
Rules
Adding an
int
toCellObj
gets the cell to the down by theint
amountSubtracting an
int
fromCellObj
gets the cell to the up by theint
amountAdding a
str
(column) toCellObj
gets the cell to the right by thestr
amountSubtracting a
str
(column) fromCellObj
gets the cell to the left by thestr
amountRowObj
andColObj
can be added and subtractedCellObj
can be added and subtractedGoing out of range results in
IndexError
Example
>>> from ooodev.utils.data_type.cell_obj import CellObj
>>>
>>> b4 = CellObj("B", 4, 0)
>>> c6 = CellObj("C", 6, 0)
>>> b4 + 3
CellObj(col='B', row=7, sheet_idx=0)
>>> b4 - 3
CellObj(col='B', row=1, sheet_idx=0)
>>> b4 + "C" # get E4 by adding 3 col
CellObj(col='E', row=4, sheet_idx=0)
>>> b4 - "A" # get A4 by subtracting 1 col
CellObj(col='A', row=4, sheet_idx=0)
>>> b4 + (b4.row_obj + 5) # get B9
CellObj(col='B', row=9, sheet_idx=0)
>>> b4 + b4.row_obj.next # same as b4.down
CellObj(col='B', row=5, sheet_idx=0)
>>> b4 + (b4.col_obj + 5) # get G4
CellObj(col='G', row=4, sheet_idx=0)
>>> b4 + b4.col_obj.next # same as b4.right
CellObj(col='C', row=4, sheet_idx=0)
>>> b4.right
CellObj(col='C', row=4, sheet_idx=0)
>>> b4 - b4.col_obj.prev # same as b4.left
CellObj(col='A', row=4, sheet_idx=0)
>>> b4.left
CellObj(col='A', row=4, sheet_idx=0)
>>> b4 + c6 # get E10 add 3 col and 6 row to b4
CellObj(col='E', row=10, sheet_idx=0)
>>> c6 - b4 # get A2 subtract 2 col and 4 row from c6
CellObj(col='A', row=2, sheet_idx=0)
>>> b4 - (b4.col_obj - 2)
IndexError
Multiply and Divide
Rules
Multiplying an
int
toCellObj
gets the cell to the downDividing an
int
fromCellObj
gets the cell to the upMultiplying a
str
(column) toCellObj
gets the cell to the rightDividing a
str
(column) fromCellObj
gets the cell to the leftRowObj
andColObj
can be multiplied and dividedCellObj
can be multiplied and dividedIf
CellObj
division results in a fraction then rounding is used (9 / 4 = 2)Going out of range results in
IndexError
Example
>>> from ooodev.utils.data_type.cell_obj import CellObj
>>>
>>> f10 = CellObj("F", 10, 0)
>>> b4 = CellObj("B", 4, 0)
>>> b2 = CellObj("B", 2, 0)
>>> f10 * 3 # multiply row by 3
CellObj(col='F', row=30, sheet_idx=0)
>>> f10 / 2 # divide row by 2
CellObj(col='F', row=5, sheet_idx=0)
>>> f10 * "C" # multiply col by 3
CellObj(col='R', row=10, sheet_idx=0)
>>> f10 / "B" # divided col by 2
CellObj(col='C', row=10, sheet_idx=0)
>>> f10 * (f10.row_obj * 10) # times 10 rows
CellObj(col='F', row=100, sheet_idx=0)
>>> f10 * (f10.col_obj * 10) # times 10 cols
CellObj(col='BH', row=10, sheet_idx=0)
>>> f10 / (f10.row_obj / 2) # get F5
CellObj(col='F', row=5, sheet_idx=0)
>>> f10 / (f10.col_obj / 2) # get C10
CellObj(col='C', row=10, sheet_idx=0)
>>> b4 * f10 # b(2) X f(6), 4 X 10
CellObj(col='L', row=40, sheet_idx=0)
>>> f10 / b2 # f(6) / b(2), 10 / 2
CellObj(col='C', row=5, sheet_idx=0)
>>> f10 / b4 # f(6) / b(4), 10 / 4, Rounding is used
CellObj(col='C', row=2, sheet_idx=0)
>>> b2 / f10
IndexError