Mask

SpreadJS supports setting a mask on a style in order to put constraints on user input.

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"> <span id="validation" v-model="validation"></span> </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> import Vue from 'vue'; import '@mescius/spread-sheets-vue' import GC from '@mescius/spread-sheets'; import './styles.css'; let App = Vue.extend({ name: "app", data: function() { return { pattern: '', placeholder: "", validation: '', excludeLiteral: false, excludePlaceholder: false, spread: null }; }, methods:{ 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); }); }, 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) { 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.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 }); } } }); new Vue({ render: h => h(App) }).$mount('#app'); </script>
<!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/vue/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- SystemJS --> <script src="$DEMOROOT$/en/vue/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); System.import('$DEMOROOT$/en/lib/vue/license.js'); </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 }, meta: { '*.css': { loader: 'css' }, '*.vue': { loader: 'vue-loader' } }, 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-vue': 'npm:@mescius/spread-sheets-vue/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js', 'jszip': 'npm:jszip/dist/jszip.js', 'css': 'npm:systemjs-plugin-css/css.js', 'vue': 'npm:vue/dist/vue.min.js', 'vue-loader': 'npm:systemjs-vue-browser/index.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' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' } } }); })(this);