Protection

Spread allows you to limit user operations in a workbook. By locking/unlocking cells or making menus visible/invisible you can decide what users can edit or not.

Description
app.component.ts
index.html
app.component.html
styles.css
Copy to CodeMine

Sometimes you don't want the values of cells in a sheet to be edited. If so, you can use the isProtected option to set and get whether the sheet is protected.

       var spread = GC.Spread.Sheets.findControl(document.getElementById('ss'));
       var sheet = spread.getActiveSheet();
       sheet.options.isProtected = true;
       sheet.options.isProtected; // true

If you want to protect the worksheet with a password, you can use protect, unprotect and hasPassword API.

       var spread = GC.Spread.Sheets.findControl(document.getElementById('ss'));
       var sheet = spread.getActiveSheet();
       var password = '<user_input>';
       sheet.protect(password);
       // ...
       if (sheet.hasPassword()) {
              var passwordUnlock = '<user_input>';
              var success = sheet.unprotect(passwordUnlock);
              // ...
       } else {
              sheet.unprotect(); // or sheet.options.isProtect = false;
       }

When a sheet is protected, you can use protectionOptions to limit user actions by providing an object with the following properties:

allowSelectLockedCells: Boolean that represents whether the user can select locked cells.

allowSelectUnlockedCells: Boolean that represents whether the user can select unlocked cells.

allowSort: Boolean that represents whether the user can sort ranges.

allowFilter: Boolean that represents whether the user can filter ranges.

allowEditObjects: Boolean that represents whether the user can edit floating objects.

allowResizeRows: Boolean that represents whether the user can resize rows.

allowResizeColumns: Boolean that represents whether the user can resize columns.

allowDragInsertRows: Boolean that represents whether the user can drag insert rows.

allowDragInsertColumns: Boolean that represents whether the user can drag insert columns.

allowInsertRows: Boolean that represents whether the user can insert rows.

allowInsertColumns: Boolean that represents whether the user can insert columns.

allowDeleteRows: Boolean that represents whether the user can delete rows.

allowDeleteColumns: Boolean that represents whether the user can delete columns.

allowOutlineRows: Boolean that represents whether the user can expand or collapse row outline.

allowOutlineColumns: Boolean that represents whether the user can expand or collapse column outline.

       var spread = GC.Spread.Sheets.findControl(document.getElementById('ss'));
       var sheet = spread.getActiveSheet();
       sheet.options.protectionOptions = {
             allowSelectLockedCells: true, 
             allowSelectUnlockedCells: true,
             allowSort: false 
             allowInsertColumns: true 
       };
       var option = sheet.options.protectionOptions;
       console.log(JSON.stringify(option, null, 2));
Sometimes you don't want the values of cells in a sheet to be edited. If so, you can use the isProtected option to set and get whether the sheet is protected. If you want to protect the worksheet with a password, you can use protect, unprotect and hasPassword API. When a sheet is protected, you can use protectionOptions to limit user actions by providing an object with the following properties: allowSelectLockedCells: Boolean that represents whether the user can select locked cells. allowSelectUnlockedCells: Boolean that represents whether the user can select unlocked cells. allowSort: Boolean that represents whether the user can sort ranges. allowFilter: Boolean that represents whether the user can filter ranges. allowEditObjects: Boolean that represents whether the user can edit floating objects. allowResizeRows: Boolean that represents whether the user can resize rows. allowResizeColumns: Boolean that represents whether the user can resize columns. allowDragInsertRows: Boolean that represents whether the user can drag insert rows. allowDragInsertColumns: Boolean that represents whether the user can drag insert columns. allowInsertRows: Boolean that represents whether the user can insert rows. allowInsertColumns: Boolean that represents whether the user can insert columns. allowDeleteRows: Boolean that represents whether the user can delete rows. allowDeleteColumns: Boolean that represents whether the user can delete columns. allowOutlineRows: Boolean that represents whether the user can expand or collapse row outline. allowOutlineColumns: Boolean that represents whether the user can expand or collapse column outline.
import { Component, NgModule, enableProdMode } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { FormsModule } from '@angular/forms'; import { SpreadSheetsModule } from '@mescius/spread-sheets-angular'; import GC from '@mescius/spread-sheets'; import './styles.css'; const passwordWrongTip = 'Password is not correct!'; const alreadyProtect = 'The worksheet is already protected!'; const unprotectImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB0UlEQVR4nO2YzU7CQBDHNyae9A30qF6NNlFv7QzhqheukN1An4Mbnv14A70YwgERX0XiWRNBKl8eMDGuGWy0LopoS0tl/8kkZLMT/r+dmW1axrS0tAKTYRjziJgCgFNEvAKAR0Tsub9pLUV72DTKsqw9ALhGRDkqaA/tZdOifD4/h4j7PxlX4g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EA4LZuUwTQrA8CNGtpb6G9lA1xtdCvRCqH2IswQAMwXQGhW2iqW4jb0En1LyVnUEkQoBwaoMdglTN4iN28De3nEixDFJL3M/KKI4524jZ05M5HZl5L65/rDc6nQtkkwmMVAAAAAElFTkSuQmCC'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { spread: GC.Spread.Sheets.Workbook; protectSheet = true; selectLockedCells = true; selectUnlockedCells = true; sort = false; filter = true; resizeRows = true; resizeColums = false; editObject = false; dragInsertRows = false; dragInsertColums = false; insrtRows = false; insertColumns = false; deleteRows = false; deleteColumn = false; operateColumnOutline = false; operateRowOutline = false; password: ''; hostStyle = { width: 'calc(100% - 280px)', height: '100%', overflow: 'hidden', float: 'left' }; protectStatus: string = protectImg; applySetting() { let option = { allowSelectLockedCells: this.selectLockedCells, allowSelectUnlockedCells: this.selectUnlockedCells, allowSort: this.sort, allowFilter: this.filter, allowResizeRows: this.resizeRows, allowResizeColumns: this.resizeColums, allowEditObjects: this.editObject, allowDragInsertRows: this.dragInsertRows, allowDragInsertColumns: this.dragInsertColums, allowInsertRows: this.insrtRows, allowInsertColumns: this.insertColumns, allowDeleteRows: this.deleteRows, allowDeleteColumns: this.deleteColumn, allowOutlineColumns: this.operateColumnOutline, allowOutlineRows: this.operateRowOutline, }; let sheet = this.spread.getActiveSheet(); sheet.options.protectionOptions = option; } setselectLockedCells(value: any) { this.selectLockedCells = value.target.checked; } setselectUnlockedCells(value: any) { this.selectUnlockedCells = value.target.checked; } setSort(value: any) { this.sort = value.target.checked; } setfilter(value: any) { this.filter = value.target.checked; } setresizeRows(value: any) { this.resizeRows = value.target.checked; } setresizeColums(value: any) { this.resizeColums = value.target.checked; } seteditObject(value: any) { this.editObject = value.target.checked; } setdragInsertRows(value: any) { this.dragInsertRows = value.target.checked; } setdragInsertColums(value: any) { this.dragInsertColums = value.target.checked; } setinsrtRows(value: any) { this.insrtRows = value.target.checked; } setinsertColumns(value: any) { this.insertColumns = value.target.checked; } setdeleteRows(value: any) { this.deleteRows = value.target.checked; } setdeleteColumn(value: any) { this.deleteColumn = value.target.checked; } setOperateColumnOutline(value: any) { this.operateColumnOutline = value.target.checked; } setOperateRowOutline(value: any) { this.operateRowOutline = value.target.checked; } setProtectSheet(value: any) { let sheet = this.spread.getActiveSheet(); if (sheet.options.isProtected) { alert(alreadyProtect); return ; } const password = this.password; this.password = ''; sheet.protect(password); this.protectSheet = sheet.options.isProtected; this.protectStatus = protectImg; } setUnprotectSheet() { const password = this.password; let sheet = this.spread.getActiveSheet(); if (sheet.hasPassword()) { let success = sheet.unprotect(password); if (!success) { alert(passwordWrongTip); return ; } } else { sheet.unprotect(); } this.password = ''; this.protectSheet = sheet.options.isProtected; this.protectStatus = unprotectImg; } initSpread($event: any) { this.spread = $event.spread; let spread = this.spread; let sheet = spread.getActiveSheet(); spread.suspendPaint(); let salesData = [ ['Salesperson', 'Region'], ['Joe', 'North'], ['Robert', 'South'], ['Michelle', 'East'], ['Erich', 'West'], ['Dafna', 'North'], ['Rob', 'South'], ['Joe', 'North'], ['Robert', 'South'], ['Michelle', 'East'], ['Erich', 'West'], ['Dafna', 'North'], ['Rob', 'South'], ['Joe', 'North'], ['Robert', 'South'], ['Michelle', 'East'] ]; sheet.setArray(0, 0, salesData); sheet.setColumnWidth(0, 120); sheet.setColumnWidth(1, 120); //unlocked cells let style = new GC.Spread.Sheets.Style(); style.locked = false; style.backColor = '#C3C3C3'; sheet.setStyle(-1, 8, style); sheet.setStyle(-1, 9, style); sheet.setStyle(15, -1, style); sheet.setStyle(16, -1, style); sheet.setStyle(8, 2, style); //locked cells let style2 = new GC.Spread.Sheets.Style(); style2.locked = true; style2.backColor = '#F4F8EB'; sheet.setStyle(13, -1, style2); sheet.setStyle(18, 8, style2); sheet.setStyle(0, 0, style2) sheet.setStyle(0, 1, style2); let filter = new GC.Spread.Sheets.Filter.HideRowFilter(new GC.Spread.Sheets.Range(1, 0, 100, 2)); sheet.rowFilter(filter); sheet.comments.add(5, 4, 'locked comment'); sheet.comments.add(22, 4, 'unlocked comment'); sheet.comments.get(5, 4).locked(true).displayMode(1); sheet.comments.get(22, 4).locked(false).displayMode(1).lockText(false); var _commandManager = spread.commandManager(); _commandManager.execute({ cmd: "outlineColumn", sheetName: sheet.name(), index: 11, count: 3 }); _commandManager.execute({ cmd: "outlineRow", sheetName: sheet.name(), index: 18, count: 3 }); spread.resumePaint(); let option = { allowSelectLockedCells: true, allowSelectUnlockedCells: true, allowFilter: true, allowSort: false, allowResizeRows: true, allowResizeColumns: false, allowEditObjects: false, allowDragInsertRows: false, allowDragInsertColumns: false, allowInsertRows: false, allowInsertColumns: false, allowDeleteRows: false, allowDeleteColumns: false }; sheet.options.protectionOptions = option; sheet.options.isProtected = true; } } @NgModule({ imports: [BrowserModule, SpreadSheetsModule, FormsModule], declarations: [AppComponent], exports: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule);
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/angular/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- Polyfills --> <script src="$DEMOROOT$/en/angular/node_modules/core-js/client/shim.min.js"></script> <script src="$DEMOROOT$/en/angular/node_modules/zone.js/fesm2015/zone.min.js"></script> <!-- SystemJS --> <script src="$DEMOROOT$/en/angular/node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.import('@angular/compiler'); System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('$DEMOROOT$/en/lib/angular/license.ts'); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html>
<div class="sample-tutorial"> <gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)"> <gc-worksheet> </gc-worksheet> </gc-spread-sheets> <div class="options-container"> <br /> <div class="option-row" style="height: 25px"> <img id = "protectStatus" style="height: 25px" [src]="protectStatus"/> </div> <div class="option-row"> <label for="protectPassword">Password (optional)</label> <input id="protectPassword" type="password" [(ngModel)]="password"/> <input type="button" value="Protect" id="protectBtn" (click)="setProtectSheet($event)" style="width:100px;" [disabled]="protectSheet"/> <input type="button" value="Unprotect" id="unprotectBtn" (click)="setUnprotectSheet($event)" style="width:100px; margin-left: 5px;" [disabled]="!protectSheet"/> </div> <div> <div class="option-row"> <input id="chkSelectLockedCells" checked (change)="setselectLockedCells($event)" type="checkbox" /> <label for="chkSelectLockedCells" class="sizedLabel">Select locked cells</label> </div> <div class="option-row"> <input id="chkSelectUnlockedCells" checked (change)="setselectUnlockedCells($event)" type="checkbox" /> <label for="chkSelectUnlockedCells" class="sizedLabel">Select unlocked cells</label> </div> <div class="option-row"> <input id="chkAllowSort" type="checkbox" (change)="setSort($event)" /> <label for="chkAllowSort" class="sizedLabel">Sort</label> </div> <div class="option-row"> <input id="chkAllowFilter" type="checkbox" checked (change)="setfilter($event)" /> <label for="chkAllowFilter" class="sizedLabel">Filter</label> </div> <div class="option-row"> <input id="chkAllowResizeRows" type="checkbox" checked (change)="setresizeRows($event)" /> <label for="chkAllowResizeRows" class="sizedLabel">Resize rows</label> </div> <div class="option-row"> <input id="chkAllowResizeColumns" type="checkbox" (change)="setresizeColums($event)"/> <label for="chkAllowResizeColumns" class="sizedLabel">Resize columns</label> </div> <div class="option-row"> <input id="chkAllowEditObjects" type="checkbox" (change)="seteditObject($event)" /> <label for="chkAllowEditObjects" class="sizedLabel">Edit objects</label> </div> <div class="option-row"> <input id="chkAllowDragInsertRows" type="checkbox" (change)="setdragInsertRows($event)" /> <label for="chkAllowDragInsertRows" class="sizedLabel">Drag insert rows</label> </div> <div class="option-row"> <input id="chkAllowDragInsertColumns" type="checkbox" (change)="setdragInsertColums($event)" /> <label for="chkAllowDragInsertColumns" class="sizedLabel">Drag insert columns</label> </div> <div class="option-row"> <input id="chkAllowInsertRows" type="checkbox" (change)="setinsrtRows($event)" /> <label for="chkAllowInsertRows" class="sizedLabel">Insert rows</label> </div> <div class="option-row"> <input id="chkAllowInsertColumns" type="checkbox" (change)="setinsertColumns($event)" /> <label for="chkAllowInsertColumns" class="sizedLabel">Insert columns</label> </div> <div class="option-row"> <input id="chkAllowDeleteRows" type="checkbox" (change)="setdeleteRows($event)" /> <label for="chkAllowDeleteRows" class="sizedLabel">Delete rows</label> </div> <div class="option-row"> <input id="chkAllowDeleteColumns" type="checkbox" (change)="setdeleteColumn($event)"/> <label for="chkAllowDeleteColumns" class="sizedLabel">Delete columns</label> </div> <div class="option-row"> <input id="chkallowOutlineRows" type="checkbox" (change)="setOperateRowOutline($event)"/> <label for="chkallowOutlineRows" class="sizedLabel">Operate row outline</label> </div> <div class="option-row"> <input id="chkallowOutlineColumns" type="checkbox" (change)="setOperateColumnOutline($event)"/> <label for="chkallowOutlineColumns" class="sizedLabel">Operate column outline</label> </div> <div class="option-row"> <input type="button" value="Set" id="setProtectionOptions" (click)="applySetting($event)" style="width: 100px " /> </div> <div class="option-row"> <label>Select the protection options and then click Set</label> </div> </div> </div> </div>
.sizedLabel { display: inline-block; width: 180px; } .colorLabel { background-color: #F4F8EB; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; } label { margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
(function (global) { System.config({ transpiler: 'ts', typescriptOptions: { tsconfig: true }, meta: { 'typescript': { "exports": "ts" }, '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'core-js': 'npm:core-js/client/shim.min.js', 'zone': 'npm:zone.js/fesm2015/zone.min.js', 'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js', '@angular/core': 'npm:@angular/core/fesm2022', '@angular/common': 'npm:@angular/common/fesm2022/common.mjs', '@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs', '@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs', '@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs', '@angular/router': 'npm:@angular/router/fesm2022/router.mjs', '@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs', 'jszip': 'npm:jszip/dist/jszip.min.js', 'typescript': 'npm:typescript/lib/typescript.js', 'ts': './plugin.js', 'tslib':'npm:tslib/tslib.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js', '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-angular': 'npm:@mescius/spread-sheets-angular/fesm2020/mescius-spread-sheets-angular.mjs', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'ts' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, "node_modules/@angular": { defaultExtension: 'mjs' }, "@mescius/spread-sheets-angular": { defaultExtension: 'mjs' }, '@angular/core': { defaultExtension: 'mjs', main: 'core.mjs' } } }); })(this);