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

Allow ANCHORARRAY as Valid DataValidation List #4203

Merged
merged 4 commits into from
Oct 30, 2024
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Deprecated

- IREADER::SKIP_EMPTY_CELLS - use its alias IGNORE_EMPTY_CELLS instead.
- IReader::SKIP_EMPTY_CELLS - use its alias IGNORE_EMPTY_CELLS instead.
- Worksheet::getProtectedCells was deprecated in release 2, but was not properly documented, and not removed in release 3. Use getProtectedCellRanges instead.
- Writer/Html::isMpdf property was deprecated in release 2, but was not properly documented, and not removed in release 3. Use instanceof Mpdf instead.

### Moved

Expand All @@ -35,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Option to Write Hyperlink Rather Than Label to Csv. [Issue #1412](https://github.com/PHPOffice/PhpSpreadsheet/issues/1412) [PR #4151](https://github.com/PHPOffice/PhpSpreadsheet/pull/4151)
- Invalid Html Due to Cached Filesize. [Issue #1107](https://github.com/PHPOffice/PhpSpreadsheet/issues/1107) [PR #4184](https://github.com/PHPOffice/PhpSpreadsheet/pull/4184)
- Excel 2003 Allows Html Entities. [Issue #2157](https://github.com/PHPOffice/PhpSpreadsheet/issues/2157) [PR #4187](https://github.com/PHPOffice/PhpSpreadsheet/pull/4187)
- Allow ANCHORARRAY as Data Validation list. [Issue #4197](https://github.com/PHPOffice/PhpSpreadsheet/issues/4197) [PR #4203](https://github.com/PHPOffice/PhpSpreadsheet/pull/4203)

## 2024-09-29 - 3.3.0 (no 3.0.\*, 3.1.\*, 3.2.\*)

Expand Down
8 changes: 6 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,19 @@ private static function castToString(?SimpleXMLElement $c): ?string
return isset($c, $c->v) ? (string) $c->v : null;
}

public static function replacePrefixes(string $formula): string
{
return str_replace(['_xlfn.', '_xlws.'], '', $formula);
}

private function castToFormula(?SimpleXMLElement $c, string $r, string &$cellDataType, mixed &$value, mixed &$calculatedValue, string $castBaseType, bool $updateSharedCells = true): void
{
if ($c === null) {
return;
}
$attr = $c->f->attributes();
$cellDataType = DataType::TYPE_FORMULA;
$formula = (string) $c->f;
$formula = str_replace(['_xlfn.', '_xlws.'], '', $formula);
$formula = self::replacePrefixes((string) $c->f);
$value = "=$formula";
$calculatedValue = self::$castBaseType($c);

Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx/DataValidations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use SimpleXMLElement;

Expand Down Expand Up @@ -55,8 +56,8 @@ public function load(): void
$docValidation->setError((string) $dataValidation['error']);
$docValidation->setPromptTitle((string) $dataValidation['promptTitle']);
$docValidation->setPrompt((string) $dataValidation['prompt']);
$docValidation->setFormula1((string) $dataValidation->formula1);
$docValidation->setFormula2((string) $dataValidation->formula2);
$docValidation->setFormula1(Xlsx::replacePrefixes((string) $dataValidation->formula1));
$docValidation->setFormula2(Xlsx::replacePrefixes((string) $dataValidation->formula2));
$docValidation->setSqref($range);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,10 +983,10 @@ private function writeDataValidations(XMLWriter $objWriter, PhpspreadsheetWorksh
$objWriter->writeAttribute('sqref', $dv->getSqref() ?? $coordinate);

if ($dv->getFormula1() !== '') {
$objWriter->writeElement('formula1', $dv->getFormula1());
$objWriter->writeElement('formula1', FunctionPrefix::addFunctionPrefix($dv->getFormula1()));
}
if ($dv->getFormula2() !== '') {
$objWriter->writeElement('formula2', $dv->getFormula2());
$objWriter->writeElement('formula2', FunctionPrefix::addFunctionPrefix($dv->getFormula2()));
}

$objWriter->endElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PHPUnit\Framework\TestCase;

class ArrayFormulaValidationTest extends TestCase
{
private string $outputFile = '';

protected function tearDown(): void
{
if ($this->outputFile !== '') {
unlink($this->outputFile);
$this->outputFile = '';
}
}

/**
* @dataProvider validationProvider
*/
public function testWriteArrayFormulaValidation(string $formula): void
{
$spreadsheet = new Spreadsheet();
Calculation::getInstance($spreadsheet)
->setInstanceArrayReturnType(
Calculation::RETURN_ARRAY_AS_ARRAY
);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('C1', 1);
$sheet->setCellValue('C2', 2);
$sheet->setCellValue('C3', 3);
$sheet->setCellValue('C4', 3);
$sheet->setCellValue('C5', 5);
$sheet->setCellValue('C6', 6);
$sheet->setCellValue('B1', '=UNIQUE(C1:C6)');

$validation = $sheet->getCell('A1')->getDataValidation();
$validation->setType(DataValidation::TYPE_LIST);
$validation->setErrorStyle(DataValidation::STYLE_STOP);
$validation->setAllowBlank(true);
$validation->setShowDropDown(true);
$validation->setShowErrorMessage(true);
$validation->setError('Invalid input');
$validation->setFormula1($formula);
$sheet->getCell('A1')->setDataValidation($validation);

$this->outputFile = File::temporaryFilename();
$writer = new XlsxWriter($spreadsheet);
$writer->save($this->outputFile);
$spreadsheet->disconnectWorksheets();

$reader = new XlsxReader();
$spreadsheet2 = $reader->load($this->outputFile);
Calculation::getInstance($spreadsheet2)->setInstanceArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY);
$sheet2 = $spreadsheet2->getActiveSheet();
$validation2 = $sheet2->getCell('A1')->getDataValidation();
self::assertSame('ANCHORARRAY($B$1)', $validation2->getFormula1());
$spreadsheet2->disconnectWorksheets();

$file = 'zip://';
$file .= $this->outputFile;
$file .= '#xl/worksheets/sheet1.xml';
$data = file_get_contents($file);
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertStringContainsString('<formula1>_xlfn.ANCHORARRAY($B$1)</formula1>', $data);
}
}

public static function validationProvider(): array
{
return [
['ANCHORARRAY($B$1)'],
['$B$1#'],
];
}
}
Loading