Skip to content

Commit 2076a07

Browse files
committed
Minor Tweaks
1 parent fed7a32 commit 2076a07

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3652,7 +3652,7 @@ public function _calculateFormulaValue(string $formula, ?string $cellID = null,
36523652
// Basic validation that this is indeed a formula
36533653
// We simply return the cell value if not
36543654
$formula = trim($formula);
3655-
if ($formula[0] != '=') {
3655+
if ($formula === '' || $formula[0] !== '=') {
36563656
return self::wrapResult($formula);
36573657
}
36583658
$formula = ltrim(substr($formula, 1));

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ public function setValueExplicit(mixed $value, string $dataType = DataType::TYPE
275275

276276
break;
277277
case DataType::TYPE_FORMULA:
278-
if (!is_string($value) || $value[0] !== '=') {
279-
throw new SpreadsheetException('Invalid value for datatype Formula');
278+
if ($value !== null && !is_scalar($value) && !($value instanceof Stringable)) {
279+
throw new SpreadsheetException('Invalid unstringable value for datatype Formula');
280280
}
281281
$this->value = (string) $value;
282282

tests/PhpSpreadsheetTests/Calculation/CalculationTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function testFormulaWithOptionalArgumentsAndRequiredCellReferenceShouldPa
8989
$cell = $sheet->getCell('F6');
9090
$cell->setValue('=OFFSET(D3, -1, -2)');
9191
self::assertEquals(5, $cell->getCalculatedValue(), 'missing arguments should be filled with null');
92+
$spreadsheet->disconnectWorksheets();
9293
}
9394

9495
public function testCellSetAsQuotedText(): void
@@ -103,18 +104,27 @@ public function testCellSetAsQuotedText(): void
103104
self::assertEquals("=cmd|'/C calc'!A0", $cell->getCalculatedValue());
104105

105106
$cell2 = $workSheet->getCell('A2');
107+
$cell2->setValueExplicit('ABC', DataType::TYPE_FORMULA);
108+
self::assertEquals('ABC', $cell2->getCalculatedValue());
109+
110+
$cell3 = $workSheet->getCell('A3');
111+
$cell3->setValueExplicit('=', DataType::TYPE_FORMULA);
112+
self::assertEquals('', $cell3->getCalculatedValue());
113+
114+
$cell4 = $workSheet->getCell('A4');
106115

107116
try {
108-
$cell2->setValueExplicit('ABC', DataType::TYPE_FORMULA);
109-
self::assertEquals('ABC', $cell2->getCalculatedValue());
110-
self::fail('setValueExplicit with invalid formula should have thrown exception');
117+
$cell4->setValueExplicit((object) null, DataType::TYPE_FORMULA);
118+
self::fail('setValueExplicit formula with unstringable object should have thrown exception');
111119
} catch (SpreadsheetException $e) {
112-
self::assertStringContainsString('Invalid value for datatype Formula', $e->getMessage());
120+
self::assertStringContainsString('Invalid unstringable value for datatype Formula', $e->getMessage());
113121
}
114122

115-
$cell3 = $workSheet->getCell('A3');
116-
$cell3->setValueExplicit('=', DataType::TYPE_FORMULA);
117-
self::assertEquals('', $cell3->getCalculatedValue());
123+
$cell5 = $workSheet->getCell('A5');
124+
$cell5->setValueExplicit(null, DataType::TYPE_FORMULA);
125+
self::assertEquals('', $cell5->getCalculatedValue());
126+
127+
$spreadsheet->disconnectWorksheets();
118128
}
119129

120130
public function testCellWithDdeExpresion(): void
@@ -126,6 +136,7 @@ public function testCellWithDdeExpresion(): void
126136
$cell->setValue("=cmd|'/C calc'!A0");
127137

128138
self::assertEquals("=cmd|'/C calc'!A0", $cell->getCalculatedValue());
139+
$spreadsheet->disconnectWorksheets();
129140
}
130141

131142
public function testFormulaReferencingWorksheetWithEscapedApostrophe(): void
@@ -144,6 +155,7 @@ public function testFormulaReferencingWorksheetWithEscapedApostrophe(): void
144155

145156
$cellValue = $workSheet->getCell('A2')->getCalculatedValue();
146157
self::assertSame('HELLO WORLD', $cellValue);
158+
$spreadsheet->disconnectWorksheets();
147159
}
148160

