Skip to Content
ClassesFWorksheetPermission

类: FWorksheetPermission

Implementation class for WorksheetPermission Provides worksheet-level permission control

属性

属性修饰符类型描述

permission$

readonly

Observable<WorksheetPermissionSnapshot>

Observable stream of permission snapshot changes (BehaviorSubject) Emits immediately on subscription with current state, then on any permission point change

pointChange$

readonly

Observable<{ oldValue: …; point: …; value: …; }>

Observable stream of individual permission point changes Emits when a specific permission point value changes

rangeProtectionChange$

readonly

Observable<{ rules: …; type: …; }>

Observable stream of range protection rule changes Emits when protection rules are added, updated, or deleted

rangeProtectionRules$

readonly

Observable<…[]>

Observable stream of current range protection rules list (BehaviorSubject) Emits immediately on subscription with current rules, then auto-updates when rules change

方法

applyConfig()

applyConfig(config): Promise<void>

Apply a permission configuration to the worksheet.

参数

参数类型描述
configIWorksheetPermissionConfigThe configuration to apply.

返回

Promise<void>

A promise that resolves when the configuration is applied.

示例

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 } });

canEdit()

canEdit(): boolean

Check if the worksheet is editable.

返回

boolean

true if the worksheet can be edited, false otherwise.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); if (permission?.canEdit()) { console.log('Worksheet is editable'); }

canEditCell()

canEditCell(row, col): boolean

Check if a specific cell can be edited.

参数

参数类型描述
rownumberRow index.
colnumberColumn index.

返回

boolean

true if the cell can be edited, false otherwise.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); const canEdit = permission?.canEditCell(0, 0); console.log(canEdit);

canViewCell()

canViewCell(_row, _col): boolean

Check if a specific cell can be viewed.

参数

参数类型描述
_rownumberRow index (unused, for API consistency).
_colnumberColumn index (unused, for API consistency).

返回

boolean

true if the cell can be viewed, false otherwise.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); const canView = permission?.canViewCell(0, 0); console.log(canView);

debugCellPermission()

debugCellPermission(row, col): FRangeProtectionRule

Debug cell permission information.

参数

参数类型描述
rownumberRow index.
colnumberColumn index.

返回

FRangeProtectionRule

Debug information about which rules affect this cell, or null if no rules apply.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); const debugInfo = permission?.debugCellPermission(0, 0); console.log(debugInfo);

dispose()

dispose(): void

Clean up resources

返回

void


getPoint()

getPoint(point): boolean

Get the value of a specific permission point.

参数

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

返回

boolean

true if allowed, false if denied.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); const canInsertRow = permission?.getPoint(univerAPI.Enum.WorksheetPermissionPoint.InsertRow); console.log(canInsertRow);

getSnapshot()

getSnapshot(): WorksheetPermissionSnapshot

Get a snapshot of all permission points.

返回

WorksheetPermissionSnapshot

An object containing all permission point values.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); const snapshot = permission?.getSnapshot(); console.log(snapshot);

isProtected()

isProtected(): boolean

Check if worksheet is currently protected.

返回

boolean

true if protected, false otherwise.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); if (permission?.isProtected()) { console.log('Worksheet is protected'); }

listRangeProtectionRules()

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

List all range protection rules for the worksheet.

返回

Promise<…[]>

Array of protection rules.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); const rules = await permission?.listRangeProtectionRules(); console.log(rules);

protect()

protect(options?): Promise<string>

Create worksheet protection with collaborators support. This must be called before setting permission points for collaboration to work.

参数

参数类型描述
options?IWorksheetProtectionOptionsProtection options including allowed users.

返回

Promise<string>

The permissionId for the created protection.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.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');

protectRanges()

protectRanges(configs): Promise<...[]>

Protect multiple ranges at once (batch operation).

参数

参数类型描述
configs{ options: …; ranges: …; }[]Array of protection configurations.

返回

Promise<…[]>

Array of created protection rules.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); const rules = await permission?.protectRanges([ { ranges: [worksheet.getRange('A1:B2')], options: { name: 'Protected Area 1', allowEdit: false, allowViewByOthers: true } }, { ranges: [worksheet.getRange('C3:D4')], options: { name: 'Protected Area 2', allowEdit: true, allowViewByOthers: false } } ]); console.log(rules);

setEditable()

setEditable(): Promise<void>

Set the worksheet to editable mode.

返回

Promise<void>

A promise that resolves when the mode is set.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); await permission?.setEditable();

setMode()

setMode(mode): Promise<void>

Set permission mode for the worksheet. Automatically creates worksheet protection if not already protected.

参数

参数类型描述
modeWorksheetModeThe permission mode to set (‘editable'

返回

Promise<void>

A promise that resolves when the mode is set.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); await permission?.setMode('readOnly');

setPoint()

setPoint(point, value): Promise<void>

Set a specific permission point for the worksheet. Automatically creates worksheet protection if not already protected.

参数

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

返回

Promise<void>

A promise that resolves when the point is set.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); await permission?.setPoint(univerAPI.Enum.WorksheetPermissionPoint.InsertRow, false);

setReadOnly()

setReadOnly(): Promise<void>

Set the worksheet to read-only mode.

返回

Promise<void>

A promise that resolves when the mode is set.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); await permission?.setReadOnly();

subscribe()

subscribe(listener): UnsubscribeFn

Subscribe to permission changes (simplified interface for users not familiar with RxJS).

参数

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

返回

UnsubscribeFn

Unsubscribe function.

示例

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

unprotect()

unprotect(): Promise<void>

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

返回

Promise<void>

A promise that resolves when protection is removed.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); await permission?.unprotect();

unprotectRules()

unprotectRules(ruleIds): Promise<void>

Remove multiple protection rules at once.

参数

参数类型描述
ruleIdsstring[]Array of rule IDs to remove.

返回

Promise<void>

A promise that resolves when the rules are removed.

示例

const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet(); const permission = worksheet?.getWorksheetPermission(); await permission?.unprotectRules(['rule1', 'rule2']);