Skip to content

Commit 4d81e53

Browse files
authored
Merge branch 'master' into customfunc
2 parents c0a966b + 3ef8ec4 commit 4d81e53

File tree

19 files changed

+176
-135
lines changed

19 files changed

+176
-135
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1010
### Added
1111

1212
- Add ability to add custom functions to Calculation. [PR #4390](https://github.com/PHPOffice/PhpSpreadsheet/pull/4390)
13+
- Add FormulaRange to IgnoredErrors. [PR #4393](https://github.com/PHPOffice/PhpSpreadsheet/pull/4393)
1314

1415
### Removed
1516

1617
- Nothing yet.
1718

1819
### Changed
1920

20-
- Nothing yet.
21+
- Phpstan Version 2. [PR #4384](https://github.com/PHPOffice/PhpSpreadsheet/pull/4384)
2122

2223
### Moved
2324

@@ -29,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2930

3031
### Fixed
3132

32-
- Nothing yet.
33+
- 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)
3334

3435
## 2025-03-02 - 4.1.0
3536

docs/topics/reading-files.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ for a working example of this code.
851851
### listWorksheetInfo
852852

853853
The `listWorksheetInfo()` method returns a nested array, with each entry
854-
listing the name and dimensions for a worksheet:
854+
listing the name, dimensions and state for a worksheet:
855855

856856
```php
857857
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
@@ -865,7 +865,8 @@ foreach ($worksheetData as $worksheet) {
865865
echo 'Rows: ', $worksheet['totalRows'],
866866
' Columns: ', $worksheet['totalColumns'], '<br />';
867867
echo 'Cell Range: A1:',
868-
$worksheet['lastColumnLetter'], $worksheet['totalRows'];
868+
$worksheet['lastColumnLetter'], $worksheet['totalRows'], '<br />';
869+
echo 'Sheet state: ', $worksheet['sheetState'];
869870
echo '</li>';
870871
}
871872
echo '</ol>';

src/PhpSpreadsheet/Calculation/Engineering/ConvertBinary.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ConvertBinary extends ConvertBase
2323
* 10 characters (10 bits), BIN2DEC returns the #NUM! error value.
2424
* Or can be an array of values
2525
*
26-
* @return array|string Result, or an error
26+
* @return array|float|int|string Result, or an error
2727
* If an array of numbers is passed as an argument, then the returned result will also be an array
2828
* with the same dimensions
2929
*/
@@ -44,10 +44,10 @@ public static function toDecimal($value)
4444
// Two's Complement
4545
$value = substr($value, -9);
4646

47-
return '-' . (512 - bindec($value));
47+
return -(512 - bindec($value));
4848
}
4949

50-
return (string) bindec($value);
50+
return bindec($value);
5151
}
5252

5353
/**

src/PhpSpreadsheet/Calculation/Engineering/ConvertHex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function toBinary($value, $places = null): array|string
7474
* #NUM! error value.
7575
* Or can be an array of values
7676
*
77-
* @return array|string Result, or an error
77+
* @return array|float|int|string Result, or an error
7878
* If an array of numbers is passed as an argument, then the returned result will also be an array
7979
* with the same dimensions
8080
*/
@@ -104,10 +104,10 @@ public static function toDecimal($value)
104104
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
105105
}
106106

107-
return (string) ((bindec($binX) + 1) * -1);
107+
return (bindec($binX) + 1) * -1;
108108
}
109109

110-
return (string) bindec($binX);
110+
return bindec($binX);
111111
}
112112

113113
/**

src/PhpSpreadsheet/Calculation/Engineering/ConvertOctal.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static function toBinary($value, $places = null): array|string
7878
* #NUM! error value.
7979
* Or can be an array of values
8080
*
81-
* @return array|string Result, or an error
81+
* @return array|float|int|string Result, or an error
8282
* If an array of numbers is passed as an argument, then the returned result will also be an array
8383
* with the same dimensions
8484
*/
@@ -104,10 +104,10 @@ public static function toDecimal($value)
104104
$binX[$i] = ($binX[$i] == '1' ? '0' : '1');
105105
}
106106

107-
return (string) ((bindec($binX) + 1) * -1);
107+
return (bindec($binX) + 1) * -1;
108108
}
109109

110-
return (string) bindec($binX);
110+
return bindec($binX);
111111
}
112112

113113
/**

src/PhpSpreadsheet/Cell/IgnoredErrors.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class IgnoredErrors
88

99
private bool $formula = false;
1010

11+
private bool $formulaRange = false;
12+
1113
private bool $twoDigitTextYear = false;
1214

1315
private bool $evalError = false;
@@ -36,6 +38,18 @@ public function getFormula(): bool
3638
return $this->formula;
3739
}
3840

41+
public function setFormulaRange(bool $value): self
42+
{
43+
$this->formulaRange = $value;
44+
45+
return $this;
46+
}
47+
48+
public function getFormulaRange(): bool
49+
{
50+
return $this->formulaRange;
51+
}
52+
3953
public function setTwoDigitTextYear(bool $value): self
4054
{
4155
$this->twoDigitTextYear = $value;

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,6 +2374,7 @@ private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet):
23742374
$sqref = (string) ($attributes['sqref'] ?? '');
23752375
$numberStoredAsText = (string) ($attributes['numberStoredAsText'] ?? '');
23762376
$formula = (string) ($attributes['formula'] ?? '');
2377+
$formulaRange = (string) ($attributes['formulaRange'] ?? '');
23772378
$twoDigitTextYear = (string) ($attributes['twoDigitTextYear'] ?? '');
23782379
$evalError = (string) ($attributes['evalError'] ?? '');
23792380
if (!empty($sqref)) {
@@ -2403,6 +2404,9 @@ private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet):
24032404
if ($formula === '1') {
24042405
$sheet->getCell("$col$row")->getIgnoredErrors()->setFormula(true);
24052406
}
2407+
if ($formulaRange === '1') {
2408+
$sheet->getCell("$col$row")->getIgnoredErrors()->setFormulaRange(true);
2409+
}
24062410
if ($twoDigitTextYear === '1') {
24072411
$sheet->getCell("$col$row")->getIgnoredErrors()->setTwoDigitTextYear(true);
24082412
}

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Worksheet extends WriterPart
2727

2828
private string $formula = '';
2929

30+
private string $formulaRange = '';
31+
3032
private string $twoDigitTextYear = '';
3133

3234
private string $evalError = '';
@@ -50,6 +52,7 @@ public function writeWorksheet(PhpspreadsheetWorksheet $worksheet, array $string
5052
$worksheet->calculateArrays($this->getParentWriter()->getPreCalculateFormulas());
5153
$this->numberStoredAsText = '';
5254
$this->formula = '';
55+
$this->formulaRange = '';
5356
$this->twoDigitTextYear = '';
5457
$this->evalError = '';
5558
// Create XML writer
@@ -180,6 +183,7 @@ private function writeIgnoredErrors(XMLWriter $objWriter): void
180183
$started = false;
181184
$this->writeIgnoredError($objWriter, $started, 'numberStoredAsText', $this->numberStoredAsText);
182185
$this->writeIgnoredError($objWriter, $started, 'formula', $this->formula);
186+
$this->writeIgnoredError($objWriter, $started, 'formulaRange', $this->formulaRange);
183187
$this->writeIgnoredError($objWriter, $started, 'twoDigitTextYear', $this->twoDigitTextYear);
184188
$this->writeIgnoredError($objWriter, $started, 'evalError', $this->evalError);
185189
if ($started) {
@@ -1392,6 +1396,9 @@ private function writeSheetData(XMLWriter $objWriter, PhpspreadsheetWorksheet $w
13921396
if ($worksheet->getCell($coord)->getIgnoredErrors()->getFormula()) {
13931397
$this->formula .= " $coord";
13941398
}
1399+
if ($worksheet->getCell($coord)->getIgnoredErrors()->getFormulaRange()) {
1400+
$this->formulaRange .= " $coord";
1401+
}
13951402
if ($worksheet->getCell($coord)->getIgnoredErrors()->getTwoDigitTextYear()) {
13961403
$this->twoDigitTextYear .= " $coord";
13971404
}

tests/PhpSpreadsheetTests/Calculation/Functions/Engineering/Bin2DecTest.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
1212
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1313
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\FormulaArguments;
14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\TestCase;
1516

1617
class Bin2DecTest extends TestCase
@@ -27,19 +28,14 @@ protected function tearDown(): void
2728
Functions::setCompatibilityMode($this->compatibilityMode);
2829
}
2930

30-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
31-
public function testDirectCallToBIN2DEC(string $expectedResult, bool|int|string $arg1): void
31+
#[DataProvider('providerBIN2DEC')]
32+
public function testDirectCallToBIN2DEC(float|int|string $expectedResult, bool|int|string $arg1): void
3233
{
3334
$result = ConvertBinary::toDecimal($arg1);
3435
self::assertSame($expectedResult, $result);
3536
}
3637

37-
private function trimIfQuoted(string $value): string
38-
{
39-
return trim($value, '"');
40-
}
41-
42-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
38+
#[DataProvider('providerBIN2DEC')]
4339
public function testBIN2DECAsFormula(mixed $expectedResult, mixed ...$args): void
4440
{
4541
$arguments = new FormulaArguments(...$args);
@@ -48,11 +44,11 @@ public function testBIN2DECAsFormula(mixed $expectedResult, mixed ...$args): voi
4844
$formula = "=BIN2DEC({$arguments})";
4945

5046
/** @var float|int|string */
51-
$result = $calculation->_calculateFormulaValue($formula);
52-
self::assertSame($expectedResult, $this->trimIfQuoted((string) $result));
47+
$result = $calculation->calculateFormula($formula);
48+
self::assertSame($expectedResult, $result);
5349
}
5450

55-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DEC')]
51+
#[DataProvider('providerBIN2DEC')]
5652
public function testBIN2DECInWorksheet(mixed $expectedResult, mixed ...$args): void
5753
{
5854
$arguments = new FormulaArguments(...$args);
@@ -75,7 +71,7 @@ public static function providerBIN2DEC(): array
7571
return require 'tests/data/Calculation/Engineering/BIN2DEC.php';
7672
}
7773

78-
#[\PHPUnit\Framework\Attributes\DataProvider('providerUnhappyBIN2DEC')]
74+
#[DataProvider('providerUnhappyBIN2DEC')]
7975
public function testBIN2DECUnhappyPath(string $expectedException, mixed ...$args): void
8076
{
8177
$arguments = new FormulaArguments(...$args);
@@ -101,10 +97,12 @@ public static function providerUnhappyBIN2DEC(): array
10197
];
10298
}
10399

104-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBIN2DECOds')]
105-
public function testBIN2DECOds(string $expectedResult, bool $arg1): void
100+
#[DataProvider('providerBIN2DECOds')]
101+
public function testBIN2DECOds(float|int|string $expectedResult, bool $arg1): void
106102
{
107-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
103+
Functions::setCompatibilityMode(
104+
Functions::COMPATIBILITY_OPENOFFICE
105+
);
108106

109107
$result = ConvertBinary::toDecimal($arg1);
110108
self::assertSame($expectedResult, $result);
@@ -120,29 +118,35 @@ public function testBIN2DECFractional(): void
120118
$calculation = Calculation::getInstance();
121119
$formula = '=BIN2DEC(101.1)';
122120

123-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_GNUMERIC);
121+
Functions::setCompatibilityMode(
122+
Functions::COMPATIBILITY_GNUMERIC
123+
);
124124
/** @var float|int|string */
125-
$result = $calculation->_calculateFormulaValue($formula);
126-
self::assertSame('5', $this->trimIfQuoted((string) $result), 'Gnumeric');
125+
$result = $calculation->calculateFormula($formula);
126+
self::assertSame(5, $result, 'Gnumeric');
127127

128-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE);
128+
Functions::setCompatibilityMode(
129+
Functions::COMPATIBILITY_OPENOFFICE
130+
);
129131
/** @var float|int|string */
130-
$result = $calculation->_calculateFormulaValue($formula);
131-
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'OpenOffice');
132+
$result = $calculation->calculateFormula($formula);
133+
self::assertSame(ExcelError::NAN(), $result, 'OpenOffice');
132134

133-
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
135+
Functions::setCompatibilityMode(
136+
Functions::COMPATIBILITY_EXCEL
137+
);
134138
/** @var float|int|string */
135-
$result = $calculation->_calculateFormulaValue($formula);
136-
self::assertSame(ExcelError::NAN(), $this->trimIfQuoted((string) $result), 'Excel');
139+
$result = $calculation->calculateFormula($formula);
140+
self::assertSame(ExcelError::NAN(), $result, 'Excel');
137141
}
138142

139-
#[\PHPUnit\Framework\Attributes\DataProvider('providerBin2DecArray')]
143+
#[DataProvider('providerBin2DecArray')]
140144
public function testBin2DecArray(array $expectedResult, string $value): void
141145
{
142146
$calculation = Calculation::getInstance();
143147

144148
$formula = "=BIN2DEC({$value})";
145-
$result = $calculation->_calculateFormulaValue($formula);
149+
$result = $calculation->calculateFormula($formula);
146150
self::assertEquals($expectedResult, $result);
147151
}
148152

0 commit comments

Comments
 (0)