Skip to content

Commit 61f24ca

Browse files
committed
Sample Submitted by @jr212
Address sample code submitted by @jr212 which was not working correctly.
1 parent b53a4b7 commit 61f24ca

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,18 @@ public function getCalculatedValue(bool $resetLog = true): mixed
361361
$result = array_shift($result);
362362
}
363363
}
364+
// if return_as_array for formula like '=sheet!cell'
365+
if (is_array($result) && count($result) === 1) {
366+
$resultKey = array_keys($result)[0];
367+
$resultValue = $result[$resultKey];
368+
if (is_int($resultKey) && is_array($resultValue) && count($resultValue) === 1) {
369+
$resultKey2 = array_keys($resultValue)[0];
370+
$resultValue2 = $resultValue[$resultKey2];
371+
if (is_string($resultKey2) && !is_array($resultValue2) && preg_match('/[a-zA-Z]{1,3}/', $resultKey2) === 1) {
372+
$result = $resultValue2;
373+
}
374+
}
375+
}
364376
} catch (SpreadsheetException $ex) {
365377
if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->calculatedValue !== null)) {
366378
return $this->calculatedValue; // Fallback for calculations referencing external files.

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private static function splitFormatForSectionSelection(array $sections, mixed $v
107107
/**
108108
* Convert a value in a pre-defined format to a PHP string.
109109
*
110-
* @param null|bool|float|int|RichText|string $value Value to format
110+
* @param null|array|bool|float|int|RichText|string $value Value to format
111111
* @param string $format Format code: see = self::FORMAT_* for predefined values;
112112
* or can be any valid MS Excel custom format string
113113
* @param ?array $callBack Callback function for additional formatting of string
@@ -116,6 +116,9 @@ private static function splitFormatForSectionSelection(array $sections, mixed $v
116116
*/
117117
public static function toFormattedString($value, string $format, ?array $callBack = null): string
118118
{
119+
while (is_array($value)) {
120+
$value = array_shift($value);
121+
}
119122
if (is_bool($value)) {
120123
return $value ? Calculation::getTRUE() : Calculation::getFALSE();
121124
}

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,19 +1402,24 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
14021402
}
14031403

14041404
$attributes = $cell->getFormulaAttributes() ?? [];
1405-
$ref = array_key_exists('ref', $attributes) ? $attributes['ref'] : $cell->getCoordinate();
1405+
$coordinate = $cell->getCoordinate();
1406+
if (isset($attributes['ref'])) {
1407+
$ref = $this->parseRef($coordinate, $attributes['ref']);
1408+
} else {
1409+
$ref = $coordinate;
1410+
}
14061411
if (is_array($calculatedValue)) {
14071412
$attributes['t'] = 'array';
14081413
$rows = max(1, count($calculatedValue));
14091414
$cols = 1;
14101415
foreach ($calculatedValue as $row) {
14111416
$cols = max($cols, is_array($row) ? count($row) : 1);
14121417
}
1413-
$firstCellArray = Coordinate::indexesFromString($ref);
1418+
$firstCellArray = Coordinate::indexesFromString($coordinate);
14141419
$lastRow = $firstCellArray[1] + $rows - 1;
14151420
$lastColumn = $firstCellArray[0] + $cols - 1;
14161421
$lastColumnString = Coordinate::stringFromColumnIndex($lastColumn);
1417-
$ref .= ":$lastColumnString$lastRow";
1422+
$ref = "$coordinate:$lastColumnString$lastRow";
14181423
}
14191424
if (($attributes['t'] ?? null) === 'array') {
14201425
$objWriter->startElement('f');
@@ -1449,6 +1454,28 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
14491454
}
14501455
}
14511456

1457+
private function parseRef(string $coordinate, string $ref): string
1458+
{
1459+
if (preg_match('/^([A-Z]{1,3})([0-9]{1,7})(:([A-Z]{1,3})([0-9]{1,7}))?$/', $ref, $matches) !== 1) {
1460+
return $ref;
1461+
}
1462+
if (!isset($matches[3])) { // single cell, not range
1463+
return $coordinate;
1464+
}
1465+
$minRow = (int) $matches[2];
1466+
$maxRow = (int) $matches[5];
1467+
$rows = $maxRow - $minRow + 1;
1468+
$minCol = Coordinate::columnIndexFromString($matches[1]);
1469+
$maxCol = Coordinate::columnIndexFromString($matches[4]);
1470+
$cols = $maxCol - $minCol + 1;
1471+
$firstCellArray = Coordinate::indexesFromString($coordinate);
1472+
$lastRow = $firstCellArray[1] + $rows - 1;
1473+
$lastColumn = $firstCellArray[0] + $cols - 1;
1474+
$lastColumnString = Coordinate::stringFromColumnIndex($lastColumn);
1475+
1476+
return "$coordinate:$lastColumnString$lastRow";
1477+
}
1478+
14521479
/**
14531480
* Write Cell.
14541481
*

0 commit comments

Comments
 (0)