Skip to content

Xlsx Reader Shared Formula with Boolean Results #4281

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

Merged
merged 3 commits into from
Dec 31, 2024
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 != '') {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/SharedFormulaeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PHPUnit\Framework\TestCase;

class SharedFormulaeTest extends TestCase
{
public function testSharedFormulae(): void
{
// Boolean functions were not handled correctly.
$filename = 'tests/data/Reader/XLSX/sharedformulae.xlsx';
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->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();
}
}
Binary file added tests/data/Reader/XLSX/sharedformulae.xlsx
Binary file not shown.
Loading