Skip to content

Commit

Permalink
Fix sheet reference (#16)
Browse files Browse the repository at this point in the history
fix #15
Fix `XlsxFastEditor::getWorksheetNumber()` which was not returning the proper number in case of workbooks in which sheets have been reordered or removed.
  • Loading branch information
Alkarex authored Jul 2, 2024
1 parent 9434749 commit ef1d6b6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ parameters:
missingCheckedExceptionInThrows: true
tooWideThrowType: true
implicitThrows: false
ignoreErrors:
- '#Only booleans are allowed in (a negated boolean|a ternary operator condition|an elseif condition|an if condition|&&|\|\|), (bool|false|int(<[0-9, max]+>)?|true|null|\|)+ given.*#'
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
24 changes: 20 additions & 4 deletions src/XlsxFastEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class XlsxFastEditor
private const CALC_CHAIN_CACHE_PATH = 'xl/calcChain.xml';
private const SHARED_STRINGS_PATH = 'xl/sharedStrings.xml';
private const WORKBOOK_PATH = 'xl/workbook.xml';
private const WORKBOOK_RELS_PATH = 'xl/_rels/workbook.xml.rels';

private \ZipArchive $zip;

Expand Down Expand Up @@ -280,9 +281,24 @@ public function getWorksheetCount(): int
public function getWorksheetNumber(string $sheetName): int
{
$xpath = $this->getXPathFromPath(self::WORKBOOK_PATH);
$sheetId = $xpath->evaluate("normalize-space(/o:workbook/o:sheets/o:sheet[@name='$sheetName'][1]/@sheetId)");
if (is_string($sheetId)) {
return (int)$sheetId;
$xpath->registerNamespace('r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
$rId = $xpath->evaluate("normalize-space(/o:workbook/o:sheets/o:sheet[@name='$sheetName'][1]/@r:id)");
if (!is_string($rId) || $rId === '') {
return -1;
}

try {
$xpath = $this->getXPathFromPath(self::WORKBOOK_RELS_PATH);
$xpath->registerNamespace('pr', 'http://schemas.openxmlformats.org/package/2006/relationships');
$target = $xpath->evaluate("normalize-space(/pr:Relationships/pr:Relationship[@Id='$rId'][1]/@Target)");
if (is_string($target) && preg_match('/(\d+)/i', $target, $matches)) {
return (int)$matches[1];
}
} catch (XlsxFastEditorFileFormatException $ex) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
}

if (preg_match('/(\d+)/i', $rId, $matches)) {
return (int)$matches[1];
}
return -1;
}
Expand All @@ -297,7 +313,7 @@ public function getWorksheetNumber(string $sheetName): int
public function getWorksheetName(int $sheetNumber): ?string
{
$xpath = $this->getXPathFromPath(self::WORKBOOK_PATH);
$sheetName = $xpath->evaluate("normalize-space(/o:workbook/o:sheets/o:sheet[$sheetNumber][1]/@name)");
$sheetName = $xpath->evaluate("normalize-space(/o:workbook/o:sheets/o:sheet[$sheetNumber]/@name)");
return is_string($sheetName) ? $sheetName : null;
}

Expand Down
5 changes: 2 additions & 3 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
assert(XlsxFastEditor::excelDateToDateTime(44865, 1904)->format('c') === '2026-11-01T00:00:00+00:00');

$sheet1 = $xlsxFastEditor->getWorksheetNumber('Sheet1');
assert($sheet1 === 1);
assert($sheet1 === 3);

assert($xlsxFastEditor->deleteRow($sheet1, 5) === true);

Expand All @@ -42,7 +42,6 @@
assert($xlsxFastEditor->readHyperlink($sheet1, 'C3') === null);

$sheet2 = $xlsxFastEditor->getWorksheetNumber('Sheet2');
assert($sheet2 === 2);
assert($xlsxFastEditor->getWorksheetName($sheet2) === 'Sheet2');

assert($xlsxFastEditor->readFormula($sheet2, 'c2') === '=Sheet1!C2*2');
Expand All @@ -66,7 +65,7 @@
assert($xlsxFastEditor->getLastRow($sheet1)?->number() === 4);

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

$row4 = $xlsxFastEditor->getRow($sheet1, 4);
Expand Down
Binary file modified tests/test.xlsx
Binary file not shown.

0 comments on commit ef1d6b6

Please sign in to comment.