类: FRangePermission
Implementation class for RangePermission Manages range-level permissions
属性
| 属性 | 修饰符 | 类型 | 描述 |
|---|---|---|---|
|
|
|
Observable stream of permission snapshot changes | |
|
|
|
Observable stream of protection state changes |
方法
canDelete()
canDelete(): booleanCheck 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(): booleanCheck 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(): booleanCheck 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(): booleanCheck 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(): voidClean up resources
返回
void
getPoint()
getPoint(point): booleanGet the value of a specific permission point.
参数
| 参数 | 类型 | 描述 |
|---|---|---|
point | RangePermissionPoint | The 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(): RangePermissionSnapshotGet the current permission snapshot.
返回
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(): booleanCheck 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? | IRangeProtectionOptions | Protection 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
参数
| 参数 | 类型 | 描述 |
|---|---|---|
point | RangePermissionPoint | The permission point to set. |
value | boolean | The 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 enabledsubscribe()
subscribe(listener): () => voidSubscribe to permission changes (simplified interface).
参数
| 参数 | 类型 | 描述 |
|---|---|---|
listener | (snapshot) => void | Callback 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();