Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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