# FWorksheetPermission

> Language fallback: requested `zh-CN`; content is `en-US`.

- Human documentation: [https://docs.univer.ai/zh-CN/reference/facade/worksheet-permission](https://docs.univer.ai/zh-CN/reference/facade/worksheet-permission)

- Agent Markdown: [https://docs.univer.ai/zh-CN/reference/facade/worksheet-permission.md](https://docs.univer.ai/zh-CN/reference/facade/worksheet-permission.md)

- Requested language: `zh-CN`

- Content language: `en-US`

- Documentation version: `1.0.0-rc.0`

- Source: [facade/worksheet-permission.mdx](https://github.com/dream-num/documentation/blob/dev/content/reference/facade/worksheet-permission.mdx)

---

| Packages | `@univerjs/sheets` |
| -------- | ------------------ |

Implementation class for WorksheetPermission
Provides worksheet-level permission control

> This class should not be instantiated directly. Use factory methods on `univerAPI` instead.

## Overview

### @univerjs/sheets

| Method                                                  | Description                                            |
| ------------------------------------------------------- | ------------------------------------------------------ |
| [`applyConfig`](#applyconfig)                           | Apply a permission configuration to the worksheet      |
| [`canEdit`](#canedit)                                   | Check if the worksheet is editable                     |
| [`canEditCell`](#caneditcell)                           | Check if a specific cell can be edited                 |
| [`canView`](#canview)                                   | Check if the worksheet is viewable                     |
| [`canViewCell`](#canviewcell)                           | Check if a specific cell can be viewed                 |
| [`debugCellPermission`](#debugcellpermission)           | Debug cell permission information                      |
| [`getPoint`](#getpoint)                                 | Get the value of a specific permission point           |
| [`getSnapshot`](#getsnapshot)                           | Get a snapshot of all permission points                |
| [`isProtected`](#isprotected)                           | Check if worksheet is currently protected              |
| [`listRangeProtectionRules`](#listrangeprotectionrules) | List all range protection rules for the worksheet      |
| [`protect`](#protect)                                   | Create worksheet protection with collaborators support |
| [`protectRanges`](#protectranges)                       | Protect multiple ranges at once (batch operation)      |
| [`setEditable`](#seteditable)                           | Set the worksheet to editable mode                     |
| [`setMode`](#setmode)                                   | Set permission mode for the worksheet                  |
| [`setPoint`](#setpoint)                                 | Set a specific permission point for the worksheet      |
| [`setReadOnly`](#setreadonly)                           | Set the worksheet to read-only mode                    |
| [`unprotect`](#unprotect)                               | Remove worksheet protection                            |
| [`unprotectRules`](#unprotectrules)                     | Remove multiple protection rules at once               |

## APIs

### Getters & Queries

### `canEdit`

Check if the worksheet is editable.

**Signature**

```typescript
canEdit(): boolean
```

**Returns**

* `boolean` — true if the worksheet can be edited, false otherwise.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
if (fWorksheet.getWorksheetPermission().canEdit()) {
  console.log('Worksheet is editable');
}
```

Source: 

`@univerjs/sheets`

### `canEditCell`

Check if a specific cell can be edited.

**Signature**

```typescript
canEditCell(row: number, col: number): boolean
```

**Parameters**

* `row` `number` — *No description*
* `col` `number` — *No description*

**Returns**

* `boolean` — true if the cell can be edited, false otherwise.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Check if cell C3 can be edited
const fRange = fWorksheet.getRange('C3');
const canEdit = fWorksheet.getWorksheetPermission().canEditCell(fRange.getRow(), fRange.getColumn());
console.log(canEdit);
```

Source: 

`@univerjs/sheets`

### `canView`

Check if the worksheet is viewable.

**Signature**

```typescript
canView(): boolean
```

**Returns**

* `boolean` — true if the worksheet can be viewed, false otherwise.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
if (fWorksheet.getWorksheetPermission().canView()) {
  console.log('Worksheet is viewable');
}
```

Source: 

`@univerjs/sheets`

### `canViewCell`

Check if a specific cell can be viewed.

**Signature**

```typescript
canViewCell(row: number, col: number): boolean
```

**Parameters**

* `row` `number` — *No description*
* `col` `number` — *No description*

**Returns**

* `boolean` — true if the cell can be viewed, false otherwise.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Check if cell C3 can be viewed
const fRange = fWorksheet.getRange('C3');
const canView = fWorksheet.getWorksheetPermission().canViewCell(fRange.getRow(), fRange.getColumn());
console.log(canView);
```

Source: 

`@univerjs/sheets`

### `getPoint`

Get the value of a specific permission point.

**Signature**

```typescript
getPoint(point: WorksheetPermissionPoint): boolean
```

**Parameters**

* `point` `WorksheetPermissionPoint` — *No description*

**Returns**

* `boolean` — true if allowed, false if denied.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const permission = fWorksheet.getWorksheetPermission();
const canInsertRow = permission.getPoint(univerAPI.Enum.WorksheetPermissionPoint.InsertRow);
console.log(canInsertRow);
```

Source: 

`@univerjs/sheets`

### `getSnapshot`

Get a snapshot of all permission points.

**Signature**

```typescript
getSnapshot(): WorksheetPermissionSnapshot
```

**Returns**

* `WorksheetPermissionSnapshot` — An object containing all permission point values.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const snapshot = fWorksheet.getWorksheetPermission().getSnapshot();
console.log(snapshot);
```

Source: 

`@univerjs/sheets`

### `isProtected`

Check if worksheet is currently protected.

**Signature**

```typescript
isProtected(): boolean
```

**Returns**

* `boolean` — true if protected, false otherwise.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
if (fWorksheet.getWorksheetPermission().isProtected()) {
  console.log('Worksheet is protected');
}
```

Source: 

`@univerjs/sheets`

### `listRangeProtectionRules`

List all range protection rules for the worksheet.

**Signature**

```typescript
async listRangeProtectionRules(options?: {
            ignoreCollaborators?: boolean; // Option to ignore fetching collaborators for performance
        }): Promise<FRangeProtectionRule[]>
```

**Parameters**

* `options` `{ ignoreCollaborators?: boolean; }` *(optional)* — *No description*

**Returns**

* `Promise<FRangeProtectionRule[]>` — Array of protection rules.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
console.log(rules);
```

Source: 

`@univerjs/sheets`

### Setters & Modifiers

### `applyConfig`

Apply a permission configuration to the worksheet.

**Signature**

```typescript
async applyConfig(config: IWorksheetPermissionConfig): Promise<void>
```

**Parameters**

* `config` `IWorksheetPermissionConfig` — *No description*

**Returns**

* `Promise<void>` — A promise that resolves when the configuration is applied.

**Examples**

```ts
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
await permission?.applyConfig({
  mode: 'readOnly',
  points: {
    [univerAPI.Enum.WorksheetPermissionPoint.View]: true,
    [univerAPI.Enum.WorksheetPermissionPoint.Edit]: false
  }
});
```

Source: 

`@univerjs/sheets`

### `setEditable`

Set the worksheet to editable mode.

**Signature**

```typescript
async setEditable(): Promise<void>
```

**Returns**

* `Promise<void>` — A promise that resolves when the mode is set.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
await fWorksheet.getWorksheetPermission().setEditable();
```

Source: 

`@univerjs/sheets`

### `setMode`

Set the permission mode for the worksheet.
Creates worksheet protection automatically if it is not already protected.

**Signature**

```typescript
async setMode(mode: WorksheetMode): Promise<void>
```

**Parameters**

* `mode` `WorksheetMode` — *No description*

**Returns**

* `Promise<void>` — A promise that resolves when the mode is set.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
await fWorksheet.getWorksheetPermission().setMode('readOnly');
```

Source: 

`@univerjs/sheets`

### `setPoint`

Set a specific permission point for the worksheet.
Creates worksheet protection automatically if it is not already protected.

**Signature**

```typescript
async setPoint(point: WorksheetPermissionPoint, value: boolean): Promise<void>
```

**Parameters**

* `point` `WorksheetPermissionPoint` — *No description*
* `value` `boolean` — *No description*

**Returns**

* `Promise<void>` — A promise that resolves when the point is set.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const permission = fWorksheet.getWorksheetPermission();
await permission.setPoint(univerAPI.Enum.WorksheetPermissionPoint.InsertRow, false);
```

Source: 

`@univerjs/sheets`

### `setReadOnly`

Set the worksheet to read-only mode.

**Signature**

```typescript
async setReadOnly(): Promise<void>
```

**Returns**

* `Promise<void>` — A promise that resolves when the mode is set.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
await fWorksheet.getWorksheetPermission().setReadOnly();
```

Source: 

`@univerjs/sheets`

### Miscellaneous

### `debugCellPermission`

Debug cell permission information.

**Signature**

```typescript
async debugCellPermission(row: number, col: number): Promise<FRangeProtectionRule | undefined>
```

**Parameters**

* `row` `number` — *No description*
* `col` `number` — *No description*

**Returns**

* `Promise<FRangeProtectionRule>` — Debug information about which rules affect this cell, or null if no rules apply.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Get debug info for cell C3
const fRange = fWorksheet.getRange('C3');
const debugInfo = await fWorksheet.getWorksheetPermission().debugCellPermission(fRange.getRow(), fRange.getColumn());
console.log(debugInfo);
```

Source: 

`@univerjs/sheets`

### `protect`

Create worksheet protection with collaborator support.
This must be called before setting permission points in a collaborative scenario.

**Signature**

```typescript
async protect(options?: IWorksheetProtectionOptions): Promise<string>
```

**Parameters**

* `options` `IWorksheetProtectionOptions` *(optional)* — *No description*

**Returns**

* `Promise<string>` — The permissionId for the created protection.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const permission = fWorksheet.getWorksheetPermission();

// Create worksheet protection with collaborators
const permissionId = await permission.protect({
  allowedUsers: ['user1', 'user2'],
  name: 'My Worksheet Protection'
});

// Now set permission points
await permission?.setMode('readOnly');
```

Source: 

`@univerjs/sheets`

### `protectRanges`

Protect multiple ranges at once (batch operation).

**Signature**

```typescript
async protectRanges(configs: Array<{
            ranges: FRange[];
            options?: IRangeProtectionOptions;
        }>): Promise<FRangeProtectionRule[]>
```

**Parameters**

* `configs` `{ ranges: FRange[]; options?: IRangeProtectionOptions; }[]` — *No description*

**Returns**

* `Promise<FRangeProtectionRule[]>` — Array of created protection rules.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().protectRanges([
  {
    ranges: [fWorksheet.getRange('A1:B2')],
    options: { name: 'Protected Area 1', allowedUsers: ['user1', 'user2'], allowViewByOthers: true }
  },
  {
    ranges: [fWorksheet.getRange('C3:D4')],
    options: { name: 'Protected Area 2', allowViewByOthers: false }
  }
]);
console.log(rules);
```

Source: 

`@univerjs/sheets`

### `unprotect`

Remove worksheet protection.
This deletes the protection rule and resets all permission points to allowed.

**Signature**

```typescript
async unprotect(): Promise<boolean>
```

**Returns**

* `Promise<boolean>` — A promise that resolves when protection is removed.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
await fWorksheet.getWorksheetPermission().unprotect();
```

Source: 

`@univerjs/sheets`

### `unprotectRules`

Remove multiple protection rules at once.

**Signature**

```typescript
async unprotectRules(ruleIds: string[]): Promise<boolean>
```

**Parameters**

* `ruleIds` `string[]` — *No description*

**Returns**

* `Promise<boolean>` — A promise that resolves when the rules are removed.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const worksheetPermission = fWorksheet.getWorksheetPermission();
const rules = await worksheetPermission.listRangeProtectionRules();
// Unprotect the first rule as an example
if (rules.length > 0) {
  const result = await worksheetPermission.unprotectRules([rules[0].id]);
  console.log(result);
}
```

Source: 

`@univerjs/sheets`
