a
    b                     @   s   d Z ddlZddlmZ ddlmZ edZdZdZ	d	Z
ed
e
 d ejZdZedee
ejZdd Zdd Zdd Zdd Zi Zi ZeddD ]ZeeZeee< eee< qdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Z dS )'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                 C   s<   t | trt| } t |tr$t|}dd t| |d D S )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.
    c                 S   s   g | ]}t |qS  get_column_letter).0xr   r   c/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/openpyxl/utils/cell.py
<listcomp>'       z'get_column_interval.<locals>.<listcomp>r   )
isinstancestrcolumn_index_from_stringrange)startendr   r   r   get_column_interval   s
    

r   c                 C   sV   t | }|s"d|  d}t|| \}}t|}|sNd|  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rowr   r   r   coordinate_from_string*   s    
r   c                 C   st   t | }|st|  d|d}| D ]\}}|r.d| ||< q.|d sZ|d r`d}nd}|jf 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fmtr   r   r   absolute_coordinate8   s    

r-   c                 C   st   d|   krdks$n t d| g }| dkrft| d\} }|dkrRd}| d8 } |t|d  q(d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)Zcol_idxletters	remainderr   r   r   _get_column_letterJ   s    
r8   igG  c                 C   s0   z
t |  W S  ty*   td| Y n0 dS )z?Convert a column index into a column letter
    (3 -> 'C')
    r.   N)_STRING_COL_CACHEKeyErrorr$   r'   )idxr   r   r   r	   i   s    
r	   c                 C   s4   zt |   W S  ty.   td| Y n0 dS )z@Convert a column name into a numerical index
    ('A' -> 1)
    z{0} is not a valid column nameN)_COL_STRING_CACHEupperr:   r$   r'   )Zstr_colr   r   r   r   s   s    r   c           
      C   s   d | }t| }|s t|| \}}}}}|rz||f}||f}	t||	 szt|rbt|	rzt|	rrt|rzt||durt|}|durt|}|durt|}n|}|durt|}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 rangeN)	r'   r#   r   r$   r   allanyr   r   )
range_stringr   r(   min_colmin_rowsepr!   r"   colsrowsr   r   r   range_boundaries~   s<    




rF   c                 #   s\   t | \}}}}t||d }dd t||d D }|D ] t fdd|D V  q:dS )[
    Get individual addresses for every cell in a range.
    Yields one row at a time.
    r   c                 S   s   g | ]}t |qS r   r   r
   colr   r   r   r      r   z#rows_from_range.<locals>.<listcomp>c                 3   s   | ]}d  | V  qdS z{0}{1}Nr'   rH   r   r   r   	<genexpr>   r   z"rows_from_range.<locals>.<genexpr>NrF   r   tupler@   rA   rB   r!   r"   rE   rD   r   rL   r   rows_from_range   s
    rQ   c                 #   s\   t | \}}}}t||d }dd t||d D }|D ] t fdd|D V  q:dS )rG   r   c                 s   s   | ]}t |V  qd S )Nr   rH   r   r   r   rM      r   z"cols_from_range.<locals>.<genexpr>c                 3   s   | ]}d   |V  qdS rJ   rK   )r
   r   rI   r   r   rM      r   NrN   rP   r   rR   r   cols_from_range   s
    rS   c                 C   sJ   t | D ]\}}|tv r qq| d|  }| |d }t|t| fS )zA
    Convert an Excel style coordinate to (row, colum) tuple
    N)	enumerater   r=   r   r<   )Z
coordinater;   crI   r   r   r   r   coordinate_to_tuple   s    rV   c                 C   sH   t | }|du rtd|dp,|d}|d}t|}||fS )zc
    Convert a worksheet range to the sheetname and maximum and minimum
    coordinate indices
    Nz)Value must be of the form sheetname!A1:E4quotedZ	notquotedcells)SHEETRANGE_REr   r$   grouprF   )r@   r(   	sheetnamerX   Z
boundariesr   r   r   range_to_tuple   s    

r\   c                 C   s"   d| v r|  dd} d| } | S )z>
    Add quotes around sheetnames if they contain spaces.
    'z''z'{0}')replacer'   )r[   r   r   r   quote_sheetname   s    
r_   )!__doc__restringr   
exceptionsr   compiler   Z	COL_RANGEZ	ROW_RANGEZ
RANGE_EXPRVERBOSEr#   ZSHEET_TITLEr'   rY   r   r   r-   r8   r<   r9   r   irI   r	   r   rF   rQ   rS   rV   r\   r_   r   r   r   r   <module>   s@   


+