Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added xml reader hyperlink support #223

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion samples/templates/Excel2003XMLTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,9 @@
<Data ss:Type="String">AE</Data>
</Cell>
<Cell ss:StyleID="ce5"/>
<Cell ss:StyleID="ce5"/>
<Cell ss:StyleID="ce5" ss:HRef="http://www.phpexcel.net/">
<Data ss:Type="String">PHPExcel</Data>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use "PHPExel" anymore, but instead "http://www.example.com" and "My website" or something similar.

</Cell>
<Cell ss:StyleID="ce5"/>
<Cell ss:StyleID="ce5"/>
<Cell ss:StyleID="ce5"/>
Expand Down
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Reader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,10 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
}
}

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'])) {
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/XEEValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@

namespace PhpOffice\PhpSpreadsheetTests\Reader;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Reader\BaseReader;
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 = __DIR__ . '/../../../samples/templates/Excel2003XMLTest.xml';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we chdir(__DIR__); in tests/bootstrap.php, you can simplify this line with:

$filename = '../samples/templates/Excel2003XMLTest.xml';


// Load into this instance
$reader = new Xml();
$this->spreadsheetXEETest = $reader->loadIntoExisting($filename, new Spreadsheet());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just $reader->load($filename) ?

}

return $this->spreadsheetXEETest;
}

/**
* @dataProvider providerInvalidXML
* @expectedException \PhpOffice\PhpSpreadsheet\Reader\Exception
Expand Down Expand Up @@ -53,4 +76,19 @@ public function providerValidXML()

return $tests;
}

/**
* Check if it can read XML Hyperlink correctly.
*/
public function testReadHyperlinks()
{
$spreadsheet = $this->loadXEETestFile();
$firstSheet = $spreadsheet->getSheet(0);

$hyperlink = $firstSheet->getCell('L1');

$this->assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
$this->assertEquals('PHPExcel', $hyperlink->getValue());
$this->assertEquals('http://www.phpexcel.net/', $hyperlink->getHyperlink()->getUrl());
}
}