
    ^b29                         d dl m Z  d dlmZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ  G d de          Z G d d	e          Zd
S )    )copy)Strict)MinMaxSequence)Serialisable)range_boundariesrange_to_tupleget_column_letterquote_sheetnamec                      e Zd ZdZ edde          Z edde          Z edde          Z edde          Z		 	 d&dZ
ed             Zed	             Zed
             Zed             Zed             Zd Zd Zd Zd Zd'dZd Zd Zd ZeZd Zd Zd ZeZd Zd Zd Z d Z!e!Z"d Z#e#Z$d Z%d(dZ&d(d Z'ed!             Z(ed"             Z)ed#             Z*ed$             Z+ed%             Z,dS ))	CellRangea  
    Represents a range in a sheet: title and coordinates.

    This object is used to perform operations on ranges, like:

    - shift, expand or shrink
    - union/intersection with another sheet range,

    We can check whether a range is:

    - equal or not equal to another,
    - disjoint of another,
    - contained in another.

    We can get:

    - the size of a range.
    - the range bounds (vertices)
    - the coordinates,
    - the string representation,

       ifG  )minmaxexpected_typei   Nc                 `   |0d|v rt          |          \  }\  }}}}nt          |          \  }}}}|| _        || _        || _        || _        || _        ||k    r&d}t          |                    ||                    ||k    r&d}t          |                    ||                    d S )N!z({max_col} must be greater than {min_col})min_colmax_colz({max_row} must be greater than {min_row})min_rowmax_row)	r	   r   r   r   r   r   title
ValueErrorformat)selfrange_stringr   r   r   r   r   fmts           =lib/python3.11/site-packages/openpyxl/worksheet/cell_range.py__init__zCellRange.__init__/   s     	Tl" T>L\>Z>Z;;'775El5S5S2'7
W 	K<CSZZZIIJJJW 	K<CSZZZIIJJJ	K 	K    c                 6    | j         | j        | j        | j        fS )z2
        Vertices of the range as a tuple
        r   r   r   r   r   s    r   boundszCellRange.boundsE   s    
 |T\4<EEr    c                     d}| j         | j        k    r| j        | j        k    rd}|                    t          | j                   | j        t          | j                  | j                  S )z9
        Excel-style representation of the range
        z%{min_col}{min_row}:{max_col}{max_row}z{min_col}{min_row}r"   )r   r   r   r   r   r
   r   r   s     r   coordzCellRange.coordM   so    
 6LDL( 	',	'&Czz%dl33L%dl33L	  
 
 	
r    c              #      K   t          | j        | j        dz             D ]-fdt          | j        | j        dz             D             V  .dS )z1
        Return cell coordinates as rows
        r   c                     g | ]}|fS  r*   ).0colrows     r   
<listcomp>z"CellRange.rows.<locals>.<listcomp>d       MMM#C:MMMr    N)ranger   r   r   r   )r   r-   s    @r   rowszCellRange.rows^   m      
 t|A~66 	N 	NCMMMMt|T\!^)L)LMMMMMMM	N 	Nr    c              #      K   t          | j        | j        dz             D ]-fdt          | j        | j        dz             D             V  .dS )z4
        Return cell coordinates as columns
        r   c                     g | ]}|fS r*   r*   )r+   r-   r,   s     r   r.   z"CellRange.cols.<locals>.<listcomp>m   r/   r    N)r0   r   r   r   r   )r   r,   s    @r   colszCellRange.colsg   r2   r    c                     ddl m}  |t          | j        | j        dz             t          | j        | j        dz                       S )Nr   )productr   )	itertoolsr7   r0   r   r   r   r   )r   r7   s     r   cellszCellRange.cellsp   sN    %%%%%%wuT\4<>::E$,PTP\]^P^<_<_```r    c                     t          |t                    s)t          t          t	          |                              |j        r| j        |j        k    rt          d          dS dS )z
        Check whether comparisons between ranges are possible.
        Cannot compare ranges from different worksheets
        Skip if the range passed in has no title.
        z1Cannot work with ranges from different worksheetsN)
isinstancer   	TypeErrorreprtyper   r   r   others     r   _check_titlezCellRange._check_titlev   su     %++ 	/De--...; 	R4:4 	RPQQQ	R 	R 	R 	Rr    c                 p    d}| j         rd}|                    | j        j        | j         | j                  S )Nz<{cls} {coord}>z<{cls} {title!r}!{coord}>)clsr   r'   )r   r   	__class____name__r'   r&   s     r   __repr__zCellRange.__repr__   s7     : 	/.Czzdn5TZtzzZZZr    c                 r    d}| j         }|rd}t          |          }|                    || j                  S )Nz{coord}z{title}!{coord})r   r'   )r   r   r   r'   )r   r   r   s      r   __str__zCellRange.__str__   s@    
 	+$C#E**EzzTZz888r    c                 h    |                      | j        | j        | j        | j        | j                  S )Nr   r   r   r   r   )rD   r   r   r   r   r   r#   s    r   __copy__zCellRange.__copy__   s3    ~~dlDL&*lDL$(J  0 0 	0r    r   c                    | j         |z   dk    s| j        |z   dk    r#t          d                    ||                    | xj         |z  c_         | xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS )a{  
        Shift the focus of the range according to the shift values (*col_shift*, *row_shift*).

        :type col_shift: int
        :param col_shift: number of columns to be moved by, can be negative
        :type row_shift: int
        :param row_shift: number of rows to be moved by, can be negative
        :raise: :class:`ValueError` if any row or column index < 1
        r   z1Invalid shift value: col_shift={0}, row_shift={1}N)r   r   r   r   r   r   )r   	col_shift	row_shifts      r   shiftzCellRange.shift   s     L9$) 	o|i'1,	oPWWXaclmmnnn	!	!	!	!r    c                     	 |                      |           n# t          $ r Y dS w xY w|j        | j        k    p/| j        |j        k    p|j        | j        k    p| j        |j        k    S )z
        Test whether the ranges are not equal.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* != *other*.
        T)rA   r   r   r   r   r   r?   s     r   __ne__zCellRange.__ne__   s    	e$$$$ 	 	 	44	 MT\) -|u},-},- |u},		
s    
&&c                 .    |                      |           S )z
        Test whether the ranges are equal.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* == *other*.
        )rQ   r?   s     r   __eq__zCellRange.__eq__   s     ;;u%%%%r    c                 V    |                      |           |                    |           S )z
        Test whether every cell in this range is also in *other*.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* <= *other*.
        rA   _CellRange__supersetr?   s     r   issubsetzCellRange.issubset   s,     	%   %%%r    c                 V    |                      |          o|                     |          S )z
        Test whether *other* contains every cell of this range, and more.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* < *other*.
        )__le__rQ   r?   s     r   __lt__zCellRange.__lt__   '     {{5!!8dkk%&8&88r    c                     | j         |j         cxk    o|j        cxk    o
| j        k    nc o*| j        |j        cxk    o|j        cxk    o
| j        k    nc S N)r   r   r   r   r?   s     r   
__supersetzCellRange.__superset   sw    \U]KKKKemKKKKt|KKKK M\U]KKKKemKKKKt|KKKK	
r    c                 V    |                      |           |                     |          S )z
        Test whether every cell in *other* is in this range.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* >= *other* (or *other* in *range*).
        rU   r?   s     r   
issupersetzCellRange.issuperset   s*     	%   u%%%r    c                 V    |                      |          }|                     |          S )zO
        Check whether the range contains a particular cell coordinate
        )rD   rV   r   r'   crs      r   __contains__zCellRange.__contains__   s'     ^^E""r"""r    c                 V    |                      |          o|                     |          S )z
        Test whether this range contains every cell in *other*, and more.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range
        :return: ``True`` if *range* > *other*.
        )__ge__rQ   r?   s     r   __gt__zCellRange.__gt__  r[   r    c                     |                      |           | j        |j        k    r|| }} | j        |j        k     p| j        |j        k     p|j        | j        k     S )aV  
        Return ``True`` if this range has no cell in common with *other*.
        Ranges are disjoint if and only if their intersection is the empty range.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range.
        :return: ``True`` if the range has no cells in common with other.
        )rA   r$   r   r   r   r   r?   s     r   
isdisjointzCellRange.isdisjoint  sg     	%    ;% 	&%Du}, 0<%-/0=4</	1r    c                 h   |                      |          r#t          d                    | |                    t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          ||||          S )aN  
        Return a new range with cells common to this range and *other*

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range.
        :return: the intersecting sheet range.
        :raise: :class:`ValueError` if the *other* range doesn't intersect
            with this range.
        zRange {0} doesn't intersect {0}r"   )
ri   r   r   r   r   r   r   r   r   r   r   r@   r   r   r   r   s         r   intersectionzCellRange.intersection%  s     ??5!! 	T>EEdERRSSSdlEM22dlEM22dlEM22dlEM22'7!(* * * 	*r    c                 .   |                      |           t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          | j        |j                  }t          ||||| j                  S )a  
        Return the minimal superset of this range and *other*. This new range
        will contain all cells from this range, *other*, and any additional
        cells required to form a rectangular ``CellRange``.

        :type other: openpyxl.worksheet.cell_range.CellRange
        :param other: Other sheet range.
        :return: a ``CellRange`` that is a superset of this and *other*.
        rJ   )	rA   r   r   r   r   r   r   r   r   rk   s         r   unionzCellRange.union=  s     	%   dlEM22dlEM22dlEM22dlEM22'7!(
< < < 	<r    c              #   X   K   | j         D ]}|dk    r	t          | |          }||fV   dS )zC
        For use as a dictionary elsewhere in the library.
        r   N)	__attrs__getattr)r   xvs      r   __iter__zCellRange.__iter__S  sP        	 	AG| a  AQ$JJJJ		 	r    c                     | xj         |z  c_         | xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS )a  
        Expand the range by the dimensions provided.

        :type right: int
        :param right: expand range to the right by this number of cells
        :type down: int
        :param down: expand range down by this number of cells
        :type left: int
        :param left: expand range to the left by this number of cells
        :type up: int
        :param up: expand range up by this number of cells
        Nr"   )r   rightdownleftups        r   expandzCellRange.expand^  sH     	r    c                     | xj         |z  c_         | xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS )a  
        Shrink the range by the dimensions provided.

        :type right: int
        :param right: shrink range from the right by this number of cells
        :type down: int
        :param down: shrink range from the top by this number of cells
        :type left: int
        :param left: shrink range from the left by this number of cells
        :type up: int
        :param up: shrink range from the bottown by this number of cells
        Nr"   )r   rv   bottomrx   tops        r   shrinkzCellRange.shrinkq  sH     	r    c                 T    | j         dz   | j        z
  }| j        dz   | j        z
  }||dS )zC Return the size of the range as a dictionary of rows and columns. r   )columnsr1   )r   r   r   r   )r   r5   r1   s      r   sizezCellRange.size  s8     |a$,.|a$,.t,,,r    c                 V      fdt           j         j        dz             D             S )z=A list of cell coordinates that comprise the top of the rangec                 "    g | ]}j         |fS r*   )r   r+   r,   r   s     r   r.   z!CellRange.top.<locals>.<listcomp>       SSSs#SSSr    r   r0   r   r   r#   s   `r   r}   zCellRange.top  3     TSSSuT\4<PQ>/R/RSSSSr    c                 V      fdt           j         j        dz             D             S )z@A list of cell coordinates that comprise the bottom of the rangec                 "    g | ]}j         |fS r*   )r   r   s     r   r.   z$CellRange.bottom.<locals>.<listcomp>  r   r    r   r   r#   s   `r   r|   zCellRange.bottom  r   r    c                 V      fdt           j         j        dz             D             S )zCA list of cell coordinates that comprise the left-side of the rangec                 "    g | ]}|j         fS r*   )r   r+   r-   r   s     r   r.   z"CellRange.left.<locals>.<listcomp>       SSSdl#SSSr    r   r0   r   r   r#   s   `r   rx   zCellRange.left  r   r    c                 V      fdt           j         j        dz             D             S )zDA list of cell coordinates that comprise the right-side of the rangec                 "    g | ]}|j         fS r*   )r   r   s     r   r.   z#CellRange.right.<locals>.<listcomp>  r   r    r   r   r#   s   `r   rv   zCellRange.right  r   r    )NNNNNN)r   r   )r   r   r   r   )-rE   
__module____qualname____doc__r   intr   r   r   r   r   propertyr$   r'   r1   r5   r9   rA   rF   rH   rK   rO   rQ   rS   rW   rY   rZ   rV   r`   rf   rd   rg   ri   rl   __and__rn   __or__rt   rz   r~   r   r}   r|   rx   rv   r*   r    r   r   r      s        . fS999Gfs;;;GfS999Gfs;;;G AE37K K K K, F F XF 
 
 X
  N N XN N N XN a a Xa

R 
R 
R[ [ [9 9 90 0 0" " " "(
 
 
*& & &
& 
& 
& F9 9 9
 
 

& 
& 
& F# # #9 9 91 1 1(* * ** G< < <& F     &   & - - X- T T XT
 T T XT
 T T XT
 T T XT T Tr    r   c                   t    e Zd Z ee          ZddZd Zd Zd Z	e	Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd ZdS )MultiCellRange)r   r*   c                 z    t          |t                    rd |                                D             }|| _        d S )Nc                 ,    g | ]}t          |          S r*   )r   r+   rs     r   r.   z+MultiCellRange.__init__.<locals>.<listcomp>  s    ;;;qill;;;r    )r;   strsplitrangesr   r   s     r   r   zMultiCellRange.__init__  s:    fc"" 	<;;FLLNN;;;Fr    c                 t    t          |t                    rt          |          }| j        D ]}||k    r dS dS )NTF)r;   r   r   r   )r   r'   r   s      r   rd   zMultiCellRange.__contains__  sO    eS!! 	%e$$E 	 	Az ttur    c                     d                     d | j        D                       }d                    | j        j        |          S )N c                 ,    g | ]}t          |          S r*   r   r   s     r   r.   z+MultiCellRange.__repr__.<locals>.<listcomp>  s    777a3q66777r    z<{0} [{1}]>)joinr   r   rD   rE   r   s     r   rF   zMultiCellRange.__repr__  s?    774;77788##DN$;VDDDr    c                 N    d                     d | j        D                       }|S )Nr   c                 ,    g | ]}t          |          S r*   r   r   s     r   r.   z*MultiCellRange.__str__.<locals>.<listcomp>  s    888qCFF888r    )r   r   r   s     r   rH   zMultiCellRange.__str__  s)    88DK88899r    c                     |}t          |t                    rt          |          }n$t          |t                    st          d          || vr| j                            |           dS dS )z4
        Add a cell coordinate or CellRange
        zYou can only add CellRangesN)r;   r   r   r   r   appendrb   s      r   addzMultiCellRange.add  s}     eS!! 	<5!!BBE9-- 	<:;;;T> 	#Kr"""""	# 	#r    c                 0    |                      |           | S r]   )r   r   r'   s     r   __iadd__zMultiCellRange.__iadd__  s    r    c                 v    t          |t                    r|                     |          }| j        |j        k    S r]   )r;   r   rD   r   r?   s     r   rS   zMultiCellRange.__eq__  s4    uc"" 	*NN5))E{el**r    c                     | |k     S r]   r*   r?   s     r   rQ   zMultiCellRange.__ne__  s    5=  r    c                 *    t          | j                  S r]   )boolr   r#   s    r   __bool__zMultiCellRange.__bool__  s    DK   r    c                     t          |t                    st          |          }| j                            |           d S r]   )r;   r   r   remover   s     r   r   zMultiCellRange.remove  s=    %++ 	%e$$E5!!!!!r    c              #   &   K   | j         D ]}|V  d S r]   )r   )r   rc   s     r   rt   zMultiCellRange.__iter__  s,      + 	 	BHHHH	 	r    c                     t                      }| j        D ])}|j                            t          |                     *|S r]   )r   r   r   r   )r   nr   s      r   rK   zMultiCellRange.__copy__  s@     	% 	%AHOODGG$$$$r    N)r*   )rE   r   r   r   r   r   r   rd   rF   rH   r   r   rS   rQ   r   r   rt   rK   r*   r    r   r   r     s         XI...F     E E E
   G
# 
# 
#  
+ + +! ! !! ! !" " "  
    r    r   N)r   openpyxl.descriptorsr   r   r   !openpyxl.descriptors.serialisabler   openpyxl.utilsr   r	   r
   r   r   r   r*   r    r   <module>r      s
         ' ' ' ' ' ' 1 1 1 1 1 1 1 1 : : : : : :           PT PT PT PT PT PT PT PTfQ Q Q Q QV Q Q Q Q Qr    