Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   7  use SimpleXMLElement;
   8  
   9  class Hyperlinks
  10  {
  11      private $worksheet;
  12  
  13      private $hyperlinks = [];
  14  
  15      public function __construct(Worksheet $workSheet)
  16      {
  17          $this->worksheet = $workSheet;
  18      }
  19  
  20      public function readHyperlinks(SimpleXMLElement $relsWorksheet): void
  21      {
  22          foreach ($relsWorksheet->Relationship as $element) {
  23              if ($element['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') {
  24                  $this->hyperlinks[(string) $element['Id']] = (string) $element['Target'];
  25              }
  26          }
  27      }
  28  
  29      public function setHyperlinks(SimpleXMLElement $worksheetXml): void
  30      {
  31          foreach ($worksheetXml->hyperlink as $hyperlink) {
  32              $this->setHyperlink($hyperlink, $this->worksheet);
  33          }
  34      }
  35  
  36      private function setHyperlink(SimpleXMLElement $hyperlink, Worksheet $worksheet): void
  37      {
  38          // Link url
  39          $linkRel = $hyperlink->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  40  
  41          foreach (Coordinate::extractAllCellReferencesInRange($hyperlink['ref']) as $cellReference) {
  42              $cell = $worksheet->getCell($cellReference);
  43              if (isset($linkRel['id'])) {
  44                  $hyperlinkUrl = $this->hyperlinks[(string) $linkRel['id']] ?? null;
  45                  if (isset($hyperlink['location'])) {
  46                      $hyperlinkUrl .= '#' . (string) $hyperlink['location'];
  47                  }
  48                  $cell->getHyperlink()->setUrl($hyperlinkUrl);
  49              } elseif (isset($hyperlink['location'])) {
  50                  $cell->getHyperlink()->setUrl('sheet://' . (string) $hyperlink['location']);
  51              }
  52  
  53              // Tooltip
  54              if (isset($hyperlink['tooltip'])) {
  55                  $cell->getHyperlink()->setTooltip((string) $hyperlink['tooltip']);
  56              }
  57          }
  58      }
  59  }