Skip to Content
ClassesFRangePermission

类: FRangePermission

Implementation class for RangePermission Manages range-level permissions

属性

属性修饰符类型描述

permission$

readonly

Observable<RangePermissionSnapshot>

Observable stream of permission snapshot changes

protectionChange$

readonly

Observable<… | …>

Observable stream of protection state changes

方法

canDelete()

canDelete(): boolean

Check if the current user can delete this protection rule.

返回

boolean

true if can delete rule, false otherwise.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); if (permission?.canDelete()) { console.log('You can delete this protection rule'); }

canEdit()

canEdit(): boolean

Check if the current user can edit this range.

返回

boolean

true if editable, false otherwise.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); if (permission?.canEdit()) { console.log('You can edit this range'); }

canManageCollaborator()

canManageCollaborator(): boolean

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

返回

boolean

true if can manage collaborators, false otherwise.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); if (permission?.canManageCollaborator()) { console.log('You can manage collaborators for this range'); }

canView()

canView(): boolean

Check if the current user can view this range.

返回

boolean

true if viewable, false otherwise.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); if (permission?.canView()) { console.log('You can view this range'); }

dispose()

dispose(): void

Clean up resources

返回

void


getPoint()

getPoint(point): boolean

Get the value of a specific permission point.

参数

参数类型描述
pointRangePermissionPointThe permission point to query.

返回

boolean

true if allowed, false if denied.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); const canEdit = permission?.getPoint(univerAPI.Enum.RangePermissionPoint.Edit); console.log(canEdit);

getSnapshot()

getSnapshot(): RangePermissionSnapshot

Get the current permission snapshot.

返回

RangePermissionSnapshot

Snapshot of all permission points.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); const snapshot = permission?.getSnapshot(); console.log(snapshot);

isProtected()

isProtected(): boolean

Check if the current range is protected.

返回

boolean

true if protected, false otherwise.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); const isProtected = permission?.isProtected(); console.log(isProtected);

listRules()

listRules(): Promise<...[]>

List all protection rules.

返回

Promise<…[]>

Array of protection rules.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); const rules = await permission?.listRules(); console.log(rules);

protect()

protect(options?): Promise<FRangeProtectionRule>

Protect the current range.

参数

参数类型描述
options?IRangeProtectionOptionsProtection options.

返回

Promise<FRangeProtectionRule>

The created protection rule.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); const rule = await permission?.protect({ name: 'My protected range', allowEdit: true, allowedUsers: ['user1', 'user2'], allowViewByOthers: false, }); console.log(rule);

setPoint()

setPoint(point, value): Promise<void>

Set a specific permission point for the range (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

参数

参数类型描述
pointRangePermissionPointThe permission point to set.
valuebooleanThe value to set (true = allowed, false = denied).

返回

Promise<void>

A promise that resolves when the point is set.

抛出

If no protection rule exists for this range.

示例

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

subscribe()

subscribe(listener): () => void

Subscribe to permission changes (simplified interface).

参数

参数类型描述
listener(snapshot) => voidCallback function to be called when permissions change.

返回

Function

Unsubscribe function.

返回

void

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); const unsubscribe = permission?.subscribe((snapshot) => { console.log('Permission changed:', snapshot); }); // Later, to stop listening: unsubscribe?.();

unprotect()

unprotect(): Promise<void>

Unprotect the current range.

返回

Promise<void>

A promise that resolves when the range is unprotected.

示例

const range = univerAPI.getActiveWorkbook()?.getActiveSheet()?.getRange('A1:B2'); const permission = range?.getRangePermission(); await permission?.unprotect();