FWorksheetPermission

GitHubEdit on GitHub
Packages@univerjs/sheets
CORE

Implementation class for WorksheetPermission Provides worksheet-level permission control

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

Overview

@univerjs/sheets

MethodDescription
applyConfigApply a permission configuration to the worksheet
canEditCheck if the worksheet is editable
canEditCellCheck if a specific cell can be edited
canViewCheck if the worksheet is viewable
canViewCellCheck if a specific cell can be viewed
debugCellPermissionDebug cell permission information
getPointGet the value of a specific permission point
getSnapshotGet a snapshot of all permission points
isProtectedCheck if worksheet is currently protected
listRangeProtectionRulesList all range protection rules for the worksheet
protectCreate worksheet protection with collaborators support
protectRangesProtect multiple ranges at once (batch operation)
setEditableSet the worksheet to editable mode
setModeSet permission mode for the worksheet
setPointSet a specific permission point for the worksheet
setReadOnlySet the worksheet to read-only mode
unprotectRemove worksheet protection
unprotectRulesRemove multiple protection rules at once

APIs

Getters & Queries

canEdit

Check if the worksheet is editable.

Signature

canEdit(): boolean

Returns

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

Examples

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

canEditCell

Check if a specific cell can be edited.

Signature

canEditCell(row: number, col: number): boolean

Parameters

  • row numberNo description
  • col numberNo description

Returns

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

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Check if cell C3 can be edited
const fRange = fWorksheet.getRange('C3');
const canEdit = fWorksheet.getWorksheetPermission().canEditCell(fRange.getRow(), fRange.getColumn());
console.log(canEdit);
Source: @univerjs/sheets

canView

Check if the worksheet is viewable.

Signature

canView(): boolean

Returns

  • boolean — true if the worksheet can be viewed, false otherwise.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
if (fWorksheet.getWorksheetPermission().canView()) {
  console.log('Worksheet is viewable');
}
Source: @univerjs/sheets

canViewCell

Check if a specific cell can be viewed.

Signature

canViewCell(row: number, col: number): boolean

Parameters

  • row numberNo description
  • col numberNo description

Returns

  • boolean — true if the cell can be viewed, false otherwise.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Check if cell C3 can be viewed
const fRange = fWorksheet.getRange('C3');
const canView = fWorksheet.getWorksheetPermission().canViewCell(fRange.getRow(), fRange.getColumn());
console.log(canView);
Source: @univerjs/sheets

getPoint

Get the value of a specific permission point.

Signature

getPoint(point: WorksheetPermissionPoint): boolean

Parameters

  • point WorksheetPermissionPointNo description

Returns

  • boolean — true if allowed, false if denied.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const permission = fWorksheet.getWorksheetPermission();
const canInsertRow = permission.getPoint(univerAPI.Enum.WorksheetPermissionPoint.InsertRow);
console.log(canInsertRow);
Source: @univerjs/sheets

getSnapshot

Get a snapshot of all permission points.

Signature

getSnapshot(): WorksheetPermissionSnapshot

Returns

  • WorksheetPermissionSnapshot — An object containing all permission point values.

Examples

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

isProtected

Check if worksheet is currently protected.

Signature

isProtected(): boolean

Returns

  • boolean — true if protected, false otherwise.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
if (fWorksheet.getWorksheetPermission().isProtected()) {
  console.log('Worksheet is protected');
}
Source: @univerjs/sheets

listRangeProtectionRules

List all range protection rules for the worksheet.

Signature

async listRangeProtectionRules(options?: {
            ignoreCollaborators?: boolean; // Option to ignore fetching collaborators for performance
        }): Promise<FRangeProtectionRule[]>

Parameters

  • options { ignoreCollaborators?: boolean; } (optional)No description

Returns

  • Promise<FRangeProtectionRule[]> — Array of protection rules.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().listRangeProtectionRules();
console.log(rules);
Source: @univerjs/sheets

Setters & Modifiers

applyConfig

Apply a permission configuration to the worksheet.

Signature

async applyConfig(config: IWorksheetPermissionConfig): Promise<void>

Parameters

  • config IWorksheetPermissionConfigNo description

Returns

  • Promise<void> — A promise that resolves when the configuration is applied.

Examples

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
  }
});
Source: @univerjs/sheets

setEditable

Set the worksheet to editable mode.

Signature

async setEditable(): Promise<void>

Returns

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

Examples

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

setMode

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

Signature

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

Parameters

  • mode WorksheetModeNo description

Returns

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

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
await fWorksheet.getWorksheetPermission().setMode('readOnly');
Source: @univerjs/sheets

setPoint

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

Signature

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

Parameters

  • point WorksheetPermissionPointNo description
  • value booleanNo description

Returns

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

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const permission = fWorksheet.getWorksheetPermission();
await permission.setPoint(univerAPI.Enum.WorksheetPermissionPoint.InsertRow, false);
Source: @univerjs/sheets

setReadOnly

Set the worksheet to read-only mode.

Signature

async setReadOnly(): Promise<void>

Returns

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

Examples

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

Miscellaneous

debugCellPermission

Debug cell permission information.

Signature

async debugCellPermission(row: number, col: number): Promise<FRangeProtectionRule | undefined>

Parameters

  • row numberNo description
  • col numberNo description

Returns

  • Promise<FRangeProtectionRule> — Debug information about which rules affect this cell, or null if no rules apply.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Get debug info for cell C3
const fRange = fWorksheet.getRange('C3');
const debugInfo = await fWorksheet.getWorksheetPermission().debugCellPermission(fRange.getRow(), fRange.getColumn());
console.log(debugInfo);
Source: @univerjs/sheets

protect

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

Signature

async protect(options?: IWorksheetProtectionOptions): Promise<string>

Parameters

  • options IWorksheetProtectionOptions (optional)No description

Returns

  • Promise<string> — The permissionId for the created protection.

Examples

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

protectRanges

Protect multiple ranges at once (batch operation).

Signature

async protectRanges(configs: Array<{
            ranges: FRange[];
            options?: IRangeProtectionOptions;
        }>): Promise<FRangeProtectionRule[]>

Parameters

  • configs { ranges: FRange[]; options?: IRangeProtectionOptions; }[]No description

Returns

  • Promise<FRangeProtectionRule[]> — Array of created protection rules.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const rules = await fWorksheet.getWorksheetPermission().protectRanges([
  {
    ranges: [fWorksheet.getRange('A1:B2')],
    options: { name: 'Protected Area 1', allowedUsers: ['user1', 'user2'], allowViewByOthers: true }
  },
  {
    ranges: [fWorksheet.getRange('C3:D4')],
    options: { name: 'Protected Area 2', allowViewByOthers: false }
  }
]);
console.log(rules);
Source: @univerjs/sheets

unprotect

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

Signature

async unprotect(): Promise<boolean>

Returns

  • Promise<boolean> — A promise that resolves when protection is removed.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
await fWorksheet.getWorksheetPermission().unprotect();
Source: @univerjs/sheets

unprotectRules

Remove multiple protection rules at once.

Signature

async unprotectRules(ruleIds: string[]): Promise<boolean>

Parameters

  • ruleIds string[]No description

Returns

  • Promise<boolean> — A promise that resolves when the rules are removed.

Examples

const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const worksheetPermission = fWorksheet.getWorksheetPermission();
const rules = await worksheetPermission.listRangeProtectionRules();
// Unprotect the first rule as an example
if (rules.length > 0) {
  const result = await worksheetPermission.unprotectRules([rules[0].id]);
  console.log(result);
}
Source: @univerjs/sheets