Sane argument names in the Reference Helper, Settings and Spreadsheet classes

This commit is contained in:
MarkBaker 2020-11-03 17:45:12 +01:00
parent 7e4248d67f
commit af3918cac8
3 changed files with 349 additions and 347 deletions

View File

@ -119,26 +119,26 @@ class ReferenceHelper
* *
* @param string $cellAddress Address of the cell we're testing * @param string $cellAddress Address of the cell we're testing
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfCols Number of columns to insert/delete (negative values indicate deletion)
* *
* @return bool * @return bool
*/ */
private static function cellAddressInDeleteRange($cellAddress, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols) private static function cellAddressInDeleteRange($cellAddress, $beforeRow, $numberOfRows, $beforeColumnIndex, $numberOfCols)
{ {
[$cellColumn, $cellRow] = Coordinate::coordinateFromString($cellAddress); [$cellColumn, $cellRow] = Coordinate::coordinateFromString($cellAddress);
$cellColumnIndex = Coordinate::columnIndexFromString($cellColumn); $cellColumnIndex = Coordinate::columnIndexFromString($cellColumn);
// Is cell within the range of rows/columns if we're deleting // Is cell within the range of rows/columns if we're deleting
if ( if (
$pNumRows < 0 && $numberOfRows < 0 &&
($cellRow >= ($beforeRow + $pNumRows)) && ($cellRow >= ($beforeRow + $numberOfRows)) &&
($cellRow < $beforeRow) ($cellRow < $beforeRow)
) { ) {
return true; return true;
} elseif ( } elseif (
$pNumCols < 0 && $numberOfCols < 0 &&
($cellColumnIndex >= ($beforeColumnIndex + $pNumCols)) && ($cellColumnIndex >= ($beforeColumnIndex + $numberOfCols)) &&
($cellColumnIndex < $beforeColumnIndex) ($cellColumnIndex < $beforeColumnIndex)
) { ) {
return true; return true;
@ -150,30 +150,30 @@ class ReferenceHelper
/** /**
* Update page breaks when inserting/deleting rows/columns. * Update page breaks when inserting/deleting rows/columns.
* *
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert/Delete before this cell address (e.g. 'A1')
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustPageBreaks(Worksheet $pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustPageBreaks(Worksheet $worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows): void
{ {
$aBreaks = $pSheet->getBreaks(); $aBreaks = $worksheet->getBreaks();
($pNumCols > 0 || $pNumRows > 0) ? ($numberOfColumns > 0 || $numberOfRows > 0) ?
uksort($aBreaks, ['self', 'cellReverseSort']) : uksort($aBreaks, ['self', 'cellSort']); uksort($aBreaks, ['self', 'cellReverseSort']) : uksort($aBreaks, ['self', 'cellSort']);
foreach ($aBreaks as $key => $value) { foreach ($aBreaks as $key => $value) {
if (self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) { if (self::cellAddressInDeleteRange($key, $beforeRow, $numberOfRows, $beforeColumnIndex, $numberOfColumns)) {
// If we're deleting, then clear any defined breaks that are within the range // If we're deleting, then clear any defined breaks that are within the range
// of rows/columns that we're deleting // of rows/columns that we're deleting
$pSheet->setBreak($key, Worksheet::BREAK_NONE); $worksheet->setBreak($key, Worksheet::BREAK_NONE);
} else { } else {
// Otherwise update any affected breaks by inserting a new break at the appropriate point // Otherwise update any affected breaks by inserting a new break at the appropriate point
// and removing the old affected break // and removing the old affected break
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference($key, $beforeCellAddress, $numberOfColumns, $numberOfRows);
if ($key != $newReference) { if ($key != $newReference) {
$pSheet->setBreak($newReference, $value) $worksheet->setBreak($newReference, $value)
->setBreak($key, Worksheet::BREAK_NONE); ->setBreak($key, Worksheet::BREAK_NONE);
} }
} }
@ -183,51 +183,51 @@ class ReferenceHelper
/** /**
* Update cell comments when inserting/deleting rows/columns. * Update cell comments when inserting/deleting rows/columns.
* *
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert/Delete before this cell address (e.g. 'A1')
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustComments($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustComments($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows): void
{ {
$aComments = $pSheet->getComments(); $aComments = $worksheet->getComments();
$aNewComments = []; // the new array of all comments $aNewComments = []; // the new array of all comments
foreach ($aComments as $key => &$value) { foreach ($aComments as $key => &$value) {
// Any comments inside a deleted range will be ignored // Any comments inside a deleted range will be ignored
if (!self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) { if (!self::cellAddressInDeleteRange($key, $beforeRow, $numberOfRows, $beforeColumnIndex, $numberOfColumns)) {
// Otherwise build a new array of comments indexed by the adjusted cell reference // Otherwise build a new array of comments indexed by the adjusted cell reference
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference($key, $beforeCellAddress, $numberOfColumns, $numberOfRows);
$aNewComments[$newReference] = $value; $aNewComments[$newReference] = $value;
} }
} }
// Replace the comments array with the new set of comments // Replace the comments array with the new set of comments
$pSheet->setComments($aNewComments); $worksheet->setComments($aNewComments);
} }
/** /**
* Update hyperlinks when inserting/deleting rows/columns. * Update hyperlinks when inserting/deleting rows/columns.
* *
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert/Delete before this cell address (e.g. 'A1')
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustHyperlinks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustHyperlinks($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows): void
{ {
$aHyperlinkCollection = $pSheet->getHyperlinkCollection(); $aHyperlinkCollection = $worksheet->getHyperlinkCollection();
($pNumCols > 0 || $pNumRows > 0) ? ($numberOfColumns > 0 || $numberOfRows > 0) ?
uksort($aHyperlinkCollection, ['self', 'cellReverseSort']) : uksort($aHyperlinkCollection, ['self', 'cellSort']); uksort($aHyperlinkCollection, ['self', 'cellReverseSort']) : uksort($aHyperlinkCollection, ['self', 'cellSort']);
foreach ($aHyperlinkCollection as $key => $value) { foreach ($aHyperlinkCollection as $key => $value) {
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference($key, $beforeCellAddress, $numberOfColumns, $numberOfRows);
if ($key != $newReference) { if ($key != $newReference) {
$pSheet->setHyperlink($newReference, $value); $worksheet->setHyperlink($newReference, $value);
$pSheet->setHyperlink($key, null); $worksheet->setHyperlink($key, null);
} }
} }
} }
@ -242,7 +242,7 @@ class ReferenceHelper
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustDataValidations($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustDataValidations(Worksheet $pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void
{ {
$aDataValidationCollection = $pSheet->getDataValidationCollection(); $aDataValidationCollection = $pSheet->getDataValidationCollection();
($pNumCols > 0 || $pNumRows > 0) ? ($pNumCols > 0 || $pNumRows > 0) ?
@ -260,44 +260,44 @@ class ReferenceHelper
/** /**
* Update merged cells when inserting/deleting rows/columns. * Update merged cells when inserting/deleting rows/columns.
* *
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert/Delete before this cell address (e.g. 'A1')
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustMergeCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustMergeCells(Worksheet $worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows): void
{ {
$aMergeCells = $pSheet->getMergeCells(); $aMergeCells = $worksheet->getMergeCells();
$aNewMergeCells = []; // the new array of all merge cells $aNewMergeCells = []; // the new array of all merge cells
foreach ($aMergeCells as $key => &$value) { foreach ($aMergeCells as $key => &$value) {
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference($key, $beforeCellAddress, $numberOfColumns, $numberOfRows);
$aNewMergeCells[$newReference] = $newReference; $aNewMergeCells[$newReference] = $newReference;
} }
$pSheet->setMergeCells($aNewMergeCells); // replace the merge cells array $worksheet->setMergeCells($aNewMergeCells); // replace the merge cells array
} }
/** /**
* Update protected cells when inserting/deleting rows/columns. * Update protected cells when inserting/deleting rows/columns.
* *
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert/Delete before this cell address (e.g. 'A1')
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustProtectedCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustProtectedCells(Worksheet $worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows): void
{ {
$aProtectedCells = $pSheet->getProtectedCells(); $aProtectedCells = $worksheet->getProtectedCells();
($pNumCols > 0 || $pNumRows > 0) ? ($numberOfColumns > 0 || $numberOfRows > 0) ?
uksort($aProtectedCells, ['self', 'cellReverseSort']) : uksort($aProtectedCells, ['self', 'cellSort']); uksort($aProtectedCells, ['self', 'cellReverseSort']) : uksort($aProtectedCells, ['self', 'cellSort']);
foreach ($aProtectedCells as $key => $value) { foreach ($aProtectedCells as $key => $value) {
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference($key, $beforeCellAddress, $numberOfColumns, $numberOfRows);
if ($key != $newReference) { if ($key != $newReference) {
$pSheet->protectCells($newReference, $value, true); $worksheet->protectCells($newReference, $value, true);
$pSheet->unprotectCells($key); $worksheet->unprotectCells($key);
} }
} }
} }
@ -305,54 +305,54 @@ class ReferenceHelper
/** /**
* Update column dimensions when inserting/deleting rows/columns. * Update column dimensions when inserting/deleting rows/columns.
* *
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert/Delete before this cell address (e.g. 'A1')
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustColumnDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustColumnDimensions(Worksheet $worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows): void
{ {
$aColumnDimensions = array_reverse($pSheet->getColumnDimensions(), true); $aColumnDimensions = array_reverse($worksheet->getColumnDimensions(), true);
if (!empty($aColumnDimensions)) { if (!empty($aColumnDimensions)) {
foreach ($aColumnDimensions as $objColumnDimension) { foreach ($aColumnDimensions as $objColumnDimension) {
$newReference = $this->updateCellReference($objColumnDimension->getColumnIndex() . '1', $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference($objColumnDimension->getColumnIndex() . '1', $beforeCellAddress, $numberOfColumns, $numberOfRows);
[$newReference] = Coordinate::coordinateFromString($newReference); [$newReference] = Coordinate::coordinateFromString($newReference);
if ($objColumnDimension->getColumnIndex() != $newReference) { if ($objColumnDimension->getColumnIndex() != $newReference) {
$objColumnDimension->setColumnIndex($newReference); $objColumnDimension->setColumnIndex($newReference);
} }
} }
$pSheet->refreshColumnDimensions(); $worksheet->refreshColumnDimensions();
} }
} }
/** /**
* Update row dimensions when inserting/deleting rows/columns. * Update row dimensions when inserting/deleting rows/columns.
* *
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert/Delete before this cell address (e.g. 'A1')
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before * @param int $beforeColumnIndex Index number of the column we're inserting/deleting before
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $beforeRow Number of the row we're inserting/deleting before * @param int $beforeRow Number of the row we're inserting/deleting before
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
*/ */
protected function adjustRowDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void protected function adjustRowDimensions(Worksheet $worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows): void
{ {
$aRowDimensions = array_reverse($pSheet->getRowDimensions(), true); $aRowDimensions = array_reverse($worksheet->getRowDimensions(), true);
if (!empty($aRowDimensions)) { if (!empty($aRowDimensions)) {
foreach ($aRowDimensions as $objRowDimension) { foreach ($aRowDimensions as $objRowDimension) {
$newReference = $this->updateCellReference('A' . $objRowDimension->getRowIndex(), $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference('A' . $objRowDimension->getRowIndex(), $beforeCellAddress, $numberOfColumns, $numberOfRows);
[, $newReference] = Coordinate::coordinateFromString($newReference); [, $newReference] = Coordinate::coordinateFromString($newReference);
if ($objRowDimension->getRowIndex() != $newReference) { if ($objRowDimension->getRowIndex() != $newReference) {
$objRowDimension->setRowIndex($newReference); $objRowDimension->setRowIndex($newReference);
} }
} }
$pSheet->refreshRowDimensions(); $worksheet->refreshRowDimensions();
$copyDimension = $pSheet->getRowDimension($beforeRow - 1); $copyDimension = $worksheet->getRowDimension($beforeRow - 1);
for ($i = $beforeRow; $i <= $beforeRow - 1 + $pNumRows; ++$i) { for ($i = $beforeRow; $i <= $beforeRow - 1 + $numberOfRows; ++$i) {
$newDimension = $pSheet->getRowDimension($i); $newDimension = $worksheet->getRowDimension($i);
$newDimension->setRowHeight($copyDimension->getRowHeight()); $newDimension->setRowHeight($copyDimension->getRowHeight());
$newDimension->setVisible($copyDimension->getVisible()); $newDimension->setVisible($copyDimension->getVisible());
$newDimension->setOutlineLevel($copyDimension->getOutlineLevel()); $newDimension->setOutlineLevel($copyDimension->getOutlineLevel());
@ -364,47 +364,47 @@ class ReferenceHelper
/** /**
* Insert a new column or row, updating all possible related data. * Insert a new column or row, updating all possible related data.
* *
* @param string $pBefore Insert before this cell address (e.g. 'A1') * @param string $beforeCellAddress Insert before this cell address (e.g. 'A1')
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) * @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) * @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
* @param Worksheet $pSheet The worksheet that we're editing * @param Worksheet $worksheet The worksheet that we're editing
*/ */
public function insertNewBefore($pBefore, $pNumCols, $pNumRows, Worksheet $pSheet): void public function insertNewBefore($beforeCellAddress, $numberOfColumns, $numberOfRows, Worksheet $worksheet): void
{ {
$remove = ($pNumCols < 0 || $pNumRows < 0); $remove = ($numberOfColumns < 0 || $numberOfRows < 0);
$allCoordinates = $pSheet->getCoordinates(); $allCoordinates = $worksheet->getCoordinates();
// Get coordinate of $pBefore // Get coordinate of $pBefore
[$beforeColumn, $beforeRow] = Coordinate::coordinateFromString($pBefore); [$beforeColumn, $beforeRow] = Coordinate::coordinateFromString($beforeCellAddress);
$beforeColumnIndex = Coordinate::columnIndexFromString($beforeColumn); $beforeColumnIndex = Coordinate::columnIndexFromString($beforeColumn);
// Clear cells if we are removing columns or rows // Clear cells if we are removing columns or rows
$highestColumn = $pSheet->getHighestColumn(); $highestColumn = $worksheet->getHighestColumn();
$highestRow = $pSheet->getHighestRow(); $highestRow = $worksheet->getHighestRow();
// 1. Clear column strips if we are removing columns // 1. Clear column strips if we are removing columns
if ($pNumCols < 0 && $beforeColumnIndex - 2 + $pNumCols > 0) { if ($numberOfColumns < 0 && $beforeColumnIndex - 2 + $numberOfColumns > 0) {
for ($i = 1; $i <= $highestRow - 1; ++$i) { for ($i = 1; $i <= $highestRow - 1; ++$i) {
for ($j = $beforeColumnIndex - 1 + $pNumCols; $j <= $beforeColumnIndex - 2; ++$j) { for ($j = $beforeColumnIndex - 1 + $numberOfColumns; $j <= $beforeColumnIndex - 2; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i; $coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i;
$pSheet->removeConditionalStyles($coordinate); $worksheet->removeConditionalStyles($coordinate);
if ($pSheet->cellExists($coordinate)) { if ($worksheet->cellExists($coordinate)) {
$pSheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL); $worksheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
$pSheet->getCell($coordinate)->setXfIndex(0); $worksheet->getCell($coordinate)->setXfIndex(0);
} }
} }
} }
} }
// 2. Clear row strips if we are removing rows // 2. Clear row strips if we are removing rows
if ($pNumRows < 0 && $beforeRow - 1 + $pNumRows > 0) { if ($numberOfRows < 0 && $beforeRow - 1 + $numberOfRows > 0) {
for ($i = $beforeColumnIndex - 1; $i <= Coordinate::columnIndexFromString($highestColumn) - 1; ++$i) { for ($i = $beforeColumnIndex - 1; $i <= Coordinate::columnIndexFromString($highestColumn) - 1; ++$i) {
for ($j = $beforeRow + $pNumRows; $j <= $beforeRow - 1; ++$j) { for ($j = $beforeRow + $numberOfRows; $j <= $beforeRow - 1; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j; $coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j;
$pSheet->removeConditionalStyles($coordinate); $worksheet->removeConditionalStyles($coordinate);
if ($pSheet->cellExists($coordinate)) { if ($worksheet->cellExists($coordinate)) {
$pSheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL); $worksheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
$pSheet->getCell($coordinate)->setXfIndex(0); $worksheet->getCell($coordinate)->setXfIndex(0);
} }
} }
} }
@ -416,85 +416,85 @@ class ReferenceHelper
$allCoordinates = array_reverse($allCoordinates); $allCoordinates = array_reverse($allCoordinates);
} }
while ($coordinate = array_pop($allCoordinates)) { while ($coordinate = array_pop($allCoordinates)) {
$cell = $pSheet->getCell($coordinate); $cell = $worksheet->getCell($coordinate);
$cellIndex = Coordinate::columnIndexFromString($cell->getColumn()); $cellIndex = Coordinate::columnIndexFromString($cell->getColumn());
if ($cellIndex - 1 + $pNumCols < 0) { if ($cellIndex - 1 + $numberOfColumns < 0) {
continue; continue;
} }
// New coordinate // New coordinate
$newCoordinate = Coordinate::stringFromColumnIndex($cellIndex + $pNumCols) . ($cell->getRow() + $pNumRows); $newCoordinate = Coordinate::stringFromColumnIndex($cellIndex + $numberOfColumns) . ($cell->getRow() + $numberOfRows);
// Should the cell be updated? Move value and cellXf index from one cell to another. // Should the cell be updated? Move value and cellXf index from one cell to another.
if (($cellIndex >= $beforeColumnIndex) && ($cell->getRow() >= $beforeRow)) { if (($cellIndex >= $beforeColumnIndex) && ($cell->getRow() >= $beforeRow)) {
// Update cell styles // Update cell styles
$pSheet->getCell($newCoordinate)->setXfIndex($cell->getXfIndex()); $worksheet->getCell($newCoordinate)->setXfIndex($cell->getXfIndex());
// Insert this cell at its new location // Insert this cell at its new location
if ($cell->getDataType() == DataType::TYPE_FORMULA) { if ($cell->getDataType() == DataType::TYPE_FORMULA) {
// Formula should be adjusted // Formula should be adjusted
$pSheet->getCell($newCoordinate) $worksheet->getCell($newCoordinate)
->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle())); ->setValue($this->updateFormulaReferences($cell->getValue(), $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle()));
} else { } else {
// Formula should not be adjusted // Formula should not be adjusted
$pSheet->getCell($newCoordinate)->setValue($cell->getValue()); $worksheet->getCell($newCoordinate)->setValue($cell->getValue());
} }
// Clear the original cell // Clear the original cell
$pSheet->getCellCollection()->delete($coordinate); $worksheet->getCellCollection()->delete($coordinate);
} else { } else {
/* We don't need to update styles for rows/columns before our insertion position, /* We don't need to update styles for rows/columns before our insertion position,
but we do still need to adjust any formulae in those cells */ but we do still need to adjust any formulae in those cells */
if ($cell->getDataType() == DataType::TYPE_FORMULA) { if ($cell->getDataType() == DataType::TYPE_FORMULA) {
// Formula should be adjusted // Formula should be adjusted
$cell->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle())); $cell->setValue($this->updateFormulaReferences($cell->getValue(), $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle()));
} }
} }
} }
// Duplicate styles for the newly inserted cells // Duplicate styles for the newly inserted cells
$highestColumn = $pSheet->getHighestColumn(); $highestColumn = $worksheet->getHighestColumn();
$highestRow = $pSheet->getHighestRow(); $highestRow = $worksheet->getHighestRow();
if ($pNumCols > 0 && $beforeColumnIndex - 2 > 0) { if ($numberOfColumns > 0 && $beforeColumnIndex - 2 > 0) {
for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) { for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
// Style // Style
$coordinate = Coordinate::stringFromColumnIndex($beforeColumnIndex - 1) . $i; $coordinate = Coordinate::stringFromColumnIndex($beforeColumnIndex - 1) . $i;
if ($pSheet->cellExists($coordinate)) { if ($worksheet->cellExists($coordinate)) {
$xfIndex = $pSheet->getCell($coordinate)->getXfIndex(); $xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
$conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ? $conditionalStyles = $worksheet->conditionalStylesExists($coordinate) ?
$pSheet->getConditionalStyles($coordinate) : false; $worksheet->getConditionalStyles($coordinate) : false;
for ($j = $beforeColumnIndex; $j <= $beforeColumnIndex - 1 + $pNumCols; ++$j) { for ($j = $beforeColumnIndex; $j <= $beforeColumnIndex - 1 + $numberOfColumns; ++$j) {
$pSheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex); $worksheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex);
if ($conditionalStyles) { if ($conditionalStyles) {
$cloned = []; $cloned = [];
foreach ($conditionalStyles as $conditionalStyle) { foreach ($conditionalStyles as $conditionalStyle) {
$cloned[] = clone $conditionalStyle; $cloned[] = clone $conditionalStyle;
} }
$pSheet->setConditionalStyles(Coordinate::stringFromColumnIndex($j) . $i, $cloned); $worksheet->setConditionalStyles(Coordinate::stringFromColumnIndex($j) . $i, $cloned);
} }
} }
} }
} }
} }
if ($pNumRows > 0 && $beforeRow - 1 > 0) { if ($numberOfRows > 0 && $beforeRow - 1 > 0) {
for ($i = $beforeColumnIndex; $i <= Coordinate::columnIndexFromString($highestColumn); ++$i) { for ($i = $beforeColumnIndex; $i <= Coordinate::columnIndexFromString($highestColumn); ++$i) {
// Style // Style
$coordinate = Coordinate::stringFromColumnIndex($i) . ($beforeRow - 1); $coordinate = Coordinate::stringFromColumnIndex($i) . ($beforeRow - 1);
if ($pSheet->cellExists($coordinate)) { if ($worksheet->cellExists($coordinate)) {
$xfIndex = $pSheet->getCell($coordinate)->getXfIndex(); $xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
$conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ? $conditionalStyles = $worksheet->conditionalStylesExists($coordinate) ?
$pSheet->getConditionalStyles($coordinate) : false; $worksheet->getConditionalStyles($coordinate) : false;
for ($j = $beforeRow; $j <= $beforeRow - 1 + $pNumRows; ++$j) { for ($j = $beforeRow; $j <= $beforeRow - 1 + $numberOfRows; ++$j) {
$pSheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex); $worksheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex);
if ($conditionalStyles) { if ($conditionalStyles) {
$cloned = []; $cloned = [];
foreach ($conditionalStyles as $conditionalStyle) { foreach ($conditionalStyles as $conditionalStyle) {
$cloned[] = clone $conditionalStyle; $cloned[] = clone $conditionalStyle;
} }
$pSheet->setConditionalStyles(Coordinate::stringFromColumnIndex($i) . $j, $cloned); $worksheet->setConditionalStyles(Coordinate::stringFromColumnIndex($i) . $j, $cloned);
} }
} }
} }
@ -502,47 +502,47 @@ class ReferenceHelper
} }
// Update worksheet: column dimensions // Update worksheet: column dimensions
$this->adjustColumnDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustColumnDimensions($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: row dimensions // Update worksheet: row dimensions
$this->adjustRowDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustRowDimensions($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: page breaks // Update worksheet: page breaks
$this->adjustPageBreaks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustPageBreaks($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: comments // Update worksheet: comments
$this->adjustComments($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustComments($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: hyperlinks // Update worksheet: hyperlinks
$this->adjustHyperlinks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustHyperlinks($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: data validations // Update worksheet: data validations
$this->adjustDataValidations($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustDataValidations($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: merge cells // Update worksheet: merge cells
$this->adjustMergeCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustMergeCells($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: protected cells // Update worksheet: protected cells
$this->adjustProtectedCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows); $this->adjustProtectedCells($worksheet, $beforeCellAddress, $beforeColumnIndex, $numberOfColumns, $beforeRow, $numberOfRows);
// Update worksheet: autofilter // Update worksheet: autofilter
$autoFilter = $pSheet->getAutoFilter(); $autoFilter = $worksheet->getAutoFilter();
$autoFilterRange = $autoFilter->getRange(); $autoFilterRange = $autoFilter->getRange();
if (!empty($autoFilterRange)) { if (!empty($autoFilterRange)) {
if ($pNumCols != 0) { if ($numberOfColumns != 0) {
$autoFilterColumns = $autoFilter->getColumns(); $autoFilterColumns = $autoFilter->getColumns();
if (count($autoFilterColumns) > 0) { if (count($autoFilterColumns) > 0) {
$column = ''; $column = '';
$row = 0; $row = 0;
sscanf($pBefore, '%[A-Z]%d', $column, $row); sscanf($beforeCellAddress, '%[A-Z]%d', $column, $row);
$columnIndex = Coordinate::columnIndexFromString($column); $columnIndex = Coordinate::columnIndexFromString($column);
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($autoFilterRange); [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($autoFilterRange);
if ($columnIndex <= $rangeEnd[0]) { if ($columnIndex <= $rangeEnd[0]) {
if ($pNumCols < 0) { if ($numberOfColumns < 0) {
// If we're actually deleting any columns that fall within the autofilter range, // If we're actually deleting any columns that fall within the autofilter range,
// then we delete any rules for those columns // then we delete any rules for those columns
$deleteColumn = $columnIndex + $pNumCols - 1; $deleteColumn = $columnIndex + $numberOfColumns - 1;
$deleteCount = abs($pNumCols); $deleteCount = abs($numberOfColumns);
for ($i = 1; $i <= $deleteCount; ++$i) { for ($i = 1; $i <= $deleteCount; ++$i) {
if (isset($autoFilterColumns[Coordinate::stringFromColumnIndex($deleteColumn + 1)])) { if (isset($autoFilterColumns[Coordinate::stringFromColumnIndex($deleteColumn + 1)])) {
$autoFilter->clearColumn(Coordinate::stringFromColumnIndex($deleteColumn + 1)); $autoFilter->clearColumn(Coordinate::stringFromColumnIndex($deleteColumn + 1));
@ -553,10 +553,10 @@ class ReferenceHelper
$startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0]; $startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0];
// Shuffle columns in autofilter range // Shuffle columns in autofilter range
if ($pNumCols > 0) { if ($numberOfColumns > 0) {
$startColRef = $startCol; $startColRef = $startCol;
$endColRef = $rangeEnd[0]; $endColRef = $rangeEnd[0];
$toColRef = $rangeEnd[0] + $pNumCols; $toColRef = $rangeEnd[0] + $numberOfColumns;
do { do {
$autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef)); $autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef));
@ -566,7 +566,7 @@ class ReferenceHelper
} else { } else {
// For delete, we shuffle from beginning to end to avoid overwriting // For delete, we shuffle from beginning to end to avoid overwriting
$startColID = Coordinate::stringFromColumnIndex($startCol); $startColID = Coordinate::stringFromColumnIndex($startCol);
$toColID = Coordinate::stringFromColumnIndex($startCol + $pNumCols); $toColID = Coordinate::stringFromColumnIndex($startCol + $numberOfColumns);
$endColID = Coordinate::stringFromColumnIndex($rangeEnd[0] + 1); $endColID = Coordinate::stringFromColumnIndex($rangeEnd[0] + 1);
do { do {
$autoFilter->shiftColumn($startColID, $toColID); $autoFilter->shiftColumn($startColID, $toColID);
@ -577,62 +577,62 @@ class ReferenceHelper
} }
} }
} }
$pSheet->setAutoFilter($this->updateCellReference($autoFilterRange, $pBefore, $pNumCols, $pNumRows)); $worksheet->setAutoFilter($this->updateCellReference($autoFilterRange, $beforeCellAddress, $numberOfColumns, $numberOfRows));
} }
// Update worksheet: freeze pane // Update worksheet: freeze pane
if ($pSheet->getFreezePane()) { if ($worksheet->getFreezePane()) {
$splitCell = $pSheet->getFreezePane(); $splitCell = $worksheet->getFreezePane();
$topLeftCell = $pSheet->getTopLeftCell(); $topLeftCell = $worksheet->getTopLeftCell();
$splitCell = $this->updateCellReference($splitCell, $pBefore, $pNumCols, $pNumRows); $splitCell = $this->updateCellReference($splitCell, $beforeCellAddress, $numberOfColumns, $numberOfRows);
$topLeftCell = $this->updateCellReference($topLeftCell, $pBefore, $pNumCols, $pNumRows); $topLeftCell = $this->updateCellReference($topLeftCell, $beforeCellAddress, $numberOfColumns, $numberOfRows);
$pSheet->freezePane($splitCell, $topLeftCell); $worksheet->freezePane($splitCell, $topLeftCell);
} }
// Page setup // Page setup
if ($pSheet->getPageSetup()->isPrintAreaSet()) { if ($worksheet->getPageSetup()->isPrintAreaSet()) {
$pSheet->getPageSetup()->setPrintArea($this->updateCellReference($pSheet->getPageSetup()->getPrintArea(), $pBefore, $pNumCols, $pNumRows)); $worksheet->getPageSetup()->setPrintArea($this->updateCellReference($worksheet->getPageSetup()->getPrintArea(), $beforeCellAddress, $numberOfColumns, $numberOfRows));
} }
// Update worksheet: drawings // Update worksheet: drawings
$aDrawings = $pSheet->getDrawingCollection(); $aDrawings = $worksheet->getDrawingCollection();
foreach ($aDrawings as $objDrawing) { foreach ($aDrawings as $objDrawing) {
$newReference = $this->updateCellReference($objDrawing->getCoordinates(), $pBefore, $pNumCols, $pNumRows); $newReference = $this->updateCellReference($objDrawing->getCoordinates(), $beforeCellAddress, $numberOfColumns, $numberOfRows);
if ($objDrawing->getCoordinates() != $newReference) { if ($objDrawing->getCoordinates() != $newReference) {
$objDrawing->setCoordinates($newReference); $objDrawing->setCoordinates($newReference);
} }
} }
// Update workbook: define names // Update workbook: define names
if (count($pSheet->getParent()->getDefinedNames()) > 0) { if (count($worksheet->getParent()->getDefinedNames()) > 0) {
foreach ($pSheet->getParent()->getDefinedNames() as $definedName) { foreach ($worksheet->getParent()->getDefinedNames() as $definedName) {
if ($definedName->getWorksheet()->getHashCode() === $pSheet->getHashCode()) { if ($definedName->getWorksheet()->getHashCode() === $worksheet->getHashCode()) {
$definedName->setValue($this->updateCellReference($definedName->getValue(), $pBefore, $pNumCols, $pNumRows)); $definedName->setValue($this->updateCellReference($definedName->getValue(), $beforeCellAddress, $numberOfColumns, $numberOfRows));
} }
} }
} }
// Garbage collect // Garbage collect
$pSheet->garbageCollect(); $worksheet->garbageCollect();
} }
/** /**
* Update references within formulas. * Update references within formulas.
* *
* @param string $pFormula Formula to update * @param string $formula Formula to update
* @param string $pBefore Insert before this one * @param string $beforeCellAddress Insert before this one
* @param int $pNumCols Number of columns to insert * @param int $numberOfColumns Number of columns to insert
* @param int $pNumRows Number of rows to insert * @param int $numberOfRows Number of rows to insert
* @param string $sheetName Worksheet name/title * @param string $worksheetName Worksheet name/title
* *
* @return string Updated formula * @return string Updated formula
*/ */
public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '') public function updateFormulaReferences($formula = '', $beforeCellAddress = 'A1', $numberOfColumns = 0, $numberOfRows = 0, $worksheetName = '')
{ {
// Update cell references in the formula // Update cell references in the formula
$formulaBlocks = explode('"', $pFormula); $formulaBlocks = explode('"', $formula);
$i = false; $i = false;
foreach ($formulaBlocks as &$formulaBlock) { foreach ($formulaBlocks as &$formulaBlock) {
// Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode) // Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode)
@ -645,11 +645,11 @@ class ReferenceHelper
foreach ($matches as $match) { foreach ($matches as $match) {
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; $fromString = ($match[2] > '') ? $match[2] . '!' : '';
$fromString .= $match[3] . ':' . $match[4]; $fromString .= $match[3] . ':' . $match[4];
$modified3 = substr($this->updateCellReference('$A' . $match[3], $pBefore, $pNumCols, $pNumRows), 2); $modified3 = substr($this->updateCellReference('$A' . $match[3], $beforeCellAddress, $numberOfColumns, $numberOfRows), 2);
$modified4 = substr($this->updateCellReference('$A' . $match[4], $pBefore, $pNumCols, $pNumRows), 2); $modified4 = substr($this->updateCellReference('$A' . $match[4], $beforeCellAddress, $numberOfColumns, $numberOfRows), 2);
if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) { if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) {
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { if (($match[2] == '') || (trim($match[2], "'") == $worksheetName)) {
$toString = ($match[2] > '') ? $match[2] . '!' : ''; $toString = ($match[2] > '') ? $match[2] . '!' : '';
$toString .= $modified3 . ':' . $modified4; $toString .= $modified3 . ':' . $modified4;
// Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more // Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more
@ -670,11 +670,11 @@ class ReferenceHelper
foreach ($matches as $match) { foreach ($matches as $match) {
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; $fromString = ($match[2] > '') ? $match[2] . '!' : '';
$fromString .= $match[3] . ':' . $match[4]; $fromString .= $match[3] . ':' . $match[4];
$modified3 = substr($this->updateCellReference($match[3] . '$1', $pBefore, $pNumCols, $pNumRows), 0, -2); $modified3 = substr($this->updateCellReference($match[3] . '$1', $beforeCellAddress, $numberOfColumns, $numberOfRows), 0, -2);
$modified4 = substr($this->updateCellReference($match[4] . '$1', $pBefore, $pNumCols, $pNumRows), 0, -2); $modified4 = substr($this->updateCellReference($match[4] . '$1', $beforeCellAddress, $numberOfColumns, $numberOfRows), 0, -2);
if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) { if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) {
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { if (($match[2] == '') || (trim($match[2], "'") == $worksheetName)) {
$toString = ($match[2] > '') ? $match[2] . '!' : ''; $toString = ($match[2] > '') ? $match[2] . '!' : '';
$toString .= $modified3 . ':' . $modified4; $toString .= $modified3 . ':' . $modified4;
// Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more // Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more
@ -695,11 +695,11 @@ class ReferenceHelper
foreach ($matches as $match) { foreach ($matches as $match) {
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; $fromString = ($match[2] > '') ? $match[2] . '!' : '';
$fromString .= $match[3] . ':' . $match[4]; $fromString .= $match[3] . ':' . $match[4];
$modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows); $modified3 = $this->updateCellReference($match[3], $beforeCellAddress, $numberOfColumns, $numberOfRows);
$modified4 = $this->updateCellReference($match[4], $pBefore, $pNumCols, $pNumRows); $modified4 = $this->updateCellReference($match[4], $beforeCellAddress, $numberOfColumns, $numberOfRows);
if ($match[3] . $match[4] !== $modified3 . $modified4) { if ($match[3] . $match[4] !== $modified3 . $modified4) {
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { if (($match[2] == '') || (trim($match[2], "'") == $worksheetName)) {
$toString = ($match[2] > '') ? $match[2] . '!' : ''; $toString = ($match[2] > '') ? $match[2] . '!' : '';
$toString .= $modified3 . ':' . $modified4; $toString .= $modified3 . ':' . $modified4;
[$column, $row] = Coordinate::coordinateFromString($match[3]); [$column, $row] = Coordinate::coordinateFromString($match[3]);
@ -723,9 +723,9 @@ class ReferenceHelper
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; $fromString = ($match[2] > '') ? $match[2] . '!' : '';
$fromString .= $match[3]; $fromString .= $match[3];
$modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows); $modified3 = $this->updateCellReference($match[3], $beforeCellAddress, $numberOfColumns, $numberOfRows);
if ($match[3] !== $modified3) { if ($match[3] !== $modified3) {
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { if (($match[2] == '') || (trim($match[2], "'") == $worksheetName)) {
$toString = ($match[2] > '') ? $match[2] . '!' : ''; $toString = ($match[2] > '') ? $match[2] . '!' : '';
$toString .= $modified3; $toString .= $modified3;
[$column, $row] = Coordinate::coordinateFromString($match[3]); [$column, $row] = Coordinate::coordinateFromString($match[3]);
@ -742,7 +742,7 @@ class ReferenceHelper
} }
} }
if ($adjustCount > 0) { if ($adjustCount > 0) {
if ($pNumCols > 0 || $pNumRows > 0) { if ($numberOfColumns > 0 || $numberOfRows > 0) {
krsort($cellTokens); krsort($cellTokens);
krsort($newCellTokens); krsort($newCellTokens);
} else { } else {
@ -762,22 +762,22 @@ class ReferenceHelper
/** /**
* Update all cell references within a formula, irrespective of worksheet. * Update all cell references within a formula, irrespective of worksheet.
*/ */
public function updateFormulaReferencesAnyWorksheet(string $formula = '', int $insertColumns = 0, int $insertRows = 0): string public function updateFormulaReferencesAnyWorksheet(string $formula = '', int $numberOfColumns = 0, int $numberOfRows = 0): string
{ {
$formula = $this->updateCellReferencesAllWorksheets($formula, $insertColumns, $insertRows); $formula = $this->updateCellReferencesAllWorksheets($formula, $numberOfColumns, $numberOfRows);
if ($insertColumns !== 0) { if ($numberOfColumns !== 0) {
$formula = $this->updateColumnRangesAllWorksheets($formula, $insertColumns); $formula = $this->updateColumnRangesAllWorksheets($formula, $numberOfColumns);
} }
if ($insertRows !== 0) { if ($numberOfRows !== 0) {
$formula = $this->updateRowRangesAllWorksheets($formula, $insertRows); $formula = $this->updateRowRangesAllWorksheets($formula, $numberOfRows);
} }
return $formula; return $formula;
} }
private function updateCellReferencesAllWorksheets(string $formula, int $insertColumns, int $insertRows): string private function updateCellReferencesAllWorksheets(string $formula, int $numberOfColumns, int $numberOfRows): string
{ {
$splitCount = preg_match_all( $splitCount = preg_match_all(
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui', '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui',
@ -804,11 +804,11 @@ class ReferenceHelper
$row = $rows[$splitCount][0]; $row = $rows[$splitCount][0];
if (!empty($column) && $column[0] !== '$') { if (!empty($column) && $column[0] !== '$') {
$column = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($column) + $insertColumns); $column = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($column) + $numberOfColumns);
$formula = substr($formula, 0, $columnOffset) . $column . substr($formula, $columnOffset + $columnLength); $formula = substr($formula, 0, $columnOffset) . $column . substr($formula, $columnOffset + $columnLength);
} }
if (!empty($row) && $row[0] !== '$') { if (!empty($row) && $row[0] !== '$') {
$row += $insertRows; $row += $numberOfRows;
$formula = substr($formula, 0, $rowOffset) . $row . substr($formula, $rowOffset + $rowLength); $formula = substr($formula, 0, $rowOffset) . $row . substr($formula, $rowOffset + $rowLength);
} }
} }
@ -816,7 +816,7 @@ class ReferenceHelper
return $formula; return $formula;
} }
private function updateColumnRangesAllWorksheets(string $formula, int $insertColumns): string private function updateColumnRangesAllWorksheets(string $formula, int $numberOfColumns): string
{ {
$splitCount = preg_match_all( $splitCount = preg_match_all(
'/' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '/mui', '/' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '/mui',
@ -843,11 +843,11 @@ class ReferenceHelper
$toColumn = $toColumns[$splitCount][0]; $toColumn = $toColumns[$splitCount][0];
if (!empty($fromColumn) && $fromColumn[0] !== '$') { if (!empty($fromColumn) && $fromColumn[0] !== '$') {
$fromColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($fromColumn) + $insertColumns); $fromColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($fromColumn) + $numberOfColumns);
$formula = substr($formula, 0, $fromColumnOffset) . $fromColumn . substr($formula, $fromColumnOffset + $fromColumnLength); $formula = substr($formula, 0, $fromColumnOffset) . $fromColumn . substr($formula, $fromColumnOffset + $fromColumnLength);
} }
if (!empty($toColumn) && $toColumn[0] !== '$') { if (!empty($toColumn) && $toColumn[0] !== '$') {
$toColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($toColumn) + $insertColumns); $toColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($toColumn) + $numberOfColumns);
$formula = substr($formula, 0, $toColumnOffset) . $toColumn . substr($formula, $toColumnOffset + $toColumnLength); $formula = substr($formula, 0, $toColumnOffset) . $toColumn . substr($formula, $toColumnOffset + $toColumnLength);
} }
} }
@ -855,7 +855,7 @@ class ReferenceHelper
return $formula; return $formula;
} }
private function updateRowRangesAllWorksheets(string $formula, int $insertRows): string private function updateRowRangesAllWorksheets(string $formula, int $numberOfRows): string
{ {
$splitCount = preg_match_all( $splitCount = preg_match_all(
'/' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '/mui', '/' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '/mui',
@ -882,11 +882,11 @@ class ReferenceHelper
$toRow = $toRows[$splitCount][0]; $toRow = $toRows[$splitCount][0];
if (!empty($fromRow) && $fromRow[0] !== '$') { if (!empty($fromRow) && $fromRow[0] !== '$') {
$fromRow += $insertRows; $fromRow += $numberOfRows;
$formula = substr($formula, 0, $fromRowOffset) . $fromRow . substr($formula, $fromRowOffset + $fromRowLength); $formula = substr($formula, 0, $fromRowOffset) . $fromRow . substr($formula, $fromRowOffset + $fromRowLength);
} }
if (!empty($toRow) && $toRow[0] !== '$') { if (!empty($toRow) && $toRow[0] !== '$') {
$toRow += $insertRows; $toRow += $numberOfRows;
$formula = substr($formula, 0, $toRowOffset) . $toRow . substr($formula, $toRowOffset + $toRowLength); $formula = substr($formula, 0, $toRowOffset) . $toRow . substr($formula, $toRowOffset + $toRowLength);
} }
} }
@ -897,29 +897,29 @@ class ReferenceHelper
/** /**
* Update cell reference. * Update cell reference.
* *
* @param string $pCellRange Cell range * @param string $cellReference Cell address or range of addresses
* @param string $pBefore Insert before this one * @param string $beforeCellAddress Insert before this one
* @param int $pNumCols Number of columns to increment * @param int $numberOfColumns Number of columns to increment
* @param int $pNumRows Number of rows to increment * @param int $numberOfRows Number of rows to increment
* *
* @return string Updated cell range * @return string Updated cell range
*/ */
public function updateCellReference($pCellRange = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0) public function updateCellReference($cellReference = 'A1', $beforeCellAddress = 'A1', $numberOfColumns = 0, $numberOfRows = 0)
{ {
// Is it in another worksheet? Will not have to update anything. // Is it in another worksheet? Will not have to update anything.
if (strpos($pCellRange, '!') !== false) { if (strpos($cellReference, '!') !== false) {
return $pCellRange; return $cellReference;
// Is it a range or a single cell? // Is it a range or a single cell?
} elseif (!Coordinate::coordinateIsRange($pCellRange)) { } elseif (!Coordinate::coordinateIsRange($cellReference)) {
// Single cell // Single cell
return $this->updateSingleCellReference($pCellRange, $pBefore, $pNumCols, $pNumRows); return $this->updateSingleCellReference($cellReference, $beforeCellAddress, $numberOfColumns, $numberOfRows);
} elseif (Coordinate::coordinateIsRange($pCellRange)) { } elseif (Coordinate::coordinateIsRange($cellReference)) {
// Range // Range
return $this->updateCellRange($pCellRange, $pBefore, $pNumCols, $pNumRows); return $this->updateCellRange($cellReference, $beforeCellAddress, $numberOfColumns, $numberOfRows);
} }
// Return original // Return original
return $pCellRange; return $cellReference;
} }
/** /**
@ -953,33 +953,33 @@ class ReferenceHelper
/** /**
* Update cell range. * Update cell range.
* *
* @param string $pCellRange Cell range (e.g. 'B2:D4', 'B:C' or '2:3') * @param string $cellRange Cell range (e.g. 'B2:D4', 'B:C' or '2:3')
* @param string $pBefore Insert before this one * @param string $beforeCellAddress Insert before this one
* @param int $pNumCols Number of columns to increment * @param int $numberOfColumns Number of columns to increment
* @param int $pNumRows Number of rows to increment * @param int $numberOfRows Number of rows to increment
* *
* @return string Updated cell range * @return string Updated cell range
*/ */
private function updateCellRange($pCellRange = 'A1:A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0) private function updateCellRange($cellRange = 'A1:A1', $beforeCellAddress = 'A1', $numberOfColumns = 0, $numberOfRows = 0)
{ {
if (!Coordinate::coordinateIsRange($pCellRange)) { if (!Coordinate::coordinateIsRange($cellRange)) {
throw new Exception('Only cell ranges may be passed to this method.'); throw new Exception('Only cell ranges may be passed to this method.');
} }
// Update range // Update range
$range = Coordinate::splitRange($pCellRange); $range = Coordinate::splitRange($cellRange);
$ic = count($range); $ic = count($range);
for ($i = 0; $i < $ic; ++$i) { for ($i = 0; $i < $ic; ++$i) {
$jc = count($range[$i]); $jc = count($range[$i]);
for ($j = 0; $j < $jc; ++$j) { for ($j = 0; $j < $jc; ++$j) {
if (ctype_alpha($range[$i][$j])) { if (ctype_alpha($range[$i][$j])) {
$r = Coordinate::coordinateFromString($this->updateSingleCellReference($range[$i][$j] . '1', $pBefore, $pNumCols, $pNumRows)); $r = Coordinate::coordinateFromString($this->updateSingleCellReference($range[$i][$j] . '1', $beforeCellAddress, $numberOfColumns, $numberOfRows));
$range[$i][$j] = $r[0]; $range[$i][$j] = $r[0];
} elseif (ctype_digit($range[$i][$j])) { } elseif (ctype_digit($range[$i][$j])) {
$r = Coordinate::coordinateFromString($this->updateSingleCellReference('A' . $range[$i][$j], $pBefore, $pNumCols, $pNumRows)); $r = Coordinate::coordinateFromString($this->updateSingleCellReference('A' . $range[$i][$j], $beforeCellAddress, $numberOfColumns, $numberOfRows));
$range[$i][$j] = $r[1]; $range[$i][$j] = $r[1];
} else { } else {
$range[$i][$j] = $this->updateSingleCellReference($range[$i][$j], $pBefore, $pNumCols, $pNumRows); $range[$i][$j] = $this->updateSingleCellReference($range[$i][$j], $beforeCellAddress, $numberOfColumns, $numberOfRows);
} }
} }
} }
@ -991,24 +991,24 @@ class ReferenceHelper
/** /**
* Update single cell reference. * Update single cell reference.
* *
* @param string $pCellReference Single cell reference * @param string $cellReference Single cell reference
* @param string $pBefore Insert before this one * @param string $beforeCellAddress Insert before this one
* @param int $pNumCols Number of columns to increment * @param int $numberOfColumns Number of columns to increment
* @param int $pNumRows Number of rows to increment * @param int $numberOfRows Number of rows to increment
* *
* @return string Updated cell reference * @return string Updated cell reference
*/ */
private function updateSingleCellReference($pCellReference = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0) private function updateSingleCellReference($cellReference = 'A1', $beforeCellAddress = 'A1', $numberOfColumns = 0, $numberOfRows = 0)
{ {
if (Coordinate::coordinateIsRange($pCellReference)) { if (Coordinate::coordinateIsRange($cellReference)) {
throw new Exception('Only single cell references may be passed to this method.'); throw new Exception('Only single cell references may be passed to this method.');
} }
// Get coordinate of $pBefore // Get coordinate of $beforeCellAddress
[$beforeColumn, $beforeRow] = Coordinate::coordinateFromString($pBefore); [$beforeColumn, $beforeRow] = Coordinate::coordinateFromString($beforeCellAddress);
// Get coordinate of $pCellReference // Get coordinate of $cellReference
[$newColumn, $newRow] = Coordinate::coordinateFromString($pCellReference); [$newColumn, $newRow] = Coordinate::coordinateFromString($cellReference);
// Verify which parts should be updated // Verify which parts should be updated
$updateColumn = (($newColumn[0] != '$') && ($beforeColumn[0] != '$') && (Coordinate::columnIndexFromString($newColumn) >= Coordinate::columnIndexFromString($beforeColumn))); $updateColumn = (($newColumn[0] != '$') && ($beforeColumn[0] != '$') && (Coordinate::columnIndexFromString($newColumn) >= Coordinate::columnIndexFromString($beforeColumn)));
@ -1016,12 +1016,12 @@ class ReferenceHelper
// Create new column reference // Create new column reference
if ($updateColumn) { if ($updateColumn) {
$newColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($newColumn) + $pNumCols); $newColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($newColumn) + $numberOfColumns);
} }
// Create new row reference // Create new row reference
if ($updateRow) { if ($updateRow) {
$newRow = $newRow + $pNumRows; $newRow = $newRow + $numberOfRows;
} }
// Return new reference // Return new reference

View File

@ -71,16 +71,16 @@ class Settings
/** /**
* Identify to PhpSpreadsheet the external library to use for rendering charts. * Identify to PhpSpreadsheet the external library to use for rendering charts.
* *
* @param string $rendererClass Class name of the chart renderer * @param string $rendererClassName Class name of the chart renderer
* eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
*/ */
public static function setChartRenderer(string $rendererClass): void public static function setChartRenderer(string $rendererClassName): void
{ {
if (!is_a($rendererClass, IRenderer::class, true)) { if (!is_a($rendererClassName, IRenderer::class, true)) {
throw new Exception('Chart renderer must implement ' . IRenderer::class); throw new Exception('Chart renderer must implement ' . IRenderer::class);
} }
self::$chartRenderer = $rendererClass; self::$chartRenderer = $rendererClassName;
} }
/** /**

View File

@ -439,27 +439,27 @@ class Spreadsheet
/** /**
* Check if a sheet with a specified code name already exists. * Check if a sheet with a specified code name already exists.
* *
* @param string $pSheetCodeName Name of the worksheet to check * @param string $codeName Name of the worksheet to check
* *
* @return bool * @return bool
*/ */
public function sheetCodeNameExists($pSheetCodeName) public function sheetCodeNameExists($codeName)
{ {
return $this->getSheetByCodeName($pSheetCodeName) !== null; return $this->getSheetByCodeName($codeName) !== null;
} }
/** /**
* Get sheet by code name. Warning : sheet don't have always a code name ! * Get sheet by code name. Warning : sheet don't have always a code name !
* *
* @param string $pName Sheet name * @param string $codeName Sheet name
* *
* @return Worksheet * @return Worksheet
*/ */
public function getSheetByCodeName($pName) public function getSheetByCodeName($codeName)
{ {
$worksheetCount = count($this->workSheetCollection); $worksheetCount = count($this->workSheetCollection);
for ($i = 0; $i < $worksheetCount; ++$i) { for ($i = 0; $i < $worksheetCount; ++$i) {
if ($this->workSheetCollection[$i]->getCodeName() == $pName) { if ($this->workSheetCollection[$i]->getCodeName() == $codeName) {
return $this->workSheetCollection[$i]; return $this->workSheetCollection[$i];
} }
} }
@ -545,9 +545,9 @@ class Spreadsheet
/** /**
* Set properties. * Set properties.
*/ */
public function setProperties(Document\Properties $pValue): void public function setProperties(Document\Properties $documentProperties): void
{ {
$this->properties = $pValue; $this->properties = $documentProperties;
} }
/** /**
@ -563,9 +563,9 @@ class Spreadsheet
/** /**
* Set security. * Set security.
*/ */
public function setSecurity(Document\Security $pValue): void public function setSecurity(Document\Security $documentSecurity): void
{ {
$this->security = $pValue; $this->security = $documentSecurity;
} }
/** /**
@ -596,76 +596,78 @@ class Spreadsheet
/** /**
* Check if a sheet with a specified name already exists. * Check if a sheet with a specified name already exists.
* *
* @param string $pSheetName Name of the worksheet to check * @param string $worksheetName Name of the worksheet to check
* *
* @return bool * @return bool
*/ */
public function sheetNameExists($pSheetName) public function sheetNameExists($worksheetName)
{ {
return $this->getSheetByName($pSheetName) !== null; return $this->getSheetByName($worksheetName) !== null;
} }
/** /**
* Add sheet. * Add sheet.
* *
* @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last) * @param Worksheet $worksheet The worskeet to add
* @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last)
* *
* @return Worksheet * @return Worksheet
* @throws Exception
*/ */
public function addSheet(Worksheet $pSheet, $iSheetIndex = null) public function addSheet(Worksheet $worksheet, $sheetIndex = null)
{ {
if ($this->sheetNameExists($pSheet->getTitle())) { if ($this->sheetNameExists($worksheet->getTitle())) {
throw new Exception( throw new Exception(
"Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first." "Workbook already contains a worksheet named '{$worksheet->getTitle()}'. Rename this worksheet first."
); );
} }
if ($iSheetIndex === null) { if ($sheetIndex === null) {
if ($this->activeSheetIndex < 0) { if ($this->activeSheetIndex < 0) {
$this->activeSheetIndex = 0; $this->activeSheetIndex = 0;
} }
$this->workSheetCollection[] = $pSheet; $this->workSheetCollection[] = $worksheet;
} else { } else {
// Insert the sheet at the requested index // Insert the sheet at the requested index
array_splice( array_splice(
$this->workSheetCollection, $this->workSheetCollection,
$iSheetIndex, $sheetIndex,
0, 0,
[$pSheet] [$worksheet]
); );
// Adjust active sheet index if necessary // Adjust active sheet index if necessary
if ($this->activeSheetIndex >= $iSheetIndex) { if ($this->activeSheetIndex >= $sheetIndex) {
++$this->activeSheetIndex; ++$this->activeSheetIndex;
} }
} }
if ($pSheet->getParent() === null) { if ($worksheet->getParent() === null) {
$pSheet->rebindParent($this); $worksheet->rebindParent($this);
} }
return $pSheet; return $worksheet;
} }
/** /**
* Remove sheet by index. * Remove sheet by index.
* *
* @param int $pIndex Active sheet index * @param int $sheetIndex Index position of the worksheet to remove
*/ */
public function removeSheetByIndex($pIndex): void public function removeSheetByIndex($sheetIndex): void
{ {
$numSheets = count($this->workSheetCollection); $numSheets = count($this->workSheetCollection);
if ($pIndex > $numSheets - 1) { if ($sheetIndex > $numSheets - 1) {
throw new Exception( throw new Exception(
"You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}." "You tried to remove a sheet by the out of bounds index: {$sheetIndex}. The actual number of sheets is {$numSheets}."
); );
} }
array_splice($this->workSheetCollection, $pIndex, 1); array_splice($this->workSheetCollection, $sheetIndex, 1);
// Adjust active sheet index if necessary // Adjust active sheet index if necessary
if ( if (
($this->activeSheetIndex >= $pIndex) && ($this->activeSheetIndex >= $sheetIndex) &&
($pIndex > count($this->workSheetCollection) - 1) ($sheetIndex > count($this->workSheetCollection) - 1)
) { ) {
--$this->activeSheetIndex; --$this->activeSheetIndex;
} }
@ -674,21 +676,21 @@ class Spreadsheet
/** /**
* Get sheet by index. * Get sheet by index.
* *
* @param int $pIndex Sheet index * @param int $sheetIndex Sheet index
* *
* @return Worksheet * @return Worksheet
*/ */
public function getSheet($pIndex) public function getSheet($sheetIndex)
{ {
if (!isset($this->workSheetCollection[$pIndex])) { if (!isset($this->workSheetCollection[$sheetIndex])) {
$numSheets = $this->getSheetCount(); $numSheets = $this->getSheetCount();
throw new Exception( throw new Exception(
"Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}." "Your requested sheet index: {$sheetIndex} is out of bounds. The actual number of sheets is {$numSheets}."
); );
} }
return $this->workSheetCollection[$pIndex]; return $this->workSheetCollection[$sheetIndex];
} }
/** /**
@ -704,15 +706,15 @@ class Spreadsheet
/** /**
* Get sheet by name. * Get sheet by name.
* *
* @param string $pName Sheet name * @param string $worksheetName Sheet name
* *
* @return null|Worksheet * @return null|Worksheet
*/ */
public function getSheetByName($pName) public function getSheetByName($worksheetName)
{ {
$worksheetCount = count($this->workSheetCollection); $worksheetCount = count($this->workSheetCollection);
for ($i = 0; $i < $worksheetCount; ++$i) { for ($i = 0; $i < $worksheetCount; ++$i) {
if ($this->workSheetCollection[$i]->getTitle() === trim($pName, "'")) { if ($this->workSheetCollection[$i]->getTitle() === trim($worksheetName, "'")) {
return $this->workSheetCollection[$i]; return $this->workSheetCollection[$i];
} }
} }
@ -725,10 +727,10 @@ class Spreadsheet
* *
* @return int index * @return int index
*/ */
public function getIndex(Worksheet $pSheet) public function getIndex(Worksheet $worksheet)
{ {
foreach ($this->workSheetCollection as $key => $value) { foreach ($this->workSheetCollection as $key => $value) {
if ($value->getHashCode() === $pSheet->getHashCode()) { if ($value->getHashCode() === $worksheet->getHashCode()) {
return $key; return $key;
} }
} }
@ -739,14 +741,14 @@ class Spreadsheet
/** /**
* Set index for sheet by sheet name. * Set index for sheet by sheet name.
* *
* @param string $sheetName Sheet name to modify index for * @param string $worksheetName Sheet name to modify index for
* @param int $newIndex New index for the sheet * @param int $newIndexPosition New index for the sheet
* *
* @return int New sheet index * @return int New sheet index
*/ */
public function setIndexByName($sheetName, $newIndex) public function setIndexByName($worksheetName, $newIndexPosition)
{ {
$oldIndex = $this->getIndex($this->getSheetByName($sheetName)); $oldIndex = $this->getIndex($this->getSheetByName($worksheetName));
$pSheet = array_splice( $pSheet = array_splice(
$this->workSheetCollection, $this->workSheetCollection,
$oldIndex, $oldIndex,
@ -754,12 +756,12 @@ class Spreadsheet
); );
array_splice( array_splice(
$this->workSheetCollection, $this->workSheetCollection,
$newIndex, $newIndexPosition,
0, 0,
$pSheet $pSheet
); );
return $newIndex; return $newIndexPosition;
} }
/** /**
@ -785,20 +787,20 @@ class Spreadsheet
/** /**
* Set active sheet index. * Set active sheet index.
* *
* @param int $pIndex Active sheet index * @param int $worksheetIndex Active sheet index
* *
* @return Worksheet * @return Worksheet
*/ */
public function setActiveSheetIndex($pIndex) public function setActiveSheetIndex($worksheetIndex)
{ {
$numSheets = count($this->workSheetCollection); $numSheets = count($this->workSheetCollection);
if ($pIndex > $numSheets - 1) { if ($worksheetIndex > $numSheets - 1) {
throw new Exception( throw new Exception(
"You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}." "You tried to set a sheet active by the out of bounds index: {$worksheetIndex}. The actual number of sheets is {$numSheets}."
); );
} }
$this->activeSheetIndex = $pIndex; $this->activeSheetIndex = $worksheetIndex;
return $this->getActiveSheet(); return $this->getActiveSheet();
} }
@ -806,19 +808,19 @@ class Spreadsheet
/** /**
* Set active sheet index by name. * Set active sheet index by name.
* *
* @param string $pValue Sheet title * @param string $worksheetName Sheet title
* *
* @return Worksheet * @return Worksheet
*/ */
public function setActiveSheetIndexByName($pValue) public function setActiveSheetIndexByName($worksheetName)
{ {
if (($worksheet = $this->getSheetByName($pValue)) instanceof Worksheet) { if (($worksheet = $this->getSheetByName($worksheetName)) instanceof Worksheet) {
$this->setActiveSheetIndex($this->getIndex($worksheet)); $this->setActiveSheetIndex($this->getIndex($worksheet));
return $worksheet; return $worksheet;
} }
throw new Exception('Workbook does not contain sheet:' . $pValue); throw new Exception('Workbook does not contain sheet:' . $worksheetName);
} }
/** /**
@ -840,35 +842,35 @@ class Spreadsheet
/** /**
* Add external sheet. * Add external sheet.
* *
* @param Worksheet $pSheet External sheet to add * @param Worksheet $worksheet External sheet to add
* @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last) * @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last)
* *
* @return Worksheet * @return Worksheet
*/ */
public function addExternalSheet(Worksheet $pSheet, $iSheetIndex = null) public function addExternalSheet(Worksheet $worksheet, $sheetIndex = null)
{ {
if ($this->sheetNameExists($pSheet->getTitle())) { if ($this->sheetNameExists($worksheet->getTitle())) {
throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first."); throw new Exception("Workbook already contains a worksheet named '{$worksheet->getTitle()}'. Rename the external sheet first.");
} }
// count how many cellXfs there are in this workbook currently, we will need this below // count how many cellXfs there are in this workbook currently, we will need this below
$countCellXfs = count($this->cellXfCollection); $countCellXfs = count($this->cellXfCollection);
// copy all the shared cellXfs from the external workbook and append them to the current // copy all the shared cellXfs from the external workbook and append them to the current
foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) { foreach ($worksheet->getParent()->getCellXfCollection() as $cellXf) {
$this->addCellXf(clone $cellXf); $this->addCellXf(clone $cellXf);
} }
// move sheet to this workbook // move sheet to this workbook
$pSheet->rebindParent($this); $worksheet->rebindParent($this);
// update the cellXfs // update the cellXfs
foreach ($pSheet->getCoordinates(false) as $coordinate) { foreach ($worksheet->getCoordinates(false) as $coordinate) {
$cell = $pSheet->getCell($coordinate); $cell = $worksheet->getCell($coordinate);
$cell->setXfIndex($cell->getXfIndex() + $countCellXfs); $cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
} }
return $this->addSheet($pSheet, $iSheetIndex); return $this->addSheet($worksheet, $sheetIndex);
} }
/** /**
@ -948,9 +950,9 @@ class Spreadsheet
/** /**
* Get named range. * Get named range.
* *
* @param null|Worksheet $pSheet Scope. Use null for global scope * @param null|Worksheet $worksheet Scope. Use null for global scope
*/ */
public function getNamedRange(string $namedRange, ?Worksheet $pSheet = null): ?NamedRange public function getNamedRange(string $namedRange, ?Worksheet $worksheet = null): ?NamedRange
{ {
$returnValue = null; $returnValue = null;
@ -959,7 +961,7 @@ class Spreadsheet
// first look for global named range // first look for global named range
$returnValue = $this->getGlobalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE); $returnValue = $this->getGlobalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE);
// then look for local named range (has priority over global named range if both names exist) // then look for local named range (has priority over global named range if both names exist)
$returnValue = $this->getLocalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE, $pSheet) ?: $returnValue; $returnValue = $this->getLocalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE, $worksheet) ?: $returnValue;
} }
return $returnValue instanceof NamedRange ? $returnValue : null; return $returnValue instanceof NamedRange ? $returnValue : null;
@ -968,9 +970,9 @@ class Spreadsheet
/** /**
* Get named formula. * Get named formula.
* *
* @param null|Worksheet $pSheet Scope. Use null for global scope * @param null|Worksheet $worksheet Scope. Use null for global scope
*/ */
public function getNamedFormula(string $namedFormula, ?Worksheet $pSheet = null): ?NamedFormula public function getNamedFormula(string $namedFormula, ?Worksheet $worksheet = null): ?NamedFormula
{ {
$returnValue = null; $returnValue = null;
@ -979,7 +981,7 @@ class Spreadsheet
// first look for global named formula // first look for global named formula
$returnValue = $this->getGlobalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA); $returnValue = $this->getGlobalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA);
// then look for local named formula (has priority over global named formula if both names exist) // then look for local named formula (has priority over global named formula if both names exist)
$returnValue = $this->getLocalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA, $pSheet) ?: $returnValue; $returnValue = $this->getLocalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA, $worksheet) ?: $returnValue;
} }
return $returnValue instanceof NamedFormula ? $returnValue : null; return $returnValue instanceof NamedFormula ? $returnValue : null;
@ -1009,9 +1011,9 @@ class Spreadsheet
/** /**
* Get named range. * Get named range.
* *
* @param null|Worksheet $pSheet Scope. Use null for global scope * @param null|Worksheet $worksheet Scope. Use null for global scope
*/ */
public function getDefinedName(string $definedName, ?Worksheet $pSheet = null): ?DefinedName public function getDefinedName(string $definedName, ?Worksheet $worksheet = null): ?DefinedName
{ {
$returnValue = null; $returnValue = null;
@ -1023,8 +1025,8 @@ class Spreadsheet
} }
// then look for local defined name (has priority over global defined name if both names exist) // then look for local defined name (has priority over global defined name if both names exist)
if (($pSheet !== null) && isset($this->definedNames[$pSheet->getTitle() . '!' . $definedName])) { if (($worksheet !== null) && isset($this->definedNames[$worksheet->getTitle() . '!' . $definedName])) {
$returnValue = $this->definedNames[$pSheet->getTitle() . '!' . $definedName]; $returnValue = $this->definedNames[$worksheet->getTitle() . '!' . $definedName];
} }
} }
@ -1034,53 +1036,53 @@ class Spreadsheet
/** /**
* Remove named range. * Remove named range.
* *
* @param null|Worksheet $pSheet scope: use null for global scope * @param null|Worksheet $worksheet scope: use null for global scope
* *
* @return $this * @return $this
*/ */
public function removeNamedRange(string $namedRange, ?Worksheet $pSheet = null): self public function removeNamedRange(string $namedRange, ?Worksheet $worksheet = null): self
{ {
if ($this->getNamedRange($namedRange, $pSheet) === null) { if ($this->getNamedRange($namedRange, $worksheet) === null) {
return $this; return $this;
} }
return $this->removeDefinedName($namedRange, $pSheet); return $this->removeDefinedName($namedRange, $worksheet);
} }
/** /**
* Remove named formula. * Remove named formula.
* *
* @param null|Worksheet $pSheet scope: use null for global scope * @param null|Worksheet $worksheet scope: use null for global scope
* *
* @return $this * @return $this
*/ */
public function removeNamedFormula(string $namedFormula, ?Worksheet $pSheet = null): self public function removeNamedFormula(string $namedFormula, ?Worksheet $worksheet = null): self
{ {
if ($this->getNamedFormula($namedFormula, $pSheet) === null) { if ($this->getNamedFormula($namedFormula, $worksheet) === null) {
return $this; return $this;
} }
return $this->removeDefinedName($namedFormula, $pSheet); return $this->removeDefinedName($namedFormula, $worksheet);
} }
/** /**
* Remove defined name. * Remove defined name.
* *
* @param null|Worksheet $pSheet scope: use null for global scope * @param null|Worksheet $worksheet scope: use null for global scope
* *
* @return $this * @return $this
*/ */
public function removeDefinedName(string $definedName, ?Worksheet $pSheet = null): self public function removeDefinedName(string $definedName, ?Worksheet $worksheet = null): self
{ {
$definedName = StringHelper::strToUpper($definedName); $definedName = StringHelper::strToUpper($definedName);
if ($pSheet === null) { if ($worksheet === null) {
if (isset($this->definedNames[$definedName])) { if (isset($this->definedNames[$definedName])) {
unset($this->definedNames[$definedName]); unset($this->definedNames[$definedName]);
} }
} else { } else {
if (isset($this->definedNames[$pSheet->getTitle() . '!' . $definedName])) { if (isset($this->definedNames[$worksheet->getTitle() . '!' . $definedName])) {
unset($this->definedNames[$pSheet->getTitle() . '!' . $definedName]); unset($this->definedNames[$worksheet->getTitle() . '!' . $definedName]);
} elseif (isset($this->definedNames[$definedName])) { } elseif (isset($this->definedNames[$definedName])) {
unset($this->definedNames[$definedName]); unset($this->definedNames[$definedName]);
} }
@ -1142,26 +1144,26 @@ class Spreadsheet
/** /**
* Get cellXf by index. * Get cellXf by index.
* *
* @param int $pIndex * @param int $cellStyleIndex
* *
* @return Style * @return Style
*/ */
public function getCellXfByIndex($pIndex) public function getCellXfByIndex($cellStyleIndex)
{ {
return $this->cellXfCollection[$pIndex]; return $this->cellXfCollection[$cellStyleIndex];
} }
/** /**
* Get cellXf by hash code. * Get cellXf by hash code.
* *
* @param string $pValue * @param string $hashcode
* *
* @return false|Style * @return false|Style
*/ */
public function getCellXfByHashCode($pValue) public function getCellXfByHashCode($hashcode)
{ {
foreach ($this->cellXfCollection as $cellXf) { foreach ($this->cellXfCollection as $cellXf) {
if ($cellXf->getHashCode() === $pValue) { if ($cellXf->getHashCode() === $hashcode) {
return $cellXf; return $cellXf;
} }
} }
@ -1172,13 +1174,13 @@ class Spreadsheet
/** /**
* Check if style exists in style collection. * Check if style exists in style collection.
* *
* @param Style $pCellStyle * @param Style $cellStyleIndex
* *
* @return bool * @return bool
*/ */
public function cellXfExists($pCellStyle) public function cellXfExists($cellStyleIndex)
{ {
return in_array($pCellStyle, $this->cellXfCollection, true); return in_array($cellStyleIndex, $this->cellXfCollection, true);
} }
/** /**
@ -1207,26 +1209,26 @@ class Spreadsheet
/** /**
* Remove cellXf by index. It is ensured that all cells get their xf index updated. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
* *
* @param int $pIndex Index to cellXf * @param int $cellStyleIndex Index to cellXf
*/ */
public function removeCellXfByIndex($pIndex): void public function removeCellXfByIndex($cellStyleIndex): void
{ {
if ($pIndex > count($this->cellXfCollection) - 1) { if ($cellStyleIndex > count($this->cellXfCollection) - 1) {
throw new Exception('CellXf index is out of bounds.'); throw new Exception('CellXf index is out of bounds.');
} }
// first remove the cellXf // first remove the cellXf
array_splice($this->cellXfCollection, $pIndex, 1); array_splice($this->cellXfCollection, $cellStyleIndex, 1);
// then update cellXf indexes for cells // then update cellXf indexes for cells
foreach ($this->workSheetCollection as $worksheet) { foreach ($this->workSheetCollection as $worksheet) {
foreach ($worksheet->getCoordinates(false) as $coordinate) { foreach ($worksheet->getCoordinates(false) as $coordinate) {
$cell = $worksheet->getCell($coordinate); $cell = $worksheet->getCell($coordinate);
$xfIndex = $cell->getXfIndex(); $xfIndex = $cell->getXfIndex();
if ($xfIndex > $pIndex) { if ($xfIndex > $cellStyleIndex) {
// decrease xf index by 1 // decrease xf index by 1
$cell->setXfIndex($xfIndex - 1); $cell->setXfIndex($xfIndex - 1);
} elseif ($xfIndex == $pIndex) { } elseif ($xfIndex == $cellStyleIndex) {
// set to default xf index 0 // set to default xf index 0
$cell->setXfIndex(0); $cell->setXfIndex(0);
} }
@ -1257,26 +1259,26 @@ class Spreadsheet
/** /**
* Get cellStyleXf by index. * Get cellStyleXf by index.
* *
* @param int $pIndex Index to cellXf * @param int $cellStyleIndex Index to cellXf
* *
* @return Style * @return Style
*/ */
public function getCellStyleXfByIndex($pIndex) public function getCellStyleXfByIndex($cellStyleIndex)
{ {
return $this->cellStyleXfCollection[$pIndex]; return $this->cellStyleXfCollection[$cellStyleIndex];
} }
/** /**
* Get cellStyleXf by hash code. * Get cellStyleXf by hash code.
* *
* @param string $pValue * @param string $hashcode
* *
* @return false|Style * @return false|Style
*/ */
public function getCellStyleXfByHashCode($pValue) public function getCellStyleXfByHashCode($hashcode)
{ {
foreach ($this->cellStyleXfCollection as $cellStyleXf) { foreach ($this->cellStyleXfCollection as $cellStyleXf) {
if ($cellStyleXf->getHashCode() === $pValue) { if ($cellStyleXf->getHashCode() === $hashcode) {
return $cellStyleXf; return $cellStyleXf;
} }
} }
@ -1287,23 +1289,23 @@ class Spreadsheet
/** /**
* Add a cellStyleXf to the workbook. * Add a cellStyleXf to the workbook.
*/ */
public function addCellStyleXf(Style $pStyle): void public function addCellStyleXf(Style $style): void
{ {
$this->cellStyleXfCollection[] = $pStyle; $this->cellStyleXfCollection[] = $style;
$pStyle->setIndex(count($this->cellStyleXfCollection) - 1); $style->setIndex(count($this->cellStyleXfCollection) - 1);
} }
/** /**
* Remove cellStyleXf by index. * Remove cellStyleXf by index.
* *
* @param int $pIndex Index to cellXf * @param int $cellStyleIndex Index to cellXf
*/ */
public function removeCellStyleXfByIndex($pIndex): void public function removeCellStyleXfByIndex($cellStyleIndex): void
{ {
if ($pIndex > count($this->cellStyleXfCollection) - 1) { if ($cellStyleIndex > count($this->cellStyleXfCollection) - 1) {
throw new Exception('CellStyleXf index is out of bounds.'); throw new Exception('CellStyleXf index is out of bounds.');
} }
array_splice($this->cellStyleXfCollection, $pIndex, 1); array_splice($this->cellStyleXfCollection, $cellStyleIndex, 1);
} }
/** /**