@@ -1402,19 +1402,24 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
1402
1402
}
1403
1403
1404
1404
$ 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
+ }
1406
1411
if (is_array ($ calculatedValue )) {
1407
1412
$ attributes ['t ' ] = 'array ' ;
1408
1413
$ rows = max (1 , count ($ calculatedValue ));
1409
1414
$ cols = 1 ;
1410
1415
foreach ($ calculatedValue as $ row ) {
1411
1416
$ cols = max ($ cols , is_array ($ row ) ? count ($ row ) : 1 );
1412
1417
}
1413
- $ firstCellArray = Coordinate::indexesFromString ($ ref );
1418
+ $ firstCellArray = Coordinate::indexesFromString ($ coordinate );
1414
1419
$ lastRow = $ firstCellArray [1 ] + $ rows - 1 ;
1415
1420
$ lastColumn = $ firstCellArray [0 ] + $ cols - 1 ;
1416
1421
$ lastColumnString = Coordinate::stringFromColumnIndex ($ lastColumn );
1417
- $ ref . = ": $ lastColumnString$ lastRow " ;
1422
+ $ ref = "$ coordinate : $ lastColumnString$ lastRow " ;
1418
1423
}
1419
1424
if (($ attributes ['t ' ] ?? null ) === 'array ' ) {
1420
1425
$ objWriter ->startElement ('f ' );
@@ -1449,6 +1454,28 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
1449
1454
}
1450
1455
}
1451
1456
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
+
1452
1479
/**
1453
1480
* Write Cell.
1454
1481
*
0 commit comments