Class: FWorksheetPermission
Implementation class for WorksheetPermission Provides worksheet-level permission control
Properties
| Property | Modifier | Type | Description |
|---|---|---|---|
|
|
|
Observable stream of permission snapshot changes (BehaviorSubject) Emits immediately on subscription with current state, then on any permission point change | |
|
|
|
Observable stream of individual permission point changes Emits when a specific permission point value changes | |
|
|
|
Observable stream of range protection rule changes Emits when protection rules are added, updated, or deleted | |
|
|
|
Observable stream of current range protection rules list (BehaviorSubject) Emits immediately on subscription with current rules, then auto-updates when rules change |
Methods
applyConfig()
applyConfig(config): Promise<void>Apply a permission configuration to the worksheet.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | IWorksheetPermissionConfig | The configuration to apply. |
Returns
Promise<void>
A promise that resolves when the configuration is applied.
Example
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(): booleanCheck if the worksheet is editable.
Returns
boolean
true if the worksheet can be edited, false otherwise.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
if (permission?.canEdit()) {
console.log('Worksheet is editable');
}canEditCell()
canEditCell(row, col): booleanCheck if a specific cell can be edited.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | Row index. |
col | number | Column index. |
Returns
boolean
true if the cell can be edited, false otherwise.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
const canEdit = permission?.canEditCell(0, 0);
console.log(canEdit);canViewCell()
canViewCell(_row, _col): booleanCheck if a specific cell can be viewed.
Parameters
| Parameter | Type | Description |
|---|---|---|
_row | number | Row index (unused, for API consistency). |
_col | number | Column index (unused, for API consistency). |
Returns
boolean
true if the cell can be viewed, false otherwise.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
const canView = permission?.canViewCell(0, 0);
console.log(canView);debugCellPermission()
debugCellPermission(row, col): FRangeProtectionRuleDebug cell permission information.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | Row index. |
col | number | Column index. |
Returns
Debug information about which rules affect this cell, or null if no rules apply.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
const debugInfo = permission?.debugCellPermission(0, 0);
console.log(debugInfo);dispose()
dispose(): voidClean up resources
Returns
void
getPoint()
getPoint(point): booleanGet the value of a specific permission point.
Parameters
| Parameter | Type | Description |
|---|---|---|
point | WorksheetPermissionPoint | The permission point to query. |
Returns
boolean
true if allowed, false if denied.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
const canInsertRow = permission?.getPoint(univerAPI.Enum.WorksheetPermissionPoint.InsertRow);
console.log(canInsertRow);getSnapshot()
getSnapshot(): WorksheetPermissionSnapshotGet a snapshot of all permission points.
Returns
An object containing all permission point values.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
const snapshot = permission?.getSnapshot();
console.log(snapshot);isProtected()
isProtected(): booleanCheck if worksheet is currently protected.
Returns
boolean
true if protected, false otherwise.
Example
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.
Returns
Promise<…[]>
Array of protection rules.
Example
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
options? | IWorksheetProtectionOptions | Protection options including allowed users. |
Returns
Promise<string>
The permissionId for the created protection.
Example
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).
Parameters
| Parameter | Type | Description |
|---|---|---|
configs | { options: …; ranges: …; }[] | Array of protection configurations. |
Returns
Promise<…[]>
Array of created protection rules.
Example
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.
Returns
Promise<void>
A promise that resolves when the mode is set.
Example
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
mode | WorksheetMode | The permission mode to set (‘editable' |
Returns
Promise<void>
A promise that resolves when the mode is set.
Example
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
point | WorksheetPermissionPoint | The permission point to set. |
value | boolean | The value to set (true = allowed, false = denied). |
Returns
Promise<void>
A promise that resolves when the point is set.
Example
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.
Returns
Promise<void>
A promise that resolves when the mode is set.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
await permission?.setReadOnly();subscribe()
subscribe(listener): UnsubscribeFnSubscribe to permission changes (simplified interface for users not familiar with RxJS).
Parameters
| Parameter | Type | Description |
|---|---|---|
listener | (snapshot) => void | Callback function to be called when permissions change. |
Returns
Unsubscribe function.
Example
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.
Returns
Promise<void>
A promise that resolves when protection is removed.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
await permission?.unprotect();unprotectRules()
unprotectRules(ruleIds): Promise<void>Remove multiple protection rules at once.
Parameters
| Parameter | Type | Description |
|---|---|---|
ruleIds | string[] | Array of rule IDs to remove. |
Returns
Promise<void>
A promise that resolves when the rules are removed.
Example
const worksheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
const permission = worksheet?.getWorksheetPermission();
await permission?.unprotectRules(['rule1', 'rule2']);