
    Nqh                        d Z ddlZddlm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 Zi Zi Z edd      D ]  Z ee      Zeee<   eee<    d Zd Zd Zd Zd Zd Zd Zd Z y)zT
Collection of utilities used within the package and also available for client code
    N)digits   )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      T/srv/www/ollama/chat-ollama/venv/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_coordinater.   8   s    ,'AL>)IJKK	BA	 1qc7AaD 	|q|5"3::??r   c                    d| cxk  rdk  sn t        dj                  |             g }| dkD  r>t        | d      \  } }|dk(  rd}| dz  } |j                  t	        |dz                | dkD  r>dj                  t        |            S )zConvert a column number into a column letter (3 -> 'C')

    Right shift the column col_idx by 26 to find column letters in reverse
    order.  These numbers are 1-based, and can be converted to ASCII
    ordinals by adding 64.

    r   ifG  Invalid column index {0}r      @   r    )r%   r(   divmodappendchrjoinreversed)col_idxletters	remainders      r   _get_column_letterr;   J   s      5 3::7CDDG
A+#GR0>IqLGs9R<() A+ 778G$%%r   igG  c                 d    	 t         |    S # t        $ r t        dj                  |             w xY w)z?Convert a column index into a column letter
    (3 -> 'C')
    r0   )_STRING_COL_CACHEKeyErrorr%   r(   )idxs    r   r   r   i   s;    A %% A3::3?@@As    $/c                     	 t         | j                            S # t        $ r t        dj	                  |             w xY w)z@Convert a column name into a numerical index
    ('A' -> 1)
    z{0} is not a valid column name)_COL_STRING_CACHEupperr>   r%   r(   )str_cols    r   r   r   s   sA    
K 11 K9@@IJJKs    $=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_boundariesrM   ~   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(   ).0colr   s     r   	<genexpr>z"rows_from_range.<locals>.<genexpr>        >#HOOC->   N)rM   r   r   tuple)	rG   rH   rI   r!   r"   rL   rT   rK   r   s	           @r   rows_from_rangerY      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)rO   r   c              3   2   K   | ]  }t        |        y w)N)r   )rS   rT   s     r   rU   z"cols_from_range.<locals>.<genexpr>   s     Hsc"Hs   c              3   B   K   | ]  }d j                  |        ywrQ   rR   )rS   r   rT   s     r   rU   z"cols_from_range.<locals>.<genexpr>   rV   rW   N)rM   r   rX   )rG   rH   rI   r!   r"   rL   rK   rT   s          @r   cols_from_ranger]      sa     
 *:,)G&GWgw'!)$DHeGWQY.GHD ?>>>>?s   AAc                     t        |       D ]  \  }}|t        v s n | d j                         }| |d }t        |      t        |   fS )zB
    Convert an Excel style coordinate to (row, column) tuple
    N)	enumerater   rB   r   rA   )
coordinater?   crT   r   s        r   coordinate_to_tuplerb      s^     J' Q; Tc

 
 
"C
ST
Cs8&s+++r   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%   grouprM   )rG   r)   	sheetnamerf   
boundariess        r   range_to_tuplerk      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.
    'z''z'{0}')replacer(   )ri   s    r   quote_sheetnamero      s2     i%%c40		*Ir   )!__doc__restringr   
exceptionsr   compiler   	COL_RANGE	ROW_RANGE
RANGE_EXPRVERBOSEr$   SHEET_TITLEr(   rg   r   r   r.   r;   rA   r=   r   irT   r   r   rM   rY   r]   rb   rk   ro   r#   r   r   <module>r{      s   
  0 2::78(		
 bjjz)3.

;:

8?? jj*A$&.   	q% A
Q
CacAK(.V	?	?	,!r   