Skip to content

Commit 24f1a2f

Browse files
authored
Merge pull request #3033 from PHPOffice/ExcelFunctions-ValueToText
Implementation of the `VALUETOTEXT()` Excel Function
2 parents 1317084 + f7a3534 commit 24f1a2f

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +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
13+
- Implementation of the `ARRAYTOTEXT()` and `VALUETOTEXT()` Excel Functions
1414
- Support for [mitoteam/jpgraph](https://packagist.org/packages/mitoteam/jpgraph) implementation of
1515
JpGraph library to render charts added.
1616
- Charts: Add Gradients, Transparency, Hidden Axes, Rounded Corners, Trendlines.

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,8 +2659,8 @@ class Calculation
26592659
],
26602660
'VALUETOTEXT' => [
26612661
'category' => Category::CATEGORY_TEXT_AND_DATA,
2662-
'functionCall' => [Functions::class, 'DUMMY'],
2663-
'argumentCount' => '?',
2662+
'functionCall' => [TextData\Format::class, 'valueToText'],
2663+
'argumentCount' => '1,2',
26642664
],
26652665
'VAR' => [
26662666
'category' => Category::CATEGORY_STATISTICAL,

src/PhpSpreadsheet/Calculation/Engineering/ConvertUOM.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class ConvertUOM
106106
'W' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Watt', 'AllowPrefix' => true],
107107
'w' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Watt', 'AllowPrefix' => true],
108108
'PS' => ['Group' => self::CATEGORY_POWER, 'Unit Name' => 'Pferdestärke', 'AllowPrefix' => false],
109+
// Magnetism
109110
'T' => ['Group' => self::CATEGORY_MAGNETISM, 'Unit Name' => 'Tesla', 'AllowPrefix' => true],
110111
'ga' => ['Group' => self::CATEGORY_MAGNETISM, 'Unit Name' => 'Gauss', 'AllowPrefix' => true],
111112
// Temperature

src/PhpSpreadsheet/Calculation/TextData/Format.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use DateTimeInterface;
66
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
7+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
78
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
89
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
910
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
1011
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
1112
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
13+
use PhpOffice\PhpSpreadsheet\RichText\RichText;
1214
use PhpOffice\PhpSpreadsheet\Shared\Date;
1315
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
1416
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
@@ -208,6 +210,38 @@ public static function VALUE($value = '')
208210
return (float) $value;
209211
}
210212

213+
/**
214+
* TEXT.
215+
*
216+
* @param mixed $value The value to format
217+
* Or can be an array of values
218+
* @param mixed $format
219+
*
220+
* @return array|string
221+
* If an array of values is passed for either of the arguments, then the returned result
222+
* will also be an array with matching dimensions
223+
*/
224+
public static function valueToText($value, $format = false)
225+
{
226+
if (is_array($value) || is_array($format)) {
227+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $format);
228+
}
229+
230+
$format = (bool) $format;
231+
232+
if (is_object($value) && $value instanceof RichText) {
233+
$value = $value->getPlainText();
234+
}
235+
if (is_string($value)) {
236+
$value = ($format === true) ? Calculation::wrapResult($value) : $value;
237+
$value = str_replace("\n", '', $value);
238+
} elseif (is_bool($value)) {
239+
$value = Calculation::$localeBoolean[$value === true ? 'TRUE' : 'FALSE'];
240+
}
241+
242+
return (string) $value;
243+
}
244+
211245
/**
212246
* @param mixed $decimalSeparator
213247
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData;
4+
5+
class ValueToTextTest extends AllSetupTeardown
6+
{
7+
/**
8+
* @dataProvider providerVALUE
9+
*
10+
* @param mixed $expectedResult
11+
* @param mixed $value
12+
* @param mixed $format
13+
*/
14+
public function testVALUETOTEXT($expectedResult, $value, $format): void
15+
{
16+
$sheet = $this->getSheet();
17+
$this->setCell('A1', $value);
18+
$sheet->getCell('B1')->setValue("=VALUETOTEXT(A1, {$format})");
19+
20+
$result = $sheet->getCell('B1')->getCalculatedValue();
21+
self::assertSame($expectedResult, $result);
22+
}
23+
24+
public function providerVALUE(): array
25+
{
26+
return require 'tests/data/Calculation/TextData/VALUETOTEXT.php';
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\RichText\RichText;
4+
5+
$richText1 = new RichText();
6+
$richText1->createTextRun('Hello');
7+
$richText1->createText(' World');
8+
9+
$richText2 = new RichText();
10+
$richText2->createTextRun('Hello');
11+
$richText2->createText("\nWorld");
12+
13+
return [
14+
['1', 1, 0],
15+
['1.23', 1.23, 0],
16+
['-123.456', -123.456, 0],
17+
['TRUE', true, 0],
18+
['FALSE', false, 0],
19+
['Hello World', 'Hello World', 0],
20+
['HelloWorld', "Hello\nWorld", 0],
21+
['"Hello World"', 'Hello World', 1],
22+
['"HelloWorld"', "Hello\nWorld", 1],
23+
['Hello World', $richText1, 0],
24+
['HelloWorld', $richText2, 0],
25+
['"Hello World"', $richText1, 1],
26+
['"HelloWorld"', $richText2, 1],
27+
];

0 commit comments

Comments
 (0)