FWorkbookPermission

GitHubEdit on GitHub
Packages@univerjs/sheets
CORE

Implementation class for WorkbookPermission Provides workbook-level permission control

This class should not be instantiated directly. Use factory methods on univerAPI instead.

Overview

@univerjs/sheets

MethodDescription
addCollaboratorAdd a single collaborator
canEditCheck if the workbook is editable
getPointGet the value of a specific permission point
getSnapshotGet a snapshot of all permission points
listCollaboratorsList all collaborators of the workbook
removeCollaboratorRemove a collaborator from the workbook
removeCollaboratorsRemove multiple collaborators at once
setCollaboratorsSet multiple collaborators at once (replaces existing collaborators)
setEditableSet the workbook to editable mode (editor mode)
setModeSet permission mode for the workbook
setPointSet a specific permission point
setReadOnlySet the workbook to read-only mode (viewer mode)
updateCollaboratorUpdate an existing collaborator's role and information

APIs

Lifecycle & Creation

addCollaborator

Add a single collaborator.

Signature

async addCollaborator(user: ICollaboratorUser, role: UnitRole): Promise<void>

Parameters

  • user ICollaboratorUserNo description
  • role UnitRoleNo description

Returns

  • Promise<void> — A promise that resolves when the collaborator is added.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.getWorkbookPermission();
await permission.addCollaborator(
  { userID: 'user1', name: 'John Doe', avatar: 'https://...' },
  univerAPI.Enum.UnitRole.Editor
);
Source: @univerjs/sheets

Getters & Queries

canEdit

Check if the workbook is editable.

Signature

canEdit(): boolean

Returns

  • boolean — true if the workbook can be edited, false otherwise.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
if (fWorkbook.getWorkbookPermission().canEdit()) {
  console.log('Workbook is editable');
}
Source: @univerjs/sheets

getPoint

Get the value of a specific permission point.

Signature

getPoint(point: WorkbookPermissionPoint): boolean

Parameters

  • point WorkbookPermissionPointNo description

Returns

  • boolean — true if allowed, false if denied.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.getWorkbookPermission();
const canPrint = permission.getPoint(univerAPI.Enum.WorkbookPermissionPoint.Print);
console.log(canPrint);
Source: @univerjs/sheets

getSnapshot

Get a snapshot of all permission points.

Signature

getSnapshot(): WorkbookPermissionSnapshot

Returns

  • WorkbookPermissionSnapshot — An object containing all permission point values.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const snapshot = fWorkbook.getWorkbookPermission().getSnapshot();
console.log(snapshot);
Source: @univerjs/sheets

listCollaborators

List all collaborators of the workbook.

Signature

async listCollaborators(): Promise<ICollaborator[]>

Returns

  • Promise<ICollaborator[]> — Array of collaborators with their roles.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.getWorkbookPermission();
const collaborators = await permission.listCollaborators();
console.log(collaborators);
Source: @univerjs/sheets

Setters & Modifiers

setCollaborators

Set multiple collaborators at once (replaces existing collaborators).

Signature

async setCollaborators(collaborators: Array<{ user: ICollaboratorUser; role: UnitRole }>): Promise<void>

Parameters

  • collaborators { user: ICollaboratorUser; role: UnitRole; }[]No description

Returns

  • Promise<void> — A promise that resolves when the collaborators are set.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.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
  }
]);
Source: @univerjs/sheets

setEditable

Set the workbook to editable mode (editor mode).

Signature

async setEditable(): Promise<void>

Returns

  • Promise<void> — A promise that resolves when the mode is set.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
await fWorkbook.getWorkbookPermission().setEditable();
Source: @univerjs/sheets

setMode

Set permission mode for the workbook.

Signature

async setMode(mode: WorkbookMode): Promise<void>

Parameters

  • mode WorkbookModeNo description

Returns

  • Promise<void> — A promise that resolves when the mode is set.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
await fWorkbook.getWorkbookPermission().setMode('editor');
Source: @univerjs/sheets

setPoint

Set a specific permission point.

Signature

async setPoint(point: WorkbookPermissionPoint, value: boolean): Promise<void>

Parameters

  • point WorkbookPermissionPointNo description
  • value booleanNo description

Returns

  • Promise<void> — A promise that resolves when the point is set.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.getWorkbookPermission();
await permission.setPoint(univerAPI.Enum.WorkbookPermissionPoint.Print, false);
Source: @univerjs/sheets

setReadOnly

Set the workbook to read-only mode (viewer mode).

Signature

async setReadOnly(): Promise<void>

Returns

  • Promise<void> — A promise that resolves when the mode is set.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
await fWorkbook.getWorkbookPermission().setReadOnly();
Source: @univerjs/sheets

updateCollaborator

Update an existing collaborator's role and information.

Signature

async updateCollaborator(user: ICollaboratorUser, role: UnitRole): Promise<void>

Parameters

  • user ICollaboratorUserNo description
  • role UnitRoleNo description

Returns

  • Promise<void> — A promise that resolves when the collaborator is updated.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.getWorkbookPermission();
await permission.updateCollaborator(
  { userID: 'user1', name: 'John Doe Updated', avatar: 'https://...' },
  univerAPI.Enum.UnitRole.Reader
);
Source: @univerjs/sheets

Actions & Operations

removeCollaborator

Remove a collaborator from the workbook.

Signature

async removeCollaborator(userId: string): Promise<void>

Parameters

  • userId stringNo description

Returns

  • Promise<void> — A promise that resolves when the collaborator is removed.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.getWorkbookPermission();
await permission.removeCollaborator('user1');
Source: @univerjs/sheets

removeCollaborators

Remove multiple collaborators at once.

Signature

async removeCollaborators(userIds: string[]): Promise<void>

Parameters

  • userIds string[]No description

Returns

  • Promise<void> — A promise that resolves when the collaborators are removed.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const permission = fWorkbook.getWorkbookPermission();
await permission.removeCollaborators(['user1', 'user2']);
Source: @univerjs/sheets