# 锁定保护

启用工作表保护后，用户在 Excel 中将无法编辑被保护的单元格。保护可以选择是否带密码，也可以与 `Format::unlocked()` 配合，单独放开特定行、列或单元格的编辑权限。

## 函数原型

```php
protection(?string $password = null): self
```

### **string $password**

> 可选参数，解除保护所需的密码。省略时工作表仍然处于保护状态，但任何人都可以在 Excel 界面中无密码地解除保护。

## 子章节

* [密码保护](/zhong-wen-jian-ti/protection/password.md)
* [解除保护](/zhong-wen-jian-ti/protection/unlock.md)

## 示例

```php
$config = ['path' => './tests'];
$excel  = new \Vtiful\Kernel\Excel($config);

$excel->fileName('tutorial.xlsx')
    ->header(['name', 'age'])
    ->data([
        ['viest', 21],
        ['wjx',   21]
    ])
    ->protection('viest') // 设置密码为 viest
    ->output();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xlswriter-docs.viest.me/zhong-wen-jian-ti/protection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
