Ods reader: fix reading of cells with hyperlinks

This commit is contained in:
Paolo Agostinetto 2017-02-18 20:59:14 +01:00
parent 1dba2d1766
commit 6d6353c0f1
2 changed files with 23 additions and 21 deletions

View File

@ -403,6 +403,7 @@ class Ods extends BaseReader implements IReader
$officeNs = $dom->lookupNamespaceUri("office"); $officeNs = $dom->lookupNamespaceUri("office");
$tableNs = $dom->lookupNamespaceUri("table"); $tableNs = $dom->lookupNamespaceUri("table");
$textNs = $dom->lookupNamespaceUri("text"); $textNs = $dom->lookupNamespaceUri("text");
$xlinkNs = $dom->lookupNamespaceUri("xlink");
$spreadsheets = $dom->getElementsByTagNameNS($officeNs, "body") $spreadsheets = $dom->getElementsByTagNameNS($officeNs, "body")
->item(0) ->item(0)
@ -513,7 +514,10 @@ class Ods extends BaseReader implements IReader
} }
// Content // Content
/** @var \DOMElement[] $paragraphs */
$paragraphs = []; $paragraphs = [];
foreach ($cellData->childNodes as $item) { foreach ($cellData->childNodes as $item) {
/** @var \DOMElement $item */ /** @var \DOMElement $item */
@ -544,12 +548,13 @@ class Ods extends BaseReader implements IReader
$type = DataType::TYPE_STRING; $type = DataType::TYPE_STRING;
$dataValue = $allCellDataText; $dataValue = $allCellDataText;
/// TODO :: Fix this: usually it's text:p > text:a, not just text:a foreach ($paragraphs as $paragraph) {
// if (isset($dataValue->a)) { $link = $paragraph->getElementsByTagNameNS($textNs, "a");
// $dataValue = $dataValue->a; if($link->length > 0){
// $cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']); $hyperlink = $link->item(0)->getAttributeNS($xlinkNs, "href");
// $hyperlink = $cellXLinkAttributes['href']; }
// } }
break; break;
case 'boolean': case 'boolean':
$type = DataType::TYPE_BOOL; $type = DataType::TYPE_BOOL;

View File

@ -15,7 +15,6 @@ use PhpOffice\PhpSpreadsheet\Style\Font;
/* /*
* @todo Fix sheet name (is not imported correctly) * @todo Fix sheet name (is not imported correctly)
* @todo Sheets count is incorrect * @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 * @todo The class doesn't read the bold/italic/underline properties
*/ */
class OdsTest extends \PHPUnit_Framework_TestCase 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 * 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()->getBold());
$this->assertTrue($style->getFont()->getItalic()); $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());
}
} }