Skip to content

Xls Writer Treat Hyperlink Starting with # as Internal #4453

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

Merged
merged 3 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

- Micro-optimization for excelToDateTimeObject. [Issue #4438](https://github.com/PHPOffice/PhpSpreadsheet/issues/4438) [PR #4442](https://github.com/PHPOffice/PhpSpreadsheet/pull/4442)
- Print Area and Row Break. [Issue #1275](https://github.com/PHPOffice/PhpSpreadsheet/issues/1275) [PR #4450](https://github.com/PHPOffice/PhpSpreadsheet/pull/4450)
- Xls Writer Treat Hyperlink Starting with # as Internal. [Issue #56](https://github.com/PHPOffice/PhpSpreadsheet/issues/56) [PR #4453](https://github.com/PHPOffice/PhpSpreadsheet/pull/4453)

## 2025-04-16 - 4.2.0

Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Cell/Hyperlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public function setTooltip(string $tooltip): static
}

/**
* Is this hyperlink internal? (to another worksheet).
* Is this hyperlink internal? (to another worksheet or a cell in this worksheet).
*/
public function isInternal(): bool
{
return str_contains($this->url, 'sheet://');
return str_starts_with($this->url, 'sheet://') || str_starts_with($this->url, '#');
}

public function getTypeHyperlink(): string
Expand Down
12 changes: 6 additions & 6 deletions src/PhpSpreadsheet/Writer/Xls/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,9 @@ public function close(): void
[$column, $row] = Coordinate::indexesFromString($coordinate);

$url = $hyperlink->getUrl();

if (str_contains($url, 'sheet://')) {
if ($url[0] === '#') {
$url = "internal:$url";
} elseif (str_starts_with($url, 'sheet://')) {
// internal to current workbook
$url = str_replace('sheet://', 'internal:', $url);
} elseif (Preg::isMatch('/^(http:|https:|ftp:|mailto:)/', $url)) {
Expand Down Expand Up @@ -955,12 +956,11 @@ private function writeUrlRange(int $row1, int $col1, int $row2, int $col2, strin
// Check for internal/external sheet links or default to web link
if (Preg::isMatch('[^internal:]', $url)) {
$this->writeUrlInternal($row1, $col1, $row2, $col2, $url);
}
if (Preg::isMatch('[^external:]', $url)) {
} elseif (Preg::isMatch('[^external:]', $url)) {
$this->writeUrlExternal($row1, $col1, $row2, $col2, $url);
} else {
$this->writeUrlWeb($row1, $col1, $row2, $col2, $url);
}

$this->writeUrlWeb($row1, $col1, $row2, $col2, $url);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xls/HyperlinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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();
}
}
38 changes: 38 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/HyperlinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

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, 'Xlsx');
$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();
}
}