58 if (!isset(self::$_instance) || is_null(self::$_instance)) {
80 $aCellCollection = $pSheet->getCellCollection();
89 $highestColumn = $pSheet->getHighestColumn();
90 $highestRow = $pSheet->getHighestRow();
94 for ($i = 1; $i <= $highestRow - 1; ++$i) {
97 $pSheet->removeConditionalStyles($coordinate);
98 if ($pSheet->cellExists($coordinate)) {
100 $pSheet->getCell($coordinate)->setXfIndex(0);
107 if ($pNumRows < 0 && $beforeRow - 1 + $pNumRows > 0) {
109 for ($j = $beforeRow + $pNumRows; $j <= $beforeRow - 1; ++$j) {
111 $pSheet->removeConditionalStyles($coordinate);
112 if ($pSheet->cellExists($coordinate)) {
114 $pSheet->getCell($coordinate)->setXfIndex(0);
122 while (($cellID = ($pNumCols < 0 || $pNumRows < 0) ? array_shift($aCellCollection) : array_pop($aCellCollection))) {
123 $cell = $pSheet->getCell($cellID);
130 ($cell->getRow() >= $beforeRow)) {
133 $pSheet->getCell($newCoordinates)->setXfIndex($cell->getXfIndex());
134 $cell->setXfIndex(0);
139 $pSheet->getCell($newCoordinates)
141 $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle()));
144 $pSheet->getCell($newCoordinates)->setValue($cell->getValue());
148 $pSheet->getCell($cell->getCoordinate())->setValue(
'');
156 $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle()));
164 $highestColumn = $pSheet->getHighestColumn();
165 $highestRow = $pSheet->getHighestRow();
168 for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
172 if ($pSheet->cellExists($coordinate)) {
173 $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
174 $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
175 $pSheet->getConditionalStyles($coordinate) :
false;
177 $pSheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex);
178 if ($conditionalStyles) {
180 foreach ($conditionalStyles as $conditionalStyle) {
181 $cloned[] = clone $conditionalStyle;
191 if ($pNumRows > 0 && $beforeRow - 1 > 0) {
196 if ($pSheet->cellExists($coordinate)) {
197 $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
198 $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
199 $pSheet->getConditionalStyles($coordinate) :
false;
200 for ($j = $beforeRow; $j <= $beforeRow - 1 + $pNumRows; ++$j) {
202 if ($conditionalStyles) {
204 foreach ($conditionalStyles as $conditionalStyle) {
205 $cloned[] = clone $conditionalStyle;
216 $aColumnDimensions = array_reverse($pSheet->getColumnDimensions(),
true);
217 if (count($aColumnDimensions) > 0) {
218 foreach ($aColumnDimensions as $objColumnDimension) {
219 $newReference = $this->
updateCellReference($objColumnDimension->getColumnIndex() .
'1', $pBefore, $pNumCols, $pNumRows);
221 if ($objColumnDimension->getColumnIndex() != $newReference) {
222 $objColumnDimension->setColumnIndex($newReference);
225 $pSheet->refreshColumnDimensions();
230 $aRowDimensions = array_reverse($pSheet->getRowDimensions(),
true);
231 if (count($aRowDimensions) > 0) {
232 foreach ($aRowDimensions as $objRowDimension) {
233 $newReference = $this->
updateCellReference(
'A' . $objRowDimension->getRowIndex(), $pBefore, $pNumCols, $pNumRows);
235 if ($objRowDimension->getRowIndex() != $newReference) {
236 $objRowDimension->setRowIndex($newReference);
239 $pSheet->refreshRowDimensions();
241 $copyDimension = $pSheet->getRowDimension($beforeRow - 1);
242 for ($i = $beforeRow; $i <= $beforeRow - 1 + $pNumRows; ++$i) {
243 $newDimension = $pSheet->getRowDimension($i);
244 $newDimension->setRowHeight($copyDimension->getRowHeight());
245 $newDimension->setVisible($copyDimension->getVisible());
246 $newDimension->setOutlineLevel($copyDimension->getOutlineLevel());
247 $newDimension->setCollapsed($copyDimension->getCollapsed());
253 $aBreaks = array_reverse($pSheet->getBreaks(),
true);
254 foreach ($aBreaks as $key => $value) {
256 if ($key != $newReference) {
257 $pSheet->setBreak( $newReference, $value );
263 $aComments = $pSheet->getComments();
264 $aNewComments = array();
265 foreach ($aComments as $key => &$value) {
267 $aNewComments[$newReference] = $value;
269 $pSheet->setComments($aNewComments);
272 $aHyperlinkCollection = array_reverse($pSheet->getHyperlinkCollection(),
true);
273 foreach ($aHyperlinkCollection as $key => $value) {
275 if ($key != $newReference) {
276 $pSheet->setHyperlink( $newReference, $value );
277 $pSheet->setHyperlink( $key, null );
283 $aDataValidationCollection = array_reverse($pSheet->getDataValidationCollection(),
true);
284 foreach ($aDataValidationCollection as $key => $value) {
286 if ($key != $newReference) {
287 $pSheet->setDataValidation( $newReference, $value );
288 $pSheet->setDataValidation( $key, null );
294 $aMergeCells = $pSheet->getMergeCells();
295 $aNewMergeCells = array();
296 foreach ($aMergeCells as $key => &$value) {
298 $aNewMergeCells[$newReference] = $newReference;
300 $pSheet->setMergeCells($aNewMergeCells);
304 $aProtectedCells = array_reverse($pSheet->getProtectedCells(),
true);
305 foreach ($aProtectedCells as $key => $value) {
307 if ($key != $newReference) {
308 $pSheet->protectCells( $newReference, $value,
true );
309 $pSheet->unprotectCells( $key );
315 if ($pSheet->getAutoFilter() !=
'') {
316 $pSheet->setAutoFilter( $this->
updateCellReference($pSheet->getAutoFilter(), $pBefore, $pNumCols, $pNumRows) );
321 if ($pSheet->getFreezePane() !=
'') {
322 $pSheet->freezePane( $this->
updateCellReference($pSheet->getFreezePane(), $pBefore, $pNumCols, $pNumRows) );
327 if ($pSheet->getPageSetup()->isPrintAreaSet()) {
328 $pSheet->getPageSetup()->setPrintArea( $this->
updateCellReference($pSheet->getPageSetup()->getPrintArea(), $pBefore, $pNumCols, $pNumRows) );
333 $aDrawings = $pSheet->getDrawingCollection();
334 foreach ($aDrawings as $objDrawing) {
335 $newReference = $this->
updateCellReference($objDrawing->getCoordinates(), $pBefore, $pNumCols, $pNumRows);
336 if ($objDrawing->getCoordinates() != $newReference) {
337 $objDrawing->setCoordinates($newReference);
343 if (count($pSheet->getParent()->getNamedRanges()) > 0) {
344 foreach ($pSheet->getParent()->getNamedRanges() as $namedRange) {
345 if ($namedRange->getWorksheet()->getHashCode() == $pSheet->getHashCode()) {
346 $namedRange->setRange(
354 $pSheet->garbageCollect();
369 $formulaBlocks = explode(
'"',$pFormula);
371 foreach($formulaBlocks as &$formulaBlock) {
375 $newCellTokens = $cellTokens = array();
377 $matchCount = preg_match_all(
'/'.self::REFHELPER_REGEXP_ROWRANGE.
'/i',
' '.$formulaBlock.
' ', $matches, PREG_SET_ORDER);
378 if ($matchCount > 0) {
379 foreach($matches as $match) {
380 $fromString = ($match[2] >
'') ? $match[2].
'!' :
'';
381 $fromString .= $match[3].
':'.$match[4];
382 $modified3 = substr($this->
updateCellReference(
'$A'.$match[3],$pBefore,$pNumCols,$pNumRows),2);
383 $modified4 = substr($this->
updateCellReference(
'$A'.$match[4],$pBefore,$pNumCols,$pNumRows),2);
385 if ($match[3].
':'.$match[4] !== $modified3.
':'.$modified4) {
386 if (($match[2] ==
'') || (trim($match[2],
"'") == $sheetName)) {
387 $toString = ($match[2] >
'') ? $match[2].
'!' :
'';
388 $toString .= $modified3.
':'.$modified4;
391 $row = 10000000+trim($match[3],
'$');
392 $cellIndex = $column.$row;
394 $newCellTokens[$cellIndex] = preg_quote($toString);
395 $cellTokens[$cellIndex] =
'/(?<!\d)'.preg_quote($fromString).
'(?!\d)/i';
402 $matchCount = preg_match_all(
'/'.self::REFHELPER_REGEXP_COLRANGE.
'/i',
' '.$formulaBlock.
' ', $matches, PREG_SET_ORDER);
403 if ($matchCount > 0) {
404 foreach($matches as $match) {
405 $fromString = ($match[2] >
'') ? $match[2].
'!' :
'';
406 $fromString .= $match[3].
':'.$match[4];
407 $modified3 = substr($this->
updateCellReference($match[3].
'$1',$pBefore,$pNumCols,$pNumRows),0,-2);
408 $modified4 = substr($this->
updateCellReference($match[4].
'$1',$pBefore,$pNumCols,$pNumRows),0,-2);
410 if ($match[3].
':'.$match[4] !== $modified3.
':'.$modified4) {
411 if (($match[2] ==
'') || (trim($match[2],
"'") == $sheetName)) {
412 $toString = ($match[2] >
'') ? $match[2].
'!' :
'';
413 $toString .= $modified3.
':'.$modified4;
417 $cellIndex = $column.$row;
419 $newCellTokens[$cellIndex] = preg_quote($toString);
420 $cellTokens[$cellIndex] =
'/(?<![A-Z])'.preg_quote($fromString).
'(?![A-Z])/i';
427 $matchCount = preg_match_all(
'/'.self::REFHELPER_REGEXP_CELLRANGE.
'/i',
' '.$formulaBlock.
' ', $matches, PREG_SET_ORDER);
428 if ($matchCount > 0) {
429 foreach($matches as $match) {
430 $fromString = ($match[2] >
'') ? $match[2].
'!' :
'';
431 $fromString .= $match[3].
':'.$match[4];
435 if ($match[3].$match[4] !== $modified3.$modified4) {
436 if (($match[2] ==
'') || (trim($match[2],
"'") == $sheetName)) {
437 $toString = ($match[2] >
'') ? $match[2].
'!' :
'';
438 $toString .= $modified3.
':'.$modified4;
443 $cellIndex = $column.$row;
445 $newCellTokens[$cellIndex] = preg_quote($toString);
446 $cellTokens[$cellIndex] =
'/(?<![A-Z])'.preg_quote($fromString).
'(?!\d)/i';
453 $matchCount = preg_match_all(
'/'.self::REFHELPER_REGEXP_CELLREF.
'/i',
' '.$formulaBlock.
' ', $matches, PREG_SET_ORDER);
454 if ($matchCount > 0) {
455 foreach($matches as $match) {
456 $fromString = ($match[2] >
'') ? $match[2].
'!' :
'';
457 $fromString .= $match[3];
460 if ($match[3] !== $modified3) {
461 if (($match[2] ==
'') || (trim($match[2],
"'") == $sheetName)) {
462 $toString = ($match[2] >
'') ? $match[2].
'!' :
'';
463 $toString .= $modified3;
468 $cellIndex = $column.$row;
470 $newCellTokens[$cellIndex] = preg_quote($toString);
471 $cellTokens[$cellIndex] =
'/(?<![A-Z])'.preg_quote($fromString).
'(?!\d)/i';
477 if ($adjustCount > 0) {
479 krsort($newCellTokens);
481 $formulaBlock = str_replace(
'\\',
'',preg_replace($cellTokens,$newCellTokens,$formulaBlock));
485 unset($formulaBlock);
488 return implode(
'"',$formulaBlocks);
503 if (strpos($pCellRange,
"!") !==
false) {
506 } elseif (strpos($pCellRange,
':') ===
false && strpos($pCellRange,
',') ===
false) {
509 } elseif (strpos($pCellRange,
':') !==
false || strpos($pCellRange,
',') !==
false) {
511 return $this->
_updateCellRange($pCellRange, $pBefore, $pNumCols, $pNumRows);
526 if ($oldName ==
'') {
531 foreach ($sheet->getCellCollection(
false) as $cellID) {
532 $cell = $sheet->getCell($cellID);
534 $formula = $cell->getValue();
535 if (strpos($formula, $oldName) !==
false) {
536 $formula = str_replace(
"'" . $oldName .
"'!",
"'" . $newName .
"'!", $formula);
537 $formula = str_replace($oldName .
"!", $newName .
"!", $formula);
555 private function _updateCellRange($pCellRange =
'A1:A1', $pBefore =
'A1', $pNumCols = 0, $pNumRows = 0) {
556 if (strpos($pCellRange,
':') !==
false || strpos($pCellRange,
',') !==
false) {
560 for ($i = 0; $i < $ic; ++$i) {
561 $jc = count($range[$i]);
562 for ($j = 0; $j < $jc; ++$j) {
563 if (ctype_alpha($range[$i][$j])) {
565 $range[$i][$j] = $r[0];
566 } elseif(ctype_digit($range[$i][$j])) {
568 $range[$i][$j] = $r[1];
578 throw new Exception(
"Only cell ranges may be passed to this method.");
593 if (strpos($pCellReference,
':') ===
false && strpos($pCellReference,
',') ===
false) {
601 $updateColumn = (($newColumn{0} !=
'$') && ($beforeColumn{0} !=
'$') &&
604 $updateRow = (($newRow{0} !=
'$') && ($beforeRow{0} !=
'$') &&
605 $newRow >= $beforeRow);
614 $newRow = $newRow + $pNumRows;
618 return $newColumn . $newRow;
620 throw new Exception(
"Only single cell references may be passed to this method.");
630 throw new Exception(
"Cloning a Singleton is not allowed!");