PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
Cell coordinate can not be zero-length string.
Error message
Cell coordinate can not be zero-length string.
What it means
removeComment() rejects coordinates that are empty after sheet-prefix trimming. An empty string can neither address an existing comment nor serve as a storage key, so removeComment('') (or 'Sheet1!', which Functions::trimSheetFromCellReference() reduces to '') throws before the comments map is consulted.
Source
Thrown at src/PhpSpreadsheet/Worksheet/Worksheet.php:2975
/**
* Remove comment from cell.
*
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*
* @return $this
*/
public function removeComment(CellAddress|string|array $cellCoordinate): self
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));
if (Coordinate::coordinateIsRange($cellAddress)) {
throw new Exception('Cell coordinate string can not be a range of cells.');
} elseif (str_contains($cellAddress, '$')) {
throw new Exception('Cell coordinate string must not be absolute.');
} elseif ($cellAddress == '') {
throw new Exception('Cell coordinate can not be zero-length string.');
}
// Check if we have a comment for this cell and delete it
if (isset($this->comments[$cellAddress])) {
unset($this->comments[$cellAddress]);
}
return $this;
}
/**
* Get comment for cell.
*
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
*/
public function getComment(CellAddress|string|array $cellCoordinate, bool $attachNew = true): Comment
{
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));View on GitHub (pinned to 65b080eef4)
Solutions
- Guard for empty strings before calling and skip blank entries.
- When concatenating sheet-prefixed refs, validate the cell part is non-empty.
- Default missing config to a real coordinate or branch around the call.
Example fix
// before
$sheet->removeComment($headers['note_col'] ?? ''); // '' when key missing -> throws
// after
$noteCol = $headers['note_col'] ?? null;
if (is_string($noteCol) && $noteCol !== '') {
$sheet->removeComment($noteCol);
} Defensive patterns
Strategy: validation
Validate before calling
if ($coord === null || $coord === '' || str_ends_with($coord, '!')) {
// no usable cell address: skip
} else {
$sheet->removeComment($coord);
} Type guard
/** Empty strings and bare sheet prefixes ('Sheet1!') are not cells. */
function isNonEmptyCoordinate(?string $address): bool
{
return is_string($address) && $address !== '' && !str_ends_with($address, '!');
} Prevention
- Skip blank coordinates in loops over optional config instead of calling anyway.
- When concatenating 'Sheet!' . $cell, assert $cell is non-empty first.
- Treat missing config keys as 'no comment to remove', not as an empty-string call.
When it happens
Trigger: removeComment(''); removeComment('Summary!') where only the sheet prefix was present; coordinates built from missing array keys or null variables stringified to ''.
Common situations: Iterating header maps or user-supplied column lists where some keys are absent; dynamic code that assembles coordinates from possibly-empty variables.
Related errors
- Cell coordinate string must not be absolute.
- Column is not within the table range.
- Freeze pane can not be set on a range of cells.
- Rows can only be inserted before at least row 1.
- Column references should not be numeric.
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/cfcd80aa69aef03a.
Report an issue: GitHub.