Skip to content

Commit

Permalink
Avoid losing calculated value type
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-G authored and Frederic Delaunay committed Oct 29, 2018
1 parent 078a688 commit 835dead
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function setCalculatedValue($pValue)
* 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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,8 @@ private function writeCell(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet
$pCell->getCalculatedValue() : $cellValue;
if (is_string($calculatedValue)) {
$objWriter->writeAttribute('t', 'str');
} elseif (is_bool($calculatedValue)) {
$objWriter->writeAttribute('t', 'b');
}

break;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
40 changes: 40 additions & 0 deletions tests/data/Functional/TypeAttributePreservation/Formula.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

return [
[
['string'],
],
[
['="string"'],
],
[
[1],
],
[
[0],
],
[
[true],
],
[
[false],
],
[
['=TRUE()'],
],
[
['=ISFORMULA(B1)', '=1+2'],
],
[
['1'],
],
[
['0'],
],
[
['null'],
],
[
[null],
],
];

0 comments on commit 835dead

Please sign in to comment.