PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception

Invalid visibility value.

Error message

Invalid visibility value.

What it means

Spreadsheet::setVisibility(?string) accepts exactly three workbook visibility states — 'visible', 'hidden', 'veryHidden' (the Spreadsheet::VISIBILITY_* constants, mirroring Excel's VBA project/ workbook visibility model). null defaults to 'visible'; anything else throws 'Invalid visibility value.'

Source

Thrown at src/PhpSpreadsheet/Spreadsheet.php:1680

     *  - 'hidden' (self::VISIBILITY_HIDDEN):
     *       Workbook window is hidden, but can be shown by the user
     *       via the user interface
     *  - 'veryHidden' (self::VISIBILITY_VERY_HIDDEN):
     *       Workbook window is hidden and cannot be shown in the
     *       user interface.
     *
     * @param null|string $visibility visibility status of the workbook
     */
    public function setVisibility(?string $visibility): void
    {
        if ($visibility === null) {
            $visibility = self::VISIBILITY_VISIBLE;
        }

        if (in_array($visibility, self::WORKBOOK_VIEW_VISIBILITY_VALUES)) {
            $this->visibility = $visibility;
        } else {
            throw new Exception('Invalid visibility value.');
        }
    }

    /**
     * Get the ratio between the workbook tabs bar and the horizontal scroll bar.
     * TabRatio is assumed to be out of 1000 of the horizontal window width.
     *
     * @return int Ratio between the workbook tabs bar and the horizontal scroll bar
     */
    public function getTabRatio(): int
    {
        return $this->tabRatio;
    }

    /**
     * Set the ratio between the workbook tabs bar and the horizontal scroll bar
     * TabRatio is assumed to be out of 1000 of the horizontal window width.
     *

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Use the constants: $spreadsheet->setVisibility(Spreadsheet::VISIBILITY_VERYHIDDEN);
  2. Whitelist input against Spreadsheet::VISIBILITY_VISIBLE / VISIBILITY_HIDDEN / VISIBILITY_VERYHIDDEN and reject early.
  3. If absence should mean visible, pass null (the setter defaults it) rather than an empty string.

Example fix

// before
$spreadsheet->setVisibility('very-hidden'); // throws

// after
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$spreadsheet->setVisibility(Spreadsheet::VISIBILITY_VERYHIDDEN);
Defensive patterns

Strategy: validation

Validate before calling

use PhpOffice\PhpSpreadsheet\Spreadsheet;
$allowed = [Spreadsheet::VISIBILITY_VISIBLE, Spreadsheet::VISIBILITY_HIDDEN, Spreadsheet::VISIBILITY_VERYHIDDEN];
if (!in_array($visibility, $allowed, true)) {
    throw new \InvalidArgumentException('visibility must be one of: ' . implode(', ', $allowed));
}
$spreadsheet->setVisibility($visibility);

Type guard

function isValidVisibility(string $v): bool
{
    return in_array($v, [
        \PhpOffice\PhpSpreadsheet\Spreadsheet::VISIBILITY_VISIBLE,
        \PhpOffice\PhpSpreadsheet\Spreadsheet::VISIBILITY_HIDDEN,
        \PhpOffice\PhpSpreadsheet\Spreadsheet::VISIBILITY_VERYHIDDEN,
    ], true);
}

Try / catch

try {
    $spreadsheet->setVisibility($visibility);
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    $spreadsheet->setVisibility(null); // defaults to 'visible'
}

Prevention

When it happens

Trigger: setVisibility('Visible') or 'VISIBLE' (wrong case); 'invisible'/'minimized' (made-up states); forwarding raw user/DB strings without validation; copy-pasting 'veryHidden' as 'veryhidden' or 'very-hidden'.

Common situations: Config files or DB enum columns whose values drifted from the allowed set; UI dropdowns with labels ('Hidden from users') passed instead of values; code written against another library's vocabulary (e.g. 'minimized') or older docs.

Related errors


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