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 * as React from 'react';
import * as ReactDOM from 'react-dom';
import './styles.css';
import { AppFunc } from './app-func';
// import { App } from './app-class';
// 1. Functional Component sample
ReactDOM.render(<AppFunc />, document.getElementById('app'));
// 2. Class Component sample
// ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets } from '@mescius/spread-sheets-react';
let spread = null;
export function AppFunc() {
const [maskOption, setMaskOption] = React.useState({
pattern: '',
placeholder: '',
excludeLiteral: false,
excludePlaceholder: false
});
const handleInputClick = (event) => {
const target = event.target;
const value = target.checked;
const name = target.name;
setMaskOption({
...maskOption,
[name]: value
});
}
const handleInputChange = (event) => {
const target = event.target;
const value = target.value;
const name = target.name;
setMaskOption({
...maskOption,
[name]: value
});
}
const handleKeyUp = (event) => {
let target = event.target;
let value = target.value;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(value);
if (validateResult.success) {
setMaskOption({
...maskOption,
validation: ''
});
} else {
setMaskOption({
...maskOption,
validation: validateResult.message || 'Invalid Pattern'
});
}
}
const initSpread = (currSpread) => {
spread = currSpread;
initSheet();
bindEvents();
}
const initSheet = () => {
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');
5
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();
}
const bindEvents = () => {
spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () {
setSettings();
});
}
const setSettings = () => {
let sheet = spread.getActiveSheet();
let state = { ...maskOption };
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;
}
state.pattern = pattern;
state.placeholder = placeholder;
state.excludeLiteral = excludeLiteral;
state.excludePlaceholder = excludePlaceholder;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(pattern);
if (validateResult.success) {
state.validation = '';
} else {
state.validation = validateResult.message || 'Invalid Pattern';
}
setMaskOption(state);
}
const set = () => {
let sheet = spread.getActiveSheet(), state = { ...maskOption };
let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
let pattern = state.pattern || undefined;
if (pattern && pattern[0] === "=") {
pattern = GC.Spread.Sheets.CalcEngine.evaluateFormula(sheet, pattern);
}
activeCell.mask({
pattern: pattern,
placeholder: state.placeholder || undefined,
excludeLiteral: state.excludeLiteral,
excludePlaceholder: state.excludePlaceholder
});
}
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={initSpread} />
</div>
<div class="options-container">
<p>Select a cell, then set mask for it.</p>
<div className="settings-row">
<span id="validation" name={'validation'}>{maskOption.validation}</span>
</div>
<div class="settings-row">
<label for="pattern">Pattern:</label><input type="text" id="pattern" name={'pattern'} class="settings-text" value={maskOption.pattern} onChange={handleInputChange} onKeyUp={handleKeyUp} />
</div>
<div class="settings-row">
<label for="placeholder">Placeholder:</label><input type="text" id="placeholder" maxlength="1" name={'placeholder'} class="settings-text" value={maskOption.placeholder} onChange={handleInputChange} />
</div>
<div class="settings-row">
<label for="excludeLiteral">ExcludeLiteral:</label><input type="checkbox" name={'excludeLiteral'} class="settings-text" id="excludeLiteral" checked={maskOption.excludeLiteral} onChange={handleInputClick} />
</div>
<div class="settings-row">
<label for="excludePlaceholder">ExcludePlaceholder:</label><input type="checkbox" name={'excludePlaceholder'} class="settings-text" id="excludePlaceholder" checked={maskOption.excludePlaceholder} onChange={handleInputClick} />
</div>
<div class="settings-row">
<button id="set-mask" class="corner-btn" onClick={set}>Set</button>
</div>
</div>
</div>
);
}
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets } from '@mescius/spread-sheets-react';
const Component = React.Component;
export class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.state = {
pattern: '',
placeholder: "",
validation: '',
excludeLiteral: false,
excludePlaceholder: false
}
}
handleInputClick = (event) => {
const target = event.target;
const value = target.checked;
const name = target.name;
this.setState({
[name]: value
});
}
handleInputChange = (event) => {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleKeyUp = (event) => {
let target = event.target;
let value = target.value;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(value);
if (validateResult.success) {
this.setState({
validation: ''
});
} else {
this.setState({
validation: validateResult.message || 'Invalid Pattern'
});
}
}
initSpread = (spread) => {
this.spread = spread;
this.initSheet(spread);
this.bindEvents(spread);
}
initSheet = (spread) => {
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');
5
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) => {
let self = this;
spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () {
self.setSettings(spread);
});
}
setSettings = (spread)=> {
let sheet = spread.getActiveSheet();
let state = this.state;
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;
}
state.pattern = pattern;
state.placeholder = placeholder;
state.excludeLiteral = excludeLiteral;
state.excludePlaceholder = excludePlaceholder;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(pattern);
if (validateResult.success) {
state.validation = '';
} else {
state.validation = validateResult.message || 'Invalid Pattern';
}
this.setState(state);
}
set = () => {
let self = this, sheet = this.spread.getActiveSheet(), state = this.state;
let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
let pattern = state.pattern || undefined;
if (pattern && pattern[0] === "=") {
pattern = GC.Spread.Sheets.CalcEngine.evaluateFormula(sheet, pattern);
}
activeCell.mask({
pattern: pattern,
placeholder: state.placeholder || undefined,
excludeLiteral: state.excludeLiteral,
excludePlaceholder: state.excludePlaceholder
});
}
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={this.initSpread} />
</div>
<div class="options-container">
<p>Select a cell, then set mask for it.</p>
<div className="settings-row">
<span id="validation" name={'validation'}>{this.state.validation}</span>
</div>
<div class="settings-row">
<label for="pattern">Pattern:</label><input type="text" id="pattern" name={'pattern'} class="settings-text" value={this.state.pattern} onChange={this.handleInputChange} onKeyUp={this.handleKeyUp}/>
</div>
<div class="settings-row">
<label for="placeholder">Placeholder:</label><input type="text" id="placeholder" maxlength="1" name={'placeholder'} class="settings-text" value={this.state.placeholder} onChange={this.handleInputChange}/>
</div>
<div class="settings-row">
<label for="excludeLiteral">ExcludeLiteral:</label><input type="checkbox" name={'excludeLiteral'} class="settings-text" id="excludeLiteral" checked={this.state.excludeLiteral} onChange={this.handleInputClick}/>
</div>
<div class="settings-row">
<label for="excludePlaceholder">ExcludePlaceholder:</label><input type="checkbox" name={'excludePlaceholder'} class="settings-text" id="excludePlaceholder" checked={this.state.excludePlaceholder} onChange={this.handleInputClick}/>
</div>
<div class="settings-row">
<button id="set-mask" class="corner-btn" onClick={this.set}>Set</button>
</div>
</div>
</div>
)
}
}
<!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/react/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/en/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/en/lib/react/license.js').then(function () {
System.import('./src/app');
});
</script>
</head>
<body>
<div id="app" style="height: 100%;"></div>
</body>
</html>
.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;
}
.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;
}
#validation {
font-size: 10px;
color: red;
}
(function (global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true,
react: true
},
meta: {
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-react': 'npm:@mescius/spread-sheets-react/index.js',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js',
'react': 'npm:react/umd/react.production.min.js',
'react-dom': 'npm:react-dom/umd/react-dom.production.min.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'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'jsx'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);