Skip to content

Commit 4cc95de

Browse files
committed
Add method getHighestColumnName
1 parent effffea commit 4cc95de

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ try {
8484
$row = $row->getNextRow();
8585
$row = $xlsxFastEditor->getLastRow($worksheetId1);
8686

87+
$xlsxFastEditor->getHighestColumnName($worksheetId1);
88+
8789
// Methods for rows
8890
$rowNumber = $row->number();
8991
$cell = $row->getCellOrNull('D2');

src/XlsxFastEditor.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,32 @@ public static function cellOrderCompare(string $ref1, string $ref2): int
565565
return strcmp($ref1, $ref2);
566566
}
567567

568+
/**
569+
* Gives the name of the highest column used in the spreadsheet (e.g. `BA`),
570+
* or null if there is none.
571+
* @throws XlsxFastEditorFileFormatException
572+
* @throws XlsxFastEditorXmlException
573+
*/
574+
public function getHighestColumnName(int $sheetNumber): ?string
575+
{
576+
$rightMostCell = '';
577+
$xpath = $this->getXPathFromPath(self::getWorksheetPath($sheetNumber));
578+
$cs = $xpath->query("/o:worksheet/o:sheetData/o:row/o:c[last()]");
579+
if ($cs !== false) {
580+
for ($i = 0; $i < $cs->length; $i++) {
581+
$c = $cs[$i];
582+
if (!($c instanceof \DOMElement)) {
583+
throw new XlsxFastEditorXmlException("Error querying XML fragment for row {$sheetNumber}!");
584+
}
585+
$cellName = $c->getAttribute('r');
586+
if ($cellName !== '' && ($rightMostCell === '' || self::cellOrderCompare($rightMostCell, $cellName) < 0)) {
587+
$rightMostCell = $cellName;
588+
}
589+
}
590+
}
591+
return $rightMostCell === '' ? null : XlsxFastEditorCell::nameToColumn($rightMostCell);
592+
}
593+
568594
/** To return `null` when accessing a row or cell that does not exist, e.g. via {@see XlsxFastEditor::getCell()} */
569595
public const ACCESS_MODE_NULL = 0;
570596
/** To throw an exception when accessing a row or cell that does not exist, e.g. via {@see XlsxFastEditor::getCell()} */

src/XlsxFastEditorCell.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,26 @@ public function name(): string
4949
}
5050

5151
/**
52-
* Column name (e.g., `'D'`).
52+
* $return column name (e.g., `'D'`).
5353
* @throws XlsxFastEditorXmlException
5454
*/
55-
public function column(): string
55+
public static function nameToColumn(string $name): string
5656
{
57-
if (preg_match('/^([A-Z]+)/', $this->name(), $matches) == 0 || empty($matches[1])) {
58-
throw new XlsxFastEditorXmlException("Error querying column name for cell {$this->name()}!");
57+
if (preg_match('/^([A-Z]+)/', $name, $matches) == 0 || empty($matches[1])) {
58+
throw new XlsxFastEditorXmlException("Error querying column name for cell name {$name}!");
5959
}
6060
return $matches[1];
6161
}
6262

63+
/**
64+
* Column name (e.g., `'D'`).
65+
* @throws XlsxFastEditorXmlException
66+
*/
67+
public function column(): string
68+
{
69+
return self::nameToColumn($this->name());
70+
}
71+
6372
/**
6473
* Access the previous existing cell, if any, `null` otherwise.
6574
* ℹ️ This is a faster method than `XlsxFastEditorRow::getCellOrNull()`

tests/test.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
try {
1616
$xlsxFastEditor = new XlsxFastEditor(__DIR__ . '/_copy.xlsx');
1717

18-
assert($xlsxFastEditor->getWorksheetCount() === 2);
18+
assert($xlsxFastEditor->getWorksheetCount() === 3);
1919

2020
date_default_timezone_set('UTC');
2121
assert($xlsxFastEditor->getWorkbookDateSystem() === 1900);
@@ -63,6 +63,10 @@
6363
assert($xlsxFastEditor->getRow($sheet1, 3)?->getLastCell()?->name() === 'F3');
6464
assert($xlsxFastEditor->getLastRow($sheet1)?->number() === 4);
6565

66+
$sheet3 = $xlsxFastEditor->getWorksheetNumber('Sheet3');
67+
assert($sheet3 === 3);
68+
assert($xlsxFastEditor->getHighestColumnName($sheet3) === 'G');
69+
6670
$row4 = $xlsxFastEditor->getRow($sheet1, 4);
6771
assert($row4 !== null);
6872
assert($row4->getPreviousRow()?->getNextRow()?->number() === 4);

tests/test.xlsx

838 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)