moodle/moodle · error · PhpSpreadsheetException

The table range must be at least 1 column and row

Error message

The table range must be at least 1 column and row

What it means

Thrown by Table::setRange() (Table.php, around line 296) after the range string has been normalized and parsed via Coordinate::rangeDimension(). It fires when the computed width or height of the range is less than 1, i.e. the range collapses to an empty selection such as a single cell mis-parsed into zero dimension. It is the second stage of range validation: the preceding check ('Table must be set on a range of cells.') already ensured a ':' separator exists; this one ensures the range spans at least one column and one row.

Solutions

  1. Ensure the data set is non-empty before computing and assigning the table range.
  2. Fix the range-computation math so width and height are at least 1.
  3. Check Coordinate::rangeDimension($range) results before calling setRange().

Example fix

// before
$table->setRange($startCol . '1:' . $endCol . '0'); // endRow 0 when data empty
// after
if ($endRow >= 1) {
    $table->setRange($startCol . '1:' . $endCol . $endRow);
}
Defensive patterns

Strategy: validation

Validate before calling

[$w, $h] = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::rangeDimension($range);
if ($w < 1 || $h < 1) {
    throw new InvalidArgumentException("Table range has zero dimension: $range");
}

Try / catch

try {
    $table->setRange($range);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    // skip table creation for empty datasets
}

Prevention

When it happens

Trigger: Calling $table->setRange('A1:A1') reversed or an inverted/empty range yielding zero dimension, e.g. setRange('B2:A1')-style ranges that produce zero width/height from rangeDimension().

Common situations: Computing the range from an empty data set (no rows/columns to include), off-by-one index math producing zero-width spans, or filtering data before defining the table and ending up with nothing.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/0a86c41333ee3252. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/phpspreadsheet/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Table.php:296

        // extract coordinate
        if ($range !== '') {
            [, $range] = Worksheet::extractSheetTitle(Validations::validateCellRange($range), true);
        }
        if (empty($range)) {
            //    Discard all column rules
            $this->columns = [];
            $this->range = '';

            return $this;
        }

        if (!str_contains($range, ':')) {
            throw new PhpSpreadsheetException('Table must be set on a range of cells.');
        }

        [$width, $height] = Coordinate::rangeDimension($range);
        if ($width < 1 || $height < 1) {
            throw new PhpSpreadsheetException('The table range must be at least 1 column and row');
        }

        $this->range = $range;
        $this->autoFilter->setRange($range);

        //    Discard any column rules that are no longer valid within this range
        [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($this->range);
        foreach ($this->columns as $key => $value) {
            $colIndex = Coordinate::columnIndexFromString($key);
            if (($rangeStart[0] > $colIndex) || ($rangeEnd[0] < $colIndex)) {
                unset($this->columns[$key]);
            }
        }

        return $this;
    }

    /**

View on GitHub (pinned to 48ea8c3322)