Some optimisation and refactoring of the code logic for evaluating a cell when accessed via the toArray() methods

This commit is contained in:
MarkBaker
2023-03-30 23:34:03 +02:00
parent 41fd431acc
commit 5bd3be8190
+33 -26
View File
@@ -2988,6 +2988,37 @@ class Worksheet implements IComparable
return $this;
}
/**
* @param mixed $nullValue
*
* @throws Exception
* @throws \PhpOffice\PhpSpreadsheet\Calculation\Exception
*
* @return mixed
*/
protected function cellToArray(Cell $cell, bool $calculateFormulas, bool $formatData, $nullValue)
{
$returnValue = $nullValue;
if ($cell->getValue() !== null) {
if ($cell->getValue() instanceof RichText) {
$returnValue = $cell->getValue()->getPlainText();
} else {
$returnValue = ($calculateFormulas) ? $cell->getCalculatedValue() : $cell->getValue();
}
if ($formatData) {
$style = $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex());
$returnValue = NumberFormat::toFormattedString(
$returnValue,
$style->getNumberFormat()->getFormatCode() ?? NumberFormat::FORMAT_GENERAL
);
}
}
return $returnValue;
}
/**
* Create array from a range of cells.
*
@@ -3035,33 +3066,9 @@ class Worksheet implements IComparable
// Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen
// so we test and retrieve directly against cellCollection
$cell = $this->cellCollection->get("{$col}{$row}");
$returnValue[$rowRef][$columnRef] = $nullValue;
if ($cell !== null) {
// Cell exists
if ($cell->getValue() !== null) {
if ($cell->getValue() instanceof RichText) {
$returnValue[$rowRef][$columnRef] = $cell->getValue()->getPlainText();
} else {
if ($calculateFormulas) {
$returnValue[$rowRef][$columnRef] = $cell->getCalculatedValue();
} else {
$returnValue[$rowRef][$columnRef] = $cell->getValue();
}
}
if ($formatData) {
$style = $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex());
$returnValue[$rowRef][$columnRef] = NumberFormat::toFormattedString(
$returnValue[$rowRef][$columnRef],
$style->getNumberFormat()->getFormatCode() ?? NumberFormat::FORMAT_GENERAL
);
}
} else {
// Cell holds a NULL
$returnValue[$rowRef][$columnRef] = $nullValue;
}
} else {
// Cell doesn't exist
$returnValue[$rowRef][$columnRef] = $nullValue;
$returnValue[$rowRef][$columnRef] = $this->cellToArray($cell, $calculateFormulas, $formatData, $nullValue);
}
}
}