Skip to content
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

Fixed inconsistent string handling in SUM() implementations, issue #3652 #3653

Merged
merged 7 commits into from
Aug 15, 2023
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
9 changes: 3 additions & 6 deletions src/PhpSpreadsheet/Calculation/MathTrig/Sum.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Sum
*
* @param mixed ...$args Data values
*
* @return float|string
* @return float|int|string
*/
public static function sumIgnoringStrings(...$args)
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public static function sumIgnoringStrings(...$args)
*
* @param mixed ...$args Data values
*
* @return float|string
* @return float|int|string
*/
public static function sumErroringStrings(...$args)
{
Expand All @@ -56,10 +56,7 @@ public static function sumErroringStrings(...$args)
$aArgs = Functions::flattenArrayIndexed($args);
foreach ($aArgs as $k => $arg) {
// Is it a numeric value?
if (is_numeric($arg) || empty($arg)) {
if (is_string($arg)) {
$arg = (int) $arg;
}
if (is_numeric($arg)) {
$returnValue += $arg;
} elseif (is_bool($arg)) {
$returnValue += (int) $arg;
Expand Down
2 changes: 2 additions & 0 deletions tests/data/Calculation/MathTrig/SUM.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[52, 5, 15, 30, 2],
[53.1, 5.7, 15, 30, 2.4],
[52.1, 5.7, '14', 30, 2.4],
[52.2, 5.7, '14.1', 30, 2.4],
[38.1, 5.7, 'X', 30, 2.4], // error if entered in formula, but not in cell
[38.1, 5.7, '', 30, 2.4], // error if entered in formula, but not in cell
[38.1, 5.7, null, 30, 2.4],
[38.1, 5.7, false, 30, 2.4],
[39.1, 5.7, true, 30, 2.4],
Expand Down
2 changes: 2 additions & 0 deletions tests/data/Calculation/MathTrig/SUMLITERALS.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[52, '5, 15, 30, 2'],
[53.1, '5.7, 15, 30, 2.4'],
[52.1, '5.7, "14", 30, 2.4'],
[52.2, '5.7, "14.1", 30, 2.4'],
['#VALUE!', '5.7, "X", 30, 2.4'], // error if entered in formula, but not in cell
['#VALUE!', '5.7, "", 30, 2.4'], // error if entered in formula, but not in cell
[38.1, '5.7, , 30, 2.4'],
[38.1, '5.7, false, 30, 2.4'],
[39.1, '5.7, true, 30, 2.4'],
Expand Down