Skip to content

Commit 67dd292

Browse files
committed
Unit tests for issue #3337
Added two unit tests for issue #3337: one that includes the xlfn prefix, and one that does not. These tests will ensure that the prefix is being added, when appropriate, within an array formula.
1 parent 2b33305 commit 67dd292

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Cell\DataType;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PhpOffice\PhpSpreadsheet\Style\Conditional;
8+
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
9+
use PhpOffice\PhpSpreadsheet\Style\Fill;
10+
use PhpOffice\PhpSpreadsheet\Style\Style;
11+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
12+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
13+
14+
class ArrayFormulaPrefixTest extends AbstractFunctional
15+
{
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
}
21+
22+
/**
23+
* Test to ensure that xlfn prefix is being added to functions
24+
* included in an array formula, if appropriate.
25+
* @return void
26+
* @throws \PhpOffice\PhpSpreadsheet\Exception
27+
*/
28+
public function testWriteArrayFormulaTextJoin(): void
29+
{
30+
$spreadsheet = new Spreadsheet();
31+
$worksheet = $spreadsheet->getActiveSheet();
32+
33+
//Write data
34+
$worksheet->getCell('A1')->setValue('Some Text');
35+
$worksheet->getCell('A2')->setValue('Some More Text');
36+
$worksheet->getCell('A3')->setValue(14.56);
37+
$worksheet->getCell('A4')->setValue(17.24);
38+
$worksheet->getCell('A5')->setValue(9.4);
39+
$worksheet->getCell('A6')->setValue(5);
40+
41+
//Write formula
42+
$cell = $worksheet->getCell('A7');
43+
$cell->setValueExplicit('=TEXTJOIN("",TRUE,IF(ISNUMBER(A1:A6), A1:A6,""))', DataType::TYPE_FORMULA);
44+
$attrs = $cell->getFormulaAttributes();
45+
$attrs['t'] = 'array';
46+
$cell->setFormulaAttributes($attrs);
47+
48+
$writer = new Xlsx($spreadsheet);
49+
$writerWorksheet = new Xlsx\Worksheet($writer);
50+
$data = $writerWorksheet->writeWorksheet($worksheet, []);
51+
52+
$expected = <<<XML
53+
<f t="array" ref="A7" aca="1" ca="1">_xlfn.TEXTJOIN(&quot;&quot;,TRUE,IF(ISNUMBER(A1:A6), A1:A6,&quot;&quot;))</f>
54+
XML;
55+
self::assertStringContainsString($expected, $data);
56+
}
57+
58+
/**
59+
* Certain functions do not have the xlfn prefix applied. Test an array formula
60+
* that includes those functions to see if they are written properly
61+
* @return void
62+
* @throws \PhpOffice\PhpSpreadsheet\Exception
63+
*/
64+
public function testWriteArrayFormulaWithoutPrefix(): void
65+
{
66+
$spreadsheet = new Spreadsheet();
67+
$worksheet = $spreadsheet->getActiveSheet();
68+
69+
//Write data
70+
$worksheet->getCell('A1')->setValue('orange');
71+
$worksheet->getCell('A2')->setValue('green');
72+
$worksheet->getCell('A3')->setValue('blue');
73+
$worksheet->getCell('A4')->setValue('yellow');
74+
$worksheet->getCell('A5')->setValue('pink');
75+
$worksheet->getCell('A6')->setValue('red');
76+
77+
//Write formula
78+
$cell = $worksheet->getCell('A7');
79+
$cell->setValueExplicit('=SUM(LEN(A1:A6))', DataType::TYPE_FORMULA);
80+
$attrs = $cell->getFormulaAttributes();
81+
$attrs['t'] = 'array';
82+
$cell->setFormulaAttributes($attrs);
83+
84+
$writer = new Xlsx($spreadsheet);
85+
$writerWorksheet = new Xlsx\Worksheet($writer);
86+
$data = $writerWorksheet->writeWorksheet($worksheet, []);
87+
88+
$expected = <<<XML
89+
<f t="array" ref="A7" aca="1" ca="1">SUM(LEN(A1:A6))</f>
90+
XML;
91+
self::assertStringContainsString($expected, $data);
92+
}
93+
}

0 commit comments

Comments
 (0)