
    fh7                        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                    ee          e	j                  Zd Zd Zd Zdg ee          z   Z ed          d             Zd  eed          D             ZdZ ed          d             Zd Z d Z!d Z"d Z#d Z$d Z%dS )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          |          }d t          | |dz             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                 ,    g | ]}t          |          S  get_column_letter).0xs     J/var/www/histauto/venv/lib/python3.11/site-packages/openpyxl/utils/cell.py
<listcomp>z'get_column_interval.<locals>.<listcomp>)   s!    @@@Qa  @@@    r   )
isinstancestrcolumn_index_from_stringrange)startends     r   get_column_intervalr      sd     % 0(//#s ,&s++@@%sQw*?*?@@@@r   c                     t                               |           }|sd|  d}t          |          |                                \  }}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'   ,   s    NN<((E ,:<:::&s+++,,..KFC
c((C ,3L333&s+++3;r   c                    t                               |           }|st          |  d          |                    d          }|                                D ]\  }}|r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}r   )ABSOLUTE_REr    
ValueError	groupdictitemsformat)r#   mdkvfmts         r   absolute_coordinater6   :   s    ,''A LLJJJKKK	BA		  1 	q77AaD| #q| #5"3:????r   r)   )maxsizec                 b   d| cxk    rdk    s$n t          d                    |                     g }| dk     rt          |          S | rSt          | d          \  } }|                    dt          |                    |s| dz  } |                    dd           | Sd                    |          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-   r0   __decimal_to_alphadivmodinsertjoin)col_idxresult	remainders      r   r   r   N   s         5    3::7CCDDDF||!'**
 "#GR00a+I6777 	"qLGMM!S!!!  " 776??r   c                     i | ]\  }}||	S r   r   )r   posletters      r   
<dictcomp>rE   o   s    RRR[S&fSRRRr   )r   r9   i  c                 v   d|  d}t          |           dk    rt          |          d}t          |                                           } t	          | t
                    D ]9\  }}	 t          |         }n# t          $ r t          |          w xY w|||z  z  }:d|cxk     rdk     sn t          |          |S )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idxrD   powerrC   s         r   r   r   r   s     VCUUUI
3xx!||###
C
399;;

CS(++  	($V,CC 	( 	( 	(Y'''	(sU{s????U????###Js   'A55Bc                 $   d                     |           }t                              |           }|st          |          |                                \  }}}}}|re||f}||f}	t          ||	z             sKt          |          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)	r0   r,   r    r-   r!   allanyr   r"   )
range_stringr$   r1   min_colmin_rowsepr*   r+   colsrowss
             r   range_boundariesr]      s7    3
9
9,
G
GC,''A oo./hhjj+GWc7G
 	" t	"II	"!$ii	" II	" "$ii	"
 S//!*733g,,*733g,,GWg--r   c              #      K   t          |           \  }}}}t          ||dz             }d t          ||dz             D             }|D ]t          fd|D                       V   dS )[
    Get individual addresses for every cell in a range.
    Yields one row at a time.
    r   c                 ,    g | ]}t          |          S r   r   r   rP   s     r   r   z#rows_from_range.<locals>.<listcomp>   s!    JJJsc""JJJr   c              3   D   K   | ]}d                      |          V  dS z{0}{1}Nr0   )r   rP   r&   s     r   	<genexpr>z"rows_from_range.<locals>.<genexpr>   1      >>#HOOC-->>>>>>r   Nr]   r   tuple)rW   rX   rY   r*   r+   r\   r[   r&   s          @r   rows_from_rangeri      s      
 *:,)G)G&GWgw'A+&&DJJeGWq[.I.IJJJD ? ?>>>>>>>>>>>>>? ?r   c              #      K   t          |           \  }}}}t          ||dz             }d t          ||dz             D             }|D ]t          fd|D                       V   dS )r_   r   c              3   4   K   | ]}t          |          V  d S )Nr   ra   s     r   re   z"cols_from_range.<locals>.<genexpr>   s+      HHsc""HHHHHHr   c              3   D   K   | ]}d                      |          V  dS rc   rd   )r   r&   rP   s     r   re   z"cols_from_range.<locals>.<genexpr>   rf   r   Nrg   )rW   rX   rY   r*   r+   r\   r[   rP   s          @r   cols_from_rangerm      s      
 *:,)G)G&GWgw'!)$$DHHeGWQY.G.GHHHD ? ?>>>>>>>>>>>>>? ?r   c                     t          |           D ]\  }}|t          v r n| d|         }| |d         }t          |          t          |          fS )zB
    Convert an Excel style coordinate to (row, column) tuple
    N)	enumerater   r"   r   )
coordinaterR   crP   r&   s        r   coordinate_to_tuplerr      sh     J''  Q;;E 
TcT
C
STT
Cs88-c2222r   c                     t                               |           }|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quoted	notquotedcells)SHEETRANGE_REr    r-   groupr]   )rW   r1   	sheetnamerv   
boundariess        r   range_to_tupler{      sv    
 	L))AyDEEE!!9QWW[%9%9IGGGE!%((Jj  r   c                 d    d| v r|                      dd          } d                    |           } | S )z>
    Add quotes around sheetnames if they contain spaces.
    rG   z''z'{0}')replacer0   )ry   s    r   quote_sheetnamer~      s;     i%%c400		**Ir   )&__doc__	functoolsr   	itertoolsr   r   stringr   r   re
exceptionsr	   compiler   	COL_RANGE	ROW_RANGE
RANGE_EXPRVERBOSEr,   SHEET_TITLEr0   rw   r   r'   r6   listr;   r   ro   rN   rM   r   r]   ri   rm   rr   r{   r~   r   r   r   <module>r      s           $ $ $ $ $ $ $ $ * * * * * * * * 				 0 0 0 0 0 0 2:788(		
 bjz)3.
;;:
8??  j* *A A A    $ TDD111 
4  @ SRIIoq4Q4QRRR 
4  0(. (. (.V	? 	? 	?	? 	? 	?	3 	3 	3! ! !    r   