Skip to content

Commit

Permalink
Merge pull request #156 from dakira/bugfix/formulas-not-interpreted-a…
Browse files Browse the repository at this point in the history
…nymore

Fix openspout backwards compatibility change that stops parsing formulas
  • Loading branch information
freekmurze authored Nov 9, 2023
2 parents 1bce12e + 7af34b8 commit 4f50a47
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.2, 8.1, 8.0]
laravel: [10.*, 9.*, 8.*]
php: [8.2, 8.1]
laravel: [10.*, 9.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: ^6.23
exclude:
- laravel: 10.*
php: 8.0


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

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ If your file already contains a header row, it will be ignored and replaced with

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.

### Working with multiple sheet documents
#### Working with multiple sheet documents

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

Expand Down Expand Up @@ -274,6 +274,16 @@ $rows = SimpleExcelReader::create($pathToCsv)
->getRows();
```

#### Reading cells that contain formulas

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.

```php
$rows = SimpleExcelReader::create($pathToXlsx)
->keepFormulas()
->getRows();
```

### Writing files

Here's how you can write a CSV file:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
}
],
"require": {
"php": "^8.0",
"openspout/openspout": "^4.8",
"illuminate/support": "^8.71|^9.0|^10.0"
"php": "^8.1",
"openspout/openspout": "^4.19",
"illuminate/support": "^9.0|^10.0"
},
"require-dev": {
"pestphp/pest-plugin-laravel": "^1.3",
Expand Down
17 changes: 16 additions & 1 deletion src/SimpleExcelReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\LazyCollection;
use InvalidArgumentException;
use OpenSpout\Common\Entity\Cell;
use OpenSpout\Common\Entity\Cell\FormulaCell;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Reader\CSV\Options as CSVOptions;
use OpenSpout\Reader\CSV\Reader as CSVReader;
Expand All @@ -21,6 +23,7 @@ class SimpleExcelReader
protected bool $processHeader = true;
protected bool $trimHeader = false;
protected bool $headersToSnakeCase = false;
protected bool $parseFormulas = true;
protected ?string $trimHeaderCharacters = null;
protected mixed $formatHeadersUsing = null;
protected ?array $headers = null;
Expand Down Expand Up @@ -122,6 +125,13 @@ public function headersToSnakeCase(): self
return $this;
}

public function keepFormulas()
{
$this->parseFormulas = false;

return $this;
}

public function getReader(): ReaderInterface
{
return $this->reader;
Expand Down Expand Up @@ -284,7 +294,12 @@ protected function toSnakeCase(string $header): string

protected function getValueFromRow(Row $row): array
{
$values = $row->toArray();
$values = array_map(function (Cell $cell) {
return $cell instanceof FormulaCell && $this->parseFormulas
? $cell->getComputedValue()
: $cell->getValue();
}, $row->getCells());

ksort($values);

$headers = $this->customHeaders ?: $this->headers;
Expand Down

0 comments on commit 4f50a47

Please sign in to comment.