U
    dj                     @   s   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ dd ZG dd dZdS )z2Write the workbook global settings to the archive.    )quote_sheetname)ARC_APPARC_CORE
ARC_CUSTOMARC_WORKBOOK
PKG_REL_NSCUSTOMUI_NSARC_ROOT_RELS)tostring
fromstring)RelationshipRelationshipList)DefinedNameDefinedNameList)ExternalReference)
ChildSheetWorkbookPackage
PivotCache)WorkbookProperties)CALENDAR_MAC_1904c                 C   s`   dd t | jD }|s td| j}| j}|r>|jdkr>|S ||d D ]}|| _|  S dS )z~
    Return the index of the active sheet.
    If the sheet set to active is hidden return the next visible sheet or None
    c                 S   s   g | ]\}}|j d kr|qS )visible)sheet_state).0idxsheet r   =/tmp/pip-unpacked-wheel-rwywncvq/openpyxl/workbook/_writer.py
<listcomp>!   s     
 z$get_active_sheet.<locals>.<listcomp>z"At least one sheet must be visibler   N)	enumerate_sheets
IndexErrorZ_active_sheet_indexactiver   )wbZvisible_sheetsr   r   r   r   r   get_active_sheet   s    r#   c                   @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )WorkbookWriterc                 C   s.   || _ t | _t | _|j| j_|j| j_d S )N)	r"   r   relsr   packagesecurityZworkbookProtectionZcalculationZcalcPr)selfr"   r   r   r   __init__3   s
    
zWorkbookWriter.__init__c                 C   s:   t  }| jjd k	r| jj|_| jjtkr.d|_|| j_d S )NT)	r   r"   Z	code_nameZcodeNameZexcel_base_dater   Zdate1904r&   Z
workbookPr)r(   propsr   r   r   write_properties;   s    
zWorkbookWriter.write_propertiesc                 C   s   t | jjdD ]r\}}t|j|d|d}t|j|jd}| j	
| |jdksrt| jjdkrjtd|j|_| jj
| qd S )N   zrId{0})nameZsheetIdidtypeTargetr   z1The only worksheet of a workbook cannot be hidden)r   r"   r   r   titleformatr   	_rel_typepathr%   appendr   len
ValueErrorstater&   Zsheets)r(   r   r   Z
sheet_noderelr   r   r   write_worksheetsE   s    
zWorkbookWriter.write_worksheetsc                 C   sX   | j jD ]J}t| j jd }t|j|jd}| j| t|j	d}| j
j| qd S )Nr,   r/   )r.   )r"   Z_external_linksr7   r%   r   r4   r5   r6   r   r.   r&   ZexternalReferences)r(   linkZrIdr:   extr   r   r   
write_refsR   s    zWorkbookWriter.write_refsc                 C   s   t | jj }t| jjD ]\}}t|j}|jrX|j }|D ]
}||_qB|	| |j
rtd|dd}| d|j
 |_|| |jrtd|d}|j|_|| |jrtd|d}|j|_|| qt|d| j_d S )	NZ_FilterDatabaseT)r-   localSheetIdhidden!ZPrint_Titles)r-   r?   Z
Print_Area)ZdefinedName)listr"   defined_namesvaluesr   Z
worksheetsr   r2   r?   extendZauto_filterr   valuer6   Zprint_titlesZ
print_arear   r&   ZdefinedNames)r(   rC   r   r   quotednamesnr-   r   r   r   write_names\   s*    




zWorkbookWriter.write_namesc                 C   sp   t  }| jjD ]\}|j|kr||j t|jd}| jj	| t
|jj|jjd}| j	| |j|_qd S )N)cacheIdTyper1   )setr"   Z_pivotscacheaddr   rK   r&   ZpivotCachesr6   r   Zrel_typer5   r%   r.   )r(   Zpivot_cachesZpivotcr:   r   r   r   write_pivots{   s    
zWorkbookWriter.write_pivotsc                 C   s0   t | j}| jjr || jjd _| jj| j_d S )Nr   )r#   r"   ZviewsZ	activeTabr&   Z	bookViews)r(   r!   r   r   r   write_views   s    
zWorkbookWriter.write_viewsc                 C   s>   |    |   |   |   |   |   t| j S )zWrite the core workbook xml.)	r+   r;   rJ   rR   rS   r>   r
   r&   to_tree)r(   r   r   r   write   s    zWorkbookWriter.writec                 C   sd   t ddd}| j| t ddd}| j| | jjrVt ddd}d|_| j| t| j S )	z%Write the workbook relationships xml.stylesz
styles.xmlr/   themeztheme/theme1.xml zvbaProject.binzAhttp://schemas.microsoft.com/office/2006/relationships/vbaProject)r   r%   r6   r"   vba_archiverM   r
   rT   )r(   rV   rW   Zvbar   r   r   
write_rels   s    zWorkbookWriter.write_relsc                 C   s   t  }tdtd}|| tt dtd}|| tdtd}|| t| jj	dkrttdt
d}|| | jjdk	rt| jjt}t |}|tD ]}|| qt| S )	zWrite the package relationshipsZofficeDocumentr/   z/metadata/core-propertiesrL   zextended-propertiesr,   zcustom-propertiesN)r   r   r   r6   r   r   r   r7   r"   Zcustom_doc_propsr   rY   r   readr	   Z	from_treefindr   r
   rT   )r(   r%   r:   xmlZ	root_relsr   r   r   write_root_rels   s     




zWorkbookWriter.write_root_relsN)__name__
__module____qualname__r)   r+   r;   r>   rJ   rR   rS   rU   rZ   r^   r   r   r   r   r$   1   s   

r$   N) __doc__Zopenpyxl.utilsr   Zopenpyxl.xml.constantsr   r   r   r   r   r   r	   Zopenpyxl.xml.functionsr
   r   Zopenpyxl.packaging.relationshipr   r   Zopenpyxl.workbook.defined_namer   r   Z$openpyxl.workbook.external_referencer   Zopenpyxl.packaging.workbookr   r   r   Zopenpyxl.workbook.propertiesr   Zopenpyxl.utils.datetimer   r#   r$   r   r   r   r   <module>   s   $	