Skip to content

Commit 4f50a47

Browse files
authored
Merge pull request #156 from dakira/bugfix/formulas-not-interpreted-anymore
Fix openspout backwards compatibility change that stops parsing formulas
2 parents 1bce12e + 7af34b8 commit 4f50a47

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

.github/workflows/run-tests.yml

+2-8
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@ jobs:
99
strategy:
1010
fail-fast: true
1111
matrix:
12-
php: [8.2, 8.1, 8.0]
13-
laravel: [10.*, 9.*, 8.*]
12+
php: [8.2, 8.1]
13+
laravel: [10.*, 9.*]
1414
dependency-version: [prefer-lowest, prefer-stable]
1515
include:
1616
- laravel: 10.*
1717
testbench: 8.*
1818
- laravel: 9.*
1919
testbench: 7.*
20-
- laravel: 8.*
21-
testbench: ^6.23
22-
exclude:
23-
- laravel: 10.*
24-
php: 8.0
25-
2620

2721
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
2822

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ If your file already contains a header row, it will be ignored and replaced with
115115

116116
If your file does not contain a header row, you should also use `noHeaderRow()`, and your headers will be used instead of numeric keys, as above.
117117

118-
### Working with multiple sheet documents
118+
#### Working with multiple sheet documents
119119

120120
Excel files can include multiple spreadsheets. You can select the sheet you want to use with the `fromSheet()` method to select by index.
121121

@@ -274,6 +274,16 @@ $rows = SimpleExcelReader::create($pathToCsv)
274274
->getRows();
275275
```
276276

277+
#### Reading cells that contain formulas
278+
279+
Normally, cells containing formulas are parsed and their computed value will be returned. If you want to keep the actual formula as a string, you can use the `keepFormulas` method.
280+
281+
```php
282+
$rows = SimpleExcelReader::create($pathToXlsx)
283+
->keepFormulas()
284+
->getRows();
285+
```
286+
277287
### Writing files
278288

279289
Here's how you can write a CSV file:

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.0",
20-
"openspout/openspout": "^4.8",
21-
"illuminate/support": "^8.71|^9.0|^10.0"
19+
"php": "^8.1",
20+
"openspout/openspout": "^4.19",
21+
"illuminate/support": "^9.0|^10.0"
2222
},
2323
"require-dev": {
2424
"pestphp/pest-plugin-laravel": "^1.3",

src/SimpleExcelReader.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Support\LazyCollection;
66
use InvalidArgumentException;
7+
use OpenSpout\Common\Entity\Cell;
8+
use OpenSpout\Common\Entity\Cell\FormulaCell;
79
use OpenSpout\Common\Entity\Row;
810
use OpenSpout\Reader\CSV\Options as CSVOptions;
911
use OpenSpout\Reader\CSV\Reader as CSVReader;
@@ -21,6 +23,7 @@ class SimpleExcelReader
2123
protected bool $processHeader = true;
2224
protected bool $trimHeader = false;
2325
protected bool $headersToSnakeCase = false;
26+
protected bool $parseFormulas = true;
2427
protected ?string $trimHeaderCharacters = null;
2528
protected mixed $formatHeadersUsing = null;
2629
protected ?array $headers = null;
@@ -122,6 +125,13 @@ public function headersToSnakeCase(): self
122125
return $this;
123126
}
124127

128+
public function keepFormulas()
129+
{
130+
$this->parseFormulas = false;
131+
132+
return $this;
133+
}
134+
125135
public function getReader(): ReaderInterface
126136
{
127137
return $this->reader;
@@ -284,7 +294,12 @@ protected function toSnakeCase(string $header): string
284294

285295
protected function getValueFromRow(Row $row): array
286296
{
287-
$values = $row->toArray();
297+
$values = array_map(function (Cell $cell) {
298+
return $cell instanceof FormulaCell && $this->parseFormulas
299+
? $cell->getComputedValue()
300+
: $cell->getValue();
301+
}, $row->getCells());
302+
288303
ksort($values);
289304

290305
$headers = $this->customHeaders ?: $this->headers;

0 commit comments

Comments
 (0)