Skip to content

Commit

Permalink
Add method getHighestColumnName
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex committed Nov 1, 2023
1 parent effffea commit 4cc95de
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ try {
$row = $row->getNextRow();
$row = $xlsxFastEditor->getLastRow($worksheetId1);

$xlsxFastEditor->getHighestColumnName($worksheetId1);

// Methods for rows
$rowNumber = $row->number();
$cell = $row->getCellOrNull('D2');
Expand Down
26 changes: 26 additions & 0 deletions src/XlsxFastEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,32 @@ public static function cellOrderCompare(string $ref1, string $ref2): int
return strcmp($ref1, $ref2);
}

/**
* Gives the name of the highest column used in the spreadsheet (e.g. `BA`),
* or null if there is none.
* @throws XlsxFastEditorFileFormatException
* @throws XlsxFastEditorXmlException
*/
public function getHighestColumnName(int $sheetNumber): ?string
{
$rightMostCell = '';
$xpath = $this->getXPathFromPath(self::getWorksheetPath($sheetNumber));
$cs = $xpath->query("/o:worksheet/o:sheetData/o:row/o:c[last()]");
if ($cs !== false) {
for ($i = 0; $i < $cs->length; $i++) {
$c = $cs[$i];
if (!($c instanceof \DOMElement)) {
throw new XlsxFastEditorXmlException("Error querying XML fragment for row {$sheetNumber}!");
}
$cellName = $c->getAttribute('r');
if ($cellName !== '' && ($rightMostCell === '' || self::cellOrderCompare($rightMostCell, $cellName) < 0)) {
$rightMostCell = $cellName;
}
}
}
return $rightMostCell === '' ? null : XlsxFastEditorCell::nameToColumn($rightMostCell);
}

/** To return `null` when accessing a row or cell that does not exist, e.g. via {@see XlsxFastEditor::getCell()} */
public const ACCESS_MODE_NULL = 0;
/** To throw an exception when accessing a row or cell that does not exist, e.g. via {@see XlsxFastEditor::getCell()} */
Expand Down
17 changes: 13 additions & 4 deletions src/XlsxFastEditorCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,26 @@ public function name(): string
}

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

/**
* Column name (e.g., `'D'`).
* @throws XlsxFastEditorXmlException
*/
public function column(): string
{
return self::nameToColumn($this->name());
}

/**
* Access the previous existing cell, if any, `null` otherwise.
* ℹ️ This is a faster method than `XlsxFastEditorRow::getCellOrNull()`
Expand Down
6 changes: 5 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
try {
$xlsxFastEditor = new XlsxFastEditor(__DIR__ . '/_copy.xlsx');

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

date_default_timezone_set('UTC');
assert($xlsxFastEditor->getWorkbookDateSystem() === 1900);
Expand Down Expand Up @@ -63,6 +63,10 @@
assert($xlsxFastEditor->getRow($sheet1, 3)?->getLastCell()?->name() === 'F3');
assert($xlsxFastEditor->getLastRow($sheet1)?->number() === 4);

$sheet3 = $xlsxFastEditor->getWorksheetNumber('Sheet3');
assert($sheet3 === 3);
assert($xlsxFastEditor->getHighestColumnName($sheet3) === 'G');

$row4 = $xlsxFastEditor->getRow($sheet1, 4);
assert($row4 !== null);
assert($row4->getPreviousRow()?->getNextRow()?->number() === 4);
Expand Down
Binary file modified tests/test.xlsx
Binary file not shown.

0 comments on commit 4cc95de

Please sign in to comment.