diff --git a/.idea/phpspec.xml b/.idea/phpspec.xml index ec7e1d4..c7cfbc2 100644 --- a/.idea/phpspec.xml +++ b/.idea/phpspec.xml @@ -8,6 +8,9 @@ + + \ No newline at end of file diff --git a/src/ext/spreadsheet/wsutils.php b/src/ext/spreadsheet/wsutils.php index dad2dd9..0f83442 100644 --- a/src/ext/spreadsheet/wsutils.php +++ b/src/ext/spreadsheet/wsutils.php @@ -6,10 +6,10 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class wsutils { static function get_highest_coords(Worksheet $ws): array { - $highestRow = $ws->getHighestRow(); $highestColumnA = $ws->getHighestColumn(); $highestCol = Coordinate::columnIndexFromString($highestColumnA); - return [$highestRow, $highestCol]; + $highestRow = $ws->getHighestRow(); + return [$highestCol, $highestRow]; } /** @@ -24,18 +24,18 @@ class wsutils { const MAX_EMPTY_THRESHOLD = 150; static function compute_max_coords(Worksheet $ws): array { - [$highestRow, $highestCol] = self::get_highest_coords($ws); + [$highestCol, $highestRow] = self::get_highest_coords($ws); - $maxRow = 1; $maxCol = 1; + $maxRow = 1; $maxEmptyRows = self::MAX_EMPTY_THRESHOLD; for ($row = 1; $row <= $highestRow; $row++) { $emptyRow = true; $maxEmptyCols = self::MAX_EMPTY_THRESHOLD; for ($col = 1; $col <= $highestCol; $col++) { $value = null; - if ($ws->cellExists([$col, $row])) { - $value = $ws->getCell([$col, $row])->getValue(); + if ($ws->cellExistsByColumnAndRow($col, $row)) { + $value = $ws->getCellByColumnAndRow($col, $row)->getValue(); } if ($value === null) { $maxEmptyCols--; @@ -54,6 +54,6 @@ class wsutils { $maxEmptyRows = self::MAX_EMPTY_THRESHOLD; } } - return [$maxRow, $maxCol]; + return [$maxCol, $maxRow]; } } diff --git a/src/file/csv/CsvBuilder.php b/src/file/csv/CsvBuilder.php index cef42bd..07f0df6 100644 --- a/src/file/csv/CsvBuilder.php +++ b/src/file/csv/CsvBuilder.php @@ -8,12 +8,7 @@ use nur\sery\web\http; * Class CsvBuilder: construction d'un fichier CSV, pour envoi à l'utilisateur */ class CsvBuilder extends AbstractBuilder { - static function with($builder): self { - if ($builder instanceof self) return $builder; - elseif (is_string($builder)) return new static($builder); - elseif (is_array($builder)) return new static(null, $builder); - else throw ValueException::invalid_type($builder, self::class); - } + use TAbstractBuilder; function __construct(?string $output, ?array $params=null) { $csvFlavour = $params["csv_flavour"] ?? null; diff --git a/src/file/csv/SpreadsheetBuilder.php b/src/file/csv/SsBuilder.php similarity index 62% rename from src/file/csv/SpreadsheetBuilder.php rename to src/file/csv/SsBuilder.php index 5cffa1b..d23740d 100644 --- a/src/file/csv/SpreadsheetBuilder.php +++ b/src/file/csv/SsBuilder.php @@ -1,6 +1,7 @@ ss = new Spreadsheet(); $this->valueBinder = new StringValueBinder(); - $this->wsname = $params["wsname"] ?? null; - parent::__construct($output, $params); + $this->setWsname($params["wsname"] ?? null); } protected Spreadsheet $ss; protected IValueBinder $valueBinder; - protected ?string $wsname; + protected ?Worksheet $ws; - protected ?Worksheet $ws = null; + protected int $nrow; - protected function ws(): Worksheet { - $ws = $this->ws; - if ($ws !== null) return $ws; + const STYLE_ROW = 0, STYLE_HEADER = 1; + + protected int $rowStyle; + + function setWsname(?string $wsname): void { $ss = $this->ss; - $wsname = $this->wsname; + $this->ws = null; + $this->nrow = 0; + $this->rowStyle = self::STYLE_ROW; if ($wsname === null) { $ws = $ss->getActiveSheet(); } else { $ws = $ss->getSheetByName($wsname); - if ($ws === null) { - $ws = $ss->createSheet()->setTitle($wsname); - } } - return $this->ws = $ws; + if ($ws === null) { + $ws = $ss->createSheet()->setTitle($wsname); + } else { + $maxRow = wsutils::compute_max_coords($ws)[1]; + $this->nrow = $maxRow - 1; + } + $this->ws = $ws; } - protected int $nrow = 0; - - const STYLE_HEADER = 0, STYLE_ROW = 1; - - protected int $style = self::STYLE_ROW; - function _write(array $row): void { - $ws = $this->ws(); + $ws = $this->ws; + $styleHeader = $this->rowStyle === self::STYLE_HEADER; $nrow = ++$this->nrow; $ncol = 1; foreach ($row as $col) { - //$cell = $ws->getCell([$ncol++, $nrow]); - $cell = $ws->getCellByColumnAndRow($ncol++, $nrow); - $cell->setValue($col, $this->valueBinder); - switch ($this->style) { - case self::STYLE_HEADER: - $cell->getStyle()->getFont()->setBold(true); - break; - case self::STYLE_ROW: - break; - } + $ws->getCellByColumnAndRow($ncol++, $nrow)->setValue($col, $this->valueBinder); + } + if ($styleHeader) { + $ws->getStyle("$nrow:$nrow")->getFont()->setBold(true); } } function writeHeaders(?array $headers=null): void { - $this->style = self::STYLE_HEADER; + $this->rowStyle = self::STYLE_HEADER; parent::writeHeaders($headers); - $this->style = self::STYLE_ROW; + $this->rowStyle = self::STYLE_ROW; } function _sendContentType(): void { diff --git a/src/file/csv/TAbstractBuilder.php b/src/file/csv/TAbstractBuilder.php new file mode 100644 index 0000000..8e2db7a --- /dev/null +++ b/src/file/csv/TAbstractBuilder.php @@ -0,0 +1,13 @@ +