FIND(), SEARCH(), LEFT(), RIGHT, MID(), CLEAN(), TRIM(), TEXTJOIN() and REPT() functions

This commit is contained in:
MarkBaker 2022-02-11 14:49:53 +01:00
parent c9886127c0
commit c41dd0afae
17 changed files with 348 additions and 53 deletions

View File

@ -1930,21 +1930,6 @@ parameters:
count: 1 count: 1
path: src/PhpSpreadsheet/Calculation/TextData.php path: src/PhpSpreadsheet/Calculation/TextData.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:SEARCHINSENSITIVE\\(\\) should return string but returns int\\|string\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:SEARCHSENSITIVE\\(\\) should return string but returns int\\|string\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:TRIMNONPRINTABLE\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Calculation/TextData.php
- -
message: "#^Parameter \\#1 \\$glue of function implode expects string, mixed given\\.$#" message: "#^Parameter \\#1 \\$glue of function implode expects string, mixed given\\.$#"
count: 1 count: 1

View File

@ -30,6 +30,8 @@ class NetworkDays
* @param mixed $dateArgs An array of dates (such as holidays) to exclude from the calculation * @param mixed $dateArgs An array of dates (such as holidays) to exclude from the calculation
* *
* @return array|int|string Interval between the dates * @return array|int|string Interval between the dates
* If an array of values is passed for the $startDate or $endDate arguments, then the returned result
* will also be an array with matching dimensions
*/ */
public static function count($startDate, $endDate, ...$dateArgs) public static function count($startDate, $endDate, ...$dateArgs)
{ {

View File

@ -26,7 +26,7 @@ class YearFrac
* *
* @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer), * @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer),
* PHP DateTime object, or a standard date string * PHP DateTime object, or a standard date string
* Or can be an array of methods * Or can be an array of values
* @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer), * @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer),
* PHP DateTime object, or a standard date string * PHP DateTime object, or a standard date string
* Or can be an array of methods * Or can be an array of methods

View File

