Merge pull request #2990 from PHPOffice/TextFunctions-ArrayToText
Initial work on the ARRAYTOTEXT() Excel Function
This commit is contained in:
commit
ada583f3cf
|
|
@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
### Added
|
||||
|
||||
- Implementation of the new `TEXTBEFORE()`, `TEXTAFTER()` and `TEXTSPLIT()` Excel Functions
|
||||
- Implementation of the `ARRAYTOTEXT()` Excel Function
|
||||
|
||||
### Changed
|
||||
|
||||
|
|
|
|||
|
|
@ -304,8 +304,8 @@ class Calculation
|
|||
],
|
||||
'ARRAYTOTEXT' => [
|
||||
'category' => Category::CATEGORY_TEXT_AND_DATA,
|
||||
'functionCall' => [Functions::class, 'DUMMY'],
|
||||
'argumentCount' => '?',
|
||||
'functionCall' => [TextData\Text::class, 'fromArray'],
|
||||
'argumentCount' => '1,2',
|
||||
],
|
||||
'ASC' => [
|
||||
'category' => Category::CATEGORY_TEXT_AND_DATA,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
|
||||
class Text
|
||||
|
|
@ -207,4 +208,47 @@ class Text
|
|||
{
|
||||
return ($matchMode === true) ? 'miu' : 'mu';
|
||||
}
|
||||
|
||||
public static function fromArray(array $array, int $format = 0): string
|
||||
{
|
||||
$result = [];
|
||||
foreach ($array as $row) {
|
||||
$cells = [];
|
||||
foreach ($row as $cellValue) {
|
||||
$value = ($format === 1) ? self::formatValueMode1($cellValue) : self::formatValueMode0($cellValue);
|
||||
$cells[] = $value;
|
||||
}
|
||||
$result[] = implode(($format === 1) ? ',' : ', ', $cells);
|
||||
}
|
||||
|
||||
$result = implode(($format === 1) ? ';' : ', ', $result);
|
||||
|
||||
return ($format === 1) ? '{' . $result . '}' : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $cellValue
|
||||
*/
|
||||
private static function formatValueMode0($cellValue): string
|
||||
{
|
||||
if (is_bool($cellValue)) {
|
||||
return ($cellValue) ? Calculation::$localeBoolean['TRUE'] : Calculation::$localeBoolean['FALSE'];
|
||||
}
|
||||
|
||||
return (string) $cellValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $cellValue
|
||||
*/
|
||||
private static function formatValueMode1($cellValue): string
|
||||
{
|
||||
if (is_string($cellValue) && Functions::isError($cellValue) === false) {
|
||||
return Calculation::FORMULA_STRING_QUOTE . $cellValue . Calculation::FORMULA_STRING_QUOTE;
|
||||
} elseif (is_bool($cellValue)) {
|
||||
return ($cellValue) ? Calculation::$localeBoolean['TRUE'] : Calculation::$localeBoolean['FALSE'];
|
||||
}
|
||||
|
||||
return (string) $cellValue;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
|
||||
|
||||
class ArrayToTextTest extends AllSetupTeardown
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerARRAYTOTEXT
|
||||
*/
|
||||
public function testArrayToText(string $expectedResult, array $testData, int $mode): void
|
||||
{
|
||||
$worksheet = $this->getSheet();
|
||||
$worksheet->fromArray($testData, null, 'A1', true);
|
||||
$worksheet->getCell('H1')->setValue("=ARRAYTOTEXT(A1:C5, {$mode})");
|
||||
|
||||
$result = $worksheet->getCell('H1')->getCalculatedValue();
|
||||
self::assertSame($expectedResult, $result);
|
||||
}
|
||||
|
||||
public function providerARRAYTOTEXT(): array
|
||||
{
|
||||
return require 'tests/data/Calculation/TextData/ARRAYTOTEXT.php';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
[
|
||||
"1, 2, 3, a, b, c, TRUE, FALSE, #DIV/0!, 44774, 1, 44777, 12345.6789, -2.4, Hello\nWorld",
|
||||
[
|
||||
[1, 2, 3],
|
||||
['a', 'b', 'c'],
|
||||
[true, false, '=12/0'],
|
||||
['=DATE(2022,8,1)', '1', '=A4+3'],
|
||||
[12345.6789, '=-12/5', "Hello\nWorld"],
|
||||
],
|
||||
0,
|
||||
],
|
||||
[
|
||||
"{1,2,3;\"a\",\"b\",\"c\";TRUE,FALSE,#DIV/0!;44774,1,44777;12345.6789,-2.4,\"Hello\nWorld\"}",
|
||||
[
|
||||
[1, 2, 3],
|
||||
['a', 'b', 'c'],
|
||||
[true, false, '=12/0'],
|
||||
['=DATE(2022,8,1)', 1, '=A4+3'],
|
||||
[12345.6789, '=-12/5', "Hello\nWorld"],
|
||||
],
|
||||
1,
|
||||
],
|
||||
];
|
||||
Loading…
Reference in New Issue