Skip to content

Commit 9875233

Browse files
committed
Tweak to Spreadsheet Clone
Spreadsheet clone already copies Calculation instanceArrayReturnType property. It should also copy some other Calculation instance properties, namely suppressFormulaErrors, calculationCacheEnabled, and branchPruningEnabled.
1 parent 74dca30 commit 9875233

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 19 additions & 6 deletions
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 = $enabled;
375379
$this->branchPruner = new BranchPruner($this->branchPruningEnabled);
380+
381+
return $this;
376382
}
377383

378384
public function enableBranchPruning(): void
@@ -2687,9 +2693,11 @@ private function evaluateDefinedName(Cell $cell, DefinedName $namedRange, Worksh
26872693
return $result;
26882694
}
26892695

2690-
public function setSuppressFormulaErrors(bool $suppressFormulaErrors): void
2696+
public function setSuppressFormulaErrors(bool $suppressFormulaErrors): self
26912697
{
26922698
$this->suppressFormulaErrors = $suppressFormulaErrors;
2699+
2700+
return $this;
26932701
}
26942702

26952703
public function getSuppressFormulaErrors(): bool
@@ -2732,4 +2740,9 @@ private static function swapOperands(Stack $stack, string $opCharacter): bool
27322740

27332741
return $retVal;
27342742
}
2743+
2744+
public function getSpreadsheet(): ?Spreadsheet
2745+
{
2746+
return $this->spreadsheet;
2747+
}
27352748
}

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 9 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
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,37 @@ 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+
$calc->setSuppressFormulaErrors($suppress);
136+
$calc->setCalculationCacheEnabled($cache);
137+
$calc->setBranchPruningEnabled($pruning);
138+
$calc->setInstanceArrayReturnType($return);
139+
if ($type === 'copy') {
140+
$this->spreadsheet2 = $this->spreadsheet->copy();
141+
} else {
142+
$this->spreadsheet2 = clone $this->spreadsheet;
143+
}
144+
$calc2 = $this->spreadsheet2->getCalculationEngine();
145+
self::assertSame($suppress, $calc2->getSuppressFormulaErrors());
146+
self::assertSame($cache, $calc2->getCalculationCacheEnabled());
147+
self::assertSame($pruning, $calc2->getBranchPruningEnabled());
148+
self::assertSame($return, $calc2->getInstanceArrayReturnType());
149+
}
115150
}

0 commit comments

Comments
 (0)