B
    ÔùÓ`Þ!  ã               @   s`  d Z dZddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ dd	lmZ dd
lmZ ejejejejfZejejejejejejejejiZeefZee e ee dƒf Z!e "d¡Z#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,e%e&e'e(e)e*e+e,fZ-e.de/dedediZ0dd„ Z1dd„ Z2G dd„ deƒZ3G dd„ deƒZ4ddd„Z5dS )züManage individual cells in a spreadsheet.

The Cell class is required to know its value and type, display options,
and any other features of an Excel cell.  Utilities for referencing
cells using Excel's 'A1' column/row nomenclature are also provided.

zrestructuredtext ené    )ÚcopyN)ÚNUMERIC_TYPESÚ
deprecated)ÚIllegalCharacterError)Úget_column_letter)ÚnumbersÚis_date_format)ÚStyleableObject)Ú	Hyperlinkz#[\000-\010]|[\013-\014]|[\016-\037])z#NULL!z#DIV/0!z#VALUE!z#REF!z#NAME?z#NUM!z#N/AÚsÚfÚnÚbZ	inlineStrÚeÚstrc             C   s@   t |tƒrd}n$t |tƒr d}nt |tƒr0d}nd S |t| < |S )Nr   r   Úd)Ú
isinstancer   ÚSTRING_TYPESÚ
TIME_TYPESÚ_TYPES)ÚtÚvalueÚdt© r   ú6/tmp/pip-unpacked-wheel-zhp1095k/openpyxl/cell/cell.pyÚget_type@   s    


r   c             C   sX   t  | ¡}|r|S x2|  ¡ dd … D ]}t  |¡}|r$|t | < |S q$W td |¡ƒ‚d S )Né   z#Could not get time format for {0!r})ÚTIME_FORMATSÚgetÚmroÚ
ValueErrorÚformat)r   r   Úbaser   r   r   Úget_time_formatM   s    

r#   c                   sò   e Zd ZdZdZd(‡ fdd„	Zedd„ ƒZedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
edd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZejdd„ ƒZedd„ ƒZedd„ ƒZejdd„ ƒZed d!„ ƒZd)d#d$„Zed%d&„ ƒZejd'd&„ ƒZ‡  ZS )*ÚCellzpDescribes cell associated properties.

    Properties of interest include style, type, value, and address.

    )ÚrowÚcolumnÚ_valueÚ	data_typeÚparentÚ
_hyperlinkÚ_commentNc                sH   t t| ƒ ||¡ || _|| _d | _d | _d| _|d k	r>|| _d | _	d S )Nr   )
Úsuperr$   Ú__init__r%   r&   r'   r*   r(   r   r+   )ÚselfÚ	worksheetr%   r&   r   Zstyle_array)Ú	__class__r   r   r-   i   s    zCell.__init__c             C   s   t | jƒ}|› | j› �S )z!This cell's coordinate (ex. 'A5'))r   r&   r%   )r.   Úcolr   r   r   Ú
coordinatex   s    
zCell.coordinatec             C   s   | j S )z!The numerical index of the column)r&   )r.   r   r   r   Úcol_idx   s    zCell.col_idxc             C   s
   t | jƒS )N)r   r&   )r.   r   r   r   Úcolumn_letter…   s    zCell.column_letterc             C   s   | j jS )N)r)   Úencoding)r.   r   r   r   r5   Š   s    zCell.encodingc             C   s
   | j j jS )N)r)   Úepoch)r.   r   r   r   Ú	base_dateŽ   s    zCell.base_datec             C   s   d  | jj| j¡S )Nz<Cell {0!r}.{1}>)r!   r)   Útitler2   )r.   r   r   r   Ú__repr__“   s    zCell.__repr__c             C   sN   |dkrdS t |tƒs"t|| jƒ}t|ƒ}|dd… }tt |¡dƒrJt‚|S )z5Check string coding, length, and line break characterNiÿ  )r   r   r5   ÚnextÚILLEGAL_CHARACTERS_REÚfinditerr   )r.   r   r   r   r   Úcheck_string–   s    
zCell.check_stringc             C   s"   yt |ƒS  tk
r   dS X dS )z Tries to convert Error" else N/Az#N/AN)r   ÚUnicodeDecodeError)r.   r   r   r   r   Úcheck_error¥   s    zCell.check_errorc             C   sÈ   d| _ t|ƒ}yt| }W n tk
r8   t||ƒ}Y nX |dkrX|dk	rXtd |¡ƒ‚|rb|| _ |dkr€t| jƒs¾t	|ƒ| _n>|dkr¾|  
|¡}t|ƒdkr°| d¡r°d| _ n|tkr¾d	| _ || _dS )
z*Given a value, infer the correct data typer   NzCannot convert {0!r} to Excelr   r   r   ú=r   r   )r(   Útyper   ÚKeyErrorr   r    r!   r   Únumber_formatr#   r=   ÚlenÚ
startswithÚERROR_CODESr'   )r.   r   r   r   r   r   r   Ú_bind_value­   s(    

zCell._bind_valuec             C   s   | j S )z“Get or set the value held in the cell.

        :type: depends on the value (string, float, int or
            :class:`datetime.datetime`)
        )r'   )r.   r   r   r   r   Ë   s    z
Cell.valuec             C   s   |   |¡ dS )z1Set the value and infer type and display options.N)rG   )r.   r   r   r   r   r   Ô   s    c             C   s   | j S )z#Always returns the value for excel.)r'   )r.   r   r   r   Úinternal_valueÙ   s    zCell.internal_valuec             C   s   | j S )z.Return the hyperlink target or an empty string)r*   )r.   r   r   r   Ú	hyperlinkÞ   s    zCell.hyperlinkc             C   sP   |dkrd| _ n<t|tƒs&td|d�}| j|_|| _ | jdkrL|jpH|j| _dS )a  Set value and display for hyperlinks in a cell.
        Automatically sets the `value` of the cell with link text,
        but you can modify it afterwards by setting the `value`
        property, and the hyperlink will remain.
        Hyperlink is removed if set to ``None``.NÚ )ÚrefÚtarget)	r*   r   r
   r2   rK   r'   rL   Úlocationr   )r.   Úvalr   r   r   rI   ä   s    

c             C   s   | j dkp| j dkot| jƒS )zFTrue if the value is formatted as a date

        :type: bool
        r   r   )r(   r   rC   )r.   r   r   r   Úis_dateö   s    
zCell.is_dater   c             C   s$   | j | }| j| }| jj||d�S )z÷Returns a cell location relative to this cell.

        :param row: number of rows to offset
        :type row: int

        :param column: number of columns to offset
        :type column: int

        :rtype: :class:`openpyxl.cell.Cell`
        )r&   r%   )r3   r%   r)   Úcell)r.   r%   r&   Zoffset_columnZ
offset_rowr   r   r   Úoffset  s    

zCell.offsetc             C   s   | j S )zn Returns the comment associated with this cell

            :type: :class:`openpyxl.comments.Comment`
        )r+   )r.   r   r   r   Úcomment  s    zCell.commentc             C   sD   |dk	r"|j rt|ƒ}| | ¡ n|dkr:| jr:| j ¡  || _dS )z,
        Assign a comment to a cell
        N)r)   r   Úbindr+   Zunbind)r.   r   r   r   r   rR     s    
)NNNN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r-   Úpropertyr2   r3   r4   r5   r7   r9   r=   r?   rG   r   ÚsetterrH   rI   rO   rQ   rR   Ú__classcell__r   r   )r0   r   r$   Y   s*   		
	r$   c                   sL   e Zd ZdZdZdZdZdZdZd	‡ fdd„	Z	dd„ Z
ejZeZeZ‡  ZS )
Ú
MergedCellz¥
    Describes the properties of a cell in a merged cell and helps to
    display the borders of the merged cell.

    The value of a MergedCell is always None.
    )r%   r&   Nr   c                s    t t| ƒ |¡ || _|| _d S )N)r,   r\   r-   r%   r&   )r.   r/   r%   r&   )r0   r   r   r-   :  s    zMergedCell.__init__c             C   s   d  | jj| j¡S )Nz<MergedCell {0!r}.{1}>)r!   r)   r8   r2   )r.   r   r   r   r9   @  s    zMergedCell.__repr__)NN)rT   rU   rV   rW   rX   r'   r(   rR   rI   r-   r9   r$   r2   r+   r   r[   r   r   )r0   r   r\   )  s   r\   c             C   s   t | dd|d�S )Nr   )r/   r&   r%   r   )r$   )Úwsr   r   r   r   ÚWriteOnlyCellH  s    r^   )NN)6rW   Z__docformat__r   ÚdatetimeÚreZopenpyxl.compatr   r   Zopenpyxl.utils.exceptionsr   Zopenpyxl.utilsr   Zopenpyxl.stylesr   r   Zopenpyxl.styles.styleabler	   Zopenpyxl.worksheet.hyperlinkr
   ÚdateÚtimeÚ	timedeltar   ZFORMAT_DATE_DATETIMEZFORMAT_DATE_YYYYMMDD2ZFORMAT_DATE_TIME6ZFORMAT_DATE_TIMEDELTAr   r   Úbytesr   ÚboolrA   ZKNOWN_TYPESÚcompiler;   rF   ZTYPE_STRINGZTYPE_FORMULAZTYPE_NUMERICZ	TYPE_BOOLZ	TYPE_NULLZTYPE_INLINEZ
TYPE_ERRORZTYPE_FORMULA_CACHE_STRINGZVALID_TYPESÚintÚfloatr   r   r#   r$   r\   r^   r   r   r   r   Ú<module>	   sH   
 Q