From 43b760501a9d78e8cb818e079b5679054ad746f0 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 28 Oct 2019 20:37:12 +0100 Subject: [PATCH 1/2] Text data locale for fixedformat (#1220) * Apply Locale settings to result of FIXEDFORMAT method call --- src/PhpSpreadsheet/Calculation/TextData.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Calculation/TextData.php b/src/PhpSpreadsheet/Calculation/TextData.php index 050c1a9b..ea00ef1c 100644 --- a/src/PhpSpreadsheet/Calculation/TextData.php +++ b/src/PhpSpreadsheet/Calculation/TextData.php @@ -273,7 +273,12 @@ class TextData $decimals = 0; } if (!$no_commas) { - $valueResult = number_format($valueResult, $decimals); + $valueResult = number_format( + $valueResult, + $decimals, + StringHelper::getDecimalSeparator(), + StringHelper::getThousandsSeparator() + ); } return (string) $valueResult; From 55209424b2cc70585d595b48150a5b6cb5e24026 Mon Sep 17 00:00:00 2001 From: Jens Hassler Date: Mon, 28 Oct 2019 21:52:30 +0100 Subject: [PATCH 2/2] support "showZeros" setting in Excel advanced worksheet options (#1199) * support "showZeros" setting in Excel advanced worksheet options * add changelog entry * change isShowZeros to getShowZeros --- CHANGELOG.md | 1 + .../Calculation/Calculation.php | 2 +- src/PhpSpreadsheet/Reader/Xlsx/SheetViews.php | 10 +++++++ src/PhpSpreadsheet/Worksheet/SheetView.php | 28 +++++++++++++++++++ src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 5 ++++ 5 files changed, 45 insertions(+), 1 deletion(-) 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());