PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Column is not within the table range.

Error message

Column is not within the table range.

What it means

Thrown by Table::setColumn() when the argument is neither a non-empty string nor a Table\Column instance: the else branch of its type dispatch. Note PHP empty() treats both '' and '0' as empty, so even a string column letter of '0' (not a valid letter anyway) lands here.

Source

Thrown at src/PhpSpreadsheet/Worksheet/Table.php:448

        $pColumn = Coordinate::stringFromColumnIndex($rangeStart[0] + $columnOffset);

        return $this->getColumn($pColumn);
    }

    /**
     * Set Table.
     *
     * @param string|Table\Column $columnObjectOrString
     *            A simple string containing a Column ID like 'A' is permitted
     */
    public function setColumn(string|Table\Column $columnObjectOrString): self
    {
        if ((is_string($columnObjectOrString)) && (!empty($columnObjectOrString))) {
            $column = $columnObjectOrString;
        } elseif ($columnObjectOrString instanceof Table\Column) {
            $column = $columnObjectOrString->getColumnIndex();
        } else {
            throw new PhpSpreadsheetException('Column is not within the table range.');
        }
        $this->isColumnInRange($column);

        if (is_string($columnObjectOrString)) {
            $this->columns[$columnObjectOrString] = new Table\Column($columnObjectOrString, $this);
        } else {
            $columnObjectOrString->setTable($this);
            $this->columns[$column] = $columnObjectOrString;
        }
        ksort($this->columns);

        return $this;
    }

    /**
     * Clear a specified Table Column.
     *
     * @param string $column Column name (e.g. A)

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Pass a real column letter string like 'A' or a Table\Column instance
  2. Skip/guard empty values before the call: if ($col !== '') { $table->setColumn($col); }
  3. When the letter comes from data, map it through Coordinate::stringFromColumnIndex() from a numeric index you control

Example fix

// before
$letter = $sheet->getCellByColumnAndRow(1, 1)->getValue(); // may be ''
$table->setColumn($letter); // throws when empty

// after
$letter = Coordinate::stringFromColumnIndex($colIndex);
if ($letter !== '') {
    $table->setColumn($letter);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (is_string($col) && $col !== '' && $col !== '0') {
    $table->setColumn($col);
} elseif ($col instanceof \PhpOffice\PhpSpreadsheet\Worksheet\Table\Column) {
    $table->setColumn($col);
}

Type guard

/** @param mixed $col */
function isValidTableColumn($col): bool
{
    return ($col instanceof \PhpOffice\PhpSpreadsheet\Worksheet\Table\Column)
        || (is_string($col) && $col !== '' && $col !== '0');
}

Prevention

When it happens

Trigger: $table->setColumn('') with an empty string (e.g. a column letter variable that was never assigned); $table->setColumn('0'); passing an object that is not PhpSpreadsheet\Worksheet\Table\Column (a look-alike DTO or an AutoFilter column).

Common situations: Deriving the column letter from an empty header cell (getCell(...)->getValue() returning null/'') and passing it straight through; mixing AutoFilter\Column and Table\Column objects in shared code.

Related errors


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