diff --git a/CHANGELOG.md b/CHANGELOG.md index 17b5c49bd1..a740cd0d21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed -- Nothing yet. +- Xlsx Reader Shared Formula with Boolean Result. Partial solution for [Issue #4280](https://github.com/PHPOffice/PhpSpreadsheet/issues/4280) [PR #4281](https://github.com/PHPOffice/PhpSpreadsheet/pull/4281) ## 2024-12-26 - 3.7.0 diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index d22f76ff4f..e7cd0b7119 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -854,6 +854,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet } // Read cell! + $useFormula = isset($c->f) + && ((string) $c->f !== '' || (isset($c->f->attributes()['t']) && strtolower((string) $c->f->attributes()['t']) === 'shared')); switch ($cellDataType) { case DataType::TYPE_STRING: if ((string) $c->v != '') { @@ -868,7 +870,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet break; case DataType::TYPE_BOOL: - if (!isset($c->f) || ((string) $c->f) === '') { + if (!$useFormula) { if (isset($c->v)) { $value = self::castToBoolean($c); } else { @@ -883,16 +885,16 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet break; case DataType::TYPE_STRING2: - if (isset($c->f)) { + if ($useFormula) { $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToString'); self::storeFormulaAttributes($c->f, $docSheet, $r); } else { - $value = self::castToString($c); + $value = self::castToString($c); } break; case DataType::TYPE_INLINE: - if (isset($c->f)) { + if ($useFormula) { $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToError'); self::storeFormulaAttributes($c->f, $docSheet, $r); } else { @@ -901,7 +903,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet break; case DataType::TYPE_ERROR: - if (!isset($c->f)) { + if (!$useFormula) { $value = self::castToError($c); } else { // Formula @@ -916,7 +918,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet break; default: - if (!isset($c->f)) { + if (!$useFormula) { $value = self::castToString($c); if (is_numeric($value)) { $value += 0; diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/SharedFormulaeTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/SharedFormulaeTest.php new file mode 100644 index 0000000000..b613fb2973 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/SharedFormulaeTest.php @@ -0,0 +1,37 @@ +load($filename); + $sheet = $spreadsheet->getActiveSheet(); + $expected = [ + [1, '=A1+1', '=A1>3', '="x"&A1'], + [2, '=A2+1', '=A2>3', '="x"&A2'], + [3, '=A3+1', '=A3>3', '="x"&A3'], + [4, '=A4+1', '=A4>3', '="x"&A4'], + [5, '=A5+1', '=A5>3', '="x"&A5'], + ]; + self::assertSame($expected, $sheet->toArray(null, false, false)); + $expected = [ + [1, 2, false, 'x1'], + [2, 3, false, 'x2'], + [3, 4, false, 'x3'], + [4, 5, true, 'x4'], + [5, 6, true, 'x5'], + ]; + self::assertSame($expected, $sheet->toArray(null, true, false)); + $spreadsheet->disconnectWorksheets(); + } +} diff --git a/tests/data/Reader/XLSX/sharedformulae.xlsx b/tests/data/Reader/XLSX/sharedformulae.xlsx new file mode 100644 index 0000000000..eab8198b34 Binary files /dev/null and b/tests/data/Reader/XLSX/sharedformulae.xlsx differ