B
    P•\œ  ã               @   s
  d dl m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x*eddƒD ]ZeeƒZeee< eee< q¦W d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 )'é    )Úabsolute_importN)Ú
basestringé   )Ú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   ú2lib/python3.7/site-packages/openpyxl/utils/cell.pyú
<listcomp>(   s    z'get_column_interval.<locals>.<listcomp>r   )Ú
isinstancer   Úcolumn_index_from_stringÚrange)ÚstartÚendr   r   r   Úget_column_interval   s
    

r   c             C   sN   t  | ¡}|sd|  }t|ƒ‚| ¡ \}}t|ƒ}|sFd|  }t|ƒ‚||fS )z;Convert a coordinate string like 'B12' to a tuple ('B', 12)zInvalid cell coordinates (%s)zThere is no row 0 (%s))Ú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¡}x&| ¡ D ]\}}|r0d |¡||< q0W |d s^|d rdd}nd}|jf |ŽS )zDConvert a coordinate to an absolute coordinate string (B12 -> $B$12)z#{0} is not a valid coordinate rangeÚ z${0}Úmax_colÚmax_rowz%{min_col}{min_row}:{max_col}{max_row}z{min_col}{min_row})ÚABSOLUTE_REr   Ú
ValueErrorÚformatÚ	groupdictÚitems)r   ÚmÚdÚkÚvZfmtr   r   r   Úabsolute_coordinate9   s    

r)   c             C   sx   d|   krdks$n t d | ¡ƒ‚g }x@| dkrht| dƒ\} }|dkrTd}| d8 } | t|d ƒ¡ q*W d t|ƒ¡S )zéConvert 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  zInvalid column index {0}r   é   é@   r   )r!   r"   ÚdivmodÚappendÚchrÚjoinÚreversed)Zcol_idxZlettersZ	remainderr   r   r   Ú_get_column_letterL   s    

r1   igG  c             C   s0   yt |  S  tk
r*   td | ¡ƒ‚Y nX dS )z?Convert a column index into a column letter
    (3 -> 'C')
    zInvalid column index {0}N)Ú_STRING_COL_CACHEÚKeyErrorr!   r"   )Úidxr   r   r   r	   k   s    r	   c             C   s4   yt |  ¡  S  tk
r.   td | ¡ƒ‚Y nX dS )z@Convert a column name into a numerical index
    ('A' -> 1)
    z{0} is not a valid column nameN)Ú_COL_STRING_CACHEÚupperr3   r!   r"   )Zstr_colr   r   r   r   u   s    r   c       
      C   sÒ   d  | ¡}t | ¡}|s t|ƒ‚| ¡ \}}}}}|rz||f}||f}	t||	 ƒszt|ƒrbt|	ƒrzt|	ƒrrt|ƒrzt|ƒ‚|dk	rŠt|ƒ}|dk	ršt|ƒ}|dk	r¬t|ƒ}n|}|dk	rÂ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 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.    



r?   c             #   s`   t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}x$|D ]‰ t‡ fdd„|D ƒƒV  q<W dS )z[
    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   ²   s    z#rows_from_range.<locals>.<listcomp>c             3   s   | ]}d   |ˆ ¡V  qdS )z{0}{1}N)r"   )r
   r@   )r   r   r   ú	<genexpr>´   s    z"rows_from_range.<locals>.<genexpr>N)r?   r   Útuple)r9   r:   r;   r   r   r>   r=   r   )r   r   Úrows_from_range«   s
    
rC   c             #   s`   t | ƒ\}}}}t||d ƒ}dd„ t||d ƒD ƒ}x$|D ]‰ t‡ fdd„|D ƒƒV  q<W dS )z[
    Get individual addresses for every cell in a range.
    Yields one row at a time.
    r   c             s   s   | ]}t |ƒV  qd S )N)r	   )r
   r@   r   r   r   rA   ¾   s    z"cols_from_range.<locals>.<genexpr>c             3   s   | ]}d   ˆ |¡V  qdS )z{0}{1}N)r"   )r
   r   )r@   r   r   rA   À   s    N)r?   r   rB   )r9   r:   r;   r   r   r>   r=   r   )r@   r   Úcols_from_range·   s
    
rD   c             C   s*   t  | ¡}|dd… \}}t|ƒt| fS )zA
    Convert an Excel style coordinate to (row, colum) tuple
    r   é   )r   Úsplitr   r5   )Z
coordinater   r@   r   r   r   r   Úcoordinate_to_tupleÃ   s    
rG   c             C   sH   t  | ¡}|dk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:E4ZquotedZ	notquotedÚcells)ÚSHEETRANGE_REr   r!   Úgroupr?   )r9   r%   Ú	sheetnamerH   Z
boundariesr   r   r   Úrange_to_tupleÌ   s    

rL   c             C   s"   d| kr|   dd¡} d | ¡} | S )z>
    Add quotes around sheetnames if they contain spaces.
    ú'z''z'{0}')Úreplacer"   )rK   r   r   r   Úquote_sheetnameÚ   s    
rO   )"Z
__future__r   ÚreZopenpyxl.compatr   Ú
exceptionsr   Úcompiler   Z	COL_RANGEZ	ROW_RANGEZ
RANGE_EXPRÚVERBOSEr    ZSHEET_TITLEr"   rI   r   r   r)   r1   r5   r2   r   Úir@   r	   r   r?   rC   rD   rG   rL   rO   r   r   r   r   Ú<module>   s:   

+	