summaryrefslogtreecommitdiffstats
path: root/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2024-05-27 13:08:29 +0200
committerAnton Luka Šijanec <anton@sijanec.eu>2024-05-27 13:08:29 +0200
commit75160b12821f7f4299cce7f0b69c83c1502ae071 (patch)
tree27e25e4ccaef45f0c58b22831164050d1af1d4db /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php
parentprvi-commit (diff)
download1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.gz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.bz2
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.lz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.xz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.zst
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.zip
Diffstat (limited to 'vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php')
-rw-r--r--vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php126
1 files changed, 126 insertions, 0 deletions
diff --git a/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php b/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php
new file mode 100644
index 0000000..8e9a896
--- /dev/null
+++ b/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php
@@ -0,0 +1,126 @@
+<?php
+
+namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
+
+use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
+use PhpOffice\PhpSpreadsheet\DefinedName;
+use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
+
+class NamedExpressions
+{
+ private $objWriter;
+
+ private $spreadsheet;
+
+ private $formulaConvertor;
+
+ public function __construct(XMLWriter $objWriter, Spreadsheet $spreadsheet, $formulaConvertor)
+ {
+ $this->objWriter = $objWriter;
+ $this->spreadsheet = $spreadsheet;
+ $this->formulaConvertor = $formulaConvertor;
+ }
+
+ public function write(): void
+ {
+ $this->objWriter->startElement('table:named-expressions');
+ $this->writeExpressions();
+ $this->objWriter->endElement();
+ }
+
+ private function writeExpressions(): void
+ {
+ $definedNames = $this->spreadsheet->getDefinedNames();
+
+ foreach ($definedNames as $definedName) {
+ if ($definedName->isFormula()) {
+ $this->objWriter->startElement('table:named-expression');
+ $this->writeNamedFormula($definedName, $this->spreadsheet->getActiveSheet());
+ } else {
+ $this->objWriter->startElement('table:named-range');
+ $this->writeNamedRange($definedName);
+ }
+
+ $this->objWriter->endElement();
+ }
+ }
+
+ private function writeNamedFormula(DefinedName $definedName, Worksheet $defaultWorksheet): void
+ {
+ $this->objWriter->writeAttribute('table:name', $definedName->getName());
+ $this->objWriter->writeAttribute(
+ 'table:expression',
+ $this->formulaConvertor->convertFormula($definedName->getValue(), $definedName->getWorksheet()->getTitle())
+ );
+ $this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress(
+ $definedName,
+ "'" . (($definedName->getWorksheet() !== null) ? $definedName->getWorksheet()->getTitle() : $defaultWorksheet->getTitle()) . "'!\$A\$1"
+ ));
+ }
+
+ private function writeNamedRange(DefinedName $definedName): void
+ {
+ $this->objWriter->writeAttribute('table:name', $definedName->getName());
+ $this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress(
+ $definedName,
+ "'" . $definedName->getWorksheet()->getTitle() . "'!\$A\$1"
+ ));
+ $this->objWriter->writeAttribute('table:cell-range-address', $this->convertAddress($definedName, $definedName->getValue()));
+ }
+
+ private function convertAddress(DefinedName $definedName, string $address): string
+ {
+ $splitCount = preg_match_all(
+ '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui',
+ $address,
+ $splitRanges,
+ PREG_OFFSET_CAPTURE
+ );
+
+ $lengths = array_map('strlen', array_column($splitRanges[0], 0));
+ $offsets = array_column($splitRanges[0], 1);
+
+ $worksheets = $splitRanges[2];
+ $columns = $splitRanges[6];
+ $rows = $splitRanges[7];
+
+ while ($splitCount > 0) {
+ --$splitCount;
+ $length = $lengths[$splitCount];
+ $offset = $offsets[$splitCount];
+ $worksheet = $worksheets[$splitCount][0];
+ $column = $columns[$splitCount][0];
+ $row = $rows[$splitCount][0];
+
+ $newRange = '';
+ if (empty($worksheet)) {
+ if (($offset === 0) || ($address[$offset - 1] !== ':')) {
+ // We need a worksheet
+ $worksheet = $definedName->getWorksheet()->getTitle();
+ }
+ } else {
+ $worksheet = str_replace("''", "'", trim($worksheet, "'"));
+ }
+ if (!empty($worksheet)) {
+ $newRange = "'" . str_replace("'", "''", $worksheet) . "'.";
+ }
+
+ if (!empty($column)) {
+ $newRange .= $column;
+ }
+ if (!empty($row)) {
+ $newRange .= $row;
+ }
+
+ $address = substr($address, 0, $offset) . $newRange . substr($address, $offset + $length);
+ }
+
+ if (substr($address, 0, 1) === '=') {
+ $address = substr($address, 1);
+ }
+
+ return $address;
+ }
+}