Skip to content

Support null and empty values in TEXTJOIN() #3197

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 1 commit into from
Nov 21, 2022
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
32 changes: 19 additions & 13 deletions src/PhpSpreadsheet/Calculation/TextData/Concatenate.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function CONCATENATE(...$args): string
* If an array of values is passed for the $delimiter or $ignoreEmpty arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args)
public static function TEXTJOIN($delimiter = '', $ignoreEmpty = true, ...$args)
{
if (is_array($delimiter) || is_array($ignoreEmpty)) {
return self::evaluateArrayArgumentsSubset(
Expand All @@ -68,29 +68,35 @@ public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args)
);
}

// Loop through arguments
$delimiter ??= '';
$ignoreEmpty ??= true;
$aArgs = Functions::flattenArray($args);
$returnValue = '';
$returnValue = self::evaluateTextJoinArray($ignoreEmpty, $aArgs);

$returnValue ??= implode($delimiter, $aArgs);
if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
$returnValue = ExcelError::CALC();
}

return $returnValue;
}

private static function evaluateTextJoinArray(bool $ignoreEmpty, array &$aArgs): ?string
{
foreach ($aArgs as $key => &$arg) {
$value = Helpers::extractString($arg);
if (ErrorValue::isError($value)) {
$returnValue = $value;

break;
return $value;
}
if ($ignoreEmpty === true && is_string($arg) && trim($arg) === '') {

if ($ignoreEmpty === true && ((is_string($arg) && trim($arg) === '') || $arg === null)) {
unset($aArgs[$key]);
} elseif (is_bool($arg)) {
$arg = Helpers::convertBooleanValue($arg);
}
}

$returnValue = ($returnValue !== '') ? $returnValue : implode($delimiter, $aArgs);
if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
$returnValue = ExcelError::CALC();
}

return $returnValue;
return null;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/data/Calculation/TextData/TEXTJOIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
'1-2-3',
['-', true, 1, 2, 3],
],
[
'A-B-C-E',
['-', true, 'A', 'B', 'C', null, 'E'],
],
[
'A-B-C--E',
['-', false, 'A', 'B', 'C', null, 'E'],
],
[
'A-B-C-',
['-', false, 'A', 'B', 'C', null],
],
[
'A-B-C--',
['-', false, 'A', 'B', 'C', null, null],
],
[
'<<::>>',
['::', true, '<<', '>>'],
Expand Down Expand Up @@ -64,4 +80,5 @@
],
'propagate REF' => ['#REF!', [',', true, '1', '=sheet99!A1', '3']],
'propagate NUM' => ['#NUM!', [',', true, '1', '=SQRT(-1)', '3']],
'propagate DIV0' => ['#DIV/0!', [',', true, '1', '=12/0', '3']],
];