@ -34,7 +34,7 @@ class TextData
* *
* @param mixed $stringValue Value to check * @param mixed $stringValue Value to check
* *
* @return string * @return null|array|string
*/ */
public static function TRIMNONPRINTABLE($stringValue = '') public static function TRIMNONPRINTABLE($stringValue = '')
{ {
@ -50,7 +50,7 @@ class TextData
* *
* @param mixed $stringValue Value to check * @param mixed $stringValue Value to check
* *
* @return string * @return array|string
*/ */
public static function TRIMSPACES($stringValue = '') public static function TRIMSPACES($stringValue = '')
{ {
@ -110,17 +110,17 @@ class TextData
} }
/** /**
* SEARCHSENSITIVE. * FIND.
* *
* @Deprecated 1.18.0 * @Deprecated 1.18.0
* *
* @see Use the sensitive() method in the TextData\Search class instead * @see Use the sensitive() method in the TextData\Search class instead
* *
* @param string $needle The string to look for * @param array|string $needle The string to look for
* @param string $haystack The string in which to look * @param array|string $haystack The string in which to look
* @param int $offset Offset within $haystack * @param array|int $offset Offset within $haystack
* *
* @return string * @return array|int|string
*/ */
public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1) public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1)
{ {
@ -128,17 +128,17 @@ class TextData
} }
/** /**
* SEARCHINSENSITIVE. * SEARCH.
* *
* @Deprecated 1.18.0 * @Deprecated 1.18.0
* *
* @see Use the insensitive() method in the TextData\Search class instead * @see Use the insensitive() method in the TextData\Search class instead
* *
* @param string $needle The string to look for * @param array|string $needle The string to look for
* @param string $haystack The string in which to look * @param array|string $haystack The string in which to look
* @param int $offset Offset within $haystack * @param array|int $offset Offset within $haystack
* *
* @return string * @return array|int|string
*/ */
public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1) public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1)
{ {
@ -170,10 +170,10 @@ class TextData
* *
* @see Use the left() method in the TextData\Extract class instead * @see Use the left() method in the TextData\Extract class instead
* *
* @param string $value Value * @param array|string $value Value
* @param int $chars Number of characters * @param array|int $chars Number of characters
* *
* @return string * @return array|string
*/ */
public static function LEFT($value = '', $chars = 1) public static function LEFT($value = '', $chars = 1)
{ {
@ -187,11 +187,11 @@ class TextData
* *
* @see Use the mid() method in the TextData\Extract class instead * @see Use the mid() method in the TextData\Extract class instead
* *
* @param string $value Value * @param array|string $value Value
* @param int $start Start character * @param array|int $start Start character
* @param int $chars Number of characters * @param array|int $chars Number of characters
* *
* @return string * @return array|string
*/ */
public static function MID($value = '', $start = 1, $chars = null) public static function MID($value = '', $start = 1, $chars = null)
{ {
@ -205,10 +205,10 @@ class TextData
* *
* @see Use the right() method in the TextData\Extract class instead * @see Use the right() method in the TextData\Extract class instead
* *
* @param string $value Value * @param array|string $value Value
* @param int $chars Number of characters * @param array|int $chars Number of characters
* *
* @return string * @return array|string
*/ */
public static function RIGHT($value = '', $chars = 1) public static function RIGHT($value = '', $chars = 1)
{ {
@ -420,7 +420,7 @@ class TextData
* @param mixed $ignoreEmpty * @param mixed $ignoreEmpty
* @param mixed $args * @param mixed $args
* *
* @return string * @return array|string
*/ */
public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args) public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args)
{ {
@ -436,10 +436,10 @@ class TextData
* *
* @see Use the builtinREPT() method in the TextData\Concatenate class instead * @see Use the builtinREPT() method in the TextData\Concatenate class instead
* *
* @param string $str Should be numeric * @param array|string $str Should be numeric
* @param mixed $number Should be int * @param mixed $number Should be int
* *
* @return string * @return array|string
*/ */
public static function builtinREPT($str, $number) public static function builtinREPT($str, $number)
{ {

View File

@ -2,10 +2,13 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class Concatenate class Concatenate
{ {
use ArrayEnabled;
/** /**
* CONCATENATE. * CONCATENATE.
* *
@ -28,12 +31,28 @@ class Concatenate
/** /**
* TEXTJOIN. * TEXTJOIN.
* *
* @param mixed $delimiter * @param mixed $delimiter The delimter to use between the joined arguments
* @param mixed $ignoreEmpty * Or can be an array of values
* @param mixed $args * @param mixed $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped
* Or can be an array of values
* @param mixed $args The values to join
*
* @return array|string The joined 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): string public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args)
{ {
if (is_array($delimiter) || is_array($ignoreEmpty)) {
return self::evaluateArrayArgumentsSubset(
[self::class, __FUNCTION__],
2,
$delimiter,
$ignoreEmpty,
...$args
);
}
$delimiter = Functions::flattenSingleValue($delimiter); $delimiter = Functions::flattenSingleValue($delimiter);
$ignoreEmpty = Functions::flattenSingleValue($ignoreEmpty); $ignoreEmpty = Functions::flattenSingleValue($ignoreEmpty);
// Loop through arguments // Loop through arguments
@ -55,10 +74,20 @@ class Concatenate
* Returns the result of builtin function round after validating args. * Returns the result of builtin function round after validating args.
* *
* @param mixed $stringValue The value to repeat * @param mixed $stringValue The value to repeat
* Or can be an array of values
* @param mixed $repeatCount The number of times the string value should be repeated * @param mixed $repeatCount The number of times the string value should be repeated
* Or can be an array of values
*
* @return array|string The repeated string
* If an array of values is passed for the $stringValue or $repeatCount arguments, then the returned result
* will also be an array with matching dimensions
*/ */
public static function builtinREPT($stringValue, $repeatCount): string public static function builtinREPT($stringValue, $repeatCount)
{ {
if (is_array($stringValue) || is_array($repeatCount)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $stringValue, $repeatCount);
}
$repeatCount = Functions::flattenSingleValue($repeatCount); $repeatCount = Functions::flattenSingleValue($repeatCount);
$stringValue = Helpers::extractString($stringValue); $stringValue = Helpers::extractString($stringValue);

View File

@ -2,18 +2,31 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
class Extract class Extract
{ {
use ArrayEnabled;
/** /**
* LEFT. * LEFT.
* *
* @param mixed $value String value from which to extract characters * @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer) * @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/ */
public static function left($value, $chars = 1): string public static function left($value, $chars = 1)
{ {
if (is_array($value) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars);
}
try { try {
$value = Helpers::extractString($value); $value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1); $chars = Helpers::extractInt($chars, 0, 1);
@ -28,11 +41,22 @@ class Extract
* MID. * MID.
* *
* @param mixed $value String value from which to extract characters * @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $start Integer offset of the first character that we want to extract * @param mixed $start Integer offset of the first character that we want to extract
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer) * @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value, $start or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/ */
public static function mid($value, $start, $chars): string public static function mid($value, $start, $chars)
{ {
if (is_array($value) || is_array($start) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $start, $chars);
}
try { try {
$value = Helpers::extractString($value); $value = Helpers::extractString($value);
$start = Helpers::extractInt($start, 1); $start = Helpers::extractInt($start, 1);
@ -48,10 +72,20 @@ class Extract
* RIGHT. * RIGHT.
* *
* @param mixed $value String value from which to extract characters * @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer) * @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/ */
public static function right($value, $chars = 1): string public static function right($value, $chars = 1)
{ {
if (is_array($value) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars);
}
try { try {
$value = Helpers::extractString($value); $value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1); $chars = Helpers::extractInt($chars, 0, 1);

View File

@ -2,23 +2,35 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
class Search class Search
{ {
use ArrayEnabled;
/** /**
* FIND (case sensitive search). * FIND (case sensitive search).
* *
* @param mixed $needle The string to look for * @param mixed $needle The string to look for
* Or can be an array of values
* @param mixed $haystack The string in which to look * @param mixed $haystack The string in which to look
* Or can be an array of values
* @param mixed $offset Integer offset within $haystack to start searching from * @param mixed $offset Integer offset within $haystack to start searching from
* Or can be an array of values
* *
* @return int|string * @return array|int|string The offset where the first occurrence of needle was found in the haystack
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/ */
public static function sensitive($needle, $haystack, $offset = 1) public static function sensitive($needle, $haystack, $offset = 1)
{ {
if (is_array($needle) || is_array($haystack) || is_array($offset)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $needle, $haystack, $offset);
}
try { try {
$needle = Helpers::extractString($needle); $needle = Helpers::extractString($needle);
$haystack = Helpers::extractString($haystack); $haystack = Helpers::extractString($haystack);
@ -45,13 +57,22 @@ class Search
* SEARCH (case insensitive search). * SEARCH (case insensitive search).
* *
* @param mixed $needle The string to look for * @param mixed $needle The string to look for
* Or can be an array of values
* @param mixed $haystack The string in which to look * @param mixed $haystack The string in which to look
* Or can be an array of values
* @param mixed $offset Integer offset within $haystack to start searching from * @param mixed $offset Integer offset within $haystack to start searching from
* Or can be an array of values
* *
* @return int|string * @return array|int|string The offset where the first occurrence of needle was found in the haystack
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/ */
public static function insensitive($needle, $haystack, $offset = 1) public static function insensitive($needle, $haystack, $offset = 1)
{ {
if (is_array($needle) || is_array($haystack) || is_array($offset)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $needle, $haystack, $offset);
}
try { try {
$needle = Helpers::extractString($needle); $needle = Helpers::extractString($needle);
$haystack = Helpers::extractString($haystack); $haystack = Helpers::extractString($haystack);

View File

@ -2,17 +2,28 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
class Trim class Trim
{ {
use ArrayEnabled;
/** /**
* CLEAN. * CLEAN.
* *
* @param mixed $stringValue String Value to check * @param mixed $stringValue String Value to check
* Or can be an array of values
* *
* @return null|string * @return null|array|string
* If an array of values is passed as the argument, then the returned result will also be an array
* with the same dimensions
*/ */
public static function nonPrintable($stringValue = '') public static function nonPrintable($stringValue = '')
{ {
if (is_array($stringValue)) {
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $stringValue);
}
$stringValue = Helpers::extractString($stringValue); $stringValue = Helpers::extractString($stringValue);
return preg_replace('/[\\x00-\\x1f]/', '', "$stringValue"); return preg_replace('/[\\x00-\\x1f]/', '', "$stringValue");
@ -22,11 +33,18 @@ class Trim
* TRIM. * TRIM.
* *
* @param mixed $stringValue String Value to check * @param mixed $stringValue String Value to check
* Or can be an array of values
* *
* @return string * @return array|string
* If an array of values is passed as the argument, then the returned result will also be an array
* with the same dimensions
*/ */
public static function spaces($stringValue = '') public static function spaces($stringValue = '')
{ {
if (is_array($stringValue)) {
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $stringValue);
}
$stringValue = Helpers::extractString($stringValue); $stringValue = Helpers::extractString($stringValue);
return trim(preg_replace('/ +/', ' ', trim("$stringValue", ' ')) ?? '', ' '); return trim(preg_replace('/ +/', ' ', trim("$stringValue", ' ')) ?? '', ' ');

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class CleanTest extends AllSetupTeardown class CleanTest extends AllSetupTeardown
{ {
/** /**
@ -28,4 +30,25 @@ class CleanTest extends AllSetupTeardown
{ {
return require 'tests/data/Calculation/TextData/CLEAN.php'; return require 'tests/data/Calculation/TextData/CLEAN.php';
} }
/**
* @dataProvider providerCleanArray
*/
public function testCleanArray(array $expectedResult, string $array): void
{
$calculation = Calculation::getInstance();
$formula = "=CLEAN({$array})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerCleanArray(): array
{
return [
'row vector' => [[['PHP', 'MS Excel', 'Open/Libre Office']], '{"PHP", "MS Excel", "Open/Libre Office"}'],
'column vector' => [[['PHP'], ['MS Excel'], ['Open/Libre Office']], '{"PHP"; "MS Excel"; "Open/Libre Office"}'],
'matrix' => [[['PHP', 'MS Excel'], ['PhpSpreadsheet', 'Open/Libre Office']], '{"PHP", "MS Excel"; "PhpSpreadsheet", "Open/Libre Office"}'],
];
}
} }

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class FindTest extends AllSetupTeardown class FindTest extends AllSetupTeardown
{ {
/** /**
@ -39,4 +41,25 @@ class FindTest extends AllSetupTeardown
{ {
return require 'tests/data/Calculation/TextData/FIND.php'; return require 'tests/data/Calculation/TextData/FIND.php';
} }
/**
* @dataProvider providerFindArray
*/
public function testFindArray(array $expectedResult, string $argument1, string $argument2): void
{
$calculation = Calculation::getInstance();
$formula = "=FIND({$argument1}, {$argument2})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerFindArray(): array
{
return [
'row vector #1' => [[[3, 4, '#VALUE!']], '"l"', '{"Hello", "World", "PhpSpreadsheet"}'],
'column vector #1' => [[[3], [4], ['#VALUE!']], '"l"', '{"Hello"; "World"; "PhpSpreadsheet"}'],
'matrix #1' => [[[3, 4], ['#VALUE!', 5]], '"l"', '{"Hello", "World"; "PhpSpreadsheet", "Excel"}'],
];
}
} }

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Settings; use PhpOffice\PhpSpreadsheet\Settings;
@ -180,4 +181,26 @@ class LeftTest extends AllSetupTeardown
], ],
]; ];
} }
/**
* @dataProvider providerLeftArray
*/
public function testLeftArray(array $expectedResult, string $argument1, string $argument2): void
{
$calculation = Calculation::getInstance();
$formula = "=LEFT({$argument1}, {$argument2})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerLeftArray(): array
{
return [
'row vector #1' => [[['Hel', 'Wor', 'Php']], '{"Hello", "World", "PhpSpreadsheet"}', '3'],
'column vector #1' => [[['Hel'], ['Wor'], ['Php']], '{"Hello"; "World"; "PhpSpreadsheet"}', '3'],
'matrix #1' => [[['Hel', 'Wor'], ['Php', 'Exc']], '{"Hello", "World"; "PhpSpreadsheet", "Excel"}', '3'],
'column vector #2' => [[['Php'], ['PhpSp']], '"PhpSpreadsheet"', '{3; 5}'],
];
}
} }

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Settings; use PhpOffice\PhpSpreadsheet\Settings;
@ -202,4 +203,24 @@ class MidTest extends AllSetupTeardown
], ],
]; ];
} }
/**
* @dataProvider providerMidArray
*/
public function testMidArray(array $expectedResult, string $argument1, string $argument2, string $argument3): void
{
$calculation = Calculation::getInstance();
$formula = "=MID({$argument1}, {$argument2}, {$argument3})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerMidArray(): array
{
return [
'row vector #1' => [[['lo Wor', 'Spread']], '{"Hello World", "PhpSpreadsheet"}', '4', '6'],
'column vector #1' => [[[' Wor'], ['read']], '{"Hello World"; "PhpSpreadsheet"}', '6', '4'],
];
}
} }

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class ReptTest extends AllSetupTeardown class ReptTest extends AllSetupTeardown
{ {
/** /**
@ -33,4 +35,26 @@ class ReptTest extends AllSetupTeardown
{ {
return require 'tests/data/Calculation/TextData/REPT.php'; return require 'tests/data/Calculation/TextData/REPT.php';
} }
/**
* @dataProvider providerReptArray
*/
public function testReptArray(array $expectedResult, string $argument1, string $argument2): void
{
$calculation = Calculation::getInstance();
$formula = "=REPT({$argument1}, {$argument2})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerReptArray(): array
{
return [
'row vector #1' => [[['PHPPHPPHP', 'HAHAHA', 'HOHOHO']], '{"PHP", "HA", "HO"}', '3'],
'column vector #1' => [[['PHPPHPPHP'], ['HAHAHA'], ['HOHOHO']], '{"PHP"; "HA"; "HO"}', '3'],
'matrix #1' => [[['PHPPHP', '❤️🐘💚❤️🐘💚'], ['HAHA', 'HOHO']], '{"PHP", "❤️🐘💚"; "HA", "HO"}', '2'],
'row vector #2' => [[[' PHP PHP PHP ', ' PHP PHP ']], '" PHP "', '{3, 2}'],
];
}
} }

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Settings; use PhpOffice\PhpSpreadsheet\Settings;
@ -180,4 +181,26 @@ class RightTest extends AllSetupTeardown
], ],
]; ];
} }
/**
* @dataProvider providerRightArray
*/
public function testRightArray(array $expectedResult, string $argument1, string $argument2): void
{
$calculation = Calculation::getInstance();
$formula = "=RIGHT({$argument1}, {$argument2})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerRightArray(): array
{
return [
'row vector #1' => [[['llo', 'rld', 'eet']], '{"Hello", "World", "PhpSpreadsheet"}', '3'],
'column vector #1' => [[['llo'], ['rld'], ['eet']], '{"Hello"; "World"; "PhpSpreadsheet"}', '3'],
'matrix #1' => [[['llo', 'rld'], ['eet', 'cel']], '{"Hello", "World"; "PhpSpreadsheet", "Excel"}', '3'],
'column vector #2' => [[['eet'], ['sheet']], '"PhpSpreadsheet"', '{3; 5}'],
];
}
} }

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class SearchTest extends AllSetupTeardown class SearchTest extends AllSetupTeardown
{ {
/** /**
@ -39,4 +41,25 @@ class SearchTest extends AllSetupTeardown
{ {
return require 'tests/data/Calculation/TextData/SEARCH.php'; return require 'tests/data/Calculation/TextData/SEARCH.php';
} }
/**
* @dataProvider providerSearchArray
*/
public function testSearchArray(array $expectedResult, string $argument1, string $argument2): void
{
$calculation = Calculation::getInstance();
$formula = "=SEARCH({$argument1}, {$argument2})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerSearchArray(): array
{
return [
'row vector #1' => [[[3, 4, '#VALUE!']], '"L"', '{"Hello", "World", "PhpSpreadsheet"}'],
'column vector #1' => [[[3], [4], ['#VALUE!']], '"L"', '{"Hello"; "World"; "PhpSpreadsheet"}'],
'matrix #1' => [[[3, 4], ['#VALUE!', 5]], '"L"', '{"Hello", "World"; "PhpSpreadsheet", "Excel"}'],
];
}
} }

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class TextJoinTest extends AllSetupTeardown class TextJoinTest extends AllSetupTeardown
{ {
/** /**
@ -32,4 +34,25 @@ class TextJoinTest extends AllSetupTeardown
{ {
return require 'tests/data/Calculation/TextData/TEXTJOIN.php'; return require 'tests/data/Calculation/TextData/TEXTJOIN.php';
} }
/**
* @dataProvider providerTextjoinArray
*/
public function testTextjoinArray(array $expectedResult, string $delimiter, string $blanks, string $texts): void
{
$calculation = Calculation::getInstance();
$formula = "=TEXTJOIN({$delimiter}, {$blanks}, {$texts})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerTextjoinArray(): array
{
return [
'row vector #1' => [[['AB,CD,EF', 'AB;CD;EF']], '{",", ";"}', 'FALSE', '"AB", "CD", "EF"'],
'column vector #1' => [[['AB--CD--EF'], ['AB|CD|EF']], '{"--"; "|"}', 'FALSE', '"AB", "CD", "EF"'],
'matrix #1' => [[['AB,CD,EF', 'AB;CD;EF'], ['AB-CD-EF', 'AB|CD|EF']], '{",", ";"; "-", "|"}', 'FALSE', '"AB", "CD", "EF"'],
];
}
} }

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
class TrimTest extends AllSetupTeardown class TrimTest extends AllSetupTeardown
{ {
/** /**
@ -28,4 +30,25 @@ class TrimTest extends AllSetupTeardown
{ {
return require 'tests/data/Calculation/TextData/TRIM.php'; return require 'tests/data/Calculation/TextData/TRIM.php';
} }
/**
* @dataProvider providerTrimArray
*/
public function testTrimArray(array $expectedResult, string $array): void
{
$calculation = Calculation::getInstance();
$formula = "=TRIM({$array})";
$result = $calculation->_calculateFormulaValue($formula);
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
}
public function providerTrimArray(): array
{
return [
'row vector' => [[['PHP', 'MS Excel', 'Open/Libre Office']], '{" PHP ", " MS Excel ", " Open/Libre Office "}'],
'column vector' => [[['PHP'], ['MS Excel'], ['Open/Libre Office']], '{" PHP "; " MS Excel "; " Open/Libre Office "}'],
'matrix' => [[['PHP', 'MS Excel'], ['PhpSpreadsheet', 'Open/Libre Office']], '{" PHP ", " MS Excel "; " PhpSpreadsheet ", " Open/Libre Office "}'],
];
}
} }