Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change hash code for worksheet branch release222 #4308

Merged
merged 2 commits into from
Jan 8, 2025
Merged
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com)
and this project adheres to [Semantic Versioning](https://semver.org).

# TBD - 2.3.6

### Deprecated

- Worksheet::getHashCode is no longer needed.

### Fixed

- Change hash code for worksheet. Backport of [PR #4207](https://github.com/PHPOffice/PhpSpreadsheet/pull/4207)
- Retitling cloned worksheets. Backport of [PR #4302](https://github.com/PHPOffice/PhpSpreadsheet/pull/4302)


# 2024-12-26 - 2.3.5

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function setValueExplicit(mixed $value, string $dataType = DataType::TYPE
self::updateIfCellIsTableHeader($this->getParent()?->getParent(), $this, $oldValue, $value);
$worksheet = $this->getWorksheet();
$spreadsheet = $worksheet->getParent();
if (isset($spreadsheet)) {
if (isset($spreadsheet) && $spreadsheet->getIndex($worksheet, true) >= 0) {
$originalSelected = $worksheet->getSelectedCells();
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
$style = $this->getStyle();
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ private function updateNamedRange(DefinedName $definedName, Worksheet $worksheet
{
$cellAddress = $definedName->getValue();
$asFormula = ($cellAddress[0] === '=');
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashCode() === $worksheet->getHashCode()) {
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashInt() === $worksheet->getHashInt()) {
/**
* If we delete the entire range that is referenced by a Named Range, MS Excel sets the value to #REF!
* PhpSpreadsheet still only does a basic adjustment, so the Named Range will still reference Cells.
Expand All @@ -940,7 +940,7 @@ private function updateNamedRange(DefinedName $definedName, Worksheet $worksheet

private function updateNamedFormula(DefinedName $definedName, Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void
{
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashCode() === $worksheet->getHashCode()) {
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashInt() === $worksheet->getHashInt()) {
/**
* If we delete the entire range that is referenced by a Named Formula, MS Excel sets the value to #REF!
* PhpSpreadsheet still only does a basic adjustment, so the Named Formula will still reference Cells.
Expand Down
24 changes: 20 additions & 4 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public function getActiveSheet(): Worksheet
public function createSheet(?int $sheetIndex = null): Worksheet
{
$newSheet = new Worksheet($this);
$this->addSheet($newSheet, $sheetIndex);
$this->addSheet($newSheet, $sheetIndex, true);

return $newSheet;
}
Expand All @@ -532,8 +532,20 @@ public function sheetNameExists(string $worksheetName): bool
* @param Worksheet $worksheet The worksheet to add
* @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last)
*/
public function addSheet(Worksheet $worksheet, ?int $sheetIndex = null): Worksheet
public function addSheet(Worksheet $worksheet, ?int $sheetIndex = null, bool $retitleIfNeeded = false): Worksheet
{
if ($retitleIfNeeded) {
$title = $worksheet->getTitle();
if ($this->sheetNameExists($title)) {
$i = 1;
$newTitle = "$title $i";
while ($this->sheetNameExists($newTitle)) {
++$i;
$newTitle = "$title $i";
}
$worksheet->setTitle($newTitle);
}
}
if ($this->sheetNameExists($worksheet->getTitle())) {
throw new Exception(
"Workbook already contains a worksheet named '{$worksheet->getTitle()}'. Rename this worksheet first."
Expand Down Expand Up @@ -657,13 +669,17 @@ public function getSheetByNameOrThrow(string $worksheetName): Worksheet
*
* @return int index
*/
public function getIndex(Worksheet $worksheet): int
public function getIndex(Worksheet $worksheet, bool $noThrow = false): int
{
$wsHash = $worksheet->getHashInt();
foreach ($this->workSheetCollection as $key => $value) {
if ($value->getHashCode() === $worksheet->getHashCode()) {
if ($value->getHashInt() === $wsHash) {
return $key;
}
}
if ($noThrow) {
return -1;
}

throw new Exception('Sheet does not exist.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/BaseDrawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function getHashCode(): string
return md5(
$this->name
. $this->description
. (($this->worksheet === null) ? '' : $this->worksheet->getHashCode())
. (($this->worksheet === null) ? '' : (string) $this->worksheet->getHashInt())
. $this->coordinates
. $this->offsetX
. $this->offsetY
Expand Down
38 changes: 18 additions & 20 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use PhpOffice\PhpSpreadsheet\Comment;
use PhpOffice\PhpSpreadsheet\DefinedName;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\IComparable;
use PhpOffice\PhpSpreadsheet\ReferenceHelper;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Shared;
Expand All @@ -32,7 +31,7 @@
use PhpOffice\PhpSpreadsheet\Style\Protection as StyleProtection;
use PhpOffice\PhpSpreadsheet\Style\Style;

class Worksheet implements IComparable
class Worksheet
{
// Break types
public const BREAK_NONE = 0;
Expand Down Expand Up @@ -305,15 +304,10 @@ class Worksheet implements IComparable
*/
private ?Color $tabColor = null;

/**
* Dirty flag.
*/
private bool $dirty = true;

/**
* Hash.
*/
private string $hash;
private int $hash;

/**
* CodeName.
Expand All @@ -327,6 +321,7 @@ public function __construct(?Spreadsheet $parent = null, string $title = 'Worksh
{
// Set parent and title
$this->parent = $parent;
$this->hash = spl_object_id($this);
$this->setTitle($title, false);
// setTitle can change $pTitle
$this->setCodeName($this->getTitle());
Expand Down Expand Up @@ -383,6 +378,12 @@ public function __destruct()
unset($this->rowDimensions, $this->columnDimensions, $this->tableCollection, $this->drawingCollection, $this->chartCollection, $this->autoFilter);
}

public function __wakeup(): void
{
$this->hash = spl_object_id($this);
$this->parent = null;
}

/**
* Return the cell collection.
*/
Expand Down Expand Up @@ -862,7 +863,7 @@ public function setTitle(string $title, bool $updateFormulaCellReferences = true
// Syntax check
self::checkSheetTitle($title);

if ($this->parent) {
if ($this->parent && $this->parent->getIndex($this, true) >= 0) {
// Is there already such sheet name?
if ($this->parent->sheetNameExists($title)) {
// Use name, but append with lowest possible integer
Expand Down Expand Up @@ -891,9 +892,8 @@ public function setTitle(string $title, bool $updateFormulaCellReferences = true

// Set title
$this->title = $title;
$this->dirty = true;

if ($this->parent && $this->parent->getCalculationEngine()) {
if ($this->parent && $this->parent->getIndex($this, true) >= 0 && $this->parent->getCalculationEngine()) {
// New title
$newTitle = $this->getTitle();
$this->parent->getCalculationEngine()
Expand Down Expand Up @@ -1026,7 +1026,6 @@ public function getProtection(): Protection
public function setProtection(Protection $protection): static
{
$this->protection = $protection;
$this->dirty = true;

return $this;
}
Expand Down Expand Up @@ -2994,7 +2993,7 @@ private function validateNamedRange(string $definedName, bool $returnNullIfInval

if ($namedRange->getLocalOnly()) {
$worksheet = $namedRange->getWorksheet();
if ($worksheet === null || $this->getHashCode() !== $worksheet->getHashCode()) {
if ($worksheet === null || $this->hash !== $worksheet->getHashInt()) {
if ($returnNullIfInvalid) {
return null;
}
Expand Down Expand Up @@ -3131,17 +3130,15 @@ public function garbageCollect(): static
}

/**
* Get hash code.
*
* @return string Hash code
* @deprecated 3.5.0 use getHashInt instead.
*/
public function getHashCode(): string
{
if ($this->dirty) {
$this->hash = md5($this->title . $this->autoFilter . ($this->protection->isProtectionEnabled() ? 't' : 'f') . __CLASS__);
$this->dirty = false;
}
return (string) $this->hash;
}

public function getHashInt(): int
{
return $this->hash;
}

Expand Down Expand Up @@ -3470,6 +3467,7 @@ public function __clone()
}
}
}
$this->hash = spl_object_id($this);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function writeStyles(Spreadsheet $spreadsheet): string

// dxf
for ($i = 0; $i < $this->getParentWriter()->getStylesConditionalHashTable()->count(); ++$i) {
/** @var ?Conditional */
$thisstyle = $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i);
if ($thisstyle !== null) {
$this->writeCellStyleDxf($objWriter, $thisstyle->getStyle());
Expand Down
4 changes: 3 additions & 1 deletion tests/PhpSpreadsheetTests/Shared/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -206,7 +207,7 @@ public function testVarious(): void
$date = Date::PHPToExcel('2020-01-01');
self::assertEquals(43831.0, $date);

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B1', 'x');
/** @var float|int|string */
Expand Down Expand Up @@ -248,5 +249,6 @@ public function testVarious(): void
->getNumberFormat()
->setFormatCode('yyyy-mm-dd');
self::assertFalse(Date::isDateTime($cella4));
$spreadsheet->disconnectWorksheets();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ public function testInvalidChar(): void
$sheet->getCell("A$row")->getValue()
);
}
$spreadsheet->disconnectWorksheets();
}
}
2 changes: 2 additions & 0 deletions tests/PhpSpreadsheetTests/Style/BorderRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function testBorderRangeInAction(): void
}
}
}
$spreadsheet->disconnectWorksheets();
}

public function testBorderRangeDirectly(): void
Expand All @@ -71,5 +72,6 @@ public function testBorderRangeDirectly(): void
$sheet = $spreadsheet->getActiveSheet();
$style = $sheet->getStyle('A1:C1')->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
self::assertSame('A1:C1', $style->getSelectedCells(), 'getSelectedCells should not change after a style operation on a border range');
$spreadsheet->disconnectWorksheets();
}
}
8 changes: 8 additions & 0 deletions tests/PhpSpreadsheetTests/Style/BorderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function testAllBorders(): void
self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle());
$spreadsheet->disconnectWorksheets();
}

public function testAllBordersArray(): void
Expand All @@ -45,6 +46,7 @@ public function testAllBordersArray(): void
self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle());
$spreadsheet->disconnectWorksheets();
}

public function testAllBordersArrayNotSupervisor(): void
Expand Down Expand Up @@ -86,6 +88,7 @@ public function testOutline(): void
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
$spreadsheet->disconnectWorksheets();
}

public function testInside(): void
Expand Down Expand Up @@ -115,6 +118,7 @@ public function testInside(): void
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
$spreadsheet->disconnectWorksheets();
}

public function testHorizontal(): void
Expand Down Expand Up @@ -144,6 +148,7 @@ public function testHorizontal(): void
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
$spreadsheet->disconnectWorksheets();
}

public function testVertical(): void
Expand Down Expand Up @@ -173,6 +178,7 @@ public function testVertical(): void
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle());
self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle());
self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle());
$spreadsheet->disconnectWorksheets();
}

public function testNoSupervisorAllBorders(): void
Expand Down Expand Up @@ -233,6 +239,7 @@ public function testBorderStyle(): void
$border->setBorderStyle(Border::BORDER_THIN)->setColor(new Color('FFFF0000'));
self::assertEquals('FFFF0000', $border->getColor()->getARGB());
self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle());
$spreadsheet->disconnectWorksheets();
}

public function testDiagonalDirection(): void
Expand All @@ -245,5 +252,6 @@ public function testDiagonalDirection(): void

self::assertSame(Border::BORDER_MEDIUM, $borders->getDiagonal()->getBorderStyle());
self::assertSame(Borders::DIAGONAL_BOTH, $borders->getDiagonalDirection());
$spreadsheet->disconnectWorksheets();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function testWizardFromConditional(string $sheetName, string $cellAddress
$wizard = Wizard::fromConditional($conditional);
self::assertEquals($expectedWizads[$index], $wizard::class);
}
$spreadsheet->disconnectWorksheets();
}

public static function conditionalProvider(): array
Expand Down
Loading
Loading