You can set the mask of a cell in the following way:
You can verify the pattern is legal with the follow static function:
The mask supports detailed information with the following settings:
pattern
placeholder
excludeLiteral
excludePlaceholder
Default Values:
placeholder: '_'
import { Component, NgModule, enableProdMode, NgZone } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { SpreadSheetsModule } from '@mescius/spread-sheets-angular';
import { FormsModule } from '@angular/forms';
import GC from '@mescius/spread-sheets';
import './styles.css';
@Component({
selector: 'app-component',
templateUrl: 'src/app.component.html'
})
export class AppComponent {
spread: GC.Spread.Sheets.Workbook;
pattern = '';
placeholder = '';
validation = '';
excludeLiteral = false;
excludePlaceholder = false;
hostStyle = {
width: 'calc(100% - 280px)',
height: '100%',
overflow: 'hidden',
float: 'left'
};
constructor (private zone: NgZone) {}
initSpread($event: any) {
let spread = $event.spread;
this.spread = spread;
this.initSheet(spread);
this.bindEvents(spread);
}
initSheet (spread: GC.Spread.Sheets.Workbook) {
let sheet = spread.getActiveSheet();
sheet.suspendPaint();
sheet.setValue(0, 0, 'Mask Cell');
sheet.setColumnWidth(0, 240);
sheet.setValue(0, 1, 'Mask Pattern or Option');
sheet.setColumnWidth(1, 240);
sheet.setValue(0, 2, 'Mask Description');
sheet.setColumnWidth(2, 240);
let style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "a"
};
sheet.setStyle(1, 0, style);
sheet.setValue(1, 1, 'a');
sheet.setValue(1, 2, 'User must enter a letter.');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: ">"
};
sheet.setStyle(2, 0, style);
sheet.setValue(2, 1, '>');
sheet.setValue(2, 2, 'User must enter a letter and auto convert it to uppercase');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "<"
};
sheet.setStyle(3, 0, style);
sheet.setValue(3, 1, '<');
sheet.setValue(3, 2, 'User must enter a letter and auto convert it to lowercase.');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "0"
};
sheet.setStyle(4, 0, style);
sheet.setValue(4, 1, '0');
sheet.setValue(4, 2, 'User must enter a digit(0-9).');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "Au\\t\\hor: (Chris|Icey|Victor|Ian|Johnson|Ivan)"
};
sheet.setStyle(5, 0, style);
sheet.setValue(5, 1, 'Au\\t\\hor: (Chris|Icey|Victor|Ian|Johnson|Ivan)');
sheet.setValue(5, 2, '() Indicates that any one of the strings in () will match.');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "Br\\an\\d\\s: (Benz|BMW|Buick|BYD|Chevrolet|Dodge|Ford|Honda|Jeep|Mazda|Toyota|Volkswagen)"
};
sheet.setStyle(6, 0, style);
sheet.setValue(6, 1, 'Br\\an\\d\\s: (Benz|BMW|Buick|BYD|Chevrolet|Dodge|Ford|Honda|Jeep|Mazda|Toyota|Volkswagen)');
sheet.setValue(6, 2, '| Used within () and acts as a delimiter between strings.');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "[a0_]"
};
sheet.setStyle(7, 0, style);
sheet.setValue(7, 1, '[a0_]');
sheet.setValue(7, 2, 'For combine the keywords and literals as a whole');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "0{3}"
};
sheet.setStyle(8, 0, style);
sheet.setValue(8, 1, '0{3}');
sheet.setValue(8, 2, 'n repeats');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "0{2,4}"
};
sheet.setStyle(9, 0, style);
sheet.setValue(9, 1, '0{2,4}');
sheet.setValue(9, 2, 'From n to m repeats');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "0{,3}"
};
sheet.setStyle(10, 0, style);
sheet.setValue(10, 1, '0{,3}');
sheet.setValue(10, 2, 'Up to m repeats');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "0{3,}"
};
sheet.setStyle(11, 0, style);
sheet.setValue(11, 1, '0{3,}');
sheet.setValue(11, 2, 'At least n repeats');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "0{1,}.\\0\\0"
};
sheet.setStyle(12, 0, style);
sheet.setValue(12, 1, '0{1,}.\\0\\0');
sheet.setValue(12, 2, 'Escape Character');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "000.000",
placeholder: "-"
};
sheet.setStyle(13, 0, style);
sheet.setValue(13, 1, '000.000');
sheet.setValue(13, 2, 'placeholder: -');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "[a0]{8}",
excludePlaceholder: true
};
sheet.setStyle(14, 0, style);
sheet.setValue(14, 1, '[a0]{8}');
sheet.setValue(14, 2, 'excludePlaceholder: true');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "Au\\t\\hor: (Chris|Icey|Victor|Ian|Johnson|Ivan)",
excludeLiteral: true
};
sheet.setStyle(15, 0, style);
sheet.setValue(15, 1, 'Au\\t\\hor: (Chris|Icey|Victor|Ian|Johnson|Ivan)');
sheet.setValue(15, 2, 'excludeLiteral: true');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "[a0]{1,}@[a0]{1,}.(com|cn|gov|edu)"
};
sheet.setStyle(16, 0, style);
sheet.setValue(16, 1, '[a0]{1,}@[a0]{1,}.(com|cn|gov|edu)');
sheet.setValue(16, 2, 'email');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "><{1,} ><{1,}"
};
sheet.setStyle(17, 0, style);
sheet.setValue(17, 1, '><{1,} ><{1,}');
sheet.setValue(17, 2, 'name(First letter auto uppercase)');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "[>0]{5}"
};
sheet.setStyle(18, 0, style);
sheet.setValue(18, 1, '[>0]{5}');
sheet.setValue(18, 2, 'Verify Code, include letter and number, letter auto convert uppercase');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "yyyy/MM/dd"
};
sheet.setStyle(19, 0, style);
sheet.setValue(19, 1, 'yyyy/MM/dd');
sheet.setValue(19, 2, 'Date');
style = new GC.Spread.Sheets.Style();
style.mask = {
pattern: "hh:mm:ss tt"
};
sheet.setStyle(20, 0, style);
sheet.setValue(20, 1, 'hh:mm:ss tt');
sheet.setValue(20, 2, 'Time');
sheet.resumePaint();
}
bindEvents (spread: GC.Spread.Sheets.Workbook) {
let self = this;
spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () {
self.setSettings(spread);
});
}
onKeyup () {
let value = this.pattern;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(value);
if (validateResult.success) {
this.validation = '';
} else {
this.validation = validateResult.message || 'Invalid Pattern';
}
}
setSettings (spread: GC.Spread.Sheets.Workbook) {
let sheet = spread.getActiveSheet();
let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());let mask = activeCell.mask(), pattern = '', placeholder = '', excludeLiteral = false, excludePlaceholder = false;
if (mask) {
pattern = mask.pattern || '';
placeholder = mask.placeholder || '';
excludeLiteral = mask.excludeLiteral || false;
excludePlaceholder = mask.excludePlaceholder || false;
}
this.zone.run(() => {
this.pattern = pattern;
this.placeholder = placeholder;
this.excludeLiteral = excludeLiteral;
this.excludePlaceholder = excludePlaceholder;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(pattern);
if (validateResult.success) {
this.validation = '';
} else {
this.validation = validateResult.message || 'Invalid Pattern';
}
});
}
set () {
let self = this, sheet = this.spread.getActiveSheet();
let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
let pattern = self.pattern || undefined;
if (pattern && pattern[0] === "=") {
pattern = GC.Spread.Sheets.CalcEngine.evaluateFormula(sheet, pattern);
}
activeCell.mask({
pattern: pattern,
placeholder: self.placeholder || undefined,
excludeLiteral: self.excludeLiteral,
excludePlaceholder: self.excludePlaceholder
});
}
}
@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-spread-sheets>
<div class="options-container">
<p>Select a cell, then set mask for it.</p>
<div class="settings-row">
<span id="validation">{{ validation }}</span>
</div>
<div class="settings-row">
<label for="pattern">Pattern:</label><input type="text" id="pattern" class="settings-text" [(ngModel)]="pattern" (keyup)="onKeyup($event)">
</div>
<div class="settings-row">
<label for="placeholder">Placeholder:</label><input type="text" maxlength="1" id="placeholder" class="settings-text" [(ngModel)]="placeholder">
</div>
<div class="settings-row">
<label for="excludeLiteral">ExcludeLiteral:</label><input type="checkbox" class="settings-text" id="excludeLiteral" [(ngModel)]="excludeLiteral">
</div>
<div class="settings-row">
<label for="excludePlaceholder">ExcludePlaceholder:</label><input type="checkbox" class="settings-text" id="excludePlaceholder" [(ngModel)]="excludePlaceholder">
</div>
<div class="settings-row">
<button id="set-mask" class="settings-btn" (click)="set()">Set</button>
</div>
</div>
</div>
.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;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
label {
display: inline-block;
width: 135px;
}
#validation {
font-size: 10px;
color: red;
}
.settings-row {
width: 100%;
height: 30px;
font-size: 13px;
}
.settings-text {
display: inline-block;
width: 100px;
}
.settings-btn {
display: inline-block;
width: 100px;
height: 30px;
margin-left: 20px;
}
(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);