From 2e37578971fea47644921d61e1887a7cc30df5c1 Mon Sep 17 00:00:00 2001 From: Jan-Sverre Riksfjord Date: Mon, 29 Jan 2018 08:24:13 +0100 Subject: [PATCH] Fix zoom scale problems on reading bad xlsx files Some computer programs will output xlsx files that do not compare 100% to the standards. Excel will open the file without any problem. setZoomScaleNormal() should throw exception when manually setting the scale to less than or equals 0, but when reading files, we should be able to read a file with such error, as Excel does. Closes #350 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Reader/Xlsx.php | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88f8ad6b..8d4bf4e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - HTML writer creates a generator meta tag - [#312](https://github.com/PHPOffice/PhpSpreadsheet/issues/312) +- Support invalid zoom value in XLSX format - [#350](https://github.com/PHPOffice/PhpSpreadsheet/pull/350) ### Fixed diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 41b8f7b5..9eddf7f1 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -702,10 +702,24 @@ class Xlsx extends BaseReader if (isset($xmlSheet->sheetViews, $xmlSheet->sheetViews->sheetView)) { if (isset($xmlSheet->sheetViews->sheetView['zoomScale'])) { - $docSheet->getSheetView()->setZoomScale((int) ($xmlSheet->sheetViews->sheetView['zoomScale'])); + $zoomScale = (int) ($xmlSheet->sheetViews->sheetView['zoomScale']); + if ($zoomScale <= 0) { + // setZoomScale will throw an Exception if the scale is less than or equals 0 + // that is OK when manually creating documents, but we should be able to read all documents + $zoomScale = 100; + } + + $docSheet->getSheetView()->setZoomScale($zoomScale); } if (isset($xmlSheet->sheetViews->sheetView['zoomScaleNormal'])) { - $docSheet->getSheetView()->setZoomScaleNormal((int) ($xmlSheet->sheetViews->sheetView['zoomScaleNormal'])); + $zoomScaleNormal = (int) ($xmlSheet->sheetViews->sheetView['zoomScaleNormal']); + if ($zoomScaleNormal <= 0) { + // setZoomScaleNormal will throw an Exception if the scale is less than or equals 0 + // that is OK when manually creating documents, but we should be able to read all documents + $zoomScaleNormal = 100; + } + + $docSheet->getSheetView()->setZoomScaleNormal($zoomScaleNormal); } if (isset($xmlSheet->sheetViews->sheetView['view'])) { $docSheet->getSheetView()->setView((string) $xmlSheet->sheetViews->sheetView['view']);