PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Sheet not found for name: {$worksheetReference[0]}

Error message

Sheet not found for name: {$worksheetReference[0]}

What it means

In Worksheet's coordinate resolver (used by getCell() and friends), when a coordinate contains '!' the part before it is treated as a sheet name and looked up with $this->getParentOrThrow()->getSheetByName(). If that lookup returns null — no worksheet with that name in this workbook — the method throws instead of guessing a target.

Source

Thrown at src/PhpSpreadsheet/Worksheet/Worksheet.php:1249

     * Get the correct Worksheet and coordinate from a coordinate that may
     * contains reference to another sheet or a named range.
     *
     * @return array{0: Worksheet, 1: string}
     */
    private function getWorksheetAndCoordinate(string $coordinate): array
    {
        $sheet = null;
        $finalCoordinate = null;

        // Worksheet reference?
        if (str_contains($coordinate, '!')) {
            $worksheetReference = self::extractSheetTitle($coordinate, true, true);

            $sheet = $this->getParentOrThrow()->getSheetByName($worksheetReference[0]);
            $finalCoordinate = strtoupper($worksheetReference[1]);

            if ($sheet === null) {
                throw new Exception('Sheet not found for name: ' . $worksheetReference[0]);
            }
        } elseif (
            !Preg::isMatch('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $coordinate)
            && Preg::isMatch('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/iu', $coordinate)
        ) {
            // Named range?
            $namedRange = $this->validateNamedRange($coordinate, true);
            if ($namedRange !== null) {
                $sheet = $namedRange->getWorksheet();
                if ($sheet === null) {
                    throw new Exception('Sheet not found for named range: ' . $namedRange->getName());
                }

                $cellCoordinate = ltrim(substr($namedRange->getValue(), (int) strrpos($namedRange->getValue(), '!')), '!');
                $finalCoordinate = str_replace('$', '', $cellCoordinate);
            }
        }

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Verify existence first: if ($spreadsheet->getSheetByName($name) === null) { handle } else { $sheet->getCell("$name!A1"); }
  2. Quote sheet names containing spaces: getCell("'My Sheet'!A1")
  3. Fix the reference: recreate/rename the target sheet, or update the stored reference string after renames

Example fix

// before
$cell = $sheet->getCell('Summary!A1'); // throws if 'Summary' does not exist

// after
if ($spreadsheet->getSheetByName('Summary') !== null) {
    $cell = $sheet->getCell('Summary!A1');
}
Defensive patterns

Strategy: validation

Validate before calling

if ($spreadsheet->getSheetByName($sheetName) !== null) {
    $cell = $sheet->getCell("$sheetName!A1");
} else {
    // create the sheet or fix the reference
}

Try / catch

try {
    $cell = $sheet->getCell($ref);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    if (str_contains($e->getMessage(), 'Sheet not found for name:')) {
        // skip or repair the reference
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: $sheet->getCell('Summary!A1') when no sheet 'Summary' exists; referencing a sheet by an old name after it was removed/renamed; typos in the sheet portion. (getSheetByName is case-insensitive, but existence is required.)

Common situations: Cross-sheet formulas resolved after the referenced sheet was deleted; user-typed references like 'My Sheet!A1' without quotes around the spaced name; importing partial workbooks that lost a sheet.

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/67a706dc3270a8e6. Report an issue: GitHub.