From a264cafe4c2541c8a3f0d6cb2147ae6843b5aa3a Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sat, 27 Jun 2020 23:03:25 +0200 Subject: [PATCH] Helper class for the conversion of cell addresses between A1 and R1C1 formats, and vice-versa (#1558) * Helper class for the conversion of cell addresses between A1 and R1C1 formats, and vice-versa --- src/PhpSpreadsheet/Cell/AddressHelper.php | 88 ++++++++++++++ .../Cell/AddressHelperTest.php | 108 ++++++++++++++++++ .../data/Cell/A1ConversionToR1C1Absolute.php | 15 +++ .../data/Cell/A1ConversionToR1C1Exception.php | 5 + .../data/Cell/A1ConversionToR1C1Relative.php | 19 +++ .../data/Cell/R1C1ConversionToA1Absolute.php | 14 +++ .../data/Cell/R1C1ConversionToA1Exception.php | 9 ++ .../data/Cell/R1C1ConversionToA1Relative.php | 14 +++ 8 files changed, 272 insertions(+) create mode 100644 src/PhpSpreadsheet/Cell/AddressHelper.php create mode 100644 tests/PhpSpreadsheetTests/Cell/AddressHelperTest.php create mode 100644 tests/data/Cell/A1ConversionToR1C1Absolute.php create mode 100644 tests/data/Cell/A1ConversionToR1C1Exception.php create mode 100644 tests/data/Cell/A1ConversionToR1C1Relative.php create mode 100644 tests/data/Cell/R1C1ConversionToA1Absolute.php create mode 100644 tests/data/Cell/R1C1ConversionToA1Exception.php create mode 100644 tests/data/Cell/R1C1ConversionToA1Relative.php diff --git a/src/PhpSpreadsheet/Cell/AddressHelper.php b/src/PhpSpreadsheet/Cell/AddressHelper.php new file mode 100644 index 00000000..77a521b0 --- /dev/null +++ b/src/PhpSpreadsheet/Cell/AddressHelper.php @@ -0,0 +1,88 @@ +expectException(Exception::class); + + AddressHelper::convertToA1($address); + } + + public function providerR1C1ConversionToA1Exception() + { + return require 'tests/data/Cell/R1C1ConversionToA1Exception.php'; + } + + /** + * @dataProvider providerA1ConversionToR1C1Absolute + */ + public function testA1ConversionToR1C1Absolute(string $expectedValue, string $address): void + { + $actualValue = AddressHelper::convertToR1C1($address); + + self::assertSame($expectedValue, $actualValue); + } + + public function providerA1ConversionToR1C1Absolute() + { + return require 'tests/data/Cell/A1ConversionToR1C1Absolute.php'; + } + + /** + * @dataProvider providerA1ConversionToR1C1Relative + */ + public function testA1ConversionToR1C1Relative(string $expectedValue, string $address, ?int $row = null, ?int $column = null): void + { + $actualValue = AddressHelper::convertToR1C1($address, $row, $column); + + self::assertSame($expectedValue, $actualValue); + } + + public function providerA1ConversionToR1C1Relative() + { + return require 'tests/data/Cell/A1ConversionToR1C1Relative.php'; + } + + /** + * @dataProvider providerA1ConversionToR1C1Exception + */ + public function testA1ConversionToR1C1Exception(string $address): void + { + $this->expectException(Exception::class); + + AddressHelper::convertToR1C1($address); + } + + public function providerA1ConversionToR1C1Exception() + { + return require 'tests/data/Cell/A1ConversionToR1C1Exception.php'; + } +} diff --git a/tests/data/Cell/A1ConversionToR1C1Absolute.php b/tests/data/Cell/A1ConversionToR1C1Absolute.php new file mode 100644 index 00000000..171672bb --- /dev/null +++ b/tests/data/Cell/A1ConversionToR1C1Absolute.php @@ -0,0 +1,15 @@ +