Files
oleibman 92d3c1bc7d Xls Writer Treat Hyperlink Starting with # as Internal
Fix #56, which had gone stale but is now reopened. The problem which it reported with Xlsx Writer was fixed long ago. However, Xls Writer has 2 problems regarding hyperlinks. First, some logic which should have been `if ... elseif ... else ...` was coded as `if ... if ... unconditional ...`, resulting in multiple writes and a corrupt worksheet (which Excel does fix correctly). Second, it treated a hyperlink url starting with `#` (pointer to a cell in the same spreadsheet) as external, when it should be treated as internal.

Also changed `Hyperlink::isInternal` to recognize a starting `#`, and to use `str_starts_with('sheet://')` rather than `str_contains`.
2025-04-30 00:23:35 -07:00

39 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class HyperlinkTest extends AbstractFunctional
{
public function testHyperlink(): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet1->setTitle('First');
$sheet2 = $spreadsheet->createSheet();
$sheet2->setTitle('Second');
$sheet2->setCellValue('A100', 'other sheet');
$sheet1->setCellValue('A100', 'this sheet');
$sheet1->setCellValue('A1', '=HYPERLINK("#A100", "here")');
$sheet1->setCellValue('A2', '=HYPERLINK("#Second!A100", "there")');
$sheet1->setCellValue('A3', '=HYPERLINK("http://example.com", "external")');
$sheet1->setCellValue('A4', 'gotoA101');
$sheet1->getCell('A4')
->getHyperlink()
->setUrl('#A101');
$robj = $this->writeAndReload($spreadsheet, 'Xls');
$spreadsheet->disconnectWorksheets();
$sheet0 = $robj->setActiveSheetIndex(0);
self::assertSame('sheet://#A100', $sheet0->getCell('A1')->getHyperlink()->getUrl());
self::assertSame('sheet://#Second!A100', $sheet0->getCell('A2')->getHyperlink()->getUrl());
self::assertSame('http://example.com', $sheet0->getCell('A3')->getHyperlink()->getUrl());
self::assertSame('sheet://#A101', $sheet0->getCell('A4')->getHyperlink()->getUrl());
$robj->disconnectWorksheets();
}
}