ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DefinedNames.php
Go to the documentation of this file.
1<?php
2
4
5use DOMElement;
8
10{
11 public function read(DOMElement $workbookData): void
12 {
13 $this->readDefinedRanges($workbookData);
14 $this->readDefinedExpressions($workbookData);
15 }
16
20 protected function readDefinedRanges(DOMElement $workbookData): void
21 {
22 $namedRanges = $workbookData->getElementsByTagNameNS($this->tableNs, 'named-range');
23 foreach ($namedRanges as $definedNameElement) {
24 $definedName = $definedNameElement->getAttributeNS($this->tableNs, 'name');
25 $baseAddress = $definedNameElement->getAttributeNS($this->tableNs, 'base-cell-address');
26 $range = $definedNameElement->getAttributeNS($this->tableNs, 'cell-range-address');
27
28 $baseAddress = $this->convertToExcelAddressValue($baseAddress);
29 $range = $this->convertToExcelAddressValue($range);
30
31 $this->addDefinedName($baseAddress, $definedName, $range);
32 }
33 }
34
38 protected function readDefinedExpressions(DOMElement $workbookData): void
39 {
40 $namedExpressions = $workbookData->getElementsByTagNameNS($this->tableNs, 'named-expression');
41 foreach ($namedExpressions as $definedNameElement) {
42 $definedName = $definedNameElement->getAttributeNS($this->tableNs, 'name');
43 $baseAddress = $definedNameElement->getAttributeNS($this->tableNs, 'base-cell-address');
44 $expression = $definedNameElement->getAttributeNS($this->tableNs, 'expression');
45
46 $baseAddress = $this->convertToExcelAddressValue($baseAddress);
47 $expression = substr($expression, strpos($expression, ':=') + 1);
48 $expression = $this->convertToExcelFormulaValue($expression);
49
50 $this->addDefinedName($baseAddress, $definedName, $expression);
51 }
52 }
53
57 private function addDefinedName(string $baseAddress, string $definedName, string $value): void
58 {
59 [$sheetReference] = Worksheet::extractSheetTitle($baseAddress, true);
60 $worksheet = $this->spreadsheet->getSheetByName($sheetReference);
61 // Worksheet might still be null if we're only loading selected sheets rather than the full spreadsheet
62 if ($worksheet !== null) {
63 $this->spreadsheet->addDefinedName(DefinedName::createInstance((string) $definedName, $worksheet, $value));
64 }
65 }
66}
An exception for terminatinating execution or to throw for unit testing.
static createInstance(string $name, ?Worksheet $worksheet=null, ?string $value=null, bool $localOnly=false, ?Worksheet $scope=null)
Create a new defined name, either a range or a formula.
Definition: DefinedName.php:84
convertToExcelAddressValue(string $openOfficeAddress)
Definition: BaseReader.php:29
convertToExcelFormulaValue(string $openOfficeFormula)
Definition: BaseReader.php:49
addDefinedName(string $baseAddress, string $definedName, string $value)
Assess scope and store the Defined Name.
readDefinedExpressions(DOMElement $workbookData)
Read any Named Formulae that are defined in this spreadsheet.
readDefinedRanges(DOMElement $workbookData)
Read any Named Ranges that are defined in this spreadsheet.