Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Some earlier branches remain supported and security fixes are applied to them; i
- `removeColumn()` no longer throws "Invalid column index 0" when it removes column A and every column up to the highest data column, e.g. `removeColumn('A')` on an empty sheet. [Issue #5049](https://github.com/PHPOffice/PhpSpreadsheet/issues/5049) [PR #5050](https://github.com/PHPOffice/PhpSpreadsheet/pull/5050)
- `removeColumn()` and `removeRow()` take the removed columns/rows out of data validation ranges: a validation that lies only on them is deleted, and a range that ends on them shrinks, instead of covering the column/row that slides into their place. [Issue #5047](https://github.com/PHPOffice/PhpSpreadsheet/issues/5047) [PR #5048](https://github.com/PHPOffice/PhpSpreadsheet/pull/5048)
- Minor cleanup in Ods Reader to fix test which was not working in Windows. [PR #5052](https://github.com/PHPOffice/PhpSpreadsheet/pull/5052)
- Removing or inserting a column that shifts the header row of a table no longer throws `Worksheet no longer exists`. [Issue #5058](https://github.com/PHPOffice/PhpSpreadsheet/issues/5058) [PR #5059](https://github.com/PHPOffice/PhpSpreadsheet/pull/5059)
- Branch pruning no longer throws "internal error" for an IF nested in the condition of another IF inside a pruned branch. [Issue #3755](https://github.com/PHPOffice/PhpSpreadsheet/issues/3755) [Issue #3790](https://github.com/PHPOffice/PhpSpreadsheet/issues/3790) [PR #5055](https://github.com/PHPOffice/PhpSpreadsheet/pull/5055)
- Xls Writer no longer loses EMF and WMF images (they are written as PNG); the resulting file could not be read back (`Call to a member function getBlipType() on null`). [Issue #274](https://github.com/PHPOffice/PhpSpreadsheet/issues/274)
- Xls Reader reads EMF and WMF pictures (as PNG `MemoryDrawing`); they were ignored. [Issue #274](https://github.com/PHPOffice/PhpSpreadsheet/issues/274)
Expand Down
14 changes: 9 additions & 5 deletions src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,24 +366,28 @@ public function setValueExplicit(mixed $value, string $dataType = DataType::TYPE

$this->updateInCollection();
$cellCoordinate = $this->getCoordinate();
self::updateIfCellIsTableHeader($this->getParent()?->getParent(), $this, $oldValue, $value);
$worksheet = $this->getWorksheet();
$cellCollection = $this->getParent();
self::updateIfCellIsTableHeader($cellCollection?->getParent(), $this, $oldValue, $value);
// Updating structured references may access other cells, which detaches this one
// from the collection, so retrieve it again to ensure that it is still attached.
$cell = $cellCollection?->get($cellCoordinate) ?? $this;
$worksheet = $cell->getWorksheet();
$spreadsheet = $worksheet->getParent();
if (isset($spreadsheet) && $spreadsheet->getIndex($worksheet, true) >= 0) {
// Avoid Worksheet::getStyle() (selection + validation) unless quotePrefix must change.
$oldQuotePrefix = $spreadsheet->getCellXfByIndex($this->getXfIndex())->getQuotePrefix();
$oldQuotePrefix = $spreadsheet->getCellXfByIndex($cell->getXfIndex())->getQuotePrefix();
if ($oldQuotePrefix !== $quotePrefix) {
$originalSelected = $worksheet->getSelectedCells();
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
$this->getStyle()->setQuotePrefix($quotePrefix);
$cell->getStyle()->setQuotePrefix($quotePrefix);
$worksheet->setSelectedCells($originalSelected);
if ($activeSheetIndex >= 0) {
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
}
}
}

return $this->getParent()?->get($cellCoordinate) ?? $this;
return $cellCollection?->get($cellCoordinate) ?? $cell;
}

public const CALCULATE_DATE_TIME_ASIS = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;

use PhpOffice\PhpSpreadsheet\Worksheet\Table;

class ColumnInsertRemoveTest extends SetupTeardown
{
private function createTable(): Table
{
$sheet = $this->getSheet();
$sheet->fromArray([
['Name', 'Qty', 'Price'],
['a', 1, 2],
['b', 3, 4],
['c', 5, 6],
]);
$sheet->setCellValue('F1', '=SUM(Sales[Price])');
$table = new Table('A1:C4', 'Sales');
$sheet->addTable($table);

return $table;
}

public function testRemoveColumnBeforeTable(): void
{
$sheet = $this->getSheet();
$sheet->setCellValue('A10', 'x');
$sheet->fromArray([
['Name', 'Qty', 'Price'],
['a', 1, 2],
], null, 'B1');
$table = new Table('B1:D2', 'Sales');
$sheet->addTable($table);

$sheet->removeColumn('A');

self::assertSame('A1:C2', $table->getRange());
self::assertSame(['Name', 'Qty', 'Price'], $sheet->rangeToArray('A1:C1')[0]);
}

public function testRemoveFirstColumnOfTable(): void
{
$sheet = $this->getSheet();
$table = $this->createTable();

$sheet->removeColumn('A');

self::assertSame('A1:B4', $table->getRange());
self::assertSame(['Qty', 'Price'], $sheet->rangeToArray('A1:B1')[0]);
self::assertSame('=SUM(Sales[Price])', $sheet->getCell('E1')->getValue());
}

public function testInsertColumnInTable(): void
{
$sheet = $this->getSheet();
$table = $this->createTable();

$sheet->insertNewColumnBefore('B');

self::assertSame('A1:D4', $table->getRange());
self::assertSame('Qty', $sheet->getCell('C1')->getValue());
self::assertSame('Price', $sheet->getCell('D1')->getValue());
self::assertSame('=SUM(Sales[Price])', $sheet->getCell('G1')->getValue());
}
}
Loading