Skip to content

Commit 9b6e4f9

Browse files
authored
Merge branch 'master' into moredatefilter
2 parents 4ab6439 + 66cd68d commit 9b6e4f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1229
-246
lines changed

CHANGELOG.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

12-
- Nothing.
12+
- Support for passing flags in the Reader `load()` and Writer `save()`methods, and through the IOFactory, to set behaviours. [PR #2136](https://github.com/PHPOffice/PhpSpreadsheet/pull/2136)
13+
- See [documentation](https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/) for details
14+
- More flexibility in the StringValueBinder to determine what datatypes should be treated as strings [PR #2138](https://github.com/PHPOffice/PhpSpreadsheet/pull/2138)
15+
- Helper class for conversion between css size Units of measure (`px`, `pt`, `pc`, `in`, `cm`, `mm`). [PR #2152](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145)
16+
- Allow Row height and Column Width to be set using different units of measure (`px`, `pt`, `pc`, `in`, `cm`, `mm`), rather than only in points or MS Excel column width units. [PR #2152](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145)
1317

1418
### Changed
1519

@@ -25,7 +29,9 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2529

2630
### Fixed
2731

28-
- Nothing.
32+
- Xls Reader changing grey background to black in Excel template [Issue #2147](Changing grey background to black in Excel template) [PR #2156](https://github.com/PHPOffice/PhpSpreadsheet/pull/2156)
33+
- Column width and Row height styles in the Html Reader when the value includes a unit of measure. [Issue #2145](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145).
34+
2935

3036
## 1.18.0 - 2021-05-31
3137

docs/topics/accessing-cells.md

+31-5
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ values beginning with `=` will be converted to a formula. Strings that
110110
aren't numeric, or that don't begin with a leading `=` will be treated
111111
as genuine string values.
112112

113+
Note that a numeric string that begins with a leading zero (that isn't
114+
immediately followed by a decimal separator) will not be converted to a
115+
numeric, so values like phone numbers (e.g. `01615991375``will remain as
116+
strings).
117+
113118
This "conversion" is handled by a cell "value binder", and you can write
114119
custom "value binders" to change the behaviour of these "conversions".
115120
The standard PhpSpreadsheet package also provides an "advanced value
@@ -138,8 +143,10 @@ Formats handled by the advanced value binder include:
138143
- When strings contain a newline character (`\n`), then the cell styling is
139144
set to wrap.
140145

141-
You can read more about value binders later in this section of the
142-
documentation.
146+
Basically, it attempts to mimic the behaviour of the MS Excel GUI.
147+
148+
You can read more about value binders [later in this section of the
149+
documentation](#using-value-binders-to-facilitate-data-entry).
143150

144151
### Setting a formula in a Cell
145152

@@ -551,8 +558,27 @@ $spreadsheet->getActiveSheet()->setCellValue('A5', 'Date/time value:');
551558
$spreadsheet->getActiveSheet()->setCellValue('B5', '21 December 1983');
552559
```
553560

554-
**Creating your own value binder is easy.** When advanced value binding
555-
is required, you can implement the
556-
`\PhpOffice\PhpSpreadsheet\Cell\IValueBinder` interface or extend the
561+
Alternatively, a `\PhpOffice\PhpSpreadsheet\Cell\StringValueBinder` class is available
562+
if you want to preserve all content as strings. This might be appropriate if you
563+
were loading a file containing values that could be interpreted as numbers (e.g. numbers
564+
with leading sign such as international phone numbers like `+441615579382`), but that
565+
should be retained as strings (non-international phone numbers with leading zeroes are
566+
already maintained as strings).
567+
568+
By default, the StringValueBinder will cast any datatype passed to it into a string. However, there are a number of settings which allow you to specify that certain datatypes shouldn't be cast to strings, but left "as is":
569+
570+
```php
571+
// Set value binder
572+
$stringValueBinder = new \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder();
573+
$stringValueBinder->setNumericConversion(false)
574+
->setBooleanConversion(false)
575+
->setNullConversion(false)
576+
->setFormulaConversion(false);
577+
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( $stringValueBinder );
578+
```
579+
580+
**Creating your own value binder is relatively straightforward.** When more specialised
581+
value binding is required, you can implement the
582+
`\PhpOffice\PhpSpreadsheet\Cell\IValueBinder` interface or extend the existing
557583
`\PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder` or
558584
`\PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder` classes.

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

+64-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Reading and writing to file
22

33
As you already know from the [architecture](./architecture.md#readers-and-writers),
4-
reading and writing to a
5-
persisted storage is not possible using the base PhpSpreadsheet classes.
4+
reading and writing to a persisted storage is not possible using the base PhpSpreadsheet classes.
65
For this purpose, PhpSpreadsheet provides readers and writers, which are
76
implementations of `\PhpOffice\PhpSpreadsheet\Reader\IReader` and
87
`\PhpOffice\PhpSpreadsheet\Writer\IWriter`.
@@ -892,8 +891,7 @@ class My_Custom_TCPDF_Writer extends \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf
892891

893892
#### Writing a spreadsheet
894893

895-
Once you have identified the Renderer that you wish to use for PDF
896-
generation, you can write a .pdf file using the following code:
894+
Once you have identified the Renderer that you wish to use for PDF generation, you can write a .pdf file using the following code:
897895

898896
```php
899897
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
@@ -905,8 +903,7 @@ first worksheet by default.
905903

906904
#### Write all worksheets
907905

908-
PDF files can contain one or more worksheets. If you want to write all
909-
sheets into a single PDF file, use the following code:
906+
PDF files can contain one or more worksheets. If you want to write all sheets into a single PDF file, use the following code:
910907

911908
```php
912909
$writer->writeAllSheets();
@@ -1020,3 +1017,64 @@ $spreadhseet = $reader->loadFromString($secondHtmlString, $spreadsheet);
10201017
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
10211018
$writer->save('write.xls');
10221019
```
1020+
1021+
## Reader/Writer Flags
1022+
1023+
Some Readers and Writers support special "Feature Flags" that need to be explicitly enabled.
1024+
An example of this is Charts in a spreadsheet. By default, when you load a spreadsheet that contains Charts, the charts will not be loaded. If all you want to do is read the data in the spreadsheet, then loading charts is an overhead for both speed of loading and memory usage.
1025+
However, there are times when you may want to load any charts in the spreadsheet as well as the data. To do so, you need to tell the Reader explicitly to include Charts.
1026+
1027+
```php
1028+
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("05featuredemo.xlsx");
1029+
$reader->setIncludeCharts(true);
1030+
$reader->load("spreadsheetWithCharts.xlsx");
1031+
```
1032+
Alternatively, you can specify this in the call to load the spreadsheet:
1033+
```php
1034+
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("spreadsheetWithCharts.xlsx");
1035+
$reader->load("spreadsheetWithCharts.xlsx", $reader::LOAD_WITH_CHARTS);
1036+
```
1037+
1038+
If you wish to use the IOFactory `load()` method rather than instantiating a specific Reader, then you can still pass these flags.
1039+
1040+
```php
1041+
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("spreadsheetWithCharts.xlsx", \PhpOffice\PhpSpreadsheet\Reader\IReader::LOAD_WITH_CHARTS);
1042+
```
1043+
1044+
Likewise, when saving a file using a Writer, loaded charts wil not be saved unless you explicitly tell the Writer to include them:
1045+
1046+
```php
1047+
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
1048+
$writer->setIncludeCharts(true);
1049+
$writer->save('mySavedFileWithCharts.xlsx');
1050+
```
1051+
1052+
As with the `load()` method, you can also pass flags in the `save()` method:
1053+
```php
1054+
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
1055+
$writer->save('mySavedFileWithCharts.xlsx', \PhpOffice\PhpSpreadsheet\Writer\IWriter::SAVE_WITH_CHARTS);
1056+
```
1057+
1058+
Currently, the only special "Feature Flag" that is supported in this way is the inclusion of Charts, and only for certain formats.
1059+
1060+
Readers | LOAD_WITH_CHARTS |
1061+
---------|------------------|
1062+
Xlsx | YES |
1063+
Xls | NO |
1064+
Xml | NO |
1065+
Ods | NO |
1066+
Gnumeric | NO |
1067+
Html | N/A |
1068+
Slk | N/A |
1069+
Csv | N/A |
1070+
1071+
1072+
Writers | SAVE_WITH_CHARTS |
1073+
--------|------------------|
1074+
Xlsx | YES |
1075+
Xls | NO |
1076+
Ods | NO |
1077+
Html | YES |
1078+
Pdf | YES |
1079+
Csv | N/A |
1080+

docs/topics/recipes.md

+49
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,16 @@ A column's width can be set using the following code:
11221122
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(12);
11231123
```
11241124

1125+
If you want to set a column width using a different unit of measure,
1126+
then you can do so by telling PhpSpreadsheet what UoM the width value
1127+
that you are setting is measured in.
1128+
Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
1129+
`cm` (centimeters) and `mm` (millimeters).
1130+
1131+
```php
1132+
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(120, 'pt');
1133+
```
1134+
11251135
If you want PhpSpreadsheet to perform an automatic width calculation,
11261136
use the following code. PhpSpreadsheet will approximate the column with
11271137
to the width of the widest column value.
@@ -1207,6 +1217,16 @@ Excel measures row height in points, where 1 pt is 1/72 of an inch (or
12071217
about 0.35mm). The default value is 12.75 pts; and the permitted range
12081218
of values is between 0 and 409 pts, where 0 pts is a hidden row.
12091219

1220+
If you want to set a row height using a different unit of measure,
1221+
then you can do so by telling PhpSpreadsheet what UoM the height value
1222+
that you are setting is measured in.
1223+
Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
1224+
`cm` (centimeters) and `mm` (millimeters).
1225+
1226+
```php
1227+
$spreadsheet->getActiveSheet()->getRowDimension('10')->setRowHeight(100, 'pt');
1228+
```
1229+
12101230
## Show/hide a row
12111231

12121232
To set a worksheet''s row visibility, you can use the following code.
@@ -1560,6 +1580,20 @@ Default column width can be set using the following code:
15601580
$spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(12);
15611581
```
15621582

1583+
Excel measures column width in its own proprietary units, based on the number
1584+
of characters that will be displayed in the default font.
1585+
1586+
If you want to set the default column width using a different unit of measure,
1587+
then you can do so by telling PhpSpreadsheet what UoM the width value
1588+
that you are setting is measured in.
1589+
Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
1590+
`cm` (centimeters) and `mm` (millimeters).
1591+
1592+
```php
1593+
$spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(400, 'pt');
1594+
```
1595+
and PhpSpreadsheet will handle the internal conversion.
1596+
15631597
## Setting the default row height
15641598

15651599
Default row height can be set using the following code:
@@ -1568,6 +1602,21 @@ Default row height can be set using the following code:
15681602
$spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
15691603
```
15701604

1605+
Excel measures row height in points, where 1 pt is 1/72 of an inch (or
1606+
about 0.35mm). The default value is 12.75 pts; and the permitted range
1607+
of values is between 0 and 409 pts, where 0 pts is a hidden row.
1608+
1609+
If you want to set a row height using a different unit of measure,
1610+
then you can do so by telling PhpSpreadsheet what UoM the height value
1611+
that you are setting is measured in.
1612+
Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
1613+
`cm` (centimeters) and `mm` (millimeters).
1614+
1615+
```php
1616+
$spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight(100, 'pt');
1617+
```
1618+
1619+
15711620
## Add a GD drawing to a worksheet
15721621

15731622
There might be a situation where you want to generate an in-memory image

phpstan-baseline.neon

+1-31
Original file line numberDiff line numberDiff line change
@@ -2487,7 +2487,7 @@ parameters:
24872487

24882488
-
24892489
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, string\\|null given\\.$#"
2490-
count: 4
2490+
count: 2
24912491
path: src/PhpSpreadsheet/Reader/Html.php
24922492

24932493
-
@@ -2775,11 +2775,6 @@ parameters:
27752775
count: 1
27762776
path: src/PhpSpreadsheet/Reader/Xls.php
27772777

2778-
-
2779-
message: "#^Parameter \\#1 \\$value of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\MemoryDrawing\\:\\:setImageResource\\(\\) expects GdImage\\|resource, resource\\|false given\\.$#"
2780-
count: 1
2781-
path: src/PhpSpreadsheet/Reader/Xls.php
2782-
27832778
-
27842779
message: "#^Parameter \\#1 \\$pValue of method PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\BaseDrawing\\:\\:setOffsetX\\(\\) expects int, float\\|int given\\.$#"
27852780
count: 1
@@ -4100,11 +4095,6 @@ parameters:
41004095
count: 1
41014096
path: src/PhpSpreadsheet/Shared/Date.php
41024097

4103-
-
4104-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Drawing\\:\\:pixelsToCellDimension\\(\\) should return int but returns float\\|int\\.$#"
4105-
count: 1
4106-
path: src/PhpSpreadsheet/Shared/Drawing.php
4107-
41084098
-
41094099
message: "#^Parameter \\#1 \\$fp of function fread expects resource, resource\\|false given\\.$#"
41104100
count: 2
@@ -4240,11 +4230,6 @@ parameters:
42404230
count: 1
42414231
path: src/PhpSpreadsheet/Shared/Font.php
42424232

4243-
-
4244-
message: "#^Parameter \\#1 \\$pValue of static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Drawing\\:\\:pixelsToCellDimension\\(\\) expects int, float\\|int given\\.$#"
4245-
count: 1
4246-
path: src/PhpSpreadsheet/Shared/Font.php
4247-
42484233
-
42494234
message: "#^Parameter \\#2 \\$pDefaultFont of static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Drawing\\:\\:pixelsToCellDimension\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font, PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\|null given\\.$#"
42504235
count: 1
@@ -4860,16 +4845,6 @@ parameters:
48604845
count: 1
48614846
path: src/PhpSpreadsheet/Shared/XMLWriter.php
48624847

4863-
-
4864-
message: "#^Parameter \\#1 \\$pValue of static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Drawing\\:\\:pointsToPixels\\(\\) expects int, float given\\.$#"
4865-
count: 1
4866-
path: src/PhpSpreadsheet/Shared/Xls.php
4867-
4868-
-
4869-
message: "#^Parameter \\#1 \\$fontSizeInPoints of static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\Font\\:\\:fontSizeToPixels\\(\\) expects int, float given\\.$#"
4870-
count: 1
4871-
path: src/PhpSpreadsheet/Shared/Xls.php
4872-
48734848
-
48744849
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\:\\:\\$workbookViewVisibilityValues has no typehint specified\\.$#"
48754850
count: 1
@@ -6215,11 +6190,6 @@ parameters:
62156190
count: 1
62166191
path: src/PhpSpreadsheet/Writer/Html.php
62176192

6218-
-
6219-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Writer\\\\IWriter\\:\\:save\\(\\) has no return typehint specified\\.$#"
6220-
count: 1
6221-
path: src/PhpSpreadsheet/Writer/IWriter.php
6222-
62236193
-
62246194
message: "#^Negated boolean expression is always false\\.$#"
62256195
count: 1

0 commit comments

Comments
 (0)