Skip to content

Commit 902d4bd

Browse files
authored
Merge pull request #4419 from oleibman/clonetweak
Tweak to Spreadsheet Clone
2 parents c54d55d + 31656b0 commit 902d4bd

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3737
- Ignore fractional part of Drawing Shadow Alpha. [Issue #4415](https://github.com/PHPOffice/PhpSpreadsheet/issues/4415) [PR #4417](https://github.com/PHPOffice/PhpSpreadsheet/pull/4417)
3838
- BIN2DEC, OCT2DEC, and HEX2DEC return numbers rather than strings. [Issue #4383](https://github.com/PHPOffice/PhpSpreadsheet/issues/4383) [PR #4389](https://github.com/PHPOffice/PhpSpreadsheet/pull/4389)
3939
- Fix TREND_BEST_FIT_NO_POLY. [Issue #4400](https://github.com/PHPOffice/PhpSpreadsheet/issues/4400) [PR #4339](https://github.com/PHPOffice/PhpSpreadsheet/pull/4339)
40+
- Tweak Spreadsheet clone. [PR #4419](https://github.com/PHPOffice/PhpSpreadsheet/pull/4419)
4041
- Better handling of Chart DisplayBlanksAs. [Issue #4411](https://github.com/PHPOffice/PhpSpreadsheet/issues/4411) [PR #4414](https://github.com/PHPOffice/PhpSpreadsheet/pull/4414)
4142

4243
## 2025-03-02 - 4.1.0

src/PhpSpreadsheet/Calculation/Calculation.php

+19-6
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,12 @@ public function getCalculationCacheEnabled(): bool
315315
/**
316316
* Enable/disable calculation cache.
317317
*/
318-
public function setCalculationCacheEnabled(bool $calculationCacheEnabled): void
318+
public function setCalculationCacheEnabled(bool $calculationCacheEnabled): self
319319
{
320320
$this->calculationCacheEnabled = $calculationCacheEnabled;
321321
$this->clearCalculationCache();
322+
323+
return $this;
322324
}
323325

324326
/**
@@ -366,13 +368,17 @@ public function renameCalculationCacheForWorksheet(string $fromWorksheetName, st
366368
}
367369
}
368370

369-
/**
370-
* Enable/disable calculation cache.
371-
*/
372-
public function setBranchPruningEnabled(mixed $enabled): void
371+
public function getBranchPruningEnabled(): bool
372+
{
373+
return $this->branchPruningEnabled;
374+
}
375+
376+
public function setBranchPruningEnabled(mixed $enabled): self
373377
{
374378
$this->branchPruningEnabled = (bool) $enabled;
375379
$this->branchPruner = new BranchPruner($this->branchPruningEnabled);
380+
381+
return $this;
376382
}
377383

378384
public function enableBranchPruning(): void
@@ -2712,9 +2718,11 @@ private function evaluateDefinedName(Cell $cell, DefinedName $namedRange, Worksh
27122718
return $result;
27132719
}
27142720

2715-
public function setSuppressFormulaErrors(bool $suppressFormulaErrors): void
2721+
public function setSuppressFormulaErrors(bool $suppressFormulaErrors): self
27162722
{
27172723
$this->suppressFormulaErrors = $suppressFormulaErrors;
2724+
2725+
return $this;
27182726
}
27192727

27202728
public function getSuppressFormulaErrors(): bool
@@ -2757,4 +2765,9 @@ private static function swapOperands(Stack $stack, string $opCharacter): bool
27572765

27582766
return $retVal;
27592767
}
2768+
2769+
public function getSpreadsheet(): ?Spreadsheet
2770+
{
2771+
return $this->spreadsheet;
2772+
}
27602773
}

src/PhpSpreadsheet/Spreadsheet.php

+9
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,15 @@ public function __clone()
10791079
$this->calculationEngine = new Calculation($this);
10801080
if ($oldCalc !== null) {
10811081
$this->calculationEngine
1082+
->setSuppressFormulaErrors(
1083+
$oldCalc->getSuppressFormulaErrors()
1084+
)
1085+
->setCalculationCacheEnabled(
1086+
$oldCalc->getCalculationCacheEnabled()
1087+
)
1088+
->setBranchPruningEnabled(
1089+
$oldCalc->getBranchPruningEnabled()
1090+
)
10821091
->setInstanceArrayReturnType(
10831092
$oldCalc->getInstanceArrayReturnType()
10841093
);

tests/PhpSpreadsheetTests/SpreadsheetCopyCloneTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function testCopyClone(string $type): void
4949
} else {
5050
$this->spreadsheet2 = clone $this->spreadsheet;
5151
}
52+
self::assertSame($this->spreadsheet, $this->spreadsheet->getCalculationEngine()?->getSpreadsheet());
53+
self::assertSame($this->spreadsheet2, $this->spreadsheet2->getCalculationEngine()?->getSpreadsheet());
5254
self::assertSame('A3', $sheet->getSelectedCells());
5355
$copysheet = $this->spreadsheet2->getActiveSheet();
5456
self::assertSame('A3', $copysheet->getSelectedCells());
@@ -112,4 +114,39 @@ public static function providerCopyClone(): array
112114
['clone'],
113115
];
114116
}
117+
118+
public static function providerCopyClone2(): array
119+
{
120+
return [
121+
['copy', true, false, true, 'array'],
122+
['clone', true, false, true, 'array'],
123+
['copy', false, true, false, 'value'],
124+
['clone', false, true, false, 'value'],
125+
['copy', false, true, true, 'error'],
126+
['clone', false, true, true, 'error'],
127+
];
128+
}
129+
130+
#[DataProvider('providerCopyClone2')]
131+
public function testCopyClone2(string $type, bool $suppress, bool $cache, bool $pruning, string $return): void
132+
{
133+
$this->spreadsheet = new Spreadsheet();
134+
$calc = $this->spreadsheet->getCalculationEngine();
135+
self::assertNotNull($calc);
136+
$calc->setSuppressFormulaErrors($suppress);
137+
$calc->setCalculationCacheEnabled($cache);
138+
$calc->setBranchPruningEnabled($pruning);
139+
$calc->setInstanceArrayReturnType($return);
140+
if ($type === 'copy') {
141+
$this->spreadsheet2 = $this->spreadsheet->copy();
142+
} else {
143+
$this->spreadsheet2 = clone $this->spreadsheet;
144+
}
145+
$calc2 = $this->spreadsheet2->getCalculationEngine();
146+
self::assertNotNull($calc2);
147+
self::assertSame($suppress, $calc2->getSuppressFormulaErrors());
148+
self::assertSame($cache, $calc2->getCalculationCacheEnabled());
149+
self::assertSame($pruning, $calc2->getBranchPruningEnabled());
150+
self::assertSame($return, $calc2->getInstanceArrayReturnType());
151+
}
115152
}

0 commit comments

Comments
 (0)