From 6d6353c0f139a3557849dd6709612557a1967a48 Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Sat, 18 Feb 2017 20:59:14 +0100 Subject: [PATCH] Ods reader: fix reading of cells with hyperlinks --- src/PhpSpreadsheet/Reader/Ods.php | 17 +++++++----- tests/PhpSpreadsheetTests/Reader/OdsTest.php | 27 +++++++++----------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Ods.php b/src/PhpSpreadsheet/Reader/Ods.php index 17cece18..9e855ab8 100644 --- a/src/PhpSpreadsheet/Reader/Ods.php +++ b/src/PhpSpreadsheet/Reader/Ods.php @@ -403,6 +403,7 @@ class Ods extends BaseReader implements IReader $officeNs = $dom->lookupNamespaceUri("office"); $tableNs = $dom->lookupNamespaceUri("table"); $textNs = $dom->lookupNamespaceUri("text"); + $xlinkNs = $dom->lookupNamespaceUri("xlink"); $spreadsheets = $dom->getElementsByTagNameNS($officeNs, "body") ->item(0) @@ -513,7 +514,10 @@ class Ods extends BaseReader implements IReader } // Content + + /** @var \DOMElement[] $paragraphs */ $paragraphs = []; + foreach ($cellData->childNodes as $item) { /** @var \DOMElement $item */ @@ -544,12 +548,13 @@ class Ods extends BaseReader implements IReader $type = DataType::TYPE_STRING; $dataValue = $allCellDataText; - /// TODO :: Fix this: usually it's text:p > text:a, not just text:a -// if (isset($dataValue->a)) { -// $dataValue = $dataValue->a; -// $cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']); -// $hyperlink = $cellXLinkAttributes['href']; -// } + foreach ($paragraphs as $paragraph) { + $link = $paragraph->getElementsByTagNameNS($textNs, "a"); + if($link->length > 0){ + $hyperlink = $link->item(0)->getAttributeNS($xlinkNs, "href"); + } + } + break; case 'boolean': $type = DataType::TYPE_BOOL; diff --git a/tests/PhpSpreadsheetTests/Reader/OdsTest.php b/tests/PhpSpreadsheetTests/Reader/OdsTest.php index 442c2c23..1b53278a 100644 --- a/tests/PhpSpreadsheetTests/Reader/OdsTest.php +++ b/tests/PhpSpreadsheetTests/Reader/OdsTest.php @@ -15,7 +15,6 @@ use PhpOffice\PhpSpreadsheet\Style\Font; /* * @todo Fix sheet name (is not imported correctly) * @todo Sheets count is incorrect - * @todo Support rich text: cells values with styles in them are not imported correctly (text missing!) * @todo The class doesn't read the bold/italic/underline properties */ class OdsTest extends \PHPUnit_Framework_TestCase @@ -182,6 +181,18 @@ class OdsTest extends \PHPUnit_Framework_TestCase } + public function testReadHyperlinks(){ + + $spreadsheet = $this->loadOOCalcTestFile(); + $firstSheet = $spreadsheet->getSheet(0); + + $hyperlink = $firstSheet->getCell("A29"); + + $this->assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType()); + $this->assertEquals("PHPExcel", $hyperlink->getValue()); + $this->assertEquals("http://www.phpexcel.net/", $hyperlink->getHyperlink()->getUrl()); + } + /* * Below some test for features not implemented yet */ @@ -207,18 +218,4 @@ class OdsTest extends \PHPUnit_Framework_TestCase $this->assertTrue($style->getFont()->getBold()); $this->assertTrue($style->getFont()->getItalic()); } - - public function testReadHyperlinks(){ - - $this->markTestSkipped("Features not implemented fully"); - - $spreadsheet = $this->loadOOCalcTestFile(); - $firstSheet = $spreadsheet->getSheet(0); - - $hyperlink = $firstSheet->getCell("A29"); - - $this->assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType()); - $this->assertEquals("PHPExcel", $hyperlink->getValue()); - $this->assertEquals("http://www.phpexcel.net/", $hyperlink->getHyperlink()->getUrl()); - } }