Avoid losing calculated value type

Closes #394
This commit is contained in:
Josh Grant 2018-02-26 20:33:47 +00:00 committed by Adrien Crivelli
parent 148bee1991
commit a089a87671
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
5 changed files with 100 additions and 6 deletions

View File

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- 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

View File

@ -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.
*

View File

@ -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;

View File

@ -0,0 +1,51 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class TypeAttributePreservationTest extends AbstractFunctional
{
public function providerFormulae()
{
$formats = ['Xlsx'];
$data = require 'data/Functional/TypeAttributePreservation/Formula.php';
$result = [];
foreach ($formats as $f) {
foreach ($data as $d) {
$result[] = [$f, $d];
}
}
return $result;
}
/**
* Ensure saved spreadsheets maintain the correct data type.
*
* @dataProvider providerFormulae
*
* @param string $format
* @param array $values
*/
public function testFormulae($format, array $values)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->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);
}
}

View File

@ -0,0 +1,40 @@
<?php
return [
[
['string'],
],
[
['="string"'],
],
[
[1],
],
[
[0],
],
[
[true],
],
[
[false],
],
[
['=TRUE()'],
],
[
['=ISFORMULA(B1)', '=1+2'],
],
[
['1'],
],
[
['0'],
],
[
['null'],
],
[
[null],
],
];