Hyperlinks and Namespacing (#2391)

Fix #2389. Hyperlinks referring to cells in the spreadsheet itself are not being handled properly. This is the first namespacing regression identified for release 19. Usual cause and fix - need to take greater care with attributes than was previously the case.
This commit is contained in:
oleibman 2021-11-14 10:27:59 -08:00 committed by GitHub
parent 4ac0c47ac7
commit 52585a9d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 6 deletions

View File

@ -47,17 +47,17 @@ class Hyperlinks
$cell = $worksheet->getCell($cellReference); $cell = $worksheet->getCell($cellReference);
if (isset($linkRel['id'])) { if (isset($linkRel['id'])) {
$hyperlinkUrl = $this->hyperlinks[(string) $linkRel['id']] ?? null; $hyperlinkUrl = $this->hyperlinks[(string) $linkRel['id']] ?? null;
if (isset($hyperlink['location'])) { if (isset($attributes['location'])) {
$hyperlinkUrl .= '#' . (string) $hyperlink['location']; $hyperlinkUrl .= '#' . (string) $attributes['location'];
} }
$cell->getHyperlink()->setUrl($hyperlinkUrl); $cell->getHyperlink()->setUrl($hyperlinkUrl);
} elseif (isset($hyperlink['location'])) { } elseif (isset($attributes['location'])) {
$cell->getHyperlink()->setUrl('sheet://' . (string) $hyperlink['location']); $cell->getHyperlink()->setUrl('sheet://' . (string) $attributes['location']);
} }
// Tooltip // Tooltip
if (isset($hyperlink['tooltip'])) { if (isset($attributes['tooltip'])) {
$cell->getHyperlink()->setTooltip((string) $hyperlink['tooltip']); $cell->getHyperlink()->setTooltip((string) $attributes['tooltip']);
} }
} }
} }

View File

@ -0,0 +1,52 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class HyperlinkTest extends AbstractFunctional
{
public function testReadAndWriteHyperlinks(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->setTitle('Sheet One');
$sheet1->getCell('A1')->setValue(100);
$sheet1->getCell('B1')->setValue('this is b1');
$spreadsheet->addNamedRange(new NamedRange('namedb1', $sheet1, '$B$1'));
$sheet1->setCellValue('A2', 'link to same sheet');
$sheet1->getCell('A2')->getHyperlink()->setUrl("sheet://'Sheet One'!A1");
$sheet1->setCellValue('A3', 'link to defined name');
$sheet1->getCell('A3')->getHyperlink()->setUrl('sheet://namedb1');
$sheet2 = $spreadsheet->createSheet();
$sheet2->setTitle('Sheet Two');
$sheet2->setCellValue('A2', 'link to other sheet');
$sheet2->getCell('A2')->getHyperlink()->setUrl("sheet://'Sheet One'!A1");
$sheet2->setCellValue('A3', 'external link');
$sheet2->getCell('A3')->getHyperlink()->setUrl('https://www.example.com');
$sheet2->setCellValue('A4', 'external link with anchor');
$sheet2->getCell('A4')->getHyperlink()->setUrl('https://www.example.com#anchor');
$sheet2->getCell('A4')->getHyperlink()->setTooltip('go to anchor tag on example.com');
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
$rsheet1 = $reloadedSpreadsheet->getSheet(0);
self::assertSame('link to same sheet', $rsheet1->getCell('A2')->getValue());
self::assertSame("sheet://'Sheet One'!A1", $rsheet1->getCell('A2')->getHyperlink()->getUrl());
self::assertSame('link to defined name', $rsheet1->getCell('A3')->getValue());
self::assertSame('sheet://namedb1', $rsheet1->getCell('A3')->getHyperlink()->getUrl());
$rsheet2 = $reloadedSpreadsheet->getSheet(1);
self::assertSame('link to other sheet', $rsheet2->getCell('A2')->getValue());
self::assertSame("sheet://'Sheet One'!A1", $rsheet2->getCell('A2')->getHyperlink()->getUrl());
self::assertSame('external link', $rsheet2->getCell('A3')->getValue());
self::assertSame('https://www.example.com', $rsheet2->getCell('A3')->getHyperlink()->getUrl());
self::assertSame('https://www.example.com#anchor', $rsheet2->getCell('A4')->getHyperlink()->getUrl());
self::assertSame('external link with anchor', $rsheet2->getCell('A4')->getValue());
self::assertSame('go to anchor tag on example.com', $rsheet2->getCell('A4')->getHyperlink()->getToolTip());
$reloadedSpreadsheet->disconnectWorksheets();
}
}