123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913 |
- <?php
- class PHPExcel_ReferenceHelper
- {
-
-
- const REFHELPER_REGEXP_CELLREF = '((\w*|\'[^!]*\')!)?(?<![:a-z\$])(\$?[a-z]{1,3}\$?\d+)(?=[^:!\d\'])';
- const REFHELPER_REGEXP_CELLRANGE = '((\w*|\'[^!]*\')!)?(\$?[a-z]{1,3}\$?\d+):(\$?[a-z]{1,3}\$?\d+)';
- const REFHELPER_REGEXP_ROWRANGE = '((\w*|\'[^!]*\')!)?(\$?\d+):(\$?\d+)';
- const REFHELPER_REGEXP_COLRANGE = '((\w*|\'[^!]*\')!)?(\$?[a-z]{1,3}):(\$?[a-z]{1,3})';
-
- private static $instance;
-
- public static function getInstance()
- {
- if (!isset(self::$instance) || (self::$instance === null)) {
- self::$instance = new PHPExcel_ReferenceHelper();
- }
- return self::$instance;
- }
-
- protected function __construct()
- {
- }
-
- public static function columnSort($a, $b)
- {
- return strcasecmp(strlen($a) . $a, strlen($b) . $b);
- }
-
- public static function columnReverseSort($a, $b)
- {
- return 1 - strcasecmp(strlen($a) . $a, strlen($b) . $b);
- }
-
- public static function cellSort($a, $b)
- {
- sscanf($a, '%[A-Z]%d', $ac, $ar);
- sscanf($b, '%[A-Z]%d', $bc, $br);
- if ($ar == $br) {
- return strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);
- }
- return ($ar < $br) ? -1 : 1;
- }
-
- public static function cellReverseSort($a, $b)
- {
- sscanf($a, '%[A-Z]%d', $ac, $ar);
- sscanf($b, '%[A-Z]%d', $bc, $br);
- if ($ar == $br) {
- return 1 - strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);
- }
- return ($ar < $br) ? 1 : -1;
- }
-
- private static function cellAddressInDeleteRange($cellAddress, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)
- {
- list($cellColumn, $cellRow) = PHPExcel_Cell::coordinateFromString($cellAddress);
- $cellColumnIndex = PHPExcel_Cell::columnIndexFromString($cellColumn);
-
- if ($pNumRows < 0 &&
- ($cellRow >= ($beforeRow + $pNumRows)) &&
- ($cellRow < $beforeRow)) {
- return true;
- } elseif ($pNumCols < 0 &&
- ($cellColumnIndex >= ($beforeColumnIndex + $pNumCols)) &&
- ($cellColumnIndex < $beforeColumnIndex)) {
- return true;
- }
- return false;
- }
-
- protected function adjustPageBreaks(PHPExcel_Worksheet $pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aBreaks = $pSheet->getBreaks();
- ($pNumCols > 0 || $pNumRows > 0) ?
- uksort($aBreaks, array('PHPExcel_ReferenceHelper','cellReverseSort')) :
- uksort($aBreaks, array('PHPExcel_ReferenceHelper','cellSort'));
- foreach ($aBreaks as $key => $value) {
- if (self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) {
-
-
- $pSheet->setBreak($key, PHPExcel_Worksheet::BREAK_NONE);
- } else {
-
-
- $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
- if ($key != $newReference) {
- $pSheet->setBreak($newReference, $value)
- ->setBreak($key, PHPExcel_Worksheet::BREAK_NONE);
- }
- }
- }
- }
-
- protected function adjustComments($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aComments = $pSheet->getComments();
- $aNewComments = array();
- foreach ($aComments as $key => &$value) {
-
- if (!self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) {
-
- $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
- $aNewComments[$newReference] = $value;
- }
- }
-
- $pSheet->setComments($aNewComments);
- }
-
- protected function adjustHyperlinks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aHyperlinkCollection = $pSheet->getHyperlinkCollection();
- ($pNumCols > 0 || $pNumRows > 0) ? uksort($aHyperlinkCollection, array('PHPExcel_ReferenceHelper','cellReverseSort')) : uksort($aHyperlinkCollection, array('PHPExcel_ReferenceHelper','cellSort'));
- foreach ($aHyperlinkCollection as $key => $value) {
- $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
- if ($key != $newReference) {
- $pSheet->setHyperlink($newReference, $value);
- $pSheet->setHyperlink($key, null);
- }
- }
- }
-
- protected function adjustDataValidations($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aDataValidationCollection = $pSheet->getDataValidationCollection();
- ($pNumCols > 0 || $pNumRows > 0) ? uksort($aDataValidationCollection, array('PHPExcel_ReferenceHelper','cellReverseSort')) : uksort($aDataValidationCollection, array('PHPExcel_ReferenceHelper','cellSort'));
-
- foreach ($aDataValidationCollection as $key => $value) {
- $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
- if ($key != $newReference) {
- $pSheet->setDataValidation($newReference, $value);
- $pSheet->setDataValidation($key, null);
- }
- }
- }
-
- protected function adjustMergeCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aMergeCells = $pSheet->getMergeCells();
- $aNewMergeCells = array();
- foreach ($aMergeCells as $key => &$value) {
- $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
- $aNewMergeCells[$newReference] = $newReference;
- }
- $pSheet->setMergeCells($aNewMergeCells);
- }
-
- protected function adjustProtectedCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aProtectedCells = $pSheet->getProtectedCells();
- ($pNumCols > 0 || $pNumRows > 0) ?
- uksort($aProtectedCells, array('PHPExcel_ReferenceHelper','cellReverseSort')) :
- uksort($aProtectedCells, array('PHPExcel_ReferenceHelper','cellSort'));
- foreach ($aProtectedCells as $key => $value) {
- $newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows);
- if ($key != $newReference) {
- $pSheet->protectCells($newReference, $value, true);
- $pSheet->unprotectCells($key);
- }
- }
- }
-
- protected function adjustColumnDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aColumnDimensions = array_reverse($pSheet->getColumnDimensions(), true);
- if (!empty($aColumnDimensions)) {
- foreach ($aColumnDimensions as $objColumnDimension) {
- $newReference = $this->updateCellReference($objColumnDimension->getColumnIndex() . '1', $pBefore, $pNumCols, $pNumRows);
- list($newReference) = PHPExcel_Cell::coordinateFromString($newReference);
- if ($objColumnDimension->getColumnIndex() != $newReference) {
- $objColumnDimension->setColumnIndex($newReference);
- }
- }
- $pSheet->refreshColumnDimensions();
- }
- }
-
- protected function adjustRowDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows)
- {
- $aRowDimensions = array_reverse($pSheet->getRowDimensions(), true);
- if (!empty($aRowDimensions)) {
- foreach ($aRowDimensions as $objRowDimension) {
- $newReference = $this->updateCellReference('A' . $objRowDimension->getRowIndex(), $pBefore, $pNumCols, $pNumRows);
- list(, $newReference) = PHPExcel_Cell::coordinateFromString($newReference);
- if ($objRowDimension->getRowIndex() != $newReference) {
- $objRowDimension->setRowIndex($newReference);
- }
- }
- $pSheet->refreshRowDimensions();
- $copyDimension = $pSheet->getRowDimension($beforeRow - 1);
- for ($i = $beforeRow; $i <= $beforeRow - 1 + $pNumRows; ++$i) {
- $newDimension = $pSheet->getRowDimension($i);
- $newDimension->setRowHeight($copyDimension->getRowHeight());
- $newDimension->setVisible($copyDimension->getVisible());
- $newDimension->setOutlineLevel($copyDimension->getOutlineLevel());
- $newDimension->setCollapsed($copyDimension->getCollapsed());
- }
- }
- }
-
- public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, PHPExcel_Worksheet $pSheet = null)
- {
- $remove = ($pNumCols < 0 || $pNumRows < 0);
- $aCellCollection = $pSheet->getCellCollection();
-
- $beforeColumn = 'A';
- $beforeRow = 1;
- list($beforeColumn, $beforeRow) = PHPExcel_Cell::coordinateFromString($pBefore);
- $beforeColumnIndex = PHPExcel_Cell::columnIndexFromString($beforeColumn);
-
- $highestColumn = $pSheet->getHighestColumn();
- $highestRow = $pSheet->getHighestRow();
-
- if ($pNumCols < 0 && $beforeColumnIndex - 2 + $pNumCols > 0) {
- for ($i = 1; $i <= $highestRow - 1; ++$i) {
- for ($j = $beforeColumnIndex - 1 + $pNumCols; $j <= $beforeColumnIndex - 2; ++$j) {
- $coordinate = PHPExcel_Cell::stringFromColumnIndex($j) . $i;
- $pSheet->removeConditionalStyles($coordinate);
- if ($pSheet->cellExists($coordinate)) {
- $pSheet->getCell($coordinate)->setValueExplicit('', PHPExcel_Cell_DataType::TYPE_NULL);
- $pSheet->getCell($coordinate)->setXfIndex(0);
- }
- }
- }
- }
-
- if ($pNumRows < 0 && $beforeRow - 1 + $pNumRows > 0) {
- for ($i = $beforeColumnIndex - 1; $i <= PHPExcel_Cell::columnIndexFromString($highestColumn) - 1; ++$i) {
- for ($j = $beforeRow + $pNumRows; $j <= $beforeRow - 1; ++$j) {
- $coordinate = PHPExcel_Cell::stringFromColumnIndex($i) . $j;
- $pSheet->removeConditionalStyles($coordinate);
- if ($pSheet->cellExists($coordinate)) {
- $pSheet->getCell($coordinate)->setValueExplicit('', PHPExcel_Cell_DataType::TYPE_NULL);
- $pSheet->getCell($coordinate)->setXfIndex(0);
- }
- }
- }
- }
-
- if ($remove) {
-
- $aCellCollection = array_reverse($aCellCollection);
- }
- while ($cellID = array_pop($aCellCollection)) {
- $cell = $pSheet->getCell($cellID);
- $cellIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
- if ($cellIndex-1 + $pNumCols < 0) {
- continue;
- }
-
- $newCoordinates = PHPExcel_Cell::stringFromColumnIndex($cellIndex-1 + $pNumCols) . ($cell->getRow() + $pNumRows);
-
- if (($cellIndex >= $beforeColumnIndex) && ($cell->getRow() >= $beforeRow)) {
-
- $pSheet->getCell($newCoordinates)->setXfIndex($cell->getXfIndex());
-
- if ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_FORMULA) {
-
- $pSheet->getCell($newCoordinates)
- ->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle()));
- } else {
-
- $pSheet->getCell($newCoordinates)->setValue($cell->getValue());
- }
-
- $pSheet->getCellCacheController()->deleteCacheData($cellID);
- } else {
-
- if ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_FORMULA) {
-
- $cell->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle()));
- }
- }
- }
-
- $highestColumn = $pSheet->getHighestColumn();
- $highestRow = $pSheet->getHighestRow();
- if ($pNumCols > 0 && $beforeColumnIndex - 2 > 0) {
- for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
-
- $coordinate = PHPExcel_Cell::stringFromColumnIndex($beforeColumnIndex - 2) . $i;
- if ($pSheet->cellExists($coordinate)) {
- $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
- $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
- $pSheet->getConditionalStyles($coordinate) : false;
- for ($j = $beforeColumnIndex - 1; $j <= $beforeColumnIndex - 2 + $pNumCols; ++$j) {
- $pSheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex);
- if ($conditionalStyles) {
- $cloned = array();
- foreach ($conditionalStyles as $conditionalStyle) {
- $cloned[] = clone $conditionalStyle;
- }
- $pSheet->setConditionalStyles(PHPExcel_Cell::stringFromColumnIndex($j) . $i, $cloned);
- }
- }
- }
- }
- }
- if ($pNumRows > 0 && $beforeRow - 1 > 0) {
- for ($i = $beforeColumnIndex - 1; $i <= PHPExcel_Cell::columnIndexFromString($highestColumn) - 1; ++$i) {
-
- $coordinate = PHPExcel_Cell::stringFromColumnIndex($i) . ($beforeRow - 1);
- if ($pSheet->cellExists($coordinate)) {
- $xfIndex = $pSheet->getCell($coordinate)->getXfIndex();
- $conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ?
- $pSheet->getConditionalStyles($coordinate) : false;
- for ($j = $beforeRow; $j <= $beforeRow - 1 + $pNumRows; ++$j) {
- $pSheet->getCell(PHPExcel_Cell::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex);
- if ($conditionalStyles) {
- $cloned = array();
- foreach ($conditionalStyles as $conditionalStyle) {
- $cloned[] = clone $conditionalStyle;
- }
- $pSheet->setConditionalStyles(PHPExcel_Cell::stringFromColumnIndex($i) . $j, $cloned);
- }
- }
- }
- }
- }
-
- $this->adjustColumnDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $this->adjustRowDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $this->adjustPageBreaks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $this->adjustComments($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $this->adjustHyperlinks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $this->adjustDataValidations($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $this->adjustMergeCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $this->adjustProtectedCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows);
-
- $autoFilter = $pSheet->getAutoFilter();
- $autoFilterRange = $autoFilter->getRange();
- if (!empty($autoFilterRange)) {
- if ($pNumCols != 0) {
- $autoFilterColumns = array_keys($autoFilter->getColumns());
- if (count($autoFilterColumns) > 0) {
- sscanf($pBefore, '%[A-Z]%d', $column, $row);
- $columnIndex = PHPExcel_Cell::columnIndexFromString($column);
- list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($autoFilterRange);
- if ($columnIndex <= $rangeEnd[0]) {
- if ($pNumCols < 0) {
-
-
- $deleteColumn = $columnIndex + $pNumCols - 1;
- $deleteCount = abs($pNumCols);
- for ($i = 1; $i <= $deleteCount; ++$i) {
- if (in_array(PHPExcel_Cell::stringFromColumnIndex($deleteColumn), $autoFilterColumns)) {
- $autoFilter->clearColumn(PHPExcel_Cell::stringFromColumnIndex($deleteColumn));
- }
- ++$deleteColumn;
- }
- }
- $startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0];
-
- if ($pNumCols > 0) {
-
- $startColID = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
- $toColID = PHPExcel_Cell::stringFromColumnIndex($startCol+$pNumCols-1);
- $endColID = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0]);
- $startColRef = $startCol;
- $endColRef = $rangeEnd[0];
- $toColRef = $rangeEnd[0]+$pNumCols;
- do {
- $autoFilter->shiftColumn(PHPExcel_Cell::stringFromColumnIndex($endColRef-1), PHPExcel_Cell::stringFromColumnIndex($toColRef-1));
- --$endColRef;
- --$toColRef;
- } while ($startColRef <= $endColRef);
- } else {
-
- $startColID = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
- $toColID = PHPExcel_Cell::stringFromColumnIndex($startCol+$pNumCols-1);
- $endColID = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0]);
- do {
- $autoFilter->shiftColumn($startColID, $toColID);
- ++$startColID;
- ++$toColID;
- } while ($startColID != $endColID);
- }
- }
- }
- }
- $pSheet->setAutoFilter($this->updateCellReference($autoFilterRange, $pBefore, $pNumCols, $pNumRows));
- }
-
- if ($pSheet->getFreezePane() != '') {
- $pSheet->freezePane($this->updateCellReference($pSheet->getFreezePane(), $pBefore, $pNumCols, $pNumRows));
- }
-
- if ($pSheet->getPageSetup()->isPrintAreaSet()) {
- $pSheet->getPageSetup()->setPrintArea($this->updateCellReference($pSheet->getPageSetup()->getPrintArea(), $pBefore, $pNumCols, $pNumRows));
- }
-
- $aDrawings = $pSheet->getDrawingCollection();
- foreach ($aDrawings as $objDrawing) {
- $newReference = $this->updateCellReference($objDrawing->getCoordinates(), $pBefore, $pNumCols, $pNumRows);
- if ($objDrawing->getCoordinates() != $newReference) {
- $objDrawing->setCoordinates($newReference);
- }
- }
-
- if (count($pSheet->getParent()->getNamedRanges()) > 0) {
- foreach ($pSheet->getParent()->getNamedRanges() as $namedRange) {
- if ($namedRange->getWorksheet()->getHashCode() == $pSheet->getHashCode()) {
- $namedRange->setRange($this->updateCellReference($namedRange->getRange(), $pBefore, $pNumCols, $pNumRows));
- }
- }
- }
-
- $pSheet->garbageCollect();
- }
-
- public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '')
- {
-
- $formulaBlocks = explode('"', $pFormula);
- $i = false;
- foreach ($formulaBlocks as &$formulaBlock) {
-
- if ($i = !$i) {
- $adjustCount = 0;
- $newCellTokens = $cellTokens = array();
-
- $matchCount = preg_match_all('/'.self::REFHELPER_REGEXP_ROWRANGE.'/i', ' '.$formulaBlock.' ', $matches, PREG_SET_ORDER);
- if ($matchCount > 0) {
- foreach ($matches as $match) {
- $fromString = ($match[2] > '') ? $match[2].'!' : '';
- $fromString .= $match[3].':'.$match[4];
- $modified3 = substr($this->updateCellReference('$A'.$match[3], $pBefore, $pNumCols, $pNumRows), 2);
- $modified4 = substr($this->updateCellReference('$A'.$match[4], $pBefore, $pNumCols, $pNumRows), 2);
- if ($match[3].':'.$match[4] !== $modified3.':'.$modified4) {
- if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
- $toString = ($match[2] > '') ? $match[2].'!' : '';
- $toString .= $modified3.':'.$modified4;
-
- $column = 100000;
- $row = 10000000 + trim($match[3], '$');
- $cellIndex = $column.$row;
- $newCellTokens[$cellIndex] = preg_quote($toString);
- $cellTokens[$cellIndex] = '/(?<!\d\$\!)'.preg_quote($fromString).'(?!\d)/i';
- ++$adjustCount;
- }
- }
- }
- }
-
- $matchCount = preg_match_all('/'.self::REFHELPER_REGEXP_COLRANGE.'/i', ' '.$formulaBlock.' ', $matches, PREG_SET_ORDER);
- if ($matchCount > 0) {
- foreach ($matches as $match) {
- $fromString = ($match[2] > '') ? $match[2].'!' : '';
- $fromString .= $match[3].':'.$match[4];
- $modified3 = substr($this->updateCellReference($match[3].'$1', $pBefore, $pNumCols, $pNumRows), 0, -2);
- $modified4 = substr($this->updateCellReference($match[4].'$1', $pBefore, $pNumCols, $pNumRows), 0, -2);
- if ($match[3].':'.$match[4] !== $modified3.':'.$modified4) {
- if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
- $toString = ($match[2] > '') ? $match[2].'!' : '';
- $toString .= $modified3.':'.$modified4;
-
- $column = PHPExcel_Cell::columnIndexFromString(trim($match[3], '$')) + 100000;
- $row = 10000000;
- $cellIndex = $column.$row;
- $newCellTokens[$cellIndex] = preg_quote($toString);
- $cellTokens[$cellIndex] = '/(?<![A-Z\$\!])'.preg_quote($fromString).'(?![A-Z])/i';
- ++$adjustCount;
- }
- }
- }
- }
-
- $matchCount = preg_match_all('/'.self::REFHELPER_REGEXP_CELLRANGE.'/i', ' '.$formulaBlock.' ', $matches, PREG_SET_ORDER);
- if ($matchCount > 0) {
- foreach ($matches as $match) {
- $fromString = ($match[2] > '') ? $match[2].'!' : '';
- $fromString .= $match[3].':'.$match[4];
- $modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows);
- $modified4 = $this->updateCellReference($match[4], $pBefore, $pNumCols, $pNumRows);
- if ($match[3].$match[4] !== $modified3.$modified4) {
- if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
- $toString = ($match[2] > '') ? $match[2].'!' : '';
- $toString .= $modified3.':'.$modified4;
- list($column, $row) = PHPExcel_Cell::coordinateFromString($match[3]);
-
- $column = PHPExcel_Cell::columnIndexFromString(trim($column, '$')) + 100000;
- $row = trim($row, '$') + 10000000;
- $cellIndex = $column.$row;
- $newCellTokens[$cellIndex] = preg_quote($toString);
- $cellTokens[$cellIndex] = '/(?<![A-Z]\$\!)'.preg_quote($fromString).'(?!\d)/i';
- ++$adjustCount;
- }
- }
- }
- }
-
- $matchCount = preg_match_all('/'.self::REFHELPER_REGEXP_CELLREF.'/i', ' '.$formulaBlock.' ', $matches, PREG_SET_ORDER);
- if ($matchCount > 0) {
- foreach ($matches as $match) {
- $fromString = ($match[2] > '') ? $match[2].'!' : '';
- $fromString .= $match[3];
- $modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows);
- if ($match[3] !== $modified3) {
- if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) {
- $toString = ($match[2] > '') ? $match[2].'!' : '';
- $toString .= $modified3;
- list($column, $row) = PHPExcel_Cell::coordinateFromString($match[3]);
-
- $column = PHPExcel_Cell::columnIndexFromString(trim($column, '$')) + 100000;
- $row = trim($row, '$') + 10000000;
- $cellIndex = $row . $column;
- $newCellTokens[$cellIndex] = preg_quote($toString);
- $cellTokens[$cellIndex] = '/(?<![A-Z\$\!])'.preg_quote($fromString).'(?!\d)/i';
- ++$adjustCount;
- }
- }
- }
- }
- if ($adjustCount > 0) {
- if ($pNumCols > 0 || $pNumRows > 0) {
- krsort($cellTokens);
- krsort($newCellTokens);
- } else {
- ksort($cellTokens);
- ksort($newCellTokens);
- }
- $formulaBlock = str_replace('\\', '', preg_replace($cellTokens, $newCellTokens, $formulaBlock));
- }
- }
- }
- unset($formulaBlock);
-
- return implode('"', $formulaBlocks);
- }
-
- public function updateCellReference($pCellRange = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0)
- {
-
- if (strpos($pCellRange, "!") !== false) {
- return $pCellRange;
-
- } elseif (strpos($pCellRange, ':') === false && strpos($pCellRange, ',') === false) {
-
- return $this->updateSingleCellReference($pCellRange, $pBefore, $pNumCols, $pNumRows);
- } elseif (strpos($pCellRange, ':') !== false || strpos($pCellRange, ',') !== false) {
-
- return $this->updateCellRange($pCellRange, $pBefore, $pNumCols, $pNumRows);
- } else {
-
- return $pCellRange;
- }
- }
-
- public function updateNamedFormulas(PHPExcel $pPhpExcel, $oldName = '', $newName = '')
- {
- if ($oldName == '') {
- return;
- }
- foreach ($pPhpExcel->getWorksheetIterator() as $sheet) {
- foreach ($sheet->getCellCollection(false) as $cellID) {
- $cell = $sheet->getCell($cellID);
- if (($cell !== null) && ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_FORMULA)) {
- $formula = $cell->getValue();
- if (strpos($formula, $oldName) !== false) {
- $formula = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $formula);
- $formula = str_replace($oldName . "!", $newName . "!", $formula);
- $cell->setValueExplicit($formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
- }
- }
- }
- }
- }
-
- private function updateCellRange($pCellRange = 'A1:A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0)
- {
- if (strpos($pCellRange, ':') !== false || strpos($pCellRange, ',') !== false) {
-
- $range = PHPExcel_Cell::splitRange($pCellRange);
- $ic = count($range);
- for ($i = 0; $i < $ic; ++$i) {
- $jc = count($range[$i]);
- for ($j = 0; $j < $jc; ++$j) {
- if (ctype_alpha($range[$i][$j])) {
- $r = PHPExcel_Cell::coordinateFromString($this->updateSingleCellReference($range[$i][$j].'1', $pBefore, $pNumCols, $pNumRows));
- $range[$i][$j] = $r[0];
- } elseif (ctype_digit($range[$i][$j])) {
- $r = PHPExcel_Cell::coordinateFromString($this->updateSingleCellReference('A'.$range[$i][$j], $pBefore, $pNumCols, $pNumRows));
- $range[$i][$j] = $r[1];
- } else {
- $range[$i][$j] = $this->updateSingleCellReference($range[$i][$j], $pBefore, $pNumCols, $pNumRows);
- }
- }
- }
-
- return PHPExcel_Cell::buildRange($range);
- } else {
- throw new PHPExcel_Exception("Only cell ranges may be passed to this method.");
- }
- }
-
- private function updateSingleCellReference($pCellReference = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0)
- {
- if (strpos($pCellReference, ':') === false && strpos($pCellReference, ',') === false) {
-
- list($beforeColumn, $beforeRow) = PHPExcel_Cell::coordinateFromString($pBefore);
-
- list($newColumn, $newRow) = PHPExcel_Cell::coordinateFromString($pCellReference);
-
- $updateColumn = (($newColumn{0} != '$') && ($beforeColumn{0} != '$') && (PHPExcel_Cell::columnIndexFromString($newColumn) >= PHPExcel_Cell::columnIndexFromString($beforeColumn)));
- $updateRow = (($newRow{0} != '$') && ($beforeRow{0} != '$') && $newRow >= $beforeRow);
-
- if ($updateColumn) {
- $newColumn = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($newColumn) - 1 + $pNumCols);
- }
-
- if ($updateRow) {
- $newRow = $newRow + $pNumRows;
- }
-
- return $newColumn . $newRow;
- } else {
- throw new PHPExcel_Exception("Only single cell references may be passed to this method.");
- }
- }
-
- final public function __clone()
- {
- throw new PHPExcel_Exception("Cloning a Singleton is not allowed!");
- }
- }
|