Skip to content

Commit ada583f

Browse files
authored
Merge pull request #2990 from PHPOffice/TextFunctions-ArrayToText
Initial work on the ARRAYTOTEXT() Excel Function
2 parents 630d92f + a8b6214 commit ada583f

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1010
### Added
1111

1212
- Implementation of the new `TEXTBEFORE()`, `TEXTAFTER()` and `TEXTSPLIT()` Excel Functions
13+
- Implementation of the `ARRAYTOTEXT()` Excel Function
1314

1415
### Changed
1516

src/PhpSpreadsheet/Calculation/Calculation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ class Calculation
304304
],
305305
'ARRAYTOTEXT' => [
306306
'category' => Category::CATEGORY_TEXT_AND_DATA,
307-
'functionCall' => [Functions::class, 'DUMMY'],
308-
'argumentCount' => '?',
307+
'functionCall' => [TextData\Text::class, 'fromArray'],
308+
'argumentCount' => '1,2',
309309
],
310310
'ASC' => [
311311
'category' => Category::CATEGORY_TEXT_AND_DATA,

src/PhpSpreadsheet/Calculation/TextData/Text.php

+44
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Text
@@ -207,4 +208,47 @@ private static function matchFlags(bool $matchMode): string
207208
{
208209
return ($matchMode === true) ? 'miu' : 'mu';
209210
}
211+
212+
public static function fromArray(array $array, int $format = 0): string
213+
{
214+
$result = [];
215+
foreach ($array as $row) {
216+
$cells = [];
217+
foreach ($row as $cellValue) {
218+
$value = ($format === 1) ? self::formatValueMode1($cellValue) : self::formatValueMode0($cellValue);
219+
$cells[] = $value;
220+
}
221+
$result[] = implode(($format === 1) ? ',' : ', ', $cells);
222+
}
223+
224+
$result = implode(($format === 1) ? ';' : ', ', $result);
225+
226+
return ($format === 1) ? '{' . $result . '}' : $result;
227+
}
228+
229+
/**
230+
* @param mixed $cellValue
231+
*/
232+
private static function formatValueMode0($cellValue): string
233+
{
234+
if (is_bool($cellValue)) {
235+
return ($cellValue) ? Calculation::$localeBoolean['TRUE'] : Calculation::$localeBoolean['FALSE'];
236+
}
237+
238+
return (string) $cellValue;
239+
}
240+
241+
/**
242+
* @param mixed $cellValue
243+
*/
244+
private static function formatValueMode1($cellValue): string
245+
{
246+
if (is_string($cellValue) && Functions::isError($cellValue) === false) {
247+
return Calculation::FORMULA_STRING_QUOTE . $cellValue . Calculation::FORMULA_STRING_QUOTE;
248+
} elseif (is_bool($cellValue)) {
249+
return ($cellValue) ? Calculation::$localeBoolean['TRUE'] : Calculation::$localeBoolean['FALSE'];
250+
}
251+
252+
return (string) $cellValue;
253+
}
210254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
4+
5+
class ArrayToTextTest extends AllSetupTeardown
6+
{
7+
/**
8+
* @dataProvider providerARRAYTOTEXT
9+
*/
10+
public function testArrayToText(string $expectedResult, array $testData, int $mode): void
11+
{
12+
$worksheet = $this->getSheet();
13+
$worksheet->fromArray($testData, null, 'A1', true);
14+
$worksheet->getCell('H1')->setValue("=ARRAYTOTEXT(A1:C5, {$mode})");
15+
16+
$result = $worksheet->getCell('H1')->getCalculatedValue();
17+
self::assertSame($expectedResult, $result);
18+
}
19+
20+
public function providerARRAYTOTEXT(): array
21+
{
22+
return require 'tests/data/Calculation/TextData/ARRAYTOTEXT.php';
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
return [
4+
[
5+
"1, 2, 3, a, b, c, TRUE, FALSE, #DIV/0!, 44774, 1, 44777, 12345.6789, -2.4, Hello\nWorld",
6+
[
7+
[1, 2, 3],
8+
['a', 'b', 'c'],
9+
[true, false, '=12/0'],
10+
['=DATE(2022,8,1)', '1', '=A4+3'],
11+
[12345.6789, '=-12/5', "Hello\nWorld"],
12+
],
13+
0,
14+
],
15+
[
16+
"{1,2,3;\"a\",\"b\",\"c\";TRUE,FALSE,#DIV/0!;44774,1,44777;12345.6789,-2.4,\"Hello\nWorld\"}",
17+
[
18+
[1, 2, 3],
19+
['a', 'b', 'c'],
20+
[true, false, '=12/0'],
21+
['=DATE(2022,8,1)', 1, '=A4+3'],
22+
[12345.6789, '=-12/5', "Hello\nWorld"],
23+
],
24+
1,
25+
],
26+
];

0 commit comments

Comments
 (0)