149161
public function testFormulaReferencingWorksheetWithUnescapedApostrophe(): void
@@ -162,6 +174,7 @@ public function testFormulaReferencingWorksheetWithUnescapedApostrophe(): void
162174

163175
$cellValue = $workSheet->getCell('A2')->getCalculatedValue();
164176
self::assertSame('HELLO WORLD', $cellValue);
177+
$spreadsheet->disconnectWorksheets();
165178
}
166179

167180
public function testCellWithFormulaTwoIndirect(): void
@@ -178,6 +191,7 @@ public function testCellWithFormulaTwoIndirect(): void
178191
$cell3->setValue('=SUM(INDIRECT("A"&ROW()),INDIRECT("B"&ROW()),INDIRECT("C"&ROW()))');
179192

180193
self::assertEquals('9', $cell3->getCalculatedValue());
194+
$spreadsheet->disconnectWorksheets();
181195
}
182196

183197
public function testCellWithStringNumeric(): void
@@ -190,6 +204,7 @@ public function testCellWithStringNumeric(): void
190204
$cell2->setValue('=100*A1');
191205

192206
self::assertSame(250.0, $cell2->getCalculatedValue());
207+
$spreadsheet->disconnectWorksheets();
193208
}
194209

195210
public function testCellWithStringFraction(): void
@@ -202,6 +217,7 @@ public function testCellWithStringFraction(): void
202217
$cell2->setValue('=100*A1');
203218

204219
self::assertSame(75.0, $cell2->getCalculatedValue());
220+
$spreadsheet->disconnectWorksheets();
205221
}
206222

207223
public function testCellWithStringPercentage(): void
@@ -214,6 +230,7 @@ public function testCellWithStringPercentage(): void
214230
$cell2->setValue('=100*A1');
215231

216232
self::assertSame(2.0, $cell2->getCalculatedValue());
233+
$spreadsheet->disconnectWorksheets();
217234
}
218235

219236
public function testCellWithStringCurrency(): void
@@ -228,6 +245,7 @@ public function testCellWithStringCurrency(): void
228245
$cell2->setValue('=100*A1');
229246

230247
self::assertSame(200.0, $cell2->getCalculatedValue());
248+
$spreadsheet->disconnectWorksheets();
231249
}
232250

233251
public function testBranchPruningFormulaParsingSimpleCase(): void
@@ -400,6 +418,7 @@ public function testFullExecutionDataPruning(
400418
$calculation->disableBranchPruning();
401419
$calculated = $cell->getCalculatedValue();
402420
self::assertEquals($expectedResult, $calculated);
421+
$spreadsheet->disconnectWorksheets();
403422
}
404423

405424
public static function dataProviderBranchPruningFullExecution(): array

tests/PhpSpreadsheetTests/Cell/CellFormulaTest.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testSetFormulaDeterminedByBinder(): void
5252

5353
public function testSetFormulaInvalidValue(): void
5454
{
55-
$formula = true;
55+
$formula = (object) true;
5656

5757
$spreadsheet = new Spreadsheet();
5858
$cell = $spreadsheet->getActiveSheet()->getCell('A1');
@@ -61,24 +61,7 @@ public function testSetFormulaInvalidValue(): void
6161
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA);
6262
self::fail('setValueExplicit should have thrown exception');
6363
} catch (SpreadsheetException $e) {
64-
self::assertStringContainsString('Invalid value for datatype Formula', $e->getMessage());
65-
}
66-
67-
$spreadsheet->disconnectWorksheets();
68-
}
69-
70-
public function testSetFormulaInvalidFormulaValue(): void
71-
{
72-
$formula = 'invalid formula';
73-
74-
$spreadsheet = new Spreadsheet();
75-
$cell = $spreadsheet->getActiveSheet()->getCell('A1');
76-
77-
try {
78-
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA);
79-
self::fail('setValueExplicit should have thrown exception');
80-
} catch (SpreadsheetException $e) {
81-
self::assertStringContainsString('Invalid value for datatype Formula', $e->getMessage());
64+
self::assertStringContainsString('Invalid unstringable value for datatype Formula', $e->getMessage());
8265
}
8366

8467
$spreadsheet->disconnectWorksheets();

0 commit comments

Comments
 (0)