ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NamedExpressions.php
Go to the documentation of this file.
1 <?php
2 
4 
10 
12 {
13  private $objWriter;
14 
15  private $spreadsheet;
16 
18 
20  {
21  $this->objWriter = $objWriter;
22  $this->spreadsheet = $spreadsheet;
23  $this->formulaConvertor = $formulaConvertor;
24  }
25 
26  public function write(): string
27  {
28  $this->objWriter->startElement('table:named-expressions');
29  $this->writeExpressions();
30  $this->objWriter->endElement();
31 
32  return '';
33  }
34 
35  private function writeExpressions(): void
36  {
37  $definedNames = $this->spreadsheet->getDefinedNames();
38 
39  foreach ($definedNames as $definedName) {
40  if ($definedName->isFormula()) {
41  $this->objWriter->startElement('table:named-expression');
42  $this->writeNamedFormula($definedName, $this->spreadsheet->getActiveSheet());
43  } else {
44  $this->objWriter->startElement('table:named-range');
45  $this->writeNamedRange($definedName);
46  }
47 
48  $this->objWriter->endElement();
49  }
50  }
51 
52  private function writeNamedFormula(DefinedName $definedName, Worksheet $defaultWorksheet): void
53  {
54  $this->objWriter->writeAttribute('table:name', $definedName->getName());
55  $this->objWriter->writeAttribute(
56  'table:expression',
57  $this->formulaConvertor->convertFormula($definedName->getValue(), $definedName->getWorksheet()->getTitle())
58  );
59  $this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress(
60  $definedName,
61  "'" . (($definedName->getWorksheet() !== null) ? $definedName->getWorksheet()->getTitle() : $defaultWorksheet->getTitle()) . "'!\$A\$1"
62  ));
63  }
64 
65  private function writeNamedRange(DefinedName $definedName): void
66  {
67  $this->objWriter->writeAttribute('table:name', $definedName->getName());
68  $this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress(
69  $definedName,
70  "'" . $definedName->getWorksheet()->getTitle() . "'!\$A\$1"
71  ));
72  $this->objWriter->writeAttribute('table:cell-range-address', $this->convertAddress($definedName, $definedName->getValue()));
73  }
74 
75  private function convertAddress(DefinedName $definedName, string $address): string
76  {
77  $splitCount = preg_match_all(
79  $address,
80  $splitRanges,
81  PREG_OFFSET_CAPTURE
82  );
83 
84  $lengths = array_map('strlen', array_column($splitRanges[0], 0));
85  $offsets = array_column($splitRanges[0], 1);
86 
87  $worksheets = $splitRanges[2];
88  $columns = $splitRanges[6];
89  $rows = $splitRanges[7];
90 
91  while ($splitCount > 0) {
92  --$splitCount;
93  $length = $lengths[$splitCount];
94  $offset = $offsets[$splitCount];
95  $worksheet = $worksheets[$splitCount][0];
96  $column = $columns[$splitCount][0];
97  $row = $rows[$splitCount][0];
98 
99  $newRange = '';
100  if (empty($worksheet)) {
101  if (($offset === 0) || ($address[$offset - 1] !== ':')) {
102  // We need a worksheet
103  $worksheet = $definedName->getWorksheet()->getTitle();
104  }
105  } else {
106  $worksheet = str_replace("''", "'", trim($worksheet, "'"));
107  }
108  if (!empty($worksheet)) {
109  $newRange = "'" . str_replace("'", "''", $worksheet) . "'.";
110  }
111 
112  if (!empty($column)) {
113  $newRange .= $column;
114  }
115  if (!empty($row)) {
116  $newRange .= $row;
117  }
118 
119  $address = substr($address, 0, $offset) . $newRange . substr($address, $offset + $length);
120  }
121 
122  if (substr($address, 0, 1) === '=') {
123  $address = substr($address, 1);
124  }
125 
126  return $address;
127  }
128 }
writeNamedFormula(DefinedName $definedName, Worksheet $defaultWorksheet)
convertAddress(DefinedName $definedName, string $address)
getValue()
Get range or formula value.
__construct(XMLWriter $objWriter, Spreadsheet $spreadsheet, $formulaConvertor)
$row
$rows
Definition: xhr_table.php:10
if(! $in) $columns
Definition: Utf8Test.php:45