diff --git a/src/ext/spreadsheet/SpoutBuilder.php b/src/ext/spreadsheet/SpoutBuilder.php index ad0d72b..113d7dc 100644 --- a/src/ext/spreadsheet/SpoutBuilder.php +++ b/src/ext/spreadsheet/SpoutBuilder.php @@ -89,6 +89,7 @@ class SpoutBuilder extends AbstractBuilder { $this->wroteHeaders = false; $this->built = false; } + $wsname ??= $params["wsname"] ?? null; if ($wsname !== null) $ws->setName($wsname); $sheetView = (new SheetView()) ->setFreezeRow(2); diff --git a/src/ext/spreadsheet/SpoutReader.php b/src/ext/spreadsheet/SpoutReader.php index 6e5551a..3df5131 100644 --- a/src/ext/spreadsheet/SpoutReader.php +++ b/src/ext/spreadsheet/SpoutReader.php @@ -11,18 +11,41 @@ class SpoutReader extends AbstractReader { function __construct($input, ?array $params=null) { parent::__construct($input, $params); $this->ssType = $params["ss_type"] ?? null; - $this->wsname = $params["wsname"] ?? static::WSNAME; + $this->allSheets = $params["all_sheets"] ?? true; + $wsname = static::WSNAME; + if ($params !== null && array_key_exists("wsname", $params)) { + # spécifié par l'utilisateur: $allSheets = false + $this->setWsname($params["wsname"]); + } elseif ($wsname !== null) { + # valeur non nulle de la classe: $allSheets = false + $this->setWsname($wsname); + } else { + # pas de valeur définie dans la classe, laisser $allSheets à sa valeur + # actuelle + $this->wsname = null; + } } protected ?string $ssType; + /** @var bool faut-il retourner les lignes de toutes les feuilles? */ + protected bool $allSheets; + + function setAllSheets(bool $allSheets=true): self { + $this->allSheets = $allSheets; + return $this; + } + protected $wsname; /** * @param string|int|null $wsname + * + * NB: appeler cette méthode réinitialise $allSheets à false */ function setWsname($wsname): self { $this->wsname = $wsname; + $this->allSheets = true; return $this; } @@ -40,27 +63,35 @@ class SpoutReader extends AbstractReader { } $ss->open($this->input); try { - $found = false; + $allSheets = $this->allSheets; + $wsname = $this->wsname; + $first = true; foreach ($ss->getSheetIterator() as $ws) { - if ($this->wsname === null || $this->wsname === $ws->getName()) { - $found = true; - break; + if ($allSheets) $found = true; + else $found = $wsname === null || $wsname === $ws->getName(); + if ($found) { + if ($first) { + $first = false; + } else { + yield null; + # on garde le même schéma le cas échéant, mais supprimer headers + # pour permettre son recalcul + $this->headers = null; + } + $this->isrc = $this->idest = 0; + foreach ($ws->getRowIterator() as $row) { + $row = $row->toArray(); + foreach ($row as &$col) { + $this->verifixCol($col); + }; unset($col); + if ($this->cook($row)) { + yield $row; + $this->idest++; + } + $this->isrc++; + } } } - if (!$found) return; - - $this->isrc = $this->idest = 0; - foreach ($ws->getRowIterator() as $row) { - $row = $row->toArray(); - foreach ($row as &$col) { - $this->verifixCol($col); - }; unset($col); - if ($this->cook($row)) { - yield $row; - $this->idest++; - } - $this->isrc++; - } } finally { $ss->close(); }