This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fixed formula/formatting bug when removing rows #70
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,22 +77,24 @@ protected function __construct() { | |
* @throws Exception | ||
*/ | ||
public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, PHPExcel_Worksheet $pSheet = null) { | ||
$remove = ($pNumCols < 0 || $pNumRows < 0); | ||
$aCellCollection = $pSheet->getCellCollection(); | ||
|
||
// Get coordinates of $pBefore | ||
$beforeColumn = 'A'; | ||
$beforeRow = 1; | ||
list($beforeColumn, $beforeRow) = PHPExcel_Cell::coordinateFromString( $pBefore ); | ||
$beforeColumnIndex = PHPExcel_Cell::columnIndexFromString($beforeColumn); | ||
|
||
|
||
// Clear cells if we are removing columns or rows | ||
$highestColumn = $pSheet->getHighestColumn(); | ||
$highestRow = $pSheet->getHighestRow(); | ||
|
||
// 1. Clear column strips if we are removing columns | ||
if ($pNumCols < 0 && PHPExcel_Cell::columnIndexFromString($beforeColumn) - 2 + $pNumCols > 0) { | ||
if ($pNumCols < 0 && $beforeColumnIndex - 2 + $pNumCols > 0) { | ||
for ($i = 1; $i <= $highestRow - 1; ++$i) { | ||
for ($j = PHPExcel_Cell::columnIndexFromString($beforeColumn) - 1 + $pNumCols; $j <= PHPExcel_Cell::columnIndexFromString($beforeColumn) - 2; ++$j) { | ||
for ($j = $beforeColumnIndex - 1 + $pNumCols; $j <= $beforeColumnIndex - 2; ++$j) { | ||
$coordinate = PHPExcel_Cell::stringFromColumnIndex($j) . $i; | ||
$pSheet->removeConditionalStyles($coordinate); | ||
if ($pSheet->cellExists($coordinate)) { | ||
|
@@ -105,7 +107,7 @@ public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, P | |
|
||
// 2. Clear row strips if we are removing rows | ||
if ($pNumRows < 0 && $beforeRow - 1 + $pNumRows > 0) { | ||
for ($i = PHPExcel_Cell::columnIndexFromString($beforeColumn) - 1; $i <= PHPExcel_Cell::columnIndexFromString($highestColumn) - 1; ++$i) { | ||
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); | ||
|
@@ -119,20 +121,23 @@ public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, P | |
|
||
|
||
// Loop through cells, bottom-up, and change cell coordinates | ||
while (($cellID = ($pNumCols < 0 || $pNumRows < 0) ? array_shift($aCellCollection) : array_pop($aCellCollection))) { | ||
while (($cellID = $remove ? array_shift($aCellCollection) : array_pop($aCellCollection))) { | ||
$cell = $pSheet->getCell($cellID); | ||
$cellIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn()); | ||
if ($cellIndex-1 + $pNumCols < 0) { | ||
continue; | ||
} | ||
|
||
// New coordinates | ||
$newCoordinates = PHPExcel_Cell::stringFromColumnIndex( PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1 + $pNumCols ) . ($cell->getRow() + $pNumRows); | ||
$newCoordinates = PHPExcel_Cell::stringFromColumnIndex($cellIndex-1 + $pNumCols) . ($cell->getRow() + $pNumRows); | ||
|
||
// Should the cell be updated? Move value and cellXf index from one cell to another. | ||
if ((PHPExcel_Cell::columnIndexFromString( $cell->getColumn() ) >= PHPExcel_Cell::columnIndexFromString($beforeColumn)) && | ||
if (($cellIndex >= $beforeColumnIndex) && | ||
($cell->getRow() >= $beforeRow)) { | ||
|
||
// Update cell styles | ||
$pSheet->getCell($newCoordinates)->setXfIndex($cell->getXfIndex()); | ||
$cell->setXfIndex(0); | ||
|
||
|
||
// Insert this cell at its new location | ||
if ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_FORMULA) { | ||
// Formula should be adjusted | ||
|
@@ -145,7 +150,7 @@ public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, P | |
} | ||
|
||
// Clear the original cell | ||
$pSheet->getCell($cell->getCoordinate())->setValue(''); | ||
$pSheet->getCellCacheController()->deleteCacheData($cellID); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prevents overwriting unspecified cells with specific "empty" ones. This could have also been fixed in PHPExcel/Writer/Excel5/Worksheet.php :: _writeBlank() if it did not write empty cells with no format specified. |
||
|
||
} else { | ||
/* We don't need to update styles for rows/columns before our insertion position, | ||
|
@@ -164,16 +169,16 @@ public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, P | |
$highestColumn = $pSheet->getHighestColumn(); | ||
$highestRow = $pSheet->getHighestRow(); | ||
|
||
if ($pNumCols > 0 && PHPExcel_Cell::columnIndexFromString($beforeColumn) - 2 > 0) { | ||
if ($pNumCols > 0 && $beforeColumnIndex - 2 > 0) { | ||
for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) { | ||
|
||
// Style | ||
$coordinate = PHPExcel_Cell::stringFromColumnIndex( PHPExcel_Cell::columnIndexFromString($beforeColumn) - 2 ) . $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 = PHPExcel_Cell::columnIndexFromString($beforeColumn) - 1; $j <= PHPExcel_Cell::columnIndexFromString($beforeColumn) - 2 + $pNumCols; ++$j) { | ||
for ($j = $beforeColumnIndex - 1; $j <= $beforeColumnIndex - 2 + $pNumCols; ++$j) { | ||
$pSheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex); | ||
if ($conditionalStyles) { | ||
$cloned = array(); | ||
|
@@ -189,7 +194,7 @@ public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, P | |
} | ||
|
||
if ($pNumRows > 0 && $beforeRow - 1 > 0) { | ||
for ($i = PHPExcel_Cell::columnIndexFromString($beforeColumn) - 1; $i <= PHPExcel_Cell::columnIndexFromString($highestColumn) - 1; ++$i) { | ||
for ($i = $beforeColumnIndex - 1; $i <= PHPExcel_Cell::columnIndexFromString($highestColumn) - 1; ++$i) { | ||
|
||
// Style | ||
$coordinate = PHPExcel_Cell::stringFromColumnIndex($i) . ($beforeRow - 1); | ||
|
@@ -417,7 +422,7 @@ public function insertNewBefore($pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, P | |
* @return string Updated formula | ||
* @throws Exception | ||
*/ | ||
public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '') { | ||
public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '') { | ||
// Update cell references in the formula | ||
$formulaBlocks = explode('"',$pFormula); | ||
$i = false; | ||
|
@@ -528,8 +533,13 @@ public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCo | |
} | ||
} | ||
if ($adjustCount > 0) { | ||
krsort($cellTokens); | ||
krsort($newCellTokens); | ||
if ($pNumCols > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change order of operations depending on whether add/remove. |
||
krsort($cellTokens); | ||
krsort($newCellTokens); | ||
} else { | ||
ksort($cellTokens); | ||
ksort($newCellTokens); | ||
} | ||
// Update cell references in the formula | ||
$formulaBlock = str_replace('\\','',preg_replace($cellTokens,$newCellTokens,$formulaBlock)); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this new check to stay in bounds.