Skip to content

HYPERLINK function not working correctly when you try to link a cell to another spreadsheet #56

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

Closed
DarkTiBe opened this issue Dec 14, 2016 · 3 comments · Fixed by #4453
Labels
writer/xlsx Writer for MS OfficeOpenXML-format (xlsx) spreadsheet files

Comments

@DarkTiBe
Copy link

DarkTiBe commented Dec 14, 2016

When you set the content of a cell to =HYPERLINK("#anotherspreadshet!A1","link") the class throws the next Fatal error:


Uncaught exception 'PhpOffice\PhpSpreadsheet\Calculation\Exception' with message 'anotherspreadshet!A1 -> Formula Error: An unexpected error occured' in \src\PhpSpreadsheet\Cell.php:280
Stack trace:
#0 src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1080): PhpOffice\PhpSpreadsheet\Cell->>getCalculatedValue()
#1 src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1027): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCell(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter), Object(PhpOffice\PhpSpreadsheet\Worksheet), 'A1', Array, Array)
#2 src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(80): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeSheetData(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter), Object(PhpOffice\PhpSpreadsheet\Worksheet), Array)
#3 srcl\PhpSpreadsheet\W src\Cell.php on line 280

@DarkTiBe DarkTiBe changed the title HYPERLINK function not working correctly when you try to link a cell on another spreadsheet HYPERLINK function not working correctly when you try to link a cell to another spreadsheet Dec 14, 2016
@MarkBaker
Copy link
Member

Nasty, nasty Microsoft.... they changed the syntax for inter-sheet references between formats.

For xls, the syntax is sheet://anotherspreadshet!A1; for xlsx, they changed it to #anotherspreadshet!A1

Will need to take a closer look at handling this, and it's made more awkward by using it in a formula

@PowerKiKi PowerKiKi added the writer/xlsx Writer for MS OfficeOpenXML-format (xlsx) spreadsheet files label Apr 18, 2017
@stale
Copy link

stale bot commented Dec 4, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Dec 4, 2017
@stale stale bot closed this as completed Dec 11, 2017
@oleibman
Copy link
Collaborator

Sometime between 7-odd years ago when this issue was marked stale and now, it was resolved. It happened so long ago that I can't even point to specific fix. Oddly, however, the issue was reported against Xlsx, and the commentary suggests that Xls was behaving. However, it seems to me that Xls is not working correctly, so I am reopening the issue, to be closed when I submit a PR to fix it, which I will do shortly.

@oleibman oleibman reopened this Apr 30, 2025
@stale stale bot removed the stale label Apr 30, 2025
github-merge-queue bot pushed a commit that referenced this issue May 7, 2025
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
writer/xlsx Writer for MS OfficeOpenXML-format (xlsx) spreadsheet files
Development

Successfully merging a pull request may close this issue.

4 participants