# tablepress/3.4/libraries/vendor/PhpSpreadsheet/Reader/Xlsx/Hyperlinks.php

TablePress – Tables in WordPress made easy, version 3.4. 71 lines.

- Page: https://pluginprobe.com/plugins/tablepress/3.4/code/libraries/vendor/PhpSpreadsheet/Reader/Xlsx/Hyperlinks.php
- Raw: https://pluginprobe.com/plugins/tablepress/3.4/raw/libraries/vendor/PhpSpreadsheet/Reader/Xlsx/Hyperlinks.php
- Modified: 2026-04-07T08:40:50+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/tablepress/3.4/code/libraries/vendor/PhpSpreadsheet/Reader/Xlsx/Hyperlinks.php#L10-L20`.

```php
<?php

namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xlsx;

use TablePress\PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use TablePress\PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use SimpleXMLElement;

class Hyperlinks
{
	private Worksheet $worksheet;

	/** @var string[] */
	private array $hyperlinks = [];

	public function __construct(Worksheet $workSheet)
	{
		$this->worksheet = $workSheet;
	}

	public function readHyperlinks(SimpleXMLElement $relsWorksheet): void
	{
		foreach ($relsWorksheet->children(Namespaces::RELATIONSHIPS)->Relationship as $elementx) {
			$element = Xlsx::getAttributes($elementx);
			if ($element->Type == Namespaces::HYPERLINK) {
				$this->hyperlinks[(string) $element->Id] = (string) $element->Target;
			}
		}
	}

	public function setHyperlinks(SimpleXMLElement $worksheetXml): void
	{
		foreach ($worksheetXml->children(Namespaces::MAIN)->hyperlink as $hyperlink) {
			$this->setHyperlink($hyperlink, $this->worksheet);
		}
	}

	private function setHyperlink(SimpleXMLElement $hyperlink, Worksheet $worksheet): void
	{
		// Link url
		$linkRel = Xlsx::getAttributes($hyperlink, Namespaces::SCHEMA_OFFICE_DOCUMENT);

		$attributes = Xlsx::getAttributes($hyperlink);
		foreach (Coordinate::extractAllCellReferencesInRange($attributes->ref) as $cellReference) {
			$cell = $worksheet->getCell($cellReference);

			$hyperlinkUrl = '';
			if (isset($linkRel['id'])) {
				$hyperlinkUrl = $this->hyperlinks[(string) $linkRel['id']] ?? '';
			}
			if (isset($attributes['location'])) {
				if ($hyperlinkUrl === '') {
					$hyperlinkUrl = 'sheet://' . (string) $attributes['location'];
				} else {
					$hyperlinkUrl .= '#' . (string) $attributes['location'];
				}
			}
			$cell->getHyperlink()->setUrl($hyperlinkUrl);
			// Tooltip
			if (isset($attributes['tooltip'])) {
				$cell->getHyperlink()->setTooltip((string) $attributes['tooltip']);
			}

			if (isset($attributes['display'])) {
				$cell->getHyperlink()->setDisplay((string) $attributes['display']);
			}
		}
	}
}

```
