From a089a876715060cc48f5df106e0ad04019e31c08 Mon Sep 17 00:00:00 2001 From: Josh Grant Date: Mon, 26 Feb 2018 20:33:47 +0000 Subject: [PATCH] Avoid losing calculated value type Closes #394 --- CHANGELOG.md | 11 ++-- src/PhpSpreadsheet/Cell/Cell.php | 2 +- src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 2 + .../TypeAttributePreservationTest.php | 51 +++++++++++++++++++ .../TypeAttributePreservation/Formula.php | 40 +++++++++++++++ 5 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php create mode 100644 tests/data/Functional/TypeAttributePreservation/Formula.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a60764c..d2622163 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,16 +9,17 @@ 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) +- 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) - Support for `_xlfn.` prefixed functions and `ISFORMULA`, `MODE.SNGL`, `STDEV.S`, `STDEV.P` - [#390](https://github.com/PHPOffice/PhpSpreadsheet/pull/390) ### Fixed -- Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354) -- Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167) -- Use proper € symbol for currency format - [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379) -- Read printing area correctly when skipping some sheets - [#371](https://github.com/PHPOffice/PhpSpreadsheet/issues/371) +- Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354) +- Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167) +- Use proper € symbol for currency format - [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379) +- Read printing area correctly when skipping some sheets - [#371](https://github.com/PHPOffice/PhpSpreadsheet/issues/371) +- Avoid incorrectly overwriting calculated value type - [#394](https://github.com/PHPOffice/PhpSpreadsheet/issues/394) ## [1.1.0] - 2018-01-28 diff --git a/src/PhpSpreadsheet/Cell/Cell.php b/src/PhpSpreadsheet/Cell/Cell.php index 6f4e3bbf..416b4a99 100644 --- a/src/PhpSpreadsheet/Cell/Cell.php +++ b/src/PhpSpreadsheet/Cell/Cell.php @@ -308,7 +308,7 @@ class Cell * Get old calculated value (cached) * This returns the value last calculated by MS Excel or whichever spreadsheet program was used to * create the original spreadsheet file. - * Note that this value is not guaranteed to refelect the actual calculated value because it is + * Note that this value is not guaranteed to reflect the actual calculated value because it is * possible that auto-calculation was disabled in the original spreadsheet, and underlying data * values used by the formula have changed since it was last calculated. * diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index ad8584ab..f551c60d 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -1054,6 +1054,8 @@ class Worksheet extends WriterPart $pCell->getCalculatedValue() : $cellValue; if (is_string($calculatedValue)) { $objWriter->writeAttribute('t', 'str'); + } elseif (is_bool($calculatedValue)) { + $objWriter->writeAttribute('t', 'b'); } break; diff --git a/tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php b/tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php new file mode 100644 index 00000000..53cc718d --- /dev/null +++ b/tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php @@ -0,0 +1,51 @@ +getActiveSheet(); + $sheet->fromArray($values); + + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); + $reloadedSheet = $reloadedSpreadsheet->getActiveSheet(); + + $expected = $sheet->getCell('A1')->getCalculatedValue(); + + if ($sheet->getCell('A1')->getDataType() === 'f') { + $actual = $reloadedSheet->getCell('A1')->getOldCalculatedValue(); + } else { + $actual = $reloadedSheet->getCell('A1')->getValue(); + } + + self::assertSame($expected, $actual); + } +} diff --git a/tests/data/Functional/TypeAttributePreservation/Formula.php b/tests/data/Functional/TypeAttributePreservation/Formula.php new file mode 100644 index 00000000..3f6b144b --- /dev/null +++ b/tests/data/Functional/TypeAttributePreservation/Formula.php @@ -0,0 +1,40 @@ +