mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-18 15:53:24 +00:00
Excel Omits Between Operator for Data Validation
Fix #3863. Data Validation default operator is `between`. When Excel writes out a data validation item, it may omit the operator. Xlsx reader will therefore initialize operator to null string. Issue indicates that user wants `between` returned for `getOperator`. A more serious problem is that `isValid` method does not handle this situation correctly. Data Validation is changed to set Operator to the default value if an attempt is made to set it to null string.
This commit is contained in:
@@ -28,6 +28,7 @@ class DataValidation
|
||||
const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual';
|
||||
const OPERATOR_NOTBETWEEN = 'notBetween';
|
||||
const OPERATOR_NOTEQUAL = 'notEqual';
|
||||
private const DEFAULT_OPERATOR = self::OPERATOR_BETWEEN;
|
||||
|
||||
/**
|
||||
* Formula 1.
|
||||
@@ -52,7 +53,7 @@ class DataValidation
|
||||
/**
|
||||
* Operator.
|
||||
*/
|
||||
private string $operator = self::OPERATOR_BETWEEN;
|
||||
private string $operator = self::DEFAULT_OPERATOR;
|
||||
|
||||
/**
|
||||
* Allow Blank.
|
||||
@@ -198,7 +199,7 @@ class DataValidation
|
||||
*/
|
||||
public function setOperator(string $operator): static
|
||||
{
|
||||
$this->operator = $operator;
|
||||
$this->operator = ($operator === '') ? self::DEFAULT_OPERATOR : $operator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DataValidator;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
||||
|
||||
class Issue3863Test extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
private static string $testbook = 'tests/data/Reader/XLSX/issue.3863.xlsx';
|
||||
|
||||
public function testPreliminaries(): void
|
||||
{
|
||||
$file = 'zip://';
|
||||
$file .= self::$testbook;
|
||||
$file .= '#xl/worksheets/sheet1.xml';
|
||||
$data = file_get_contents($file);
|
||||
if ($data === false) {
|
||||
self::fail('Unable to read file');
|
||||
} else {
|
||||
// Only 1 Data Validation and it does not specify operator
|
||||
self::assertStringContainsString('<dataValidations count="1"><dataValidation type="whole" allowBlank="1" showInputMessage="1" showErrorMessage="1" sqref="A1" xr:uid="{D0F98CC5-7234-4ADF-BD42-F33321DCD3CA}"><formula1>5</formula1><formula2>10</formula2></dataValidation></dataValidations>', $data);
|
||||
}
|
||||
}
|
||||
|
||||
public function testValidData(): void
|
||||
{
|
||||
$reader = new Xlsx();
|
||||
$spreadsheet = $reader->load(self::$testbook);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
self::assertSame('between', $sheet->getCell('A1')->getDataValidation()->getOperator());
|
||||
$validator = new DataValidator();
|
||||
self::assertTrue($validator->isValid($sheet->getCell('A1')));
|
||||
$sheet->getCell('A1')->setValue(3);
|
||||
self::assertFalse($validator->isValid($sheet->getCell('A1')));
|
||||
$sheet->getCell('A1')->setValue(7);
|
||||
self::assertTrue($validator->isValid($sheet->getCell('A1')));
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user