PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

{$unitOfMeasure} is not a vaid unit of measure

Error message

{$unitOfMeasure} is not a vaid unit of measure

What it means

PhpSpreadsheet's Dimension helper converts drawing/cell dimensions between units via toUnit(). The target unit is lowercased and looked up in the fixed ABSOLUTE_UNITS map (cm, mm, in, px, pt, pc — all relative to 96 dpi); any string not in that map throws this exception. Relative/CSS units like '%', 'em', or synonyms like 'inch'/'points' are not accepted (the message even contains an upstream typo, 'vaid').

Source

Thrown at src/PhpSpreadsheet/Helper/Dimension.php:95

    public function width(): float
    {
        return (float) ($this->unit === null)
            ? $this->size
            : round(Drawing::pixelsToCellDimension((int) $this->size, new Font(false)), 4);
    }

    public function height(): float
    {
        return (float) ($this->unit === null)
            ? $this->size
            : $this->toUnit(self::UOM_POINTS);
    }

    public function toUnit(string $unitOfMeasure): float
    {
        $unitOfMeasure = strtolower($unitOfMeasure);
        if (!array_key_exists($unitOfMeasure, self::ABSOLUTE_UNITS)) {
            throw new Exception("{$unitOfMeasure} is not a vaid unit of measure");
        }

        $size = $this->size;
        if ($this->unit === null) {
            $size = Drawing::cellDimensionToPixels($size, new Font(false));
        }

        return $size / self::ABSOLUTE_UNITS[$unitOfMeasure];
    }
}

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Pass a Dimension::UOM_* constant instead of a raw string
  2. Normalize external input before calling: lowercase it and map synonyms (inch→in, points→pt) onto the six accepted keys
  3. If you accept arbitrary units, validate against array keys of Dimension::ABSOLUTE_UNITS and reject early with your own error message

Example fix

// before
$d = new Dimension('1.5in');
$px = $d->toUnit('inch'); // throws: 'inch' is not in ABSOLUTE_UNITS

// after
$d = new Dimension('1.5in');
$px = $d->toUnit(Dimension::UOM_PIXELS); // 144.0
Defensive patterns

Strategy: validation

Validate before calling

$unit = strtolower(trim($inputUnit));
if (!array_key_exists($unit, Dimension::ABSOLUTE_UNITS)) {
    throw new InvalidArgumentException("Unsupported unit '$inputUnit'; expected one of: " . implode(', ', array_keys(Dimension::ABSOLUTE_UNITS)));
}
return $dimension->toUnit($unit);

Type guard

function isSupportedDimensionUnit(string $unit): bool
{
    return array_key_exists(strtolower(trim($unit)), Dimension::ABSOLUTE_UNITS);
}

Prevention

When it happens

Trigger: Calling $dimension->toUnit('inch'), toUnit('%'), toUnit('em'), or piping a raw user-supplied/CSS unit string into toUnit()/width()/height() instead of one of the Dimension::UOM_* constants (UOM_CENTIMETERS, UOM_MILLIMETERS, UOM_INCHES, UOM_PIXELS, UOM_POINTS, UOM_PICA).

Common situations: Building image dimensions from HTML attributes or UI dropdowns where units arrive as 'inches', 'pts', or '%'; assuming the helper accepts the full CSS unit vocabulary; case is handled (strtolower) but spelling is not.

Related errors


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