
    Mwg7                        d Z ddlmZ ddlmZmZ ddlmZmZ ddl	Z	ddl
mZ  e	j                  d      Zd	Zd
ZdZ e	j                  dez   dz   e	j"                        ZdZ e	j                  dj)                  ee      e	j"                        Zd Zd Zd Zdg ee      z   Z ed      d        Z eed      D  ci c]  \  } }|| 
 c}} ZdZ ed      d        Zd Z d Z!d Z"d Z#d Z$d Z%yc c}} w )zT
Collection of utilities used within the package and also available for client code
    )	lru_cache)chainproduct)ascii_uppercasedigitsN   )CellCoordinatesExceptionz^[$]?([A-Za-z]{1,3})[$]?(\d+)$z[A-Z]{1,3}:[A-Z]{1,3}:z\d+:\d+:zq
[$]?(?P<min_col>[A-Za-z]{1,3})?
[$]?(?P<min_row>\d+)?
(:[$]?(?P<max_col>[A-Za-z]{1,3})?
[$]?(?P<max_row>\d+)?)?
^$z7
(('(?P<quoted>([^']|'')*)')|(?P<notquoted>[^'^ ^!]*))!z{0}(?P<cells>{1})(?=,?)c                     t        | t              rt        |       } t        |t              rt        |      }t        | |dz         D cg c]  }t	        |       c}S c c}w )z
    Given the start and end columns, return all the columns in the series.

    The start and end columns can be either column letters or 1-based
    indexes.
    r   )
isinstancestrcolumn_index_from_stringrangeget_column_letter)startendxs      J/var/www/horilla/myenv/lib/python3.12/site-packages/openpyxl/utils/cell.pyget_column_intervalr      sQ     %(/#s&s+*/sQw*?@Qa @@@s   Ac                     t         j                  |       }|sd|  d}t        |      |j                         \  }}t	        |      }|sd|  d}t        |      ||fS )z;Convert a coordinate string like 'B12' to a tuple ('B', 12)zInvalid cell coordinates ()zThere is no row 0 ()COORD_REmatchr	   groupsint)coord_stringr   msgcolumnrows        r   coordinate_from_stringr!   ,   sj    NN<(E*<.:&s++,,.KFC
c(C#L>3&s++3;    c                     t         j                  |       }|st        |  d      |j                  d      }|j	                         D ]  \  }}|s	d| ||<    |d   s|d   rd}nd} |j
                  di |S )	zDConvert a coordinate to an absolute coordinate string (B12 -> $B$12)z  is not a valid coordinate range r   max_colmax_rowz%{min_col}{min_row}:{max_col}{max_row}z{min_col}{min_row} )ABSOLUTE_REr   
ValueError	groupdictitemsformat)r   mdkvfmts         r   absolute_coordinater2   :   s    ,'AL>)IJKK	BA	 1qc7AaD 	|q|5"3::??r"   r$   )maxsizec                    d| cxk  rdk  sn t        dj                  |             g }| dk  r	t        |    S | rDt        | d      \  } }|j	                  dt        |          |s| dz  } |j	                  dd       | rDdj                  |      S )a  
    Convert decimal column position to its ASCII (base 26) form.

    Because column indices are 1-based, strides are actually pow(26, n) + 26
    Hence, a correction is applied between pow(26, n) and pow(26, 2) + 26 to
    prevent and additional column letter being prepended

    "A" == 1 == pow(26, 0)
    "Z" == 26 == pow(26, 0) + 26 // decimal equivalent 10
    "AA" == 27 == pow(26, 1) + 1
    "ZZ" == 702 == pow(26, 2) + 26 // decimal equivalent 100
    r   ifG  zInvalid column index {0}   r   Zr$   )r)   r,   __decimal_to_alphadivmodinsertjoin)col_idxresult	remainders      r   r   r   N   s      5 3::7CDDF|!'**
#GR0a+I67qLGMM!S!  776?r"   )r   r5   i  c                 B   d|  d}t        |       dkD  rt        |      d}t        | j                               } t	        | t
              D ]  \  }}	 t        |   }|||z  z  } d|cxk  rdk  st        |       t        |      |S # t        $ r t        |      w xY w)z
    Convert ASCII column name (base 26) to decimal with 1-based index

    Characters represent descending multiples of powers of 26

    "AFZ" == 26 * pow(26, 0) + 6 * pow(26, 1) + 1 * pow(26, 2)
    'z<' is not a valid column name. Column names are from A to ZZZ   r   igG  )lenr)   reversedupperzip__powers__alpha_to_decimalKeyError)col	error_msgidxletterpowerposs         r   r   r   r   s     C5TUI
3x!|##
C
399;
CS(+ 	($V,C 	sU{ s?U?## ##J  	(Y''	(s   	B		Bc                    dj                  |       }t        j                  |       }|st        |      |j	                         \  }}}}}|rM||f}||f}	t        ||	z         s7t        |      rt        |	      r!t        |	      rt        |      rt        |      |t        |      }|t        |      }|t        |      }n|}|t        |      }n|}||||fS )z
    Convert a range string into a tuple of boundaries:
    (min_col, min_row, max_col, max_row)
    Cell coordinates will be converted into a range with the cell at both end
    z&{0} is not a valid coordinate or range)	r,   r(   r   r)   r   allanyr   r   )
range_stringr   r-   min_colmin_rowsepr%   r&   colsrowss
             r   range_boundariesrW      s     3
9
9,
GC,'Ao./hhj+GWc7G
 tIc$iIc$iS/!*73g,*73g,GWg--r"   c              #      K   t        |       \  }}}}t        ||dz         }t        ||dz         D cg c]  }t        |       }}|D ]  t        fd|D                yc c}w w)[
    Get individual addresses for every cell in a range.
    Yields one row at a time.
    r   c              3   B   K   | ]  }d j                  |        ywz{0}{1}Nr,   ).0rH   r    s     r   	<genexpr>z"rows_from_range.<locals>.<genexpr>        >#HOOC->   N)rW   r   r   tuple)	rQ   rR   rS   r%   r&   rV   rH   rU   r    s	           @r   rows_from_rangerb      sv     
 *:,)G&GWgw'A+&D.3GWq[.IJsc"JDJ ?>>>>? Ks   1A+A&%A+c              #      K   t        |       \  }}}}t        ||dz         }d t        ||dz         D        }|D ]  t        fd|D                yw)rY   r   c              3   2   K   | ]  }t        |        y w)N)r   )r]   rH   s     r   r^   z"cols_from_range.<locals>.<genexpr>   s     Hsc"Hs   c              3   B   K   | ]  }d j                  |        ywr[   r\   )r]   r    rH   s     r   r^   z"cols_from_range.<locals>.<genexpr>   r_   r`   N)rW   r   ra   )rQ   rR   rS   r%   r&   rV   rU   rH   s          @r   cols_from_rangerf      sa     
 *:,)G&GWgw'!)$DHeGWQY.GHD ?>>>>?s   AAc                 |    t        |       D ]  \  }}|t        v s n | d }| |d }t        |      t        |      fS )zB
    Convert an Excel style coordinate to (row, column) tuple
    N)	enumerater   r   r   )
coordinaterJ   crH   r    s        r   coordinate_to_tuplerk      sU     J' Q; Tc
C
ST
Cs8-c222r"   c                     t         j                  |       }|t        d      |j                  d      xs |j                  d      }|j                  d      }t	        |      }||fS )zc
    Convert a worksheet range to the sheetname and maximum and minimum
    coordinate indices
    z)Value must be of the form sheetname!A1:E4quoted	notquotedcells)SHEETRANGE_REr   r)   grouprW   )rQ   r-   	sheetnamero   
boundariess        r   range_to_tuplert      sd    
 	L)AyDEE!9QWW[%9IGGGE!%(Jj  r"   c                 T    d| v r| j                  dd      } dj                  |       } | S )z>
    Add quotes around sheetnames if they contain spaces.
    r?   z''z'{0}')replacer,   )rr   s    r   quote_sheetnamerw      s2     i%%c40		*Ir"   )&__doc__	functoolsr   	itertoolsr   r   stringr   r   re
exceptionsr	   compiler   	COL_RANGE	ROW_RANGE
RANGE_EXPRVERBOSEr(   SHEET_TITLEr,   rp   r   r!   r2   listr7   r   rh   rF   rE   r   rW   rb   rf   rk   rt   rw   )rM   rK   s   00r   <module>r      s2     $ * 	 0 2::78(		
 bjjz)3.

;:

8?? jj*A$ TD11 
4 @ 5>oq4QR[S&fSjR 
4 0(.V	?	?	3!s Ss   =C2