2010-08-26 19:14:53 +00:00
|
|
|
<?php
|
2015-05-03 22:37:32 +00:00
|
|
|
|
|
|
|
/** PHPExcel root directory */
|
|
|
|
if (!defined('PHPEXCEL_ROOT')) {
|
|
|
|
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
|
|
|
|
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
|
|
|
}
|
|
|
|
|
2010-08-26 19:14:53 +00:00
|
|
|
/**
|
|
|
|
* PHPExcel
|
|
|
|
*
|
2015-05-02 22:50:37 +00:00
|
|
|
* Copyright (c) 2006 - 2015 PHPExcel
|
2010-08-26 19:14:53 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* @category PHPExcel
|
|
|
|
* @package PHPExcel
|
2015-05-02 22:50:37 +00:00
|
|
|
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
|
2012-11-25 12:26:48 +00:00
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2010-08-26 19:14:53 +00:00
|
|
|
* @version ##VERSION##, ##DATE##
|
|
|
|
*/
|
|
|
|
class PHPExcel
|
|
|
|
{
|
2013-02-04 17:26:27 +00:00
|
|
|
/**
|
|
|
|
* Unique ID
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $uniqueID;
|
2013-02-04 17:26:27 +00:00
|
|
|
|
2012-11-25 12:26:48 +00:00
|
|
|
/**
|
|
|
|
* Document properties
|
|
|
|
*
|
|
|
|
* @var PHPExcel_DocumentProperties
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $properties;
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Document security
|
|
|
|
*
|
|
|
|
* @var PHPExcel_DocumentSecurity
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $security;
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Collection of Worksheet objects
|
|
|
|
*
|
|
|
|
* @var PHPExcel_Worksheet[]
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $workSheetCollection = array();
|
2012-11-25 12:26:48 +00:00
|
|
|
|
2013-02-15 15:42:06 +00:00
|
|
|
/**
|
2015-05-03 22:37:32 +00:00
|
|
|
* Calculation Engine
|
|
|
|
*
|
|
|
|
* @var PHPExcel_Calculation
|
|
|
|
*/
|
|
|
|
private $calculationEngine;
|
2013-02-04 17:26:27 +00:00
|
|
|
|
2012-11-25 12:26:48 +00:00
|
|
|
/**
|
|
|
|
* Active sheet index
|
|
|
|
*
|
2015-05-03 22:37:32 +00:00
|
|
|
* @var integer
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $activeSheetIndex = 0;
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Named ranges
|
|
|
|
*
|
|
|
|
* @var PHPExcel_NamedRange[]
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $namedRanges = array();
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CellXf supervisor
|
|
|
|
*
|
|
|
|
* @var PHPExcel_Style
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $cellXfSupervisor;
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CellXf collection
|
|
|
|
*
|
|
|
|
* @var PHPExcel_Style[]
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $cellXfCollection = array();
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CellStyleXf collection
|
|
|
|
*
|
|
|
|
* @var PHPExcel_Style[]
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
private $cellStyleXfCollection = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hasMacros : this workbook have macros ?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasMacros = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* macrosCode : all macros code (the vbaProject.bin file, this include form, code, etc.), null if no macro
|
|
|
|
*
|
|
|
|
* @var binary
|
|
|
|
*/
|
|
|
|
private $macrosCode;
|
|
|
|
/**
|
|
|
|
* macrosCertificate : if macros are signed, contains vbaProjectSignature.bin file, null if not signed
|
|
|
|
*
|
|
|
|
* @var binary
|
|
|
|
*/
|
|
|
|
private $macrosCertificate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI
|
|
|
|
*
|
|
|
|
* @var null|string
|
|
|
|
*/
|
|
|
|
private $ribbonXMLData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements
|
|
|
|
* ignored if $ribbonXMLData is null
|
|
|
|
*
|
|
|
|
* @var null|array
|
|
|
|
*/
|
|
|
|
private $ribbonBinObjects;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The workbook has macros ?
|
|
|
|
*
|
|
|
|
* @return true if workbook has macros, false if not
|
|
|
|
*/
|
|
|
|
public function hasMacros()
|
|
|
|
{
|
|
|
|
return $this->hasMacros;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define if a workbook has macros
|
|
|
|
*
|
|
|
|
* @param boolean $hasMacros true|false
|
|
|
|
*/
|
|
|
|
public function setHasMacros($hasMacros = false)
|
|
|
|
{
|
|
|
|
$this->hasMacros = (bool) $hasMacros;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the macros code
|
|
|
|
*
|
|
|
|
* @param string $MacrosCode string|null
|
|
|
|
*/
|
|
|
|
public function setMacrosCode($MacrosCode = null)
|
|
|
|
{
|
|
|
|
$this->macrosCode=$MacrosCode;
|
|
|
|
$this->setHasMacros(!is_null($MacrosCode));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the macros code
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getMacrosCode()
|
|
|
|
{
|
|
|
|
return $this->macrosCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the macros certificate
|
|
|
|
*
|
|
|
|
* @param string|null $Certificate
|
|
|
|
*/
|
|
|
|
public function setMacrosCertificate($Certificate = null)
|
|
|
|
{
|
|
|
|
$this->macrosCertificate=$Certificate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the project signed ?
|
|
|
|
*
|
|
|
|
* @return boolean true|false
|
|
|
|
*/
|
|
|
|
public function hasMacrosCertificate()
|
|
|
|
{
|
|
|
|
return !is_null($this->macrosCertificate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the macros certificate
|
|
|
|
*
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getMacrosCertificate()
|
|
|
|
{
|
|
|
|
return $this->macrosCertificate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all macros, certificate from spreadsheet
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function discardMacros()
|
|
|
|
{
|
|
|
|
$this->hasMacros=false;
|
|
|
|
$this->macrosCode=null;
|
|
|
|
$this->macrosCertificate=null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set ribbon XML data
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setRibbonXMLData($Target = null, $XMLData = null)
|
|
|
|
{
|
|
|
|
if (!is_null($Target) && !is_null($XMLData)) {
|
|
|
|
$this->ribbonXMLData = array('target' => $Target, 'data' => $XMLData);
|
|
|
|
} else {
|
|
|
|
$this->ribbonXMLData = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retrieve ribbon XML Data
|
|
|
|
*
|
|
|
|
* return string|null|array
|
|
|
|
*/
|
|
|
|
public function getRibbonXMLData($What = 'all') //we need some constants here...
|
|
|
|
{
|
|
|
|
$ReturnData = null;
|
|
|
|
$What = strtolower($What);
|
|
|
|
switch ($What){
|
|
|
|
case 'all':
|
|
|
|
$ReturnData = $this->ribbonXMLData;
|
|
|
|
break;
|
|
|
|
case 'target':
|
|
|
|
case 'data':
|
|
|
|
if (is_array($this->ribbonXMLData) && array_key_exists($What, $this->ribbonXMLData)) {
|
|
|
|
$ReturnData = $this->ribbonXMLData[$What];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ReturnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store binaries ribbon objects (pictures)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setRibbonBinObjects($BinObjectsNames = null, $BinObjectsData = null)
|
|
|
|
{
|
|
|
|
if (!is_null($BinObjectsNames) && !is_null($BinObjectsData)) {
|
|
|
|
$this->ribbonBinObjects = array('names' => $BinObjectsNames, 'data' => $BinObjectsData);
|
|
|
|
} else {
|
|
|
|
$this->ribbonBinObjects = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function getExtensionOnly($ThePath)
|
|
|
|
{
|
|
|
|
return pathinfo($ThePath, PATHINFO_EXTENSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retrieve Binaries Ribbon Objects
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getRibbonBinObjects($What = 'all')
|
|
|
|
{
|
|
|
|
$ReturnData = null;
|
|
|
|
$What = strtolower($What);
|
|
|
|
switch($What) {
|
|
|
|
case 'all':
|
|
|
|
return $this->ribbonBinObjects;
|
|
|
|
break;
|
|
|
|
case 'names':
|
|
|
|
case 'data':
|
|
|
|
if (is_array($this->ribbonBinObjects) && array_key_exists($What, $this->ribbonBinObjects)) {
|
|
|
|
$ReturnData=$this->ribbonBinObjects[$What];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'types':
|
|
|
|
if (is_array($this->ribbonBinObjects) &&
|
|
|
|
array_key_exists('data', $this->ribbonBinObjects) && is_array($this->ribbonBinObjects['data'])) {
|
|
|
|
$tmpTypes=array_keys($this->ribbonBinObjects['data']);
|
|
|
|
$ReturnData = array_unique(array_map(array($this, 'getExtensionOnly'), $tmpTypes));
|
|
|
|
} else {
|
|
|
|
$ReturnData=array(); // the caller want an array... not null if empty
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $ReturnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This workbook have a custom UI ?
|
|
|
|
*
|
|
|
|
* @return true|false
|
|
|
|
*/
|
|
|
|
public function hasRibbon()
|
|
|
|
{
|
|
|
|
return !is_null($this->ribbonXMLData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This workbook have additionnal object for the ribbon ?
|
|
|
|
*
|
|
|
|
* @return true|false
|
|
|
|
*/
|
|
|
|
public function hasRibbonBinObjects()
|
|
|
|
{
|
|
|
|
return !is_null($this->ribbonBinObjects);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-17 00:11:45 +00:00
|
|
|
* Check if a sheet with a specified code name already exists
|
|
|
|
*
|
|
|
|
* @param string $pSheetCodeName Name of the worksheet to check
|
|
|
|
* @return boolean
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
2013-11-17 00:11:45 +00:00
|
|
|
public function sheetCodeNameExists($pSheetCodeName)
|
2012-11-25 12:26:48 +00:00
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return ($this->getSheetByCodeName($pSheetCodeName) !== null);
|
2013-11-17 00:11:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
/**
|
|
|
|
* Get sheet by code name. Warning : sheet don't have always a code name !
|
|
|
|
*
|
|
|
|
* @param string $pName Sheet name
|
|
|
|
* @return PHPExcel_Worksheet
|
|
|
|
*/
|
|
|
|
public function getSheetByCodeName($pName = '')
|
|
|
|
{
|
|
|
|
$worksheetCount = count($this->workSheetCollection);
|
|
|
|
for ($i = 0; $i < $worksheetCount; ++$i) {
|
|
|
|
if ($this->workSheetCollection[$i]->getCodeName() == $pName) {
|
|
|
|
return $this->workSheetCollection[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new PHPExcel with one Worksheet
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->uniqueID = uniqid();
|
|
|
|
$this->calculationEngine = PHPExcel_Calculation::getInstance($this);
|
|
|
|
|
|
|
|
// Initialise worksheet collection and add one worksheet
|
|
|
|
$this->workSheetCollection = array();
|
|
|
|
$this->workSheetCollection[] = new PHPExcel_Worksheet($this);
|
|
|
|
$this->activeSheetIndex = 0;
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// Create document properties
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->properties = new PHPExcel_DocumentProperties();
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// Create document security
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->security = new PHPExcel_DocumentSecurity();
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// Set named ranges
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->namedRanges = array();
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// Create the cellXf supervisor
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->cellXfSupervisor = new PHPExcel_Style(true);
|
|
|
|
$this->cellXfSupervisor->bindParent($this);
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// Create the default style
|
|
|
|
$this->addCellXf(new PHPExcel_Style);
|
|
|
|
$this->addCellStyleXf(new PHPExcel_Style);
|
|
|
|
}
|
|
|
|
|
2013-02-13 13:17:16 +00:00
|
|
|
/**
|
|
|
|
* Code to execute when this worksheet is unset()
|
|
|
|
*
|
2013-02-04 17:26:27 +00:00
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2013-02-04 17:26:27 +00:00
|
|
|
PHPExcel_Calculation::unsetInstance($this);
|
|
|
|
$this->disconnectWorksheets();
|
2015-05-03 22:37:32 +00:00
|
|
|
}
|
2013-02-04 17:26:27 +00:00
|
|
|
|
2012-11-25 12:26:48 +00:00
|
|
|
/**
|
|
|
|
* Disconnect all worksheets from this PHPExcel workbook object,
|
|
|
|
* typically so that the PHPExcel object can be unset
|
|
|
|
*
|
|
|
|
*/
|
2013-02-12 12:44:31 +00:00
|
|
|
public function disconnectWorksheets()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
$worksheet = null;
|
|
|
|
foreach ($this->workSheetCollection as $k => &$worksheet) {
|
2012-11-25 12:26:48 +00:00
|
|
|
$worksheet->disconnectCells();
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->workSheetCollection[$k] = null;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
unset($worksheet);
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->workSheetCollection = array();
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
/**
|
|
|
|
* Return the calculation engine for this worksheet
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Calculation
|
|
|
|
*/
|
|
|
|
public function getCalculationEngine()
|
|
|
|
{
|
|
|
|
return $this->calculationEngine;
|
|
|
|
} // function getCellCacheController()
|
2013-02-04 17:26:27 +00:00
|
|
|
|
2012-11-25 12:26:48 +00:00
|
|
|
/**
|
|
|
|
* Get properties
|
|
|
|
*
|
|
|
|
* @return PHPExcel_DocumentProperties
|
|
|
|
*/
|
|
|
|
public function getProperties()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->properties;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set properties
|
|
|
|
*
|
|
|
|
* @param PHPExcel_DocumentProperties $pValue
|
|
|
|
*/
|
|
|
|
public function setProperties(PHPExcel_DocumentProperties $pValue)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->properties = $pValue;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get security
|
|
|
|
*
|
|
|
|
* @return PHPExcel_DocumentSecurity
|
|
|
|
*/
|
|
|
|
public function getSecurity()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->security;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set security
|
|
|
|
*
|
|
|
|
* @param PHPExcel_DocumentSecurity $pValue
|
|
|
|
*/
|
|
|
|
public function setSecurity(PHPExcel_DocumentSecurity $pValue)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->security = $pValue;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get active sheet
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Worksheet
|
2014-09-08 15:16:31 +00:00
|
|
|
*
|
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function getActiveSheet()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->getSheet($this->activeSheetIndex);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create sheet and add it to this workbook
|
|
|
|
*
|
2012-11-25 12:26:48 +00:00
|
|
|
* @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
|
2010-08-26 19:14:53 +00:00
|
|
|
* @return PHPExcel_Worksheet
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2010-08-26 19:14:53 +00:00
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function createSheet($iSheetIndex = null)
|
2010-08-26 19:14:53 +00:00
|
|
|
{
|
|
|
|
$newSheet = new PHPExcel_Worksheet($this);
|
|
|
|
$this->addSheet($newSheet, $iSheetIndex);
|
|
|
|
return $newSheet;
|
|
|
|
}
|
|
|
|
|
2012-05-25 06:45:01 +00:00
|
|
|
/**
|
2013-01-30 09:04:04 +00:00
|
|
|
* Check if a sheet with a specified name already exists
|
2012-05-25 06:45:01 +00:00
|
|
|
*
|
2012-11-25 12:26:48 +00:00
|
|
|
* @param string $pSheetName Name of the worksheet to check
|
2012-05-25 06:45:01 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function sheetNameExists($pSheetName)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return ($this->getSheetByName($pSheetName) !== null);
|
2012-05-25 06:45:01 +00:00
|
|
|
}
|
|
|
|
|
2010-08-26 19:14:53 +00:00
|
|
|
/**
|
|
|
|
* Add sheet
|
|
|
|
*
|
2012-11-25 12:26:48 +00:00
|
|
|
* @param PHPExcel_Worksheet $pSheet
|
|
|
|
* @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
|
2010-08-26 19:14:53 +00:00
|
|
|
* @return PHPExcel_Worksheet
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2010-08-26 19:14:53 +00:00
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null)
|
2010-08-26 19:14:53 +00:00
|
|
|
{
|
2012-11-25 12:26:48 +00:00
|
|
|
if ($this->sheetNameExists($pSheet->getTitle())) {
|
2013-02-12 12:44:31 +00:00
|
|
|
throw new PHPExcel_Exception(
|
2015-05-03 22:37:32 +00:00
|
|
|
"Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
|
2013-02-12 12:44:31 +00:00
|
|
|
);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
2012-05-25 06:45:01 +00:00
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
if ($iSheetIndex === null) {
|
|
|
|
if ($this->activeSheetIndex < 0) {
|
|
|
|
$this->activeSheetIndex = 0;
|
2012-09-04 11:46:30 +00:00
|
|
|
}
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->workSheetCollection[] = $pSheet;
|
2012-01-25 23:07:22 +00:00
|
|
|
} else {
|
2010-08-26 19:14:53 +00:00
|
|
|
// Insert the sheet at the requested index
|
|
|
|
array_splice(
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->workSheetCollection,
|
2010-08-26 19:14:53 +00:00
|
|
|
$iSheetIndex,
|
|
|
|
0,
|
|
|
|
array($pSheet)
|
2015-05-03 22:37:32 +00:00
|
|
|
);
|
2010-08-26 19:14:53 +00:00
|
|
|
|
2012-11-25 12:26:48 +00:00
|
|
|
// Adjust active sheet index if necessary
|
2015-05-03 22:37:32 +00:00
|
|
|
if ($this->activeSheetIndex >= $iSheetIndex) {
|
|
|
|
++$this->activeSheetIndex;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-08 10:15:07 +00:00
|
|
|
|
|
|
|
if ($pSheet->getParent() === null) {
|
|
|
|
$pSheet->rebindParent($this);
|
|
|
|
}
|
|
|
|
|
2012-11-25 12:26:48 +00:00
|
|
|
return $pSheet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove sheet by index
|
|
|
|
*
|
|
|
|
* @param int $pIndex Active sheet index
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function removeSheetByIndex($pIndex = 0)
|
|
|
|
{
|
2013-02-12 12:44:31 +00:00
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
$numSheets = count($this->workSheetCollection);
|
2013-02-12 12:44:31 +00:00
|
|
|
if ($pIndex > $numSheets - 1) {
|
|
|
|
throw new PHPExcel_Exception(
|
2015-05-03 22:37:32 +00:00
|
|
|
"You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
|
2013-02-12 12:44:31 +00:00
|
|
|
);
|
2012-11-25 12:26:48 +00:00
|
|
|
} else {
|
2015-05-03 22:37:32 +00:00
|
|
|
array_splice($this->workSheetCollection, $pIndex, 1);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
// Adjust active sheet index if necessary
|
2015-05-03 22:37:32 +00:00
|
|
|
if (($this->activeSheetIndex >= $pIndex) &&
|
|
|
|
($pIndex > count($this->workSheetCollection) - 1)) {
|
|
|
|
--$this->activeSheetIndex;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sheet by index
|
|
|
|
*
|
|
|
|
* @param int $pIndex Sheet index
|
|
|
|
* @return PHPExcel_Worksheet
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function getSheet($pIndex = 0)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
if (!isset($this->workSheetCollection[$pIndex])) {
|
2014-09-08 15:16:31 +00:00
|
|
|
$numSheets = $this->getSheetCount();
|
2013-02-12 12:44:31 +00:00
|
|
|
throw new PHPExcel_Exception(
|
2014-09-08 15:16:31 +00:00
|
|
|
"Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
|
|
|
|
);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
2014-09-08 15:16:31 +00:00
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->workSheetCollection[$pIndex];
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all sheets
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Worksheet[]
|
|
|
|
*/
|
|
|
|
public function getAllSheets()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->workSheetCollection;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sheet by name
|
|
|
|
*
|
|
|
|
* @param string $pName Sheet name
|
|
|
|
* @return PHPExcel_Worksheet
|
|
|
|
*/
|
|
|
|
public function getSheetByName($pName = '')
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
$worksheetCount = count($this->workSheetCollection);
|
2012-11-25 12:26:48 +00:00
|
|
|
for ($i = 0; $i < $worksheetCount; ++$i) {
|
2015-05-03 22:37:32 +00:00
|
|
|
if ($this->workSheetCollection[$i]->getTitle() === $pName) {
|
|
|
|
return $this->workSheetCollection[$i];
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
return null;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get index for sheet
|
|
|
|
*
|
|
|
|
* @param PHPExcel_Worksheet $pSheet
|
|
|
|
* @return Sheet index
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function getIndex(PHPExcel_Worksheet $pSheet)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
foreach ($this->workSheetCollection as $key => $value) {
|
2012-11-25 12:26:48 +00:00
|
|
|
if ($value->getHashCode() == $pSheet->getHashCode()) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
2012-11-26 18:01:15 +00:00
|
|
|
|
|
|
|
throw new PHPExcel_Exception("Sheet does not exist.");
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set index for sheet by sheet name.
|
|
|
|
*
|
|
|
|
* @param string $sheetName Sheet name to modify index for
|
|
|
|
* @param int $newIndex New index for the sheet
|
|
|
|
* @return New sheet index
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
2010-08-26 19:14:53 +00:00
|
|
|
public function setIndexByName($sheetName, $newIndex)
|
|
|
|
{
|
|
|
|
$oldIndex = $this->getIndex($this->getSheetByName($sheetName));
|
|
|
|
$pSheet = array_splice(
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->workSheetCollection,
|
2010-08-26 19:14:53 +00:00
|
|
|
$oldIndex,
|
|
|
|
1
|
2012-11-25 17:56:27 +00:00
|
|
|
);
|
2010-08-26 19:14:53 +00:00
|
|
|
array_splice(
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->workSheetCollection,
|
2010-08-26 19:14:53 +00:00
|
|
|
$newIndex,
|
|
|
|
0,
|
|
|
|
$pSheet
|
2012-11-25 17:56:27 +00:00
|
|
|
);
|
2010-08-26 19:14:53 +00:00
|
|
|
return $newIndex;
|
|
|
|
}
|
|
|
|
|
2012-11-25 12:26:48 +00:00
|
|
|
/**
|
|
|
|
* Get sheet count
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getSheetCount()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return count($this->workSheetCollection);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get active sheet index
|
|
|
|
*
|
|
|
|
* @return int Active sheet index
|
|
|
|
*/
|
|
|
|
public function getActiveSheetIndex()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->activeSheetIndex;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set active sheet index
|
|
|
|
*
|
|
|
|
* @param int $pIndex Active sheet index
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
* @return PHPExcel_Worksheet
|
|
|
|
*/
|
|
|
|
public function setActiveSheetIndex($pIndex = 0)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
$numSheets = count($this->workSheetCollection);
|
2013-02-12 12:44:31 +00:00
|
|
|
|
|
|
|
if ($pIndex > $numSheets - 1) {
|
|
|
|
throw new PHPExcel_Exception(
|
2015-05-03 22:37:32 +00:00
|
|
|
"You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
|
2013-02-12 12:44:31 +00:00
|
|
|
);
|
2012-11-25 12:26:48 +00:00
|
|
|
} else {
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->activeSheetIndex = $pIndex;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
return $this->getActiveSheet();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set active sheet index by name
|
|
|
|
*
|
|
|
|
* @param string $pValue Sheet title
|
|
|
|
* @return PHPExcel_Worksheet
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function setActiveSheetIndexByName($pValue = '')
|
|
|
|
{
|
|
|
|
if (($worksheet = $this->getSheetByName($pValue)) instanceof PHPExcel_Worksheet) {
|
|
|
|
$this->setActiveSheetIndex($this->getIndex($worksheet));
|
|
|
|
return $worksheet;
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:01:15 +00:00
|
|
|
throw new PHPExcel_Exception('Workbook does not contain sheet:' . $pValue);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sheet names
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getSheetNames()
|
|
|
|
{
|
|
|
|
$returnValue = array();
|
|
|
|
$worksheetCount = $this->getSheetCount();
|
|
|
|
for ($i = 0; $i < $worksheetCount; ++$i) {
|
|
|
|
$returnValue[] = $this->getSheet($i)->getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add external sheet
|
|
|
|
*
|
|
|
|
* @param PHPExcel_Worksheet $pSheet External sheet to add
|
|
|
|
* @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
* @return PHPExcel_Worksheet
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null)
|
|
|
|
{
|
2012-11-25 12:26:48 +00:00
|
|
|
if ($this->sheetNameExists($pSheet->getTitle())) {
|
2012-11-26 18:01:15 +00:00
|
|
|
throw new PHPExcel_Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// count how many cellXfs there are in this workbook currently, we will need this below
|
2015-05-03 22:37:32 +00:00
|
|
|
$countCellXfs = count($this->cellXfCollection);
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// copy all the shared cellXfs from the external workbook and append them to the current
|
|
|
|
foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
|
|
|
|
$this->addCellXf(clone $cellXf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// move sheet to this workbook
|
|
|
|
$pSheet->rebindParent($this);
|
|
|
|
|
|
|
|
// update the cellXfs
|
|
|
|
foreach ($pSheet->getCellCollection(false) as $cellID) {
|
|
|
|
$cell = $pSheet->getCell($cellID);
|
2015-05-03 22:37:32 +00:00
|
|
|
$cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->addSheet($pSheet, $iSheetIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get named ranges
|
|
|
|
*
|
|
|
|
* @return PHPExcel_NamedRange[]
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function getNamedRanges()
|
|
|
|
{
|
|
|
|
return $this->namedRanges;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add named range
|
|
|
|
*
|
|
|
|
* @param PHPExcel_NamedRange $namedRange
|
|
|
|
* @return PHPExcel
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function addNamedRange(PHPExcel_NamedRange $namedRange)
|
|
|
|
{
|
2012-11-25 12:26:48 +00:00
|
|
|
if ($namedRange->getScope() == null) {
|
|
|
|
// global scope
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->namedRanges[$namedRange->getName()] = $namedRange;
|
2012-11-25 12:26:48 +00:00
|
|
|
} else {
|
|
|
|
// local scope
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get named range
|
|
|
|
*
|
|
|
|
* @param string $namedRange
|
|
|
|
* @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
|
|
|
|
* @return PHPExcel_NamedRange|null
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null)
|
|
|
|
{
|
2012-11-25 12:26:48 +00:00
|
|
|
$returnValue = null;
|
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
if ($namedRange != '' && ($namedRange !== null)) {
|
2012-11-25 12:26:48 +00:00
|
|
|
// first look for global defined name
|
2015-05-03 22:37:32 +00:00
|
|
|
if (isset($this->namedRanges[$namedRange])) {
|
|
|
|
$returnValue = $this->namedRanges[$namedRange];
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// then look for local defined name (has priority over global defined name if both names exist)
|
2015-05-03 22:37:32 +00:00
|
|
|
if (($pSheet !== null) && isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
|
|
|
|
$returnValue = $this->namedRanges[$pSheet->getTitle() . '!' . $namedRange];
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove named range
|
|
|
|
*
|
|
|
|
* @param string $namedRange
|
|
|
|
* @param PHPExcel_Worksheet|null $pSheet Scope: use null for global scope.
|
|
|
|
* @return PHPExcel
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null)
|
|
|
|
{
|
|
|
|
if ($pSheet === null) {
|
|
|
|
if (isset($this->namedRanges[$namedRange])) {
|
|
|
|
unset($this->namedRanges[$namedRange]);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-03 22:37:32 +00:00
|
|
|
if (isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
|
|
|
|
unset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get worksheet iterator
|
|
|
|
*
|
|
|
|
* @return PHPExcel_WorksheetIterator
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function getWorksheetIterator()
|
|
|
|
{
|
2012-11-25 12:26:48 +00:00
|
|
|
return new PHPExcel_WorksheetIterator($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy workbook (!= clone!)
|
|
|
|
*
|
|
|
|
* @return PHPExcel
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function copy()
|
|
|
|
{
|
2012-11-25 12:26:48 +00:00
|
|
|
$copied = clone $this;
|
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
$worksheetCount = count($this->workSheetCollection);
|
2012-11-25 12:26:48 +00:00
|
|
|
for ($i = 0; $i < $worksheetCount; ++$i) {
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->workSheetCollection[$i] = $this->workSheetCollection[$i]->copy();
|
|
|
|
$this->workSheetCollection[$i]->rebindParent($this);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $copied;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement PHP __clone to create a deep clone, not just a shallow copy.
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
foreach ($this as $key => $val) {
|
2012-11-25 12:26:48 +00:00
|
|
|
if (is_object($val) || (is_array($val))) {
|
|
|
|
$this->{$key} = unserialize(serialize($val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the workbook collection of cellXfs
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Style[]
|
|
|
|
*/
|
|
|
|
public function getCellXfCollection()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->cellXfCollection;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get cellXf by index
|
|
|
|
*
|
|
|
|
* @param int $pIndex
|
|
|
|
* @return PHPExcel_Style
|
|
|
|
*/
|
|
|
|
public function getCellXfByIndex($pIndex = 0)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->cellXfCollection[$pIndex];
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get cellXf by hash code
|
|
|
|
*
|
|
|
|
* @param string $pValue
|
|
|
|
* @return PHPExcel_Style|false
|
|
|
|
*/
|
|
|
|
public function getCellXfByHashCode($pValue = '')
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
foreach ($this->cellXfCollection as $cellXf) {
|
2012-11-25 12:26:48 +00:00
|
|
|
if ($cellXf->getHashCode() == $pValue) {
|
|
|
|
return $cellXf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if style exists in style collection
|
|
|
|
*
|
2013-04-27 13:49:08 +00:00
|
|
|
* @param PHPExcel_Style $pCellStyle
|
2012-11-25 12:26:48 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function cellXfExists($pCellStyle = null)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return in_array($pCellStyle, $this->cellXfCollection, true);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get default style
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Style
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function getDefaultStyle()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
if (isset($this->cellXfCollection[0])) {
|
|
|
|
return $this->cellXfCollection[0];
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
2012-11-26 18:01:15 +00:00
|
|
|
throw new PHPExcel_Exception('No default style found for this workbook');
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a cellXf to the workbook
|
|
|
|
*
|
|
|
|
* @param PHPExcel_Style $style
|
|
|
|
*/
|
|
|
|
public function addCellXf(PHPExcel_Style $style)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->cellXfCollection[] = $style;
|
|
|
|
$style->setIndex(count($this->cellXfCollection) - 1);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove cellXf by index. It is ensured that all cells get their xf index updated.
|
|
|
|
*
|
2015-05-03 22:37:32 +00:00
|
|
|
* @param integer $pIndex Index to cellXf
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function removeCellXfByIndex($pIndex = 0)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
if ($pIndex > count($this->cellXfCollection) - 1) {
|
2012-11-26 18:01:15 +00:00
|
|
|
throw new PHPExcel_Exception("CellXf index is out of bounds.");
|
2012-11-25 12:26:48 +00:00
|
|
|
} else {
|
|
|
|
// first remove the cellXf
|
2015-05-03 22:37:32 +00:00
|
|
|
array_splice($this->cellXfCollection, $pIndex, 1);
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// then update cellXf indexes for cells
|
2015-05-03 22:37:32 +00:00
|
|
|
foreach ($this->workSheetCollection as $worksheet) {
|
2012-11-25 12:26:48 +00:00
|
|
|
foreach ($worksheet->getCellCollection(false) as $cellID) {
|
|
|
|
$cell = $worksheet->getCell($cellID);
|
|
|
|
$xfIndex = $cell->getXfIndex();
|
2015-05-03 22:37:32 +00:00
|
|
|
if ($xfIndex > $pIndex) {
|
2012-11-25 12:26:48 +00:00
|
|
|
// decrease xf index by 1
|
|
|
|
$cell->setXfIndex($xfIndex - 1);
|
2015-05-03 22:37:32 +00:00
|
|
|
} elseif ($xfIndex == $pIndex) {
|
2012-11-25 12:26:48 +00:00
|
|
|
// set to default xf index 0
|
|
|
|
$cell->setXfIndex(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cellXf supervisor
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Style
|
|
|
|
*/
|
|
|
|
public function getCellXfSupervisor()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->cellXfSupervisor;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the workbook collection of cellStyleXfs
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Style[]
|
|
|
|
*/
|
|
|
|
public function getCellStyleXfCollection()
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->cellStyleXfCollection;
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get cellStyleXf by index
|
|
|
|
*
|
2015-05-03 22:37:32 +00:00
|
|
|
* @param integer $pIndex Index to cellXf
|
2012-11-25 12:26:48 +00:00
|
|
|
* @return PHPExcel_Style
|
|
|
|
*/
|
|
|
|
public function getCellStyleXfByIndex($pIndex = 0)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
return $this->cellStyleXfCollection[$pIndex];
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get cellStyleXf by hash code
|
|
|
|
*
|
|
|
|
* @param string $pValue
|
|
|
|
* @return PHPExcel_Style|false
|
|
|
|
*/
|
|
|
|
public function getCellStyleXfByHashCode($pValue = '')
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
foreach ($this->cellStyleXfCollection as $cellStyleXf) {
|
2012-11-25 12:26:48 +00:00
|
|
|
if ($cellStyleXf->getHashCode() == $pValue) {
|
|
|
|
return $cellStyleXf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a cellStyleXf to the workbook
|
|
|
|
*
|
|
|
|
* @param PHPExcel_Style $pStyle
|
|
|
|
*/
|
|
|
|
public function addCellStyleXf(PHPExcel_Style $pStyle)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->cellStyleXfCollection[] = $pStyle;
|
|
|
|
$pStyle->setIndex(count($this->cellStyleXfCollection) - 1);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove cellStyleXf by index
|
|
|
|
*
|
2015-05-03 22:37:32 +00:00
|
|
|
* @param integer $pIndex Index to cellXf
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2012-11-25 12:26:48 +00:00
|
|
|
*/
|
|
|
|
public function removeCellStyleXfByIndex($pIndex = 0)
|
|
|
|
{
|
2015-05-03 22:37:32 +00:00
|
|
|
if ($pIndex > count($this->cellStyleXfCollection) - 1) {
|
2012-11-26 18:01:15 +00:00
|
|
|
throw new PHPExcel_Exception("CellStyleXf index is out of bounds.");
|
2012-11-25 12:26:48 +00:00
|
|
|
} else {
|
2015-05-03 22:37:32 +00:00
|
|
|
array_splice($this->cellStyleXfCollection, $pIndex, 1);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
|
|
|
|
* and columns in the workbook
|
|
|
|
*/
|
|
|
|
public function garbageCollect()
|
|
|
|
{
|
|
|
|
// how many references are there to each cellXf ?
|
|
|
|
$countReferencesCellXf = array();
|
2015-05-03 22:37:32 +00:00
|
|
|
foreach ($this->cellXfCollection as $index => $cellXf) {
|
2012-11-25 12:26:48 +00:00
|
|
|
$countReferencesCellXf[$index] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->getWorksheetIterator() as $sheet) {
|
|
|
|
// from cells
|
|
|
|
foreach ($sheet->getCellCollection(false) as $cellID) {
|
|
|
|
$cell = $sheet->getCell($cellID);
|
|
|
|
++$countReferencesCellXf[$cell->getXfIndex()];
|
|
|
|
}
|
|
|
|
|
|
|
|
// from row dimensions
|
|
|
|
foreach ($sheet->getRowDimensions() as $rowDimension) {
|
|
|
|
if ($rowDimension->getXfIndex() !== null) {
|
|
|
|
++$countReferencesCellXf[$rowDimension->getXfIndex()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from column dimensions
|
|
|
|
foreach ($sheet->getColumnDimensions() as $columnDimension) {
|
|
|
|
++$countReferencesCellXf[$columnDimension->getXfIndex()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove cellXfs without references and create mapping so we can update xfIndex
|
|
|
|
// for all cells and columns
|
|
|
|
$countNeededCellXfs = 0;
|
2015-05-03 22:37:32 +00:00
|
|
|
foreach ($this->cellXfCollection as $index => $cellXf) {
|
2012-11-25 12:26:48 +00:00
|
|
|
if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
|
|
|
|
++$countNeededCellXfs;
|
|
|
|
} else {
|
2015-05-03 22:37:32 +00:00
|
|
|
unset($this->cellXfCollection[$index]);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
$map[$index] = $countNeededCellXfs - 1;
|
|
|
|
}
|
2015-05-03 22:37:32 +00:00
|
|
|
$this->cellXfCollection = array_values($this->cellXfCollection);
|
2012-11-25 12:26:48 +00:00
|
|
|
|
|
|
|
// update the index for all cellXfs
|
2015-05-03 22:37:32 +00:00
|
|
|
foreach ($this->cellXfCollection as $i => $cellXf) {
|
2012-11-25 12:26:48 +00:00
|
|
|
$cellXf->setIndex($i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure there is always at least one cellXf (there should be)
|
2015-05-03 22:37:32 +00:00
|
|
|
if (empty($this->cellXfCollection)) {
|
|
|
|
$this->cellXfCollection[] = new PHPExcel_Style();
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the xfIndex for all cells, row dimensions, column dimensions
|
|
|
|
foreach ($this->getWorksheetIterator() as $sheet) {
|
|
|
|
// for all cells
|
|
|
|
foreach ($sheet->getCellCollection(false) as $cellID) {
|
|
|
|
$cell = $sheet->getCell($cellID);
|
2015-05-03 22:37:32 +00:00
|
|
|
$cell->setXfIndex($map[$cell->getXfIndex()]);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// for all row dimensions
|
|
|
|
foreach ($sheet->getRowDimensions() as $rowDimension) {
|
|
|
|
if ($rowDimension->getXfIndex() !== null) {
|
2015-05-03 22:37:32 +00:00
|
|
|
$rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for all column dimensions
|
|
|
|
foreach ($sheet->getColumnDimensions() as $columnDimension) {
|
2015-05-03 22:37:32 +00:00
|
|
|
$columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]);
|
2012-11-25 12:26:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 22:37:32 +00:00
|
|
|
// also do garbage collection for all the sheets
|
2012-11-25 12:26:48 +00:00
|
|
|
$sheet->garbageCollect();
|
|
|
|
}
|
|
|
|
}
|
2010-08-26 19:14:53 +00:00
|
|
|
|
2013-04-27 13:49:08 +00:00
|
|
|
/**
|
|
|
|
* Return the unique ID value assigned to this spreadsheet workbook
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-03 22:37:32 +00:00
|
|
|
public function getID()
|
|
|
|
{
|
|
|
|
return $this->uniqueID;
|
2013-02-04 17:26:27 +00:00
|
|
|
}
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|