Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible fix for issue #3337 #3338

Merged
merged 4 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
$objWriter->writeAttribute('ref', $cell->getCoordinate());
$objWriter->writeAttribute('aca', '1');
$objWriter->writeAttribute('ca', '1');
$objWriter->text(substr($cellValue, 1));
$objWriter->text(FunctionPrefix::addFunctionPrefixStripEquals($cellValue));
$objWriter->endElement();
} else {
$objWriter->writeElement('f', FunctionPrefix::addFunctionPrefixStripEquals($cellValue));
Expand Down
88 changes: 88 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/ArrayFormulaPrefixTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class ArrayFormulaPrefixTest extends AbstractFunctional
{
protected function setUp(): void
{
parent::setUp();
}

/**
* Test to ensure that xlfn prefix is being added to functions
* included in an array formula, if appropriate.
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function testWriteArrayFormulaTextJoin(): void
{
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

//Write data
$worksheet->getCell('A1')->setValue('Some Text');
$worksheet->getCell('A2')->setValue('Some More Text');
$worksheet->getCell('A3')->setValue(14.56);
$worksheet->getCell('A4')->setValue(17.24);
$worksheet->getCell('A5')->setValue(9.4);
$worksheet->getCell('A6')->setValue(5);

//Write formula
$cell = $worksheet->getCell('A7');
$cell->setValueExplicit('=TEXTJOIN("",TRUE,IF(ISNUMBER(A1:A6), A1:A6,""))', DataType::TYPE_FORMULA);
$attrs = $cell->getFormulaAttributes();
$attrs['t'] = 'array';
$cell->setFormulaAttributes($attrs);

$writer = new Xlsx($spreadsheet);
$writerWorksheet = new Xlsx\Worksheet($writer);
$data = $writerWorksheet->writeWorksheet($worksheet, []);

$expected = <<<XML
<f t="array" ref="A7" aca="1" ca="1">_xlfn.TEXTJOIN(&quot;&quot;,TRUE,IF(ISNUMBER(A1:A6), A1:A6,&quot;&quot;))</f>
XML;
self::assertStringContainsString($expected, $data);
}

/**
* Certain functions do not have the xlfn prefix applied. Test an array formula
* that includes those functions to see if they are written properly.
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function testWriteArrayFormulaWithoutPrefix(): void
{
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

//Write data
$worksheet->getCell('A1')->setValue('orange');
$worksheet->getCell('A2')->setValue('green');
$worksheet->getCell('A3')->setValue('blue');
$worksheet->getCell('A4')->setValue('yellow');
$worksheet->getCell('A5')->setValue('pink');
$worksheet->getCell('A6')->setValue('red');

//Write formula
$cell = $worksheet->getCell('A7');
$cell->setValueExplicit('=SUM(LEN(A1:A6))', DataType::TYPE_FORMULA);
$attrs = $cell->getFormulaAttributes();
$attrs['t'] = 'array';
$cell->setFormulaAttributes($attrs);

$writer = new Xlsx($spreadsheet);
$writerWorksheet = new Xlsx\Worksheet($writer);
$data = $writerWorksheet->writeWorksheet($worksheet, []);

$expected = <<<XML
<f t="array" ref="A7" aca="1" ca="1">SUM(LEN(A1:A6))</f>
XML;
self::assertStringContainsString($expected, $data);
}
}