diff --git a/CHANGELOG.md b/CHANGELOG.md index 00465650..83d2bb22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Added - Implementation of IFNA() logical function +- Support "showZeros" worksheet option to change how Excel shows and handles "null" values returned from a calculation ### Fixed diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 6358866d..2d63a939 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -2813,7 +2813,7 @@ class Calculation } self::$returnArrayAsType = $returnArrayAsType; - if ($result === null) { + if ($result === null && $pCell->getWorksheet()->getSheetView()->getShowZeros()) { return 0; } elseif ((is_float($result)) && ((is_nan($result)) || (is_infinite($result)))) { return Functions::NAN(); diff --git a/src/PhpSpreadsheet/Reader/Xlsx/SheetViews.php b/src/PhpSpreadsheet/Reader/Xlsx/SheetViews.php index 2caaec31..88c01ead 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx/SheetViews.php +++ b/src/PhpSpreadsheet/Reader/Xlsx/SheetViews.php @@ -24,6 +24,7 @@ class SheetViews extends BaseParserClass $this->gridLines(); $this->headers(); $this->direction(); + $this->showZeros(); if (isset($this->sheetViewXml->pane)) { $this->pane(); @@ -92,6 +93,15 @@ class SheetViews extends BaseParserClass } } + private function showZeros() + { + if (isset($this->sheetViewXml['showZeros'])) { + $this->worksheet->getSheetView()->setShowZeros( + self::boolean((string) $this->sheetViewXml['showZeros']) + ); + } + } + private function pane() { $xSplit = 0; diff --git a/src/PhpSpreadsheet/Worksheet/SheetView.php b/src/PhpSpreadsheet/Worksheet/SheetView.php index 17282324..0a95f213 100644 --- a/src/PhpSpreadsheet/Worksheet/SheetView.php +++ b/src/PhpSpreadsheet/Worksheet/SheetView.php @@ -35,6 +35,16 @@ class SheetView */ private $zoomScaleNormal = 100; + /** + * ShowZeros. + * + * If true, "null" values from a calculation will be shown as "0". This is the default Excel behaviour and can be changed + * with the advanced worksheet option "Show a zero in cells that have zero value" + * + * @var bool + */ + private $showZeros = true; + /** * View. * @@ -115,6 +125,24 @@ class SheetView return $this; } + /** + * Set ShowZeroes setting. + * + * @param bool $pValue + */ + public function setShowZeros($pValue) + { + $this->showZeros = $pValue; + } + + /** + * @return bool + */ + public function getShowZeros() + { + return $this->showZeros; + } + /** * Get View. * diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 0086f391..c8e7062d 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -221,6 +221,11 @@ class Worksheet extends WriterPart $objWriter->writeAttribute('zoomScaleNormal', $pSheet->getSheetView()->getZoomScaleNormal()); } + // Show zeros (Excel also writes this attribute only if set to false) + if ($pSheet->getSheetView()->getShowZeros() === false) { + $objWriter->writeAttribute('showZeros', 0); + } + // View Layout Type if ($pSheet->getSheetView()->getView() !== SheetView::SHEETVIEW_NORMAL) { $objWriter->writeAttribute('view', $pSheet->getSheetView()->getView());