Loading src/Calc.php +60 −23 Changes for src/Calc.php: 60 added lines, 23 removed lines. Original line number Diff line number Diff line Loading @@ -2,13 +2,8 @@ namespace Unicaen\OpenDocument; use Exception; use DOMDocument; use DOMElement; use DOMNode; use DOMNodeList; use Unicaen\OpenDocument\Calc\Cell; use ZipArchive; use Unicaen\OpenDocument\Calc\Sheet; class Calc { Loading @@ -24,9 +19,9 @@ class Calc private $document; /** * @var Cell[] * @var Sheet[] */ private $cells; private array $sheets = []; Loading Loading @@ -54,23 +49,65 @@ class Calc public function read() public function getSheet(int $index): Sheet { $content = $this->getDocument()->getContent(); $spreadsheet = $this->getDocument()->find( $content, 'office:spreadsheet')[0]; $table = $this->getDocument()->find($spreadsheet, 'table:table')[0]; $rcs = $table->childNodes; foreach( $rcs as $rc ){ switch($rc->tagName){ case 'office:forms': break; case 'table:table-column': break; case 'table:table-row': xmlDump($rc); break; if (!isset($this->sheets[$index])) { $this->sheets[$index] = new Sheet($this, $index); } return $this->sheets[$index]; } public static function numberToLetter(int $number): string { $sl = floor(($number - 1) / 26); $tl = floor(($sl - 1) / 26); $ql = floor(($tl - 1) / 26); $letter = ''; if ($ql > 0) $letter .= chr((($ql - 1) % 26) + 65); if ($tl > 0) $letter .= chr((($tl - 1) % 26) + 65); if ($sl > 0) $letter .= chr((($sl - 1) % 26) + 65); if ($number > 0) $letter .= chr((($number - 1) % 26) + 65); return $letter; } public static function letterToNumber(string $letter): int { $len = strlen($letter); $number = 0; for ($i = 0; $i < $len; $i++) { $number += (ord(substr($letter, $i, 1)) - 64) * pow(26, $len - $i - 1); } return $number; } public static function cellNameToCoords(string $name): array { $col = ''; $row = ''; for ($i = 0; $i < strlen($name); $i++) { $chr = substr($name, $i, 1); $num = ord($chr); if ($num > 64 && $num < 65 + 26) { $col .= $chr; } else { $row .= $chr; } } return [ 'col' => self::letterToNumber($col), 'row' => (int)$row, ]; } } No newline at end of file src/Calc/Cell.php +217 −3 Changes for src/Calc/Cell.php: 217 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -2,18 +2,25 @@ namespace Unicaen\OpenDocument\Calc; use Unicaen\OpenDocument\Calc; class Cell { /** * @var int */ private $line; private $row; /** * @var int */ private $col; /*** * @var string */ private string $type; /** * @var string */ Loading @@ -24,11 +31,218 @@ class Cell */ private $formule; /** * @var mixed */ private $value; /** * @var int */ private int $colSpan = 1; /** * @var int */ private int $rowSpan = 1; /** * @return int */ public function getRow(): int { return $this->row; } /** * @param int $row * * @return Cell */ public function setRow(int $row): Cell { $this->row = $row; return $this; } /** * @return int */ public function getCol(): int { return $this->col; } public function __construct( int $line, int $col ) /** * @param int $col * * @return Cell */ public function setCol(int $col): Cell { $this->line = $line; $this->col = $col; return $this; } /** * @return string */ public function getType(): string { return $this->type; } /** * @param string $type * * @return Cell */ public function setType(string $type): Cell { $this->type = $type; return $this; } /** * @return string */ public function getContent(): string { return $this->content; } /** * @param string $content * * @return Cell */ public function setContent(string $content): Cell { $this->content = $content; return $this; } /** * @return string */ public function getFormule(): ?string { return $this->formule; } /** * @param string $formule * * @return Cell */ public function setFormule(string $formule): Cell { $this->formule = $formule; return $this; } /** * @return mixed */ public function getValue() { return $this->value; } /** * @param mixed $value * * @return Cell */ public function setValue($value) { $this->value = $value; return $this; } /** * @return int */ public function getColSpan(): int { return $this->colSpan; } /** * @param int $colSpan * * @return Cell */ public function setColSpan(int $colSpan): Cell { $this->colSpan = $colSpan; return $this; } /** * @return int */ public function getRowSpan(): int { return $this->rowSpan; } /** * @param int $rowSpan * * @return Cell */ public function setRowSpan(int $rowSpan): Cell { $this->rowSpan = $rowSpan; return $this; } public function getName() { return Calc::numberToLetter($this->getCol()).$this->getRow(); } } No newline at end of file src/Calc/Sheet.php 0 → 100644 +232 −0 Changes for src/Calc/Sheet.php: 232 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Unicaen\OpenDocument\Calc; use Unicaen\OpenDocument\Calc; use DOMNode; class Sheet { /** * @var Calc */ private Calc $calc; /** * @var int */ private int $index; /** * @var Cell[][] */ private $cellTab; /** * @var Cell[] */ private $cells; /** * @var int */ private $maxCol = 0; /** * @var int */ private $maxRow = 0; public function __construct(Calc $calc, int $index) { $this->calc = $calc; $this->index = $index; } public function read() { $content = $this->calc->getDocument()->getContent(); $spreadsheet = $this->calc->getDocument()->find($content, 'office:spreadsheet')[0]; $feuille = $this->calc->getDocument()->find($spreadsheet, 'table:table')[$this->index]; $this->cellTab = []; $this->cells = []; $this->maxRow = 0; $this->maxCol = 0; $rowIndex = 1; $rcs = $feuille->childNodes; foreach ($rcs as $rc) { switch ($rc->tagName) { case 'office:forms': break; case 'table:table-column': break; case 'table:table-row': $this->readRow($rc, $rowIndex++); break; } } } protected function readRow(DOMNode $rowNode, int $row) { $cells = $rowNode->childNodes; $col = 1; foreach ($cells as $c) { if ('table:table-cell' == $c->tagName) { $cell = $this->readCell($c); $init = true; for ($c = 0; $c < $cell->getColSpan(); $c++) { while ($this->hasCellByCoords($col, $row)) { $col++; } for ($r = 0; $r < $cell->getRowSpan(); $r++) { if (!isset($this->cellTab[$col])) { $this->cellTab[$col] = []; } $this->cellTab[$col][$row + $r] = $cell; if ($row + $r > $this->maxRow) $this->maxRow = $row + $r; if ($col > $this->maxCol) $this->maxCol = $col; if ($init) { $cell->setCol($col); $cell->setRow($row + $r); $this->cells[$cell->getName()] = $cell; $init = false; } } } } } } protected function readCell(DOMNode $cell): Cell { $c = new Cell; if ($cell->hasAttributes()) { $colSpan = $cell->attributes->getNamedItem('number-columns-spanned'); if ($colSpan) $c->setColSpan((int)$colSpan->value); $rowSpan = $cell->attributes->getNamedItem('number-rows-spanned'); if ($rowSpan) $c->setRowSpan((int)$rowSpan->value); $formule = $cell->attributes->getNamedItem('formula'); if ($formule) $c->setFormule($formule->value); $value = $cell->attributes->getNamedItem('value'); if ($value) $c->setValue($value->value); $type = $cell->attributes->getNamedItem('value-type'); if ($type) $c->setType($type->value); } $c->setContent($cell->textContent); return $c; } public function getCells(): array { return $this->cells; } public function getRefCells(Cell $cell, $recursive=false): array { $refs = []; $formule = $cell->getFormule(); foreach( $this->cells as $n => $c){ $row = $c->getRow(); $col = Calc::numberToLetter($c->getCol()); $variants = [ "[.$col$row]", "[.\$$col$row]", "[.$col\$$row]", "[.\$$col\$$row]", ]; foreach( $variants as $vn ){ if (false !== strpos($formule, $vn)){ $refs[$n] = $c; } } } if (true === $recursive){ foreach($refs as $ref){ $rRefs = $this->getRefCells($ref, true); foreach( $rRefs as $rn => $rc ){ $refs[$rn] = $rc; } } } return $refs; } public function getMaxCol(): int { return $this->maxCol; } public function getMaxRow(): int { return $this->maxRow; } public function hasCellByCoords(int $col, int $row): bool { return array_key_exists($col, $this->cellTab) && array_key_exists($row, $this->cellTab[$col]); } public function getCellByCoords(int $col, int $row): ?Cell { if ($this->hasCellByCoords($col, $row)) { return $this->cellTab[$col][$row]; } return null; } public function hasCell(string $name): bool { return array_key_exists($name, $this->cells); } public function getCell(string $name): ?Cell { if ($this->hasCell($name)) { return $this->cells[$name]; } return null; } } No newline at end of file src/Document.php +20 −0 Changes for src/Document.php: 20 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -28,6 +28,11 @@ class Document */ private $publisher; /** * @var Calc */ private $calc; /** * @var Stylist */ Loading Loading @@ -532,6 +537,21 @@ class Document /** * @return Publisher */ public function getCalc(): Calc { if (!$this->calc) { $this->calc = new Calc(); $this->calc->setDocument($this); } return $this->calc; } /** * @return Stylist */ Loading Loading
src/Calc.php +60 −23 Changes for src/Calc.php: 60 added lines, 23 removed lines. Original line number Diff line number Diff line Loading @@ -2,13 +2,8 @@ namespace Unicaen\OpenDocument; use Exception; use DOMDocument; use DOMElement; use DOMNode; use DOMNodeList; use Unicaen\OpenDocument\Calc\Cell; use ZipArchive; use Unicaen\OpenDocument\Calc\Sheet; class Calc { Loading @@ -24,9 +19,9 @@ class Calc private $document; /** * @var Cell[] * @var Sheet[] */ private $cells; private array $sheets = []; Loading Loading @@ -54,23 +49,65 @@ class Calc public function read() public function getSheet(int $index): Sheet { $content = $this->getDocument()->getContent(); $spreadsheet = $this->getDocument()->find( $content, 'office:spreadsheet')[0]; $table = $this->getDocument()->find($spreadsheet, 'table:table')[0]; $rcs = $table->childNodes; foreach( $rcs as $rc ){ switch($rc->tagName){ case 'office:forms': break; case 'table:table-column': break; case 'table:table-row': xmlDump($rc); break; if (!isset($this->sheets[$index])) { $this->sheets[$index] = new Sheet($this, $index); } return $this->sheets[$index]; } public static function numberToLetter(int $number): string { $sl = floor(($number - 1) / 26); $tl = floor(($sl - 1) / 26); $ql = floor(($tl - 1) / 26); $letter = ''; if ($ql > 0) $letter .= chr((($ql - 1) % 26) + 65); if ($tl > 0) $letter .= chr((($tl - 1) % 26) + 65); if ($sl > 0) $letter .= chr((($sl - 1) % 26) + 65); if ($number > 0) $letter .= chr((($number - 1) % 26) + 65); return $letter; } public static function letterToNumber(string $letter): int { $len = strlen($letter); $number = 0; for ($i = 0; $i < $len; $i++) { $number += (ord(substr($letter, $i, 1)) - 64) * pow(26, $len - $i - 1); } return $number; } public static function cellNameToCoords(string $name): array { $col = ''; $row = ''; for ($i = 0; $i < strlen($name); $i++) { $chr = substr($name, $i, 1); $num = ord($chr); if ($num > 64 && $num < 65 + 26) { $col .= $chr; } else { $row .= $chr; } } return [ 'col' => self::letterToNumber($col), 'row' => (int)$row, ]; } } No newline at end of file
src/Calc/Cell.php +217 −3 Changes for src/Calc/Cell.php: 217 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -2,18 +2,25 @@ namespace Unicaen\OpenDocument\Calc; use Unicaen\OpenDocument\Calc; class Cell { /** * @var int */ private $line; private $row; /** * @var int */ private $col; /*** * @var string */ private string $type; /** * @var string */ Loading @@ -24,11 +31,218 @@ class Cell */ private $formule; /** * @var mixed */ private $value; /** * @var int */ private int $colSpan = 1; /** * @var int */ private int $rowSpan = 1; /** * @return int */ public function getRow(): int { return $this->row; } /** * @param int $row * * @return Cell */ public function setRow(int $row): Cell { $this->row = $row; return $this; } /** * @return int */ public function getCol(): int { return $this->col; } public function __construct( int $line, int $col ) /** * @param int $col * * @return Cell */ public function setCol(int $col): Cell { $this->line = $line; $this->col = $col; return $this; } /** * @return string */ public function getType(): string { return $this->type; } /** * @param string $type * * @return Cell */ public function setType(string $type): Cell { $this->type = $type; return $this; } /** * @return string */ public function getContent(): string { return $this->content; } /** * @param string $content * * @return Cell */ public function setContent(string $content): Cell { $this->content = $content; return $this; } /** * @return string */ public function getFormule(): ?string { return $this->formule; } /** * @param string $formule * * @return Cell */ public function setFormule(string $formule): Cell { $this->formule = $formule; return $this; } /** * @return mixed */ public function getValue() { return $this->value; } /** * @param mixed $value * * @return Cell */ public function setValue($value) { $this->value = $value; return $this; } /** * @return int */ public function getColSpan(): int { return $this->colSpan; } /** * @param int $colSpan * * @return Cell */ public function setColSpan(int $colSpan): Cell { $this->colSpan = $colSpan; return $this; } /** * @return int */ public function getRowSpan(): int { return $this->rowSpan; } /** * @param int $rowSpan * * @return Cell */ public function setRowSpan(int $rowSpan): Cell { $this->rowSpan = $rowSpan; return $this; } public function getName() { return Calc::numberToLetter($this->getCol()).$this->getRow(); } } No newline at end of file
src/Calc/Sheet.php 0 → 100644 +232 −0 Changes for src/Calc/Sheet.php: 232 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Unicaen\OpenDocument\Calc; use Unicaen\OpenDocument\Calc; use DOMNode; class Sheet { /** * @var Calc */ private Calc $calc; /** * @var int */ private int $index; /** * @var Cell[][] */ private $cellTab; /** * @var Cell[] */ private $cells; /** * @var int */ private $maxCol = 0; /** * @var int */ private $maxRow = 0; public function __construct(Calc $calc, int $index) { $this->calc = $calc; $this->index = $index; } public function read() { $content = $this->calc->getDocument()->getContent(); $spreadsheet = $this->calc->getDocument()->find($content, 'office:spreadsheet')[0]; $feuille = $this->calc->getDocument()->find($spreadsheet, 'table:table')[$this->index]; $this->cellTab = []; $this->cells = []; $this->maxRow = 0; $this->maxCol = 0; $rowIndex = 1; $rcs = $feuille->childNodes; foreach ($rcs as $rc) { switch ($rc->tagName) { case 'office:forms': break; case 'table:table-column': break; case 'table:table-row': $this->readRow($rc, $rowIndex++); break; } } } protected function readRow(DOMNode $rowNode, int $row) { $cells = $rowNode->childNodes; $col = 1; foreach ($cells as $c) { if ('table:table-cell' == $c->tagName) { $cell = $this->readCell($c); $init = true; for ($c = 0; $c < $cell->getColSpan(); $c++) { while ($this->hasCellByCoords($col, $row)) { $col++; } for ($r = 0; $r < $cell->getRowSpan(); $r++) { if (!isset($this->cellTab[$col])) { $this->cellTab[$col] = []; } $this->cellTab[$col][$row + $r] = $cell; if ($row + $r > $this->maxRow) $this->maxRow = $row + $r; if ($col > $this->maxCol) $this->maxCol = $col; if ($init) { $cell->setCol($col); $cell->setRow($row + $r); $this->cells[$cell->getName()] = $cell; $init = false; } } } } } } protected function readCell(DOMNode $cell): Cell { $c = new Cell; if ($cell->hasAttributes()) { $colSpan = $cell->attributes->getNamedItem('number-columns-spanned'); if ($colSpan) $c->setColSpan((int)$colSpan->value); $rowSpan = $cell->attributes->getNamedItem('number-rows-spanned'); if ($rowSpan) $c->setRowSpan((int)$rowSpan->value); $formule = $cell->attributes->getNamedItem('formula'); if ($formule) $c->setFormule($formule->value); $value = $cell->attributes->getNamedItem('value'); if ($value) $c->setValue($value->value); $type = $cell->attributes->getNamedItem('value-type'); if ($type) $c->setType($type->value); } $c->setContent($cell->textContent); return $c; } public function getCells(): array { return $this->cells; } public function getRefCells(Cell $cell, $recursive=false): array { $refs = []; $formule = $cell->getFormule(); foreach( $this->cells as $n => $c){ $row = $c->getRow(); $col = Calc::numberToLetter($c->getCol()); $variants = [ "[.$col$row]", "[.\$$col$row]", "[.$col\$$row]", "[.\$$col\$$row]", ]; foreach( $variants as $vn ){ if (false !== strpos($formule, $vn)){ $refs[$n] = $c; } } } if (true === $recursive){ foreach($refs as $ref){ $rRefs = $this->getRefCells($ref, true); foreach( $rRefs as $rn => $rc ){ $refs[$rn] = $rc; } } } return $refs; } public function getMaxCol(): int { return $this->maxCol; } public function getMaxRow(): int { return $this->maxRow; } public function hasCellByCoords(int $col, int $row): bool { return array_key_exists($col, $this->cellTab) && array_key_exists($row, $this->cellTab[$col]); } public function getCellByCoords(int $col, int $row): ?Cell { if ($this->hasCellByCoords($col, $row)) { return $this->cellTab[$col][$row]; } return null; } public function hasCell(string $name): bool { return array_key_exists($name, $this->cells); } public function getCell(string $name): ?Cell { if ($this->hasCell($name)) { return $this->cells[$name]; } return null; } } No newline at end of file
src/Document.php +20 −0 Changes for src/Document.php: 20 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -28,6 +28,11 @@ class Document */ private $publisher; /** * @var Calc */ private $calc; /** * @var Stylist */ Loading Loading @@ -532,6 +537,21 @@ class Document /** * @return Publisher */ public function getCalc(): Calc { if (!$this->calc) { $this->calc = new Calc(); $this->calc->setDocument($this); } return $this->calc; } /** * @return Stylist */ Loading