30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../');
38 require_once PHPEXCEL_ROOT .
'PHPExcel/Worksheet.php';
41 require_once PHPEXCEL_ROOT .
'PHPExcel/Cell.php';
44 require_once PHPEXCEL_ROOT .
'PHPExcel/Cell/DataType.php';
47 require_once PHPEXCEL_ROOT .
'PHPExcel/Style.php';
50 require_once PHPEXCEL_ROOT .
'PHPExcel/Worksheet/Drawing.php';
53 require_once PHPEXCEL_ROOT .
'PHPExcel/Calculation/FormulaParser.php';
56 require_once PHPEXCEL_ROOT .
'PHPExcel/Calculation/FormulaToken.php';
81 if (!isset(self::$_instance) || is_null(self::$_instance)) {
109 $aCellCollection = $pSheet->getCellCollection();
118 $highestColumn = $pSheet->getHighestColumn();
119 $highestRow = $pSheet->getHighestRow();
123 for ($i = 1; $i <= $highestRow - 1; ++$i) {
126 $pSheet->removeConditionalStyles($coordinate);
127 if ($pSheet->cellExists($coordinate)) {
129 $pSheet->getCell($coordinate)->setXfIndex(0);
136 if ($pNumRows < 0 && $beforeRow - 1 + $pNumRows > 0) {
138 for ($j = $beforeRow + $pNumRows; $j <= $beforeRow - 1; ++$j) {
140 $pSheet->removeConditionalStyles($coordinate);
141 if ($pSheet->cellExists($coordinate)) {
143 $pSheet->getCell($coordinate)->setXfIndex(0);
151 while ( ($cell = ($pNumCols < 0 || $pNumRows < 0) ? array_shift($aCellCollection) : array_pop($aCellCollection)) ) {
158 ($cell->getRow() >= $beforeRow)
162 $pSheet->getCell($newCoordinates)->setXfIndex($cell->getXfIndex());
163 $cell->setXfIndex(0);
168 $pSheet->setCellValue(
174 $pSheet->setCellValue($newCoordinates, $cell->getValue());
178 $pSheet->setCellValue($cell->getCoordinate(),
'');
184 $highestColumn = $pSheet->getHighestColumn();
185 $highestRow = $pSheet->getHighestRow();
188 for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
192 if ($pSheet->cellExists($coordinate)) {
193 $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
194 $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
195 $pSheet->getConditionalStyles($coordinate) :
false;
197 $pSheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex);
198 if ($conditionalStyles) {
200 foreach ($conditionalStyles as $conditionalStyle) {
201 $cloned[] = clone $conditionalStyle;
211 if ($pNumRows > 0 && $beforeRow - 1 > 0) {
216 if ($pSheet->cellExists($coordinate)) {
217 $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
218 $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
219 $pSheet->getConditionalStyles($coordinate) :
false;
220 for ($j = $beforeRow; $j <= $beforeRow - 1 + $pNumRows; ++$j) {
222 if ($conditionalStyles) {
224 foreach ($conditionalStyles as $conditionalStyle) {
225 $cloned[] = clone $conditionalStyle;
236 $aColumnDimensions = array_reverse($pSheet->getColumnDimensions(),
true);
237 if (count($aColumnDimensions) > 0) {
238 foreach ($aColumnDimensions as $objColumnDimension) {
239 $newReference = $this->
updateCellReference($objColumnDimension->getColumnIndex() .
'1', $pBefore, $pNumCols, $pNumRows);
241 if ($objColumnDimension->getColumnIndex() != $newReference) {
242 $objColumnDimension->setColumnIndex($newReference);
245 $pSheet->refreshColumnDimensions();
250 $aRowDimensions = array_reverse($pSheet->getRowDimensions(),
true);
251 if (count($aRowDimensions) > 0) {
252 foreach ($aRowDimensions as $objRowDimension) {
253 $newReference = $this->
updateCellReference(
'A' . $objRowDimension->getRowIndex(), $pBefore, $pNumCols, $pNumRows);
255 if ($objRowDimension->getRowIndex() != $newReference) {
256 $objRowDimension->setRowIndex($newReference);
259 $pSheet->refreshRowDimensions();
261 $copyDimension = $pSheet->getRowDimension($beforeRow - 1);
262 for ($i = $beforeRow; $i <= $beforeRow - 1 + $pNumRows; ++$i) {
263 $newDimension = $pSheet->getRowDimension($i);
264 $newDimension->setRowHeight($copyDimension->getRowHeight());
265 $newDimension->setVisible($copyDimension->getVisible());
266 $newDimension->setOutlineLevel($copyDimension->getOutlineLevel());
267 $newDimension->setCollapsed($copyDimension->getCollapsed());
273 $aBreaks = array_reverse($pSheet->getBreaks(),
true);
274 foreach ($aBreaks as
$key => $value) {
276 if (
$key != $newReference) {
277 $pSheet->setBreak( $newReference, $value );
284 $aHyperlinkCollection = array_reverse($pSheet->getHyperlinkCollection(),
true);
285 foreach ($aHyperlinkCollection as
$key => $value) {
287 if (
$key != $newReference) {
288 $pSheet->setHyperlink( $newReference, $value );
289 $pSheet->setHyperlink(
$key, null );
295 $aDataValidationCollection = array_reverse($pSheet->getDataValidationCollection(),
true);
296 foreach ($aDataValidationCollection as
$key => $value) {
298 if (
$key != $newReference) {
299 $pSheet->setDataValidation( $newReference, $value );
300 $pSheet->setDataValidation(
$key, null );
306 $aMergeCells = array_reverse($pSheet->getMergeCells(),
true);
307 foreach ($aMergeCells as
$key => $value) {
309 if (
$key != $newReference) {
310 $pSheet->mergeCells( $newReference );
311 $pSheet->unmergeCells(
$key );
317 $aProtectedCells = array_reverse($pSheet->getProtectedCells(),
true);
318 foreach ($aProtectedCells as
$key => $value) {
320 if (
$key != $newReference) {
321 $pSheet->protectCells( $newReference, $value,
true );
322 $pSheet->unprotectCells(
$key );
328 if ($pSheet->getAutoFilter() !=
'') {
329 $pSheet->setAutoFilter( $this->
updateCellReference($pSheet->getAutoFilter(), $pBefore, $pNumCols, $pNumRows) );
334 if ($pSheet->getFreezePane() !=
'') {
335 $pSheet->freezePane( $this->
updateCellReference($pSheet->getFreezePane(), $pBefore, $pNumCols, $pNumRows) );
340 if ($pSheet->getPageSetup()->isPrintAreaSet()) {
341 $pSheet->getPageSetup()->setPrintArea( $this->
updateCellReference($pSheet->getPageSetup()->getPrintArea(), $pBefore, $pNumCols, $pNumRows) );
346 $aDrawings = $pSheet->getDrawingCollection();
347 foreach ($aDrawings as $objDrawing) {
348 $newReference = $this->
updateCellReference($objDrawing->getCoordinates(), $pBefore, $pNumCols, $pNumRows);
349 if ($objDrawing->getCoordinates() != $newReference) {
350 $objDrawing->setCoordinates($newReference);
356 if (count($pSheet->getParent()->getNamedRanges()) > 0) {
357 foreach ($pSheet->getParent()->getNamedRanges() as $namedRange) {
358 if ($namedRange->getWorksheet()->getHashCode() == $pSheet->getHashCode()) {
359 $namedRange->setRange(
367 $pSheet->garbageCollect();
382 $executableFormulaArray = array();
386 $newCellTokens = $cellTokens = array();
388 foreach($tokenisedFormula as $token) {
391 $cellTokens[] =
'/'.$token.
'/';
396 $formulaBlocks = explode(
'"',$pFormula);
398 foreach($formulaBlocks as $formulaBlockKey => $formulaBlock) {
400 if (($i++ % 2) == 0) {
401 $formulaBlocks[$formulaBlockKey] = preg_replace($cellTokens,$newCellTokens,$formulaBlock);
405 return implode(
'"',$formulaBlocks);
420 if (strpos($pCellRange,
"!") !==
false) {
423 }
elseif (strpos($pCellRange,
':') ===
false && strpos($pCellRange,
',') ===
false) {
426 }
else if (strpos($pCellRange,
':') !==
false || strpos($pCellRange,
',') !==
false) {
428 return $this->
_updateCellRange($pCellRange, $pBefore, $pNumCols, $pNumRows);
444 foreach ($sheet->getCellCollection(
false) as $cell) {
446 $formula = $cell->getValue();
447 if (strpos($formula, $oldName) !==
false) {
448 $formula = str_replace(
"'" . $oldName .
"'!",
"'" . $newName .
"'!", $formula);
449 $formula = str_replace($oldName .
"!", $newName .
"!", $formula);
467 private function _updateCellRange($pCellRange =
'A1:A1', $pBefore =
'A1', $pNumCols = 0, $pNumRows = 0) {
468 if (strpos($pCellRange,
':') !==
false || strpos($pCellRange,
',') !==
false) {
471 for ($i = 0; $i < count($range); $i++) {
472 for ($j = 0; $j < count($range[$i]); $j++) {
480 throw new Exception(
"Only cell ranges may be passed to this method.");
495 if (strpos($pCellReference,
':') ===
false && strpos($pCellReference,
',') ===
false) {
507 if ($newColumn ==
'' && $newRow ==
'')
509 return $pCellReference;
514 && (strpos($newColumn,
'$') ===
false)
515 && (strpos($beforeColumn,
'$') ===
false);
517 $updateRow = ($newRow >= $beforeRow)
518 && (strpos($newRow,
'$') ===
false)
519 && (strpos($beforeRow,
'$') ===
false);
528 $newRow = $newRow + $pNumRows;
532 return $newColumn . $newRow;
534 throw new Exception(
"Only single cell references may be passed to this method.");
544 throw new Exception(
"Cloning a Singleton is not allowed!");