ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DefinedNames.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use DOMElement;
8 
9 class DefinedNames extends BaseReader
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 }
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
static extractSheetTitle($pRange, $returnRange=false)
Extract worksheet title from range.
Definition: Worksheet.php:2719
convertToExcelAddressValue(string $openOfficeAddress)
Definition: BaseReader.php:29
convertToExcelFormulaValue(string $openOfficeFormula)
Definition: BaseReader.php:49
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.
addDefinedName(string $baseAddress, string $definedName, string $value)
Assess scope and store the Defined Name.