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");
$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;

View File

@ -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());
}
}