o
    ×Ügý  ã                   @   sR   d Z ddlZddlmZmZ ddlmZmZmZ G dd„ de	ƒZ
G dd	„ d	ƒZdS )
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                   @   s   e Zd ZdZdS )ÚTranslatorErrora  
    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__Ú__doc__© r   r   úT/var/www/html/api-tag/env/lib/python3.10/site-packages/openpyxl/formula/translate.pyr      s    r   c                   @   sx   e Zd ZdZdd„ Zdd„ Ze d¡Ze d¡Z	e d¡Z
ed	d
„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZddd„ZdS )Ú
Translatora5  
    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                 C   s   t |ƒ\| _| _t|ƒ| _d S ©N)r   ÚrowÚcolr   Ú	tokenizer)ÚselfÚformulaÚoriginr   r   r   Ú__init__-   s   zTranslator.__init__c                 C   s   | j jS )z6Returns a list with the tokens comprising the formula.)r   Úitems)r   r   r   r   Ú
get_tokens4   s   zTranslator.get_tokensz*(\$?[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                 C   s2   |   d¡r| S t| ƒ| }|dkrtdƒ‚t|ƒS )zL
        Translate a range row-snippet by the given number of rows.
        ú$r   úFormula out of range)Ú
startswithÚintr   Ústr)Úrow_strÚrdeltaÚnew_rowr   r   r   Útranslate_row<   s   
zTranslator.translate_rowc                 C   s8   |   d¡r| S z	tt| ƒ| ƒW S  ty   tdƒ‚w )zN
        Translate a range col-snippet by the given number of columns
        r   r   )r   r   r   Ú
ValueErrorr   )Úcol_strÚcdeltar   r   r   Útranslate_colI   s   

ÿÿzTranslator.translate_colc                 C   s,   d| v r|   dd¡\}} |d | fS d| fS )zCSplits out the worksheet reference, if any, from a range reference.ú!r   Ú )Úrsplit)Ú	range_strÚsheetr   r   r   Ústrip_ws_nameW   s   	zTranslator.strip_ws_namec                    sü   ˆ  |¡\}}ˆj |¡}|dur'|ˆ | d¡ˆ¡ d ˆ | d¡ˆ¡ S ˆj |¡}|durG|ˆ | d¡ˆ ¡ d ˆ | d¡ˆ ¡ S d|v r^|d ‡ ‡‡fdd„| d¡D ƒ¡ S ˆj	 |¡}|du rj|S |ˆ | d¡ˆ ¡ ˆ | d¡ˆ¡ S )aV  
        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.

        Nr   ú:é   c                 3   s    | ]
}ˆ  |ˆˆ ¡V  qd S r   )Útranslate_range)Ú.0Úpiece©r%   Úclsr    r   r   Ú	<genexpr>   s
   € ÿ
ÿz-Translator.translate_range.<locals>.<genexpr>)
r,   ÚROW_RANGE_REÚmatchr"   ÚgroupÚCOL_RANGE_REr&   ÚjoinÚsplitÚCELL_REF_RE)r3   r*   r    r%   Úws_partr6   r   r2   r   r/   e   s*   ÿÿ
þÿzTranslator.translate_rangeNr   c           	      C   s¤   |   ¡ }|sdS |d jtjkr|d jS dg}|r*t|ƒ\}}|| j }|| j }|D ] }|jtjkrF|j	tj
krF| |  |j||¡¡ q,| |j¡ q,d |¡S )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).

        r(   r   ú=)r   Útyper   ÚLITERALÚvaluer   r   r   ÚOPERANDÚsubtypeÚRANGEÚappendr/   r9   )	r   ÚdestÚ	row_deltaÚ	col_deltaÚtokensÚoutr   r   Útokenr   r   r   Útranslate_formulaˆ   s$   


ÿ
zTranslator.translate_formula)Nr   r   )r	   r
   r   r   r   r   ÚreÚcompiler5   r8   r;   Ústaticmethodr"   r&   r,   Úclassmethodr/   rK   r   r   r   r   r   !   s     







"r   )r   rL   r   r   r   Úopenpyxl.utilsr   r   r   Ú	Exceptionr   r   r   r   r   r   Ú<module>   s    	