# FRangeProtectionRule

- Human documentation: [https://docs.univer.ai/reference/facade/range-protection-rule](https://docs.univer.ai/reference/facade/range-protection-rule)

- Agent Markdown: [https://docs.univer.ai/reference/facade/range-protection-rule.md](https://docs.univer.ai/reference/facade/range-protection-rule.md)

- Requested language: `en-US`

- Content language: `en-US`

- Documentation version: `1.0.0-rc.0`

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

---

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

Implementation class for range protection rules
Encapsulates operations on a single protection rule

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

## Overview

### @univerjs/sheets

| Method                                            | Description                                                        |
| ------------------------------------------------- | ------------------------------------------------------------------ |
| [`canDelete`](#candelete)                         | Check if the current user can delete this protection rule          |
| [`canEdit`](#canedit)                             | Check if the current user can edit this range                      |
| [`canManageCollaborator`](#canmanagecollaborator) | Check if the current user can manage collaborators for this range  |
| [`canView`](#canview)                             | Check if the current user can view this range                      |
| [`getPoint`](#getpoint)                           | Get the value of a specific permission point                       |
| [`getSnapshot`](#getsnapshot)                     | Get the current permission snapshot                                |
| [`remove`](#remove)                               | Delete the current protection rule                                 |
| [`setPoint`](#setpoint)                           | Set a specific permission point for the range rule (low-level API) |
| [`updateRanges`](#updateranges)                   | Update the protected ranges                                        |

## APIs

### Getters & Queries

### `canDelete`

Check if the current user can delete this protection rule.

**Signature**

```typescript
canDelete(): boolean
```

**Returns**

* `boolean` — true if the user can delete the rule, false otherwise.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Check if the first rule allows deleting the rule
const rule = rules[0];
if (rule?.canDelete()) {
  console.log(`You can delete this protection rule for this range ${rule.ranges.map(r => r.getA1Notation()).join(', ')}`);
}
```

Source: 

`@univerjs/sheets`

### `canEdit`

Check if the current user can edit this range.

**Signature**

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

**Returns**

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

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Check if the first rule allows editing
const rule = rules[0];
if (rule?.canEdit()) {
  console.log(`You can edit this range ${rule.ranges.map(r => r.getA1Notation()).join(', ')}`);
}
```

Source: 

`@univerjs/sheets`

### `canManageCollaborator`

Check if the current user can manage collaborators for this range.

**Signature**

```typescript
canManageCollaborator(): boolean
```

**Returns**

* `boolean` — true if the user can manage collaborators, false otherwise.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Check if the first rule allows managing collaborators
const rule = rules[0];
if (rule?.canManageCollaborator()) {
  console.log(`You can manage collaborators for this range ${rule.ranges.map(r => r.getA1Notation()).join(', ')}`);
}
```

Source: 

`@univerjs/sheets`

### `canView`

Check if the current user can view this range.

**Signature**

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

**Returns**

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

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Check if the first rule allows viewing
const rule = rules[0];
if (rule?.canView()) {
  console.log(`You can view this range ${rule.ranges.map(r => r.getA1Notation()).join(', ')}`);
}
```

Source: 

`@univerjs/sheets`

### `getPoint`

Get the value of a specific permission point.

**Signature**

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

**Parameters**

* `point` `RangePermissionPoint` — *No description*

**Returns**

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

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Check if the first rule allows editing
if (rules.length > 0) {
  const rule = rules[0];
  const canEdit = rule.getPoint(univerAPI.Enum.RangePermissionPoint.Edit);
  console.log(canEdit);
}
```

Source: 

`@univerjs/sheets`

### `getSnapshot`

Get the current permission snapshot.

**Signature**

```typescript
getSnapshot(): RangePermissionSnapshot
```

**Returns**

* `RangePermissionSnapshot` — Snapshot of all permission points.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Get the permission snapshot of the first rule
if (rules.length > 0) {
  const rule = rules[0];
  const snapshot = rule.getSnapshot();
  console.log(snapshot);
}
```

Source: 

`@univerjs/sheets`

### Setters & Modifiers

### `setPoint`

Set a specific permission point for the range rule (low-level API).

Important: This method only updates the permission point value for an existing protection rule.
It does NOT create permission checks that will block actual editing operations.
You must call `protect()` first to create a protection rule before using this method.

This method is useful for:

* Fine-tuning permissions after creating a protection rule with `protect()`
* Dynamically adjusting permissions based on runtime conditions
* Advanced permission management scenarios

**Signature**

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

**Parameters**

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

**Returns**

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

**Tags**

* `@throws` — If no protection rule exists for this range.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A1:B2');
// First, create a protection rule
const rule = await fRange.getRangePermission().protect({ name: 'My Range', allowViewByOthers: true });
// Then you can dynamically update permission points
await rule.setPoint(univerAPI.Enum.RangePermissionPoint.Edit, false); // Now disable edit
await rule.setPoint(univerAPI.Enum.RangePermissionPoint.View, true);  // Ensure view is enabled
```

Source: 

`@univerjs/sheets`

### `updateRanges`

Update the protected ranges.

**Signature**

```typescript
async updateRanges(ranges: FRange[]): Promise<boolean>
```

**Parameters**

* `ranges` `FRange[]` — *No description*

**Returns**

* `Promise<boolean>` — A promise that resolves when the ranges are updated.

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Update the ranges to A1:C3 for the first rule
if (rules.length > 0) {
  const rule = rules[0];
  const result = await rule.updateRanges([fWorksheet.getRange('A1:C3')]);
  console.log(result);
}
```

Source: 

`@univerjs/sheets`

### Actions & Operations

### `remove`

Delete the current protection rule.

**Signature**

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

**Returns**

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

**Examples**

```ts
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
// Remove the first protection rule
if (rules.length > 0) {
  const rule = rules[0];
  const result = await rule.remove();
  console.log(result);
}
```

Source: 

`@univerjs/sheets`
