Class: FWorkbookPermission
Implementation class for WorkbookPermission Provides workbook-level permission control
Implements
Properties
| Property | Modifier | Type | Description |
|---|---|---|---|
|
|
|
Observable stream of collaborator changes Emits when collaborators are added, updated, or removed | |
|
|
|
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 |
Methods
addCollaborator()
addCollaborator(user, role): Promise<void>Add a single collaborator.
Parameters
| Parameter | Type | Description |
|---|---|---|
user | User$1 | The user information (userID, name, avatar). |
role | UnitRole$1 | The role to assign. |
Returns
Promise<void>
A promise that resolves when the collaborator is added.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.addCollaborator(
{ userID: 'user1', name: 'John Doe', avatar: 'https://...' },
univerAPI.Enum.UnitRole.Editor
);Implementation of
IWorkbookPermission.addCollaborator
canEdit()
canEdit(): booleanCheck if the workbook is editable.
Returns
boolean
true if the workbook can be edited, false otherwise.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
if (permission?.canEdit()) {
console.log('Workbook is editable');
}Implementation of
dispose()
dispose(): voidClean up resources
Returns
void
getPoint()
getPoint(point): booleanGet the value of a specific permission point.
Parameters
| Parameter | Type | Description |
|---|---|---|
point | WorkbookPermissionPoint | The permission point to query. |
Returns
boolean
true if allowed, false if denied.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
const canPrint = permission?.getPoint(univerAPI.Enum.WorkbookPermissionPoint.Print);
console.log(canPrint);Implementation of
getSnapshot()
getSnapshot(): WorkbookPermissionSnapshotGet a snapshot of all permission points.
Returns
An object containing all permission point values.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
const snapshot = permission?.getSnapshot();
console.log(snapshot);Implementation of
IWorkbookPermission.getSnapshot
listCollaborators()
listCollaborators(): Promise<...[]>List all collaborators of the workbook.
Returns
Promise<…[]>
Array of collaborators with their roles.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
const collaborators = await permission?.listCollaborators();
console.log(collaborators);Implementation of
IWorkbookPermission.listCollaborators
removeCollaborator()
removeCollaborator(userId): Promise<void>Remove a collaborator from the workbook.
Parameters
| Parameter | Type | Description |
|---|---|---|
userId | string | The user ID to remove. |
Returns
Promise<void>
A promise that resolves when the collaborator is removed.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.removeCollaborator('user1');Implementation of
IWorkbookPermission.removeCollaborator
removeCollaborators()
removeCollaborators(userIds): Promise<void>Remove multiple collaborators at once.
Parameters
| Parameter | Type | Description |
|---|---|---|
userIds | string[] | Array of user IDs to remove. |
Returns
Promise<void>
A promise that resolves when the collaborators are removed.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.removeCollaborators(['user1', 'user2']);Implementation of
IWorkbookPermission.removeCollaborators
setCollaborators()
setCollaborators(collaborators): Promise<void>Set multiple collaborators at once (replaces existing collaborators).
Parameters
| Parameter | Type | Description |
|---|---|---|
collaborators | { role: …; user: …; }[] | Array of collaborators with user information and role. |
Returns
Promise<void>
A promise that resolves when the collaborators are set.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.setCollaborators([
{
user: { userID: 'user1', name: 'John Doe', avatar: 'https://...' },
role: univerAPI.Enum.UnitRole.Editor
},
{
user: { userID: 'user2', name: 'Jane Smith', avatar: '' },
role: univerAPI.Enum.UnitRole.Reader
}
]);Implementation of
IWorkbookPermission.setCollaborators
setEditable()
setEditable(): Promise<void>Set the workbook to editable mode (editor mode).
Returns
Promise<void>
A promise that resolves when the mode is set.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.setEditable();Implementation of
IWorkbookPermission.setEditable
setMode()
setMode(mode): Promise<void>Listen to permission point changes /** Set permission mode for the workbook.
Parameters
| Parameter | Type | Description |
|---|---|---|
mode | WorkbookMode | The permission mode to set (‘owner' |
Returns
Promise<void>
A promise that resolves when the mode is set.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.setMode('editor');Implementation of
setPoint()
setPoint(point, value): Promise<void>Set a specific permission point.
Parameters
| Parameter | Type | Description |
|---|---|---|
point | WorkbookPermissionPoint | 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 workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.setPoint(univerAPI.Enum.WorkbookPermissionPoint.Print, false);Implementation of
setReadOnly()
setReadOnly(): Promise<void>Set the workbook to read-only mode (viewer mode).
Returns
Promise<void>
A promise that resolves when the mode is set.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.setReadOnly();Implementation of
IWorkbookPermission.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 workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
const unsubscribe = permission?.subscribe((snapshot) => {
console.log('Permission changed:', snapshot);
});
// Later, to stop listening:
unsubscribe?.();Implementation of
updateCollaborator()
updateCollaborator(user, role): Promise<void>Update an existing collaborator’s role and information.
Parameters
| Parameter | Type | Description |
|---|---|---|
user | User$1 | The updated user information (userID, name, avatar). |
role | UnitRole$1 | The new role to assign. |
Returns
Promise<void>
A promise that resolves when the collaborator is updated.
Example
const workbook = univerAPI.getActiveWorkbook();
const permission = workbook?.getWorkbookPermission();
await permission?.updateCollaborator(
{ userID: 'user1', name: 'John Doe Updated', avatar: 'https://...' },
univerAPI.Enum.UnitRole.Reader
);