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: '_'
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
</gc-spread-sheets>
<div class="options-container">
<p>Select a cell, then set mask for it.</p>
<div class="settings-row">
<input id="validation" v-model="validation" class="input-span-like"/>
</div>
<div class="settings-row">
<label for="pattern">Pattern:</label><input type="text" id="pattern" class="settings-text" v-model="pattern"
v-on:keyup="onKeyup">
</div>
<div class="settings-row">
<label for="placeholder">Placeholder:</label><input type="text" id="placeholder" class="settings-text"
maxlength="1" v-model="placeholder">
</div>
<div class="settings-row">
<label for="excludeLiteral">ExcludeLiteral:</label><input type="checkbox" class="settings-text"
id="excludeLiteral" v-model="excludeLiteral">
</div>
<div class="settings-row">
<label for="excludePlaceholder">ExcludePlaceholder:</label><input type="checkbox" class="settings-text"
id="excludePlaceholder" v-model="excludePlaceholder">
</div>
<div class="settings-row">
<button id="set-mask" class="settings-btn" v-on:click="set">Set</button>
</div>
</div>
</div>
</template>
<script setup>
import '@mescius/spread-sheets-vue'
import { ref } from "vue";
import GC from "@mescius/spread-sheets";
const spreadRef = ref(null);
const pattern = ref('');
const placeholder = ref('');
const validation = ref('');
const excludeLiteral = ref(false);
const excludePlaceholder = ref(false);
function initSpread(spread) {
spreadRef.value = spread;
initSheet(spread);
bindEvents(spread);
}
function 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');
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();
}
function bindEvents(spread) {
spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () {
setSettings(spread);
});
}
function onKeyup() {
let value = pattern.value;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(value);
if (validateResult.success) {
validation.value = '';
} else {
validation.value = validateResult.message || 'Invalid Pattern';
}
}
function setSettings(spread) {
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;
}
pattern.value = _pattern;
placeholder.value = _placeholder;
excludeLiteral.value = _excludeLiteral;
excludePlaceholder.value = _excludePlaceholder;
let validateResult = GC.Spread.Sheets.InputMask.validatePattern(_pattern);
if (validateResult.success) {
validation.value = '';
} else {
validation.value = validateResult.message || 'Invalid Pattern';
}
}
function set() {
let sheet = spreadRef.value.getActiveSheet();
let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
let _pattern = pattern.value || undefined;
if (_pattern && _pattern[0] === "=") {
_pattern = GC.Spread.Sheets.CalcEngine.evaluateFormula(sheet, _pattern);
}
activeCell.mask({
pattern: _pattern,
placeholder: placeholder.value || undefined,
excludeLiteral: excludeLiteral.value,
excludePlaceholder: excludePlaceholder.value
});
}
</script>
<style scoped>
.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;
}
.input-span-like {
border: none;
background: none;
padding: 0;
margin: 0;
font: inherit;
color: inherit;
display: inline;
}
</style>
<!DOCTYPE html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SpreadJS VUE</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/en/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/en/vue3/node_modules/systemjs/dist/system.src.js"></script>
<script src="./systemjs.config.js"></script>
<script src="./compiler.js" type="module"></script>
<script>
var System = SystemJS;
System.import("./src/app.js");
System.import('$DEMOROOT$/en/lib/vue3/license.js');
</script>
</head>
<body>
<div id="app" style="height: 100%;"></div>
</body>
</html>
(function (global) {
SystemJS.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
packageConfigPaths: [
'./node_modules/*/package.json',
"./node_modules/@mescius/*/package.json",
"./node_modules/@babel/*/package.json",
"./node_modules/@vue/*/package.json"
],
map: {
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js',
'vue': "npm:vue/dist/vue.esm-browser.js",
'tiny-emitter': 'npm:tiny-emitter/index.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
"systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js",
},
meta: {
'*.css': { loader: 'systemjs-plugin-css' },
'*.vue': { loader: "../plugin-vue/index.js" }
}
});
})(this);