Skip to content

Commit a089a87

Browse files
Josh-GPowerKiKi
authored andcommitted
Avoid losing calculated value type
Closes #394
1 parent 148bee1 commit a089a87

File tree

5 files changed

+100
-6
lines changed

5 files changed

+100
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12-
- HTML writer creates a generator meta tag - [#312](https://github.com/PHPOffice/PhpSpreadsheet/issues/312)
12+
- HTML writer creates a generator meta tag - [#312](https://github.com/PHPOffice/PhpSpreadsheet/issues/312)
1313
- Support invalid zoom value in XLSX format - [#350](https://github.com/PHPOffice/PhpSpreadsheet/pull/350)
1414
- Support for `_xlfn.` prefixed functions and `ISFORMULA`, `MODE.SNGL`, `STDEV.S`, `STDEV.P` - [#390](https://github.com/PHPOffice/PhpSpreadsheet/pull/390)
1515

1616
### Fixed
1717

18-
- Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354)
19-
- Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167)
20-
- Use proper € symbol for currency format - [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379)
21-
- Read printing area correctly when skipping some sheets - [#371](https://github.com/PHPOffice/PhpSpreadsheet/issues/371)
18+
- Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354)
19+
- Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167)
20+
- Use proper € symbol for currency format - [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379)
21+
- Read printing area correctly when skipping some sheets - [#371](https://github.com/PHPOffice/PhpSpreadsheet/issues/371)
22+
- Avoid incorrectly overwriting calculated value type - [#394](https://github.com/PHPOffice/PhpSpreadsheet/issues/394)
2223

2324
## [1.1.0] - 2018-01-28
2425

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function setCalculatedValue($pValue)
308308
* Get old calculated value (cached)
309309
* This returns the value last calculated by MS Excel or whichever spreadsheet program was used to
310310
* create the original spreadsheet file.
311-
* Note that this value is not guaranteed to refelect the actual calculated value because it is
311+
* Note that this value is not guaranteed to reflect the actual calculated value because it is
312312
* possible that auto-calculation was disabled in the original spreadsheet, and underlying data
313313
* values used by the formula have changed since it was last calculated.
314314
*

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ private function writeCell(XMLWriter $objWriter, PhpspreadsheetWorksheet $pSheet
10541054
$pCell->getCalculatedValue() : $cellValue;
10551055
if (is_string($calculatedValue)) {
10561056
$objWriter->writeAttribute('t', 'str');
1057+
} elseif (is_bool($calculatedValue)) {
1058+
$objWriter->writeAttribute('t', 'b');
10571059
}
10581060

10591061
break;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Functional;
4+
5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
7+
class TypeAttributePreservationTest extends AbstractFunctional
8+
{
9+
public function providerFormulae()
10+
{
11+
$formats = ['Xlsx'];
12+
$data = require 'data/Functional/TypeAttributePreservation/Formula.php';
13+
14+
$result = [];
15+
foreach ($formats as $f) {
16+
foreach ($data as $d) {
17+
$result[] = [$f, $d];
18+
}
19+
}
20+
21+
return $result;
22+
}
23+
24+
/**
25+
* Ensure saved spreadsheets maintain the correct data type.
26+
*
27+
* @dataProvider providerFormulae
28+
*
29+
* @param string $format
30+
* @param array $values
31+
*/
32+
public function testFormulae($format, array $values)
33+
{
34+
$spreadsheet = new Spreadsheet();
35+
$sheet = $spreadsheet->getActiveSheet();
36+
$sheet->fromArray($values);
37+
38+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
39+
$reloadedSheet = $reloadedSpreadsheet->getActiveSheet();
40+
41+
$expected = $sheet->getCell('A1')->getCalculatedValue();
42+
43+
if ($sheet->getCell('A1')->getDataType() === 'f') {
44+
$actual = $reloadedSheet->getCell('A1')->getOldCalculatedValue();
45+
} else {
46+
$actual = $reloadedSheet->getCell('A1')->getValue();
47+
}
48+
49+
self::assertSame($expected, $actual);
50+
}
51+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
return [
4+
[
5+
['string'],
6+
],
7+
[
8+
['="string"'],
9+
],
10+
[
11+
[1],
12+
],
13+
[
14+
[0],
15+
],
16+
[
17+
[true],
18+
],
19+
[
20+
[false],
21+
],
22+
[
23+
['=TRUE()'],
24+
],
25+
[
26+
['=ISFORMULA(B1)', '=1+2'],
27+
],
28+
[
29+
['1'],
30+
],
31+
[
32+
['0'],
33+
],
34+
[
35+
['null'],
36+
],
37+
[
38+
[null],
39+
],
40+
];

0 commit comments

Comments
 (0)