Skip to content

Commit c31b1de

Browse files
authored
Merge pull request #3256 from PHPOffice/Reader-Writer-Flags
Reader/Writer flags
2 parents b3109d6 + f5b5ed8 commit c31b1de

File tree

6 files changed

+65
-24
lines changed

6 files changed

+65
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

12+
- Extended flag options for the Reader `load()` and Writer `save()` methods
1213
- Apply Row/Column limits (1048576 and XFD) in ReferenceHelper [PR #3213](https://github.com/PHPOffice/PhpSpreadsheet/pull/3213)
1314
- Allow the creation of In-Memory Drawings from a string of binary image data, or from a stream. [PR #3157](https://github.com/PHPOffice/PhpSpreadsheet/pull/3157)
1415
- Xlsx Reader support for Pivot Tables [PR #2829](https://github.com/PHPOffice/PhpSpreadsheet/pull/2829)

docs/topics/reading-and-writing-to-file.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,11 @@ of different libraries.
861861

862862
Currently, the following libraries are supported:
863863

864-
Library | Downloadable from | PhpSpreadsheet writer
865-
--------|-------------------------------------|----------------------
866-
TCPDF | https://github.com/tecnickcom/tcpdf | Tcpdf
867-
mPDF | https://github.com/mpdf/mpdf | Mpdf
868-
Dompdf | https://github.com/dompdf/dompdf | Dompdf
864+
| Library | Downloadable from | PhpSpreadsheet writer |
865+
|---------|-------------------------------------|-----------------------|
866+
| TCPDF | https://github.com/tecnickcom/tcpdf | Tcpdf |
867+
| mPDF | https://github.com/mpdf/mpdf | Mpdf |
868+
| Dompdf | https://github.com/dompdf/dompdf | Dompdf |
869869

870870
The different libraries have different strengths and weaknesses. Some
871871
generate better formatted output than others, some are faster or use
@@ -1083,6 +1083,22 @@ If you wish to use the IOFactory `load()` method rather than instantiating a spe
10831083
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("spreadsheetWithCharts.xlsx", \PhpOffice\PhpSpreadsheet\Reader\IReader::LOAD_WITH_CHARTS);
10841084
```
10851085

1086+
Flags that are available that can be passed to the Reader in this way include:
1087+
- $reader::LOAD_WITH_CHARTS
1088+
- $reader::READ_DATA_ONLY
1089+
- $reader::SKIP_EMPTY_CELLS
1090+
1091+
| Readers | LOAD_WITH_CHARTS | READ_DATA_ONLY | SKIP_EMPTY_CELLS |
1092+
|----------|------------------|----------------|------------------|
1093+
| Xlsx | YES | YES | YES |
1094+
| Xls | NO | YES | YES |
1095+
| Xml | NO | NO | NO |
1096+
| Ods | NO | YES | NO |
1097+
| Gnumeric | NO | YES | NO |
1098+
| Html | N/A | N/A | N/A |
1099+
| Slk | N/A | NO | NO |
1100+
| Csv | N/A | NO | NO |
1101+
10861102
Likewise, when saving a file using a Writer, loaded charts wil not be saved unless you explicitly tell the Writer to include them:
10871103

10881104
```php
@@ -1097,26 +1113,25 @@ $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
10971113
$writer->save('mySavedFileWithCharts.xlsx', \PhpOffice\PhpSpreadsheet\Writer\IWriter::SAVE_WITH_CHARTS);
10981114
```
10991115

1100-
Currently, the only special "Feature Flag" that is supported in this way is the inclusion of Charts, and only for certain formats.
1116+
Flags that are available that can be passed to the Reader in this way include:
1117+
- $reader::SAVE_WITH_CHARTS
1118+
- $reader::DISABLE_PRECALCULATE_FORMULAE
11011119

1102-
Readers | LOAD_WITH_CHARTS |
1103-
---------|------------------|
1104-
Xlsx | YES |
1105-
Xls | NO |
1106-
Xml | NO |
1107-
Ods | NO |
1108-
Gnumeric | NO |
1109-
Html | N/A |
1110-
Slk | N/A |
1111-
Csv | N/A |
1120+
| Writers | SAVE_WITH_CHARTS | DISABLE_PRECALCULATE_FORMULAE |
1121+
|---------|------------------|-------------------------------|
1122+
| Xlsx | YES | YES |
1123+
| Xls | NO | NO |
1124+
| Ods | NO | YES |
1125+
| Html | YES | YES |
1126+
| Pdf | YES | YES |
1127+
| Csv | N/A | YES |
11121128

1129+
### Combining Flags
11131130

1114-
Writers | SAVE_WITH_CHARTS |
1115-
--------|------------------|
1116-
Xlsx | YES |
1117-
Xls | NO |
1118-
Ods | NO |
1119-
Html | YES |
1120-
Pdf | YES |
1121-
Csv | N/A |
1131+
One benefit of flags is that you can pass several flags in a single method call.
1132+
Two or more flags can be passed together using PHP's `|` operator.
11221133

1134+
```php
1135+
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("myExampleFile.xlsx");
1136+
$reader->load("spreadsheetWithCharts.xlsx", $reader::READ_DATA_ONLY | $reader::SKIP_EMPTY_CELLS);
1137+
```

src/PhpSpreadsheet/Reader/BaseReader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ protected function processFlags(int $flags): void
144144
if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) {
145145
$this->setIncludeCharts(true);
146146
}
147+
if (((bool) ($flags & self::READ_DATA_ONLY)) === true) {
148+
$this->setReadDataOnly(true);
149+
}
150+
if (((bool) ($flags & self::SKIP_EMPTY_CELLS)) === true) {
151+
$this->setReadEmptyCells(false);
152+
}
147153
}
148154

149155
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet

src/PhpSpreadsheet/Reader/IReader.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ interface IReader
66
{
77
public const LOAD_WITH_CHARTS = 1;
88

9+
public const READ_DATA_ONLY = 2;
10+
11+
public const SKIP_EMPTY_CELLS = 4;
12+
913
/**
1014
* IReader constructor.
1115
*/
@@ -123,6 +127,13 @@ public function setReadFilter(IReadFilter $readFilter);
123127
/**
124128
* Loads PhpSpreadsheet from file.
125129
*
130+
* @param string $filename The name of the file to load
131+
* @param int $flags Flags that can change the behaviour of the Writer:
132+
* self::LOAD_WITH_CHARTS Load any charts that are defined (if the Reader supports Charts)
133+
* self::READ_DATA_ONLY Read only data, not style or structure information, from the file
134+
* self::SKIP_EMPTY_CELLS Don't read empty cells (cells that contain a null value,
135+
* empty string, or a string containing only whitespace characters)
136+
*
126137
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet
127138
*/
128139
public function load(string $filename, int $flags = 0);

src/PhpSpreadsheet/Writer/BaseWriter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ protected function processFlags(int $flags): void
9999
if (((bool) ($flags & self::SAVE_WITH_CHARTS)) === true) {
100100
$this->setIncludeCharts(true);
101101
}
102+
if (((bool) ($flags & self::DISABLE_PRECALCULATE_FORMULAE)) === true) {
103+
$this->setPreCalculateFormulas(false);
104+
}
102105
}
103106

104107
/**

src/PhpSpreadsheet/Writer/IWriter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ interface IWriter
88
{
99
public const SAVE_WITH_CHARTS = 1;
1010

11+
public const DISABLE_PRECALCULATE_FORMULAE = 2;
12+
1113
/**
1214
* IWriter constructor.
1315
*
@@ -62,6 +64,9 @@ public function setPreCalculateFormulas($precalculateFormulas);
6264
* Save PhpSpreadsheet to file.
6365
*
6466
* @param resource|string $filename Name of the file to save
67+
* @param int $flags Flags that can change the behaviour of the Writer:
68+
* self::SAVE_WITH_CHARTS Save any charts that are defined (if the Writer supports Charts)
69+
* self::DISABLE_PRECALCULATE_FORMULAE Don't Precalculate formulae before saving the file
6570
*
6671
* @throws Exception
6772
*/

0 commit comments

Comments
 (0)