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
- Pass a Dimension::UOM_* constant instead of a raw string
- Normalize external input before calling: lowercase it and map synonyms (inch→in, points→pt) onto the six accepted keys
- 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
- Whitelist units at your API boundary; never pass raw user strings to toUnit()
- Map common synonyms once (inch→in, points→pt, pixels→px) before calling
- Add a unit-vocabulary regression test alongside the Dimension constants so new spellings fail loudly
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
- #VALUE!
- Item is not a drawing
- No wizard exists for this CF rule type
- A Worksheet has already been assigned. Drawings can only exi
- Invalid protocol for linked drawing
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/9f13b7370095d61b.
Report an issue: GitHub.