Fix for Issue 1495 (#1500)

#1495 reports that ActiveSheet can change when calculation
involves jumping around between sheets.
Save index before calculation, restore after, add test.
This commit is contained in:
oleibman 2020-06-19 11:57:20 -07:00 committed by GitHub
parent 1a44ef9109
commit 3844186397
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -251,9 +251,11 @@ class Cell
{
if ($this->dataType == DataType::TYPE_FORMULA) {
try {
$index = $this->getWorksheet()->getParent()->getActiveSheetIndex();
$result = Calculation::getInstance(
$this->getWorksheet()->getParent()
)->calculateCellValue($this, $resetLog);
$this->getWorksheet()->getParent()->setActiveSheetIndex($index);
// We don't yet handle array returns
if (is_array($result)) {
while (is_array($result)) {

View File

@ -46,4 +46,23 @@ class CellTest extends TestCase
{
return require 'tests/data/Cell/SetValueExplicitException.php';
}
public function testNoChangeToActiveSheet(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->setTitle('Sheet 1');
$sheet3 = $spreadsheet->createSheet();
$sheet3->setTitle('Sheet 3');
$sheet1->setCellValue('C1', 123);
$sheet1->setCellValue('D1', 124);
$sheet3->setCellValue('A1', "='Sheet 1'!C1+'Sheet 1'!D1");
$sheet1->setCellValue('A1', "='Sheet 3'!A1");
$cell = 'A1';
$spreadsheet->setActiveSheetIndex(0);
self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
$value = $spreadsheet->getActiveSheet()->getCell($cell)->getCalculatedValue();
self::assertEquals(0, $spreadsheet->getActiveSheetIndex());
self::assertEquals(247, $value);
}
}