Completed
Push — master ( 350c9e...38ea66 )
by Mark
33s queued 28s
created

testWriteArrayFormulaWithoutPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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