Protect sheet in Angular Spreadsheet component
2 Jul 202424 minutes to read
Sheet protection helps you to prevent the users from modifying the data in the spreadsheet.
Protect Sheet
Protect sheet feature helps you to prevent the unknown users from accidentally changing, editing, moving, or deleting data in a spreadsheet. And you can also protect the sheet with password.
You can use the isProtected
property to enable or disable the Protecting functionality.
- The default value for
isProtected
property isfalse
.
By default in protected sheet, selecting, formatting, inserting, deleting functionalities are disabled. To enable some of the above said functionalities the protectSettings
options are used in a protected spreadsheet.
The available protectSettings
options in spreadsheet are,
Options | Uses |
---|---|
Select Cells |
Used to perform Cell Selection. |
Format Cells |
Used to perform Cell formatting. |
Format Rows |
Used to perform Row formatting. |
Format Columns |
Used to perform Column formatting. |
Insert Link |
Used to perform Hyperlink Insertions. |
- The default value for all
protectSettings
options arefalse
.
By default, the Protect Sheet
module is injected internally into the Spreadsheet to perform sheet protection function.
User Interface:
In the active Spreadsheet, the sheet protection can be done by any of the following ways:
- Select the Protect Sheet item in the Ribbon toolbar under the Data Tab, and then select your desired options.
- Right-click the sheet tab, select the Protect Sheet item in the context menu, and then select your desired options.
- Use the
protectSheet()
method programmatically.
The following example shows Protect Sheet
functionality with password in the Spreadsheet control.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet'
import { Component, ViewChild } from '@angular/core';
import { SpreadsheetComponent } from '@syncfusion/ej2-angular-spreadsheet';
import { enableRipple } from '@syncfusion/ej2-base';
import { dataSource1, dataSource2 } from './datasource';
enableRipple(true);
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: `<ejs-spreadsheet #spreadsheet (created)="created()">
<e-sheets>
<e-sheet name="Budget" [isProtected]="true" [protectSettings]="{ selectCells: true }">
<e-ranges>
<e-range [dataSource]="budgetData"></e-range>
</e-ranges>
<e-columns>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
</e-columns>
</e-sheet>
<e-sheet name="Salary">
<e-ranges>
<e-range [dataSource]="salaryData"></e-range>
</e-ranges>
<e-columns>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>`
})
export class AppComponent {
@ViewChild('spreadsheet')
spreadsheetObj: SpreadsheetComponent | undefined;
budgetData: object[] = dataSource1;
salaryData: object[] = dataSource2;
created() {
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:D1');
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold' }, 'A11:D11');
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'Salary!A1:D1');
this.spreadsheetObj!.protectSheet(1, { selectCells: true }, "syncfusion"); // protect sheet with password
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Limitations of Protect sheet
- Password encryption is not supported
Unprotect Sheet
Unprotect sheet is used to enable all the functionalities that are already disabled in a protected spreadsheet.
User Interface:
In the active Spreadsheet, the sheet Unprotection can be done by any of the following ways:
- Select the
Unprotect Sheet
item in the Ribbon toolbar under the Data Tab. - Right-click the sheet tab, select the
Unprotect Sheet
item in the context menu. - Use the
unprotectSheet()
method programmatically.
Unlock the particular cells in the protected sheet
In protected spreadsheet, to make some particular cell or range of cells are editable, you can use lockCells()
method, with the parameter range
and isLocked
property as false.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet'
import { Component, ViewChild } from '@angular/core';
import { SpreadsheetComponent } from '@syncfusion/ej2-angular-spreadsheet';
import { Dialog } from '@syncfusion/ej2-popups';
import { enableRipple } from '@syncfusion/ej2-base';
import { dataSource1, dataSource2 } from './datasource';
enableRipple(true);
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: `<button class="e-btn" style="margin: 5px 0;" (click)="btnClick()">
Unlock cells</button>
<div id="dialog"></div>
<ejs-spreadsheet #spreadsheet id="spreadsheet" (created)="created()">
<e-sheets>
<e-sheet name="Budget" [isProtected]="true" [protectSettings]="{ selectCells: true }">
<e-ranges>
<e-range [dataSource]="budgetData"></e-range>
</e-ranges>
<e-columns>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
</e-columns>
</e-sheet>
<e-sheet name="Salary">
<e-ranges>
<e-range [dataSource]="salaryData"></e-range>
</e-ranges>
<e-columns>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
<e-column [width]=100></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>`
})
export class AppComponent {
@ViewChild('spreadsheet')
spreadsheetObj: SpreadsheetComponent | undefined;
dialogObj: Dialog | undefined;
budgetData: object[] = dataSource1;
salaryData: object[] = dataSource2;
created() {
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:D1');
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold'}, 'A11:D11');
this.spreadsheetObj!.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'Salary!A1:D1');
// Creating dialog component,
this.dialogObj = new Dialog({
header: 'Spreadsheet',
target: document.getElementById('spreadsheet') as any,
content: '"A1:F3" range of cells has been unlocked.',
showCloseIcon: true,
visible: false,
width: '500px',
buttons: [{
click: this.lockCells.bind(this), buttonModel: { content: 'Ok', isPrimary: true }
}],
});
this.dialogObj.appendTo('#dialog');
}
btnClick (): void {
this.dialogObj!.show();
}
lockCells(): void {
this.spreadsheetObj!.lockCells('A1:F3', false);
this.dialogObj!.hide();
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Make cells read-only without protecting worksheet
Previously, you could make cells read-only by protecting the entire sheet using the protectSheet method or through the UI option. Meanwhile, to make a specific range of cells editable within a protected sheet, you needed to use the lockCells method, passing the range
parameter and setting the isLocked
property to false.
Now, you can make an entire row, an entire column, or a specific range of cells read-only using the setRangeReadOnly method without protecting the entire sheet. This method accepts three parameters, as detailed in the following table:
Parameter | Description | |
---|---|---|
readOnly |
Specifies whether an entire row, an entire column, or a specific range of cells should be set as read-only (true) or editable (false). | . |
range |
Specifies the particular range of cells to be set as read-only. | |
sheetIndex |
Specifies the index of the sheet. |
You can make an entire row, an entire column, or a specific range of cells read-only by passing the range as shown in the code snippet below:
// To set read-only for single cell.
spreadsheet.setRangeReadOnly(true, 'A2', 0)
// To set read-only for range of cells.
spreadsheet.setRangeReadOnly(true, 'A2:B5', 0)
// To set read-only for entire row.
spreadsheet.setRangeReadOnly(true, '3:3', 0)
// To set read-only for entire column.
spreadsheet.setRangeReadOnly(true, 'A:A', 0)
You can make the cells read-only in the cell data binding by setting the isReadOnly
property to true for the respective rows, columns, and cells. Please refer to the code snippet below to see how to set cells to read-only in the cell data binding:
<ejs-spreadsheet>
<e-sheets>
<e-sheet>
<e-rows>
<!-- To set read-only for entire row. -->
<e-row [index]="3" [isReadOnly]="true"></e-row>
<e-row [index]="4">
<e-cells>
<!-- To set read-only for the cell. -->
<e-cell [index]="4" [isReadOnly]="true"></e-cell>
</e-cells>
</e-row>
</e-rows>
<e-columns>
<!-- To set read-only for entire column. -->
<e-column [isReadOnly]="true"></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>
The following example demonstrates how to make rows, columns, and cells read-only without protecting the sheet:
import { NgModule, ViewChild } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule, SpreadsheetComponent, created } from '@syncfusion/ej2-angular-spreadsheet'
import { Component } from '@angular/core';
import { getDefaultData } from './datasource';
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: `<div class="control-section">
<button className="e-btn custom-btn" (click)="readOnlyRow()">Make Row 2 readOnly</button>
<button className="e-btn custom-btn" (click)="readOnlyCell()">Make E5 cell readOnly</button>
<button className="e-btn custom-btn" (click)="removeReadOnly()">Remove readOnly</button>
<button className="e-btn custom-btn" (click)="readOnlyCol()">Make Column A readOnly</button>
<ejs-spreadsheet #spreadsheet [openUrl]="openUrl" [saveUrl]="saveUrl">
<e-sheets>
<e-sheet name="Car Sales Report">
<e-ranges>
<e-range [dataSource]="data"></e-range>
</e-ranges>
<e-rows>
<e-row [index]="3" [isReadOnly]="true"></e-row>
<e-row [index]="4">
<e-cells>
<e-cell [index]="5" [isReadOnly]="true"></e-cell>
</e-cells>
</e-row>
</e-rows>
<e-columns>
<e-column [width]="180"></e-column>
<e-column [width]="130"></e-column>
<e-column [isReadOnly]="true" [width]="130"></e-column>
<e-column [width]="180"></e-column>
<e-column [width]="130"></e-column>
<e-column [width]="120"></e-column>
</e-columns>
</e-sheet>
</e-sheets>
</ejs-spreadsheet>
</div>`
})
export class AppComponent {
constructor() {
}
@ViewChild('spreadsheet')
public spreadsheetObj!: SpreadsheetComponent;
public data: Object[] = getDefaultData();
public openUrl = 'https://services.syncfusion.com/angular/production/api/spreadsheet/open';
public saveUrl = 'https://services.syncfusion.com/angular/production/api/spreadsheet/save';
readOnlyRow(): void {
this.spreadsheetObj.setRangeReadOnly(true, '2:2', this.spreadsheetObj.activeSheetIndex);
}
readOnlyCol(): void {
this.spreadsheetObj.setRangeReadOnly(true, 'A:A', this.spreadsheetObj.activeSheetIndex);
}
readOnlyCell(): void {
this.spreadsheetObj.setRangeReadOnly(true, 'E5:E5', this.spreadsheetObj.activeSheetIndex);
}
removeReadOnly(): void {
this.spreadsheetObj.setRangeReadOnly(false, '2:2', this.spreadsheetObj.activeSheetIndex);
this.spreadsheetObj.setRangeReadOnly(false, 'A:A', this.spreadsheetObj.activeSheetIndex);
this.spreadsheetObj.setRangeReadOnly(false, 'E5:E5', this.spreadsheetObj.activeSheetIndex);
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js'
bootstrapApplication(AppComponent)
.catch((err) => console.error(err));
Protect Workbook
Protect workbook feature helps you to protect the workbook so that users cannot insert, delete, rename, hide the sheets in the spreadsheet.
You can use the password
property to protect workbook with password.
You can use the isProtected
property to protect or unprotect the workbook without the password.
The default value for
isProtected
property isfalse
.
User Interface:
In the active Spreadsheet, you can protect the worksheet by selecting the Data tab in the Ribbon toolbar and choosing the Protect Workbook
item. Then, enter the password and confirm it and click on OK.
The following example shows Protect Workbook
by using the isProtected
property in the Spreadsheet control.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet'
import { Component, OnInit,ViewChild } from '@angular/core';
import { SpreadsheetComponent } from '@syncfusion/ej2-angular-spreadsheet';
import { data } from './datasource';
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: "<ejs-spreadsheet #spreadsheet [isProtected]='true'> <e-sheets> <e-sheet> <e-ranges> <e-range [dataSource]='data'></e-range></e-ranges><e-columns><e-column [width]=90></e-column><e-column [width]=100></e-column><e-column [width]=96></e-column><e-column [width]=120></e-column><e-column [width]=130></e-column><e-column [width]=120></e-column></e-columns></e-sheet></e-sheets></ejs-spreadsheet>"
})
export class AppComponent implements OnInit {
public data?: object[];
@ViewChild('spreadsheet') public spreadsheetObj?: SpreadsheetComponent;
ngOnInit(): void {
this.data = data;
}
};
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
The following example shows Protect Workbook
by using the password
property in the Spreadsheet control. To unprotect the workbook, click the unprotect workbook button in the data tab and provide the password as syncfusion in the dialog box.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SpreadsheetAllModule } from '@syncfusion/ej2-angular-spreadsheet'
import { Component, OnInit,ViewChild } from '@angular/core';
import { SpreadsheetComponent } from '@syncfusion/ej2-angular-spreadsheet';
import { data } from './datasource';
@Component({
imports: [
SpreadsheetAllModule
],
standalone: true,
selector: 'app-container',
template: "<ejs-spreadsheet #spreadsheet password='syncfusion'> <e-sheets> <e-sheet> <e-ranges> <e-range [dataSource]='data'></e-range></e-ranges><e-columns><e-column [width]=90></e-column><e-column [width]=100></e-column><e-column [width]=96></e-column><e-column [width]=120></e-column><e-column [width]=130></e-column><e-column [width]=120></e-column></e-columns></e-sheet></e-sheets></ejs-spreadsheet>"
})
export class AppComponent implements OnInit {
public data?: object[];
@ViewChild('spreadsheet') public spreadsheetObj?: SpreadsheetComponent;
ngOnInit(): void {
this.data = data;
}
};
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Unprotect Workbook
Unprotect Workbook is used to enable the insert, delete, rename, move, copy, hide or unhide sheets feature in the spreadsheet.
User Interface:
In the active Spreadsheet, the workbook Unprotection can be done in any of the following ways:
- Select the
Unprotect Workbook
item in the Ribbon toolbar under the Data Tab and provide the valid password in the dialog box.
Note
You can refer to our Angular Spreadsheet feature tour page for its groundbreaking feature representations. You can also explore our Angular Spreadsheet example to knows how to present and manipulate data.