From 7aa6233185fb94c727354a5a80097b61c49d81b9 Mon Sep 17 00:00:00 2001 From: GreatHumorist Date: Mon, 18 Sep 2017 16:03:32 +0800 Subject: [PATCH] Added xml reader hyperlink support Closes #223 --- CHANGELOG.md | 1 + docs/references/features-cross-reference.md | 4 +- samples/templates/Excel2003XMLTest.xml | 4 +- src/PhpSpreadsheet/Reader/Xml.php | 4 ++ .../Reader/XEEValidatorTest.php | 39 ++++++++++++++++++- 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad37f9fb..7ee38170 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Support for chart fill color - @CrazyBite [#158](https://github.com/PHPOffice/PhpSpreadsheet/pull/158) +- Support for read Hyperlink for xml - [@GreatHumorist](https://github.com/GreatHumorist) [#223](https://github.com/PHPOffice/PhpSpreadsheet/pull/223) ### Changed diff --git a/docs/references/features-cross-reference.md b/docs/references/features-cross-reference.md index 7ab9d068..a02558b3 100644 --- a/docs/references/features-cross-reference.md +++ b/docs/references/features-cross-reference.md @@ -1167,7 +1167,7 @@ Hyperlinks ✔ ✔ - ✖ + ✔ ✔ ✖ ✖ @@ -1496,4 +1496,4 @@ 1. Only BIFF8 files support Rich Text. Prior to that, comments could only be plain text -2. Only BIFF8 files support alignment and rotation. Prior to that, comments could only be unformatted text \ No newline at end of file +2. Only BIFF8 files support alignment and rotation. Prior to that, comments could only be unformatted text diff --git a/samples/templates/Excel2003XMLTest.xml b/samples/templates/Excel2003XMLTest.xml index d386a0cf..fb75ab06 100644 --- a/samples/templates/Excel2003XMLTest.xml +++ b/samples/templates/Excel2003XMLTest.xml @@ -549,7 +549,9 @@ AE - + + PhpSpreadsheet + diff --git a/src/PhpSpreadsheet/Reader/Xml.php b/src/PhpSpreadsheet/Reader/Xml.php index b52637f8..bfbc6e07 100644 --- a/src/PhpSpreadsheet/Reader/Xml.php +++ b/src/PhpSpreadsheet/Reader/Xml.php @@ -610,6 +610,10 @@ class Xml extends BaseReader implements IReader } } + if (isset($cell_ss['HRef'])) { + $spreadsheet->getActiveSheet()->getCell($cellRange)->getHyperlink()->setUrl($cell_ss['HRef']); + } + if ((isset($cell_ss['MergeAcross'])) || (isset($cell_ss['MergeDown']))) { $columnTo = $columnID; if (isset($cell_ss['MergeAcross'])) { diff --git a/tests/PhpSpreadsheetTests/Reader/XEEValidatorTest.php b/tests/PhpSpreadsheetTests/Reader/XEEValidatorTest.php index 7b2ff48a..0c8f186d 100644 --- a/tests/PhpSpreadsheetTests/Reader/XEEValidatorTest.php +++ b/tests/PhpSpreadsheetTests/Reader/XEEValidatorTest.php @@ -2,13 +2,35 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader; +use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Reader\BaseReader; -use PhpOffice\PhpSpreadsheet\Reader\Exception; use PhpOffice\PhpSpreadsheet\Reader\Xml; +use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit_Framework_TestCase; class XEEValidatorTest extends PHPUnit_Framework_TestCase { + /** + * @var Spreadsheet + */ + private $spreadsheetXEETest; + + /** + * @return Spreadsheet + */ + protected function loadXEETestFile() + { + if (!$this->spreadsheetXEETest) { + $filename = '../samples/templates/Excel2003XMLTest.xml'; + + // Load into this instance + $reader = new Xml(); + $this->spreadsheetXEETest = $reader->load($filename); + } + + return $this->spreadsheetXEETest; + } + /** * @dataProvider providerInvalidXML * @expectedException \PhpOffice\PhpSpreadsheet\Reader\Exception @@ -77,4 +99,19 @@ class XEEValidatorTest extends PHPUnit_Framework_TestCase return $tests; } + + /** + * Check if it can read XML Hyperlink correctly. + */ + public function testReadHyperlinks() + { + $spreadsheet = $this->loadXEETestFile(); + $firstSheet = $spreadsheet->getSheet(0); + + $hyperlink = $firstSheet->getCell('L1'); + + self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType()); + self::assertEquals('PhpSpreadsheet', $hyperlink->getValue()); + self::assertEquals('http://phpspreadsheet.readthedocs.io/', $hyperlink->getHyperlink()->getUrl()); + } }