Skip to content

Commit 9e89c36

Browse files
committed
Excel Omits Between Operator for Data Validation
Fix PHPOffice#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.
1 parent ae72efe commit 9e89c36

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/PhpSpreadsheet/Cell/DataValidation.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DataValidation
2828
const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual';
2929
const OPERATOR_NOTBETWEEN = 'notBetween';
3030
const OPERATOR_NOTEQUAL = 'notEqual';
31+
private const DEFAULT_OPERATOR = self::OPERATOR_BETWEEN;
3132

3233
/**
3334
* Formula 1.
@@ -52,7 +53,7 @@ class DataValidation
5253
/**
5354
* Operator.
5455
*/
55-
private string $operator = self::OPERATOR_BETWEEN;
56+
private string $operator = self::DEFAULT_OPERATOR;
5657

5758
/**
5859
* Allow Blank.
@@ -198,7 +199,7 @@ public function getOperator(): string
198199
*/
199200
public function setOperator(string $operator): static
200201
{
201-
$this->operator = $operator;
202+
$this->operator = ($operator === '') ? self::DEFAULT_OPERATOR : $operator;
202203

203204
return $this;
204205
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Cell\DataValidator;
8+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
9+
10+
class Issue3863Test extends \PHPUnit\Framework\TestCase
11+
{
12+
private static string $testbook = 'tests/data/Reader/XLSX/issue.3863.xlsx';
13+
14+
public function testPreliminaries(): void
15+
{
16+
$file = 'zip://';
17+
$file .= self::$testbook;
18+
$file .= '#xl/worksheets/sheet1.xml';
19+
$data = file_get_contents($file);
20+
if ($data === false) {
21+
self::fail('Unable to read file');
22+
} else {
23+
// Only 1 Data Validation and it does not specify operator
24+
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);
25+
}
26+
}
27+
28+
public function testValidData(): void
29+
{
30+
$reader = new Xlsx();
31+
$spreadsheet = $reader->load(self::$testbook);
32+
$sheet = $spreadsheet->getActiveSheet();
33+
self::assertSame('between', $sheet->getCell('A1')->getDataValidation()->getOperator());
34+
$validator = new DataValidator();
35+
self::assertTrue($validator->isValid($sheet->getCell('A1')));
36+
$sheet->getCell('A1')->setValue(3);
37+
self::assertFalse($validator->isValid($sheet->getCell('A1')));
38+
$sheet->getCell('A1')->setValue(7);
39+
self::assertTrue($validator->isValid($sheet->getCell('A1')));
40+
$spreadsheet->disconnectWorksheets();
41+
}
42+
}
8.33 KB
Binary file not shown.

0 commit comments

Comments
 (0)