Skip to content

Commit 8434189

Browse files
author
MarkBaker
committed
Update docblock documentation for setting cell values explicit to indicate that value/datatype matching is the responsibility of the end-user developer making this call; and that values may be changed to reflect the specified datatype.
No doubt some developers will complain that it should be the other way round, that datatpe should be modified to match the specified value; but then they should be using setValue() instead; the use of setValueExplicit() is explicit.
1 parent 4f22b39 commit 8434189

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2121

2222
### Changed
2323

24+
- Better enforcement of value modification to match specified datatype when using setValueExplicit()
25+
- Relax validation of merge cells to allow merge for a single cell reference [Issue #2776](https://github.com/PHPOffice/PhpSpreadsheet/issues/2776)
2426
- Memory and speed improvements, particularly for the Cell Collection, and the Writers.
2527

2628
See [the Discussion section on github](https://github.com/PHPOffice/PhpSpreadsheet/discussions/2821) for details of performance across versions

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ public function setValue($value)
202202
*
203203
* @param mixed $value Value
204204
* @param string $dataType Explicit data type, see DataType::TYPE_*
205+
* Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this
206+
* method, then it is your responsibility as an end-user developer to validate that the value and
207+
* the datatype match.
208+
* If you do mismatch value and datatpe, then the value you enter may be changed to match the datatype
209+
* that you specify.
205210
*
206211
* @return Cell
207212
*/
@@ -210,7 +215,7 @@ public function setValueExplicit($value, $dataType)
210215
// set the value according to data type
211216
switch ($dataType) {
212217
case DataType::TYPE_NULL:
213-
$this->value = $value;
218+
$this->value = null;
214219

215220
break;
216221
case DataType::TYPE_STRING2:

src/PhpSpreadsheet/Cell/DataType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function getErrorCodes()
4848
*
4949
* @param null|RichText|string $textValue Value to sanitize to an Excel string
5050
*
51-
* @return null|RichText|string Sanitized value
51+
* @return RichText|string Sanitized value
5252
*/
5353
public static function checkString($textValue)
5454
{
@@ -58,7 +58,7 @@ public static function checkString($textValue)
5858
}
5959

6060
// string must never be longer than 32,767 characters, truncate if necessary
61-
$textValue = StringHelper::substring($textValue, 0, 32767);
61+
$textValue = StringHelper::substring((string) $textValue, 0, 32767);
6262

6363
// we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
6464
$textValue = str_replace(["\r\n", "\r"], "\n", $textValue);

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,11 @@ public function setCellValueByColumnAndRow($columnIndex, $row, $value)
11771177
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
11781178
* @param mixed $value Value of the cell
11791179
* @param string $dataType Explicit data type, see DataType::TYPE_*
1180+
* Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this
1181+
* method, then it is your responsibility as an end-user developer to validate that the value and
1182+
* the datatype match.
1183+
* If you do mismatch value and datatpe, then the value you enter may be changed to match the datatype
1184+
* that you specify.
11801185
*
11811186
* @return $this
11821187
*/
@@ -1199,6 +1204,11 @@ public function setCellValueExplicit($coordinate, $value, $dataType)
11991204
* @param int $row Numeric row coordinate of the cell
12001205
* @param mixed $value Value of the cell
12011206
* @param string $dataType Explicit data type, see DataType::TYPE_*
1207+
* Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this
1208+
* method, then it is your responsibility as an end-user developer to validate that the value and
1209+
* the datatype match.
1210+
* If you do mismatch value and datatpe, then the value you enter may be changed to match the datatype
1211+
* that you specify.
12021212
*
12031213
* @return $this
12041214
*/
@@ -1770,6 +1780,10 @@ public function mergeCells($range)
17701780
$numberRows = $lastRow - $firstRow;
17711781
$numberColumns = $lastColumnIndex - $firstColumnIndex;
17721782

1783+
if ($numberRows === 1 && $numberColumns === 1) {
1784+
return $this;
1785+
}
1786+
17731787
// create upper left cell if it does not already exist
17741788
$upperLeft = "{$firstColumn}{$firstRow}";
17751789
if (!$this->cellExists($upperLeft)) {

0 commit comments

Comments
 (0)