U o‰^ã@sTdZddlZddlmZmZddlmZmZmZGdd„de ƒZ Gdd „d e ƒZ dS) 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@seZdZdZdS)Ú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/private/var/folders/sd/whlwsn6x1_qgglc0mjv25_695qk2gl/T/pip-install-4zq3fp6i/openpyxl/openpyxl/formula/translate.pyrsrc@sxeZdZdZdd„Zdd„Ze d¡Ze d¡Z e d¡Z e d d „ƒZ e d d „ƒZ e d d„ƒZedd„ƒZddd„ZdS)Ú 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). cCst|ƒ\|_|_t|ƒ|_dS©N)rÚrowÚcolrÚ tokenizer)ÚselfZformulaÚoriginr r rÚ__init__-szTranslator.__init__cCs|jjS)z6Returns a list with the tokens comprising the formula.)rÚitems)rr r rÚ get_tokens4szTranslator.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})$cCs6| d¡r|St|ƒ|}|dkr*tdƒ‚t|ƒSdS)zL Translate a range row-snippet by the given number of rows. ú$rúFormula out of rangeN)Ú startswithÚintrÚstr)Zrow_strÚrdeltaZnew_rowr r rÚ translate_row<s   zTranslator.translate_rowcCsB| d¡r|Sztt|ƒ|ƒWStk r<tdƒ‚YnXdS)zN Translate a range col-snippet by the given number of columns rrN)rrrÚ ValueErrorr)Zcol_strÚcdeltar r rÚ translate_colIs  ÿzTranslator.translate_colcCs,d|kr$| dd¡\}}|d|fSd|fS)zCSplits out the worksheet reference, if any, from a range reference.ú!rÚ)Úrsplit)Ú range_strZsheetr r rÚ strip_ws_nameWs  zTranslator.strip_ws_namecsüˆ |¡\}}ˆj |¡}|dk rN|ˆ | d¡ˆ¡dˆ | d¡ˆ¡Sˆj |¡}|dk rŽ|ˆ | d¡ˆ¡dˆ | d¡ˆ¡Sd|kr¼|d ‡‡‡fdd„| d¡Dƒ¡Sˆj  |¡}|dkrÔ|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ú:éc3s|]}ˆ |ˆˆ¡VqdSr)Útranslate_range)Ú.0Zpiece©r!Úclsrr rÚ sÿz-Translator.translate_range..) r'Ú ROW_RANGE_REÚmatchrÚgroupÚ COL_RANGE_REr"ÚjoinÚsplitÚ CELL_REF_RE)r-r&rr!Zws_partr0r r,rr*es*  ÿ ÿþ  ÿzTranslator.translate_rangeNrc Cs¤| ¡}|sdS|djtjkr*|djSdg}|rTt|ƒ\}}||j}||j}|D]@}|jtjkrŒ|j tj krŒ|  |  |j||¡¡qX|  |j¡qXd  |¡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ÚvaluerrrZOPERANDÚsubtypeÚRANGEÚappendr*r3) rÚdestZ row_deltaZ col_deltaÚtokensÚoutrrÚtokenr r rÚtranslate_formulaˆs&      ÿÿzTranslator.translate_formula)Nrr)r r r r rrÚreÚcompiler/r2r5Ú staticmethodrr"r'Ú classmethodr*rAr r r rr!s        "r) r rBrrrZopenpyxl.utilsrrrÚ ExceptionrÚobjectrr r r rÚs