
    	h                     ^    S r SSKrSSKJrJr  SSKJrJrJr   " S S\	5      r
 " S S	5      rg)
a!  
This module contains code to translate formulae across cells in a worksheet.

The idea is that if A1 has formula "=B1+C1", then translating it to cell A2
results in formula "=B2+C2". The algorithm relies on the formula tokenizer
to identify the parts of the formula that need to change.

    N   )	TokenizerToken)coordinate_to_tuplecolumn_index_from_stringget_column_letterc                       \ rS rSrSrSrg)TranslatorError   a  
Raised when a formula can't be translated across cells.

This error arises when a formula's references would be translated outside
the worksheet's bounds on the top or left. Excel represents these
situations with a #REF! literal error. E.g., if the formula at B2 is
'=A1', attempting to translate the formula to B1 raises TranslatorError,
since there's no cell above A1. Similarly, translating the same formula
from B2 to A2 raises TranslatorError, since there's no cell to the left of
A1.

 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       L/var/www/html/env/lib/python3.13/site-packages/openpyxl/formula/translate.pyr
   r
      s    r   r
   c                       \ rS rSrSrS rS r\R                  " S5      r	\R                  " S5      r
\R                  " S5      r\S 5       r\S	 5       r\S
 5       r\S 5       rSS jrSrg)
Translator!   a  
Modifies a formula so that it can be translated from one cell to another.

`formula`: The str string to translate. Must include the leading '='
           character.
`origin`: The cell address (in A1 notation) where this formula was
          defined (excluding the worksheet name).

c                 T    [        U5      u  U l        U l        [        U5      U l        g N)r   rowcolr   	tokenizer)selfformulaorigins      r   __init__Translator.__init__-   s#     18$("7+r   c                 .    U R                   R                  $ )z6Returns a list with the tokens comprising the formula.)r   items)r   s    r   
get_tokensTranslator.get_tokens4   s    ~~###r   z*(\$?[1-9][0-9]{0,6}):(\$?[1-9][0-9]{0,6})$z&(\$?[A-Za-z]{1,3}):(\$?[A-Za-z]{1,3})$z'(\$?[A-Za-z]{1,3})(\$?[1-9][0-9]{0,6})$c                     U R                  S5      (       a  U $ [        U 5      U-   nUS::  a  [        S5      e[        U5      $ )z<
Translate a range row-snippet by the given number of rows.
$r   Formula out of range)
startswithintr
   str)row_strrdeltanew_rows      r   translate_rowTranslator.translate_row<   sE    
 c""N'lV+G!|%&<==w<r   c                     U R                  S5      (       a  U $  [        [        U 5      U-   5      $ ! [         a    [	        S5      ef = f)z>
Translate a range col-snippet by the given number of columns
r'   r(   )r)   r   r   
ValueErrorr
   )col_strcdeltas     r   translate_colTranslator.translate_colI   sV    
 c""N>(,W5>@ @ >%&<==>s	   1 Ac                 L    SU ;   a  U R                  SS5      u  pUS-   U 4$ SU 4$ )zCSplits out the worksheet reference, if any, from a range reference.!r    )rsplit)	range_strsheets     r   strip_ws_nameTranslator.strip_ws_nameW   s:     )(//Q7E3;	))9}r   c                    ^ ^^ T R                  U5      u  pAT R                  R                  U5      nUbI  UT R                  UR	                  S5      T5      -   S-   T R                  UR	                  S5      T5      -   $ T R
                  R                  U5      nUbI  UT R                  UR	                  S5      T5      -   S-   T R                  UR	                  S5      T5      -   $ SU;   a/  USR                  UU U4S jUR                  S5       5       5      -   $ T R                  R                  U5      nUc  U$ UT R                  UR	                  S5      T5      -   T R                  UR	                  S5      T5      -   $ )a&  
Translate an A1-style range reference to the destination cell.

`rdelta`: the row offset to add to the range
`cdelta`: the column offset to add to the range
`range_str`: an A1-style reference to a range. Potentially includes
             the worksheet reference. Could also be a named range.

r   :   c              3   J   >#    U  H  nTR                  UTT5      v   M     g 7fr   )translate_range).0piecer4   clsr-   s     r   	<genexpr>-Translator.translate_range.<locals>.<genexpr>   s*      &31E ##E66::1s    #)
r=   ROW_RANGE_REmatchr/   groupCOL_RANGE_REr5   joinsplitCELL_REF_RE)rF   r;   r-   r4   ws_partrJ   s   ` ``  r   rC   Translator.translate_rangee   st    !..y9  &&y1c//AGG#M''A?@ A  &&y1c//AGG#M''A?@ A) SXX &3&__S1&3 3 3 3 %%i0=#++EKKNFCC##EKKNF;< 	=r   Nc                 6   U R                  5       nU(       d  gUS   R                  [        R                  :X  a  US   R                  $ S/nU(       a)  [        U5      u  pgX`R                  -
  nXpR                  -
  nU H  nUR                  [        R                  :X  aL  UR                  [        R                  :X  a.  UR                  U R                  UR                  UU5      5        Mm  UR                  UR                  5        M     SR                  U5      $ )z
Convert the formula into A1 notation, or as row and column coordinates

The formula is converted into A1 assuming it is assigned to the cell
whose address is `dest` (no worksheet name).

r9   r   =)r$   typer   LITERALvaluer   r   r   OPERANDsubtypeRANGEappendrC   rM   )	r   dest	row_delta	col_deltatokensoutr   r   tokens	            r   translate_formulaTranslator.translate_formula   s     "AY^^u}},!9??"e *40HChhIhhIE

emm+MMU[[0

4//Y09; < 

5;;'  wws|r   )r   r   r   )Nr   r   )r   r   r   r   r   r    r$   recompilerI   rL   rO   staticmethodr/   r5   r=   classmethodrC   ra   r   r   r   r   r   r   !   s    ,$ ::KLL::GHL**GHK
  
  > >    =  =Dr   r   )r   rc   r   r   r   openpyxl.utilsr   r   r   	Exceptionr
   r   r   r   r   <module>ri      s4    
 ' i E Er   