60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
|
<?php
|
||
|
namespace nur\sery\ext\spreadsheet;
|
||
|
|
||
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||
|
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];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @var int nombre de colonnes/lignes au bout desquels on arrête de chercher
|
||
|
* si on n'a trouvé que des cellules vides.
|
||
|
*
|
||
|
* c'est nécessaire à cause de certains fichiers provenant d'Excel que j'ai
|
||
|
* reçus qui ont jusqu'à 10000 colonne vides et/ou 1048576 lignes vides. un
|
||
|
* algorithme "bête" perd énormément de temps à chercher dans le vide, donnant
|
||
|
* l'impression que le processus a planté.
|
||
|
*/
|
||
|
const MAX_EMPTY_THRESHOLD = 150;
|
||
|
|
||
|
static function compute_max_coords(Worksheet $ws): array {
|
||
|
[$highestRow, $highestCol] = self::get_highest_coords($ws);
|
||
|
|
||
|
$maxRow = 1;
|
||
|
$maxCol = 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 ($value === null) {
|
||
|
$maxEmptyCols--;
|
||
|
if ($maxEmptyCols == 0) break;
|
||
|
} else {
|
||
|
$maxEmptyCols = self::MAX_EMPTY_THRESHOLD;
|
||
|
if ($row > $maxRow) $maxRow = $row;
|
||
|
if ($col > $maxCol) $maxCol = $col;
|
||
|
$emptyRow = false;
|
||
|
}
|
||
|
}
|
||
|
if ($emptyRow) {
|
||
|
$maxEmptyRows--;
|
||
|
if ($maxEmptyRows == 0) break;
|
||
|
} else {
|
||
|
$maxEmptyRows = self::MAX_EMPTY_THRESHOLD;
|
||
|
}
|
||
|
}
|
||
|
return [$maxRow, $maxCol];
|
||
|
}
|
||
|
}
|