Format Cells (Angular)

FlexSheet allows the format to be set for each cell using the applyCellsStyle method. The format includes the data format of the cell value (Date/Number format), font style, fill color and horizontal alignment.

Learn about FlexSheet | FlexSheet API Reference

This example uses Angular.

import 'bootstrap.css'; import '@mescius/wijmo.styles/wijmo.css'; import './styles.css'; import '@angular/compiler'; import { Component, enableProdMode, NgModule, ViewChild } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjInputModule } from '@mescius/wijmo.angular2.input'; import { WjGridSheetModule } from '@mescius/wijmo.angular2.grid.sheet'; import * as wijmo from '@mescius/wijmo'; import * as input from '@mescius/wijmo.input'; import * as grid from '@mescius/wijmo.grid'; import * as sheet from '@mescius/wijmo.grid.sheet'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { @ViewChild('flex', { static: true }) flex: sheet.FlexSheet; @ViewChild('cboFontName', { static: true }) cboFontName: input.ComboBox; @ViewChild('cboFontSize', { static: true }) cboFontSize: input.ComboBox; @ViewChild('cboTableStyles', { static: true }) cboTableStyles: input.ComboBox; @ViewChild('colorPicker', { static: true }) colorPicker: input.ColorPicker; fonts = [ { name: 'Arial', value: 'Arial, Helvetica, sans-serif' }, { name: 'Arial Black', value: '"Arial Black", Gadget, sans-serif' }, { name: 'Comic Sans MS', value: '"Comic Sans MS", cursive, sans-serif' }, { name: 'Courier New', value: '"Courier New", Courier, monospace' }, { name: 'Georgia', value: 'Georgia, serif' }, { name: 'Impact', value: 'Impact, Charcoal, sans-serif' }, { name: 'Lucida Console', value: '"Lucida Console", Monaco, monospace' }, { name: 'Lucida Sans Unicode', value: '"Lucida Sans Unicode", "Lucida Grande", sans-serif' }, { name: 'Palatino Linotype', value: '"Palatino Linotype", "Book Antiqua", Palatino, serif' }, { name: 'Tahoma', value: 'Tahoma, Geneva, sans-serif' }, { name: 'Segoe UI', value: '"Segoe UI", "Roboto", sans-serif' }, { name: 'Times New Roman', value: '"Times New Roman", Times, serif' }, { name: 'Trebuchet MS', value: '"Trebuchet MS", Helvetica, sans-serif' }, { name: 'Verdana', value: 'Verdana, Geneva, sans-serif' } ]; fontSizeList = [ { name: '8', value: '8px' }, { name: '9', value: '9px' }, { name: '10', value: '10px' }, { name: '11', value: '11px' }, { name: '12', value: '12px' }, { name: '14', value: '14px' }, { name: '16', value: '16px' }, { name: '18', value: '18px' }, { name: '20', value: '20px' }, { name: '22', value: '22px' }, { name: '24', value: '24px' } ]; selectionFormatState: sheet.IFormatState = {}; private _updatingSelection = false; private _applyFillColor = false; private _format = ''; // Gets or sets _format for the formatSheet. get format(): string { return this._format; } set format(value: string) { if (this._format !== value) { this._format = value; if (!this._updatingSelection) { this.flex.applyCellsStyle({ format: this._format }); } } } initializeFlexSheet(flex: sheet.FlexSheet) { flex.deferUpdate(() => { for (let sheetIdx = 0; sheetIdx < flex.sheets.length; sheetIdx++) { flex.selectedSheetIndex = sheetIdx; let sheetName = flex.selectedSheet.name; for (let colIdx = 0; colIdx < flex.columns.length; colIdx++) { for (let rowIdx = 0; rowIdx < flex.rows.length; rowIdx++) { if (sheetName === 'Number') { flex.setCellData(rowIdx, colIdx, colIdx + rowIdx); } else { let date = new Date(2015, colIdx, rowIdx + 1); flex.setCellData(rowIdx, colIdx, date); } } } } flex.selectedSheetIndex = 0; setTimeout(() => this._updateSelection(flex, flex.selection), 100); }); flex.selectionChanged.addHandler((sender: sheet.FlexSheet, args: grid.CellRangeEventArgs) => { this._updateSelection(flex, args.range); }); } fontChanged(sender: input.ComboBox) { if (sender.selectedItem && !this._updatingSelection) { this.flex.applyCellsStyle({ fontFamily: sender.selectedItem.value }); } } fontSizeChanged(sender: input.ComboBox) { if (sender.selectedItem && !this._updatingSelection) { this.flex.applyCellsStyle({ fontSize: sender.selectedItem.value }); } } colorPickerInit(colorPicker: input.ColorPicker) { // if the browser is Firefox, we should bind the blur event. (TFS #124387) // if the browser is IE, we should bind the focusout event. (TFS #124500) let blurEvt = /firefox/i.test(window.navigator.userAgent) ? 'blur' : 'focusout'; // Hide the color picker control when it lost the focus. colorPicker.hostElement.addEventListener(blurEvt, () => { setTimeout(() => { if (!colorPicker.containsFocus()) { this._applyFillColor = false; colorPicker.hostElement.style.display = 'none'; } }, 0); }); // Initialize the value changed event handler for the color picker control. colorPicker.valueChanged.addHandler(() => { if (this._applyFillColor) { this.flex.applyCellsStyle({ backgroundColor: colorPicker.value }); } else { this.flex.applyCellsStyle({ color: colorPicker.value }); } }); } // apply the text alignment for the selected cells applyCellTextAlign(textAlign: string) { this.flex.applyCellsStyle({ textAlign: textAlign }); this.selectionFormatState.textAlign = textAlign; } // apply the bold font weight for the selected cells applyBoldStyle() { this.flex.applyCellsStyle({ fontWeight: this.selectionFormatState.isBold ? 'none' : 'bold' }); this.selectionFormatState.isBold = !this.selectionFormatState.isBold; } // apply the underline text decoration for the selected cells applyUnderlineStyle() { this.flex.applyCellsStyle({ textDecoration: this.selectionFormatState.isUnderline ? 'none' : 'underline' }); this.selectionFormatState.isUnderline = !this.selectionFormatState.isUnderline; } // apply the italic font style for the selected cells applyItalicStyle() { this.flex.applyCellsStyle({ fontStyle: this.selectionFormatState.isItalic ? 'none' : 'italic' }); this.selectionFormatState.isItalic = !this.selectionFormatState.isItalic; } // show the color picker control showColorPicker(e: MouseEvent, isFillColor: boolean) { let offset = this._cumulativeOffset(<HTMLElement>e.target), he = this.colorPicker.hostElement; he.style.display = 'inline'; he.style.left = offset.left + 'px'; he.style.top = (offset.top - he.clientHeight - 5) + 'px'; he.focus(); this._applyFillColor = isFillColor; }; // Update the selection object of the scope. private _updateSelection(fs: sheet.FlexSheet, sel: grid.CellRange) { let rCnt = fs.rows.length, cCnt = fs.columns.length; this._updatingSelection = true; if (sel.row > -1 && sel.col > -1 && rCnt > 0 && cCnt > 0 && sel.col < cCnt && sel.col2 < cCnt && sel.row < rCnt && sel.row2 < rCnt) { let cellContent = fs.getCellData(sel.row, sel.col, false), cellStyle = fs.selectedSheet.getCellStyle(sel.row, sel.col), cellFormat: string; if (cellStyle) { this.cboFontName.selectedIndex = this._checkFontfamily(cellStyle.fontFamily); this.cboFontSize.selectedIndex = this._checkFontSize(cellStyle.fontSize); cellFormat = cellStyle.format; } else { this.cboFontName.selectedIndex = 0; this.cboFontSize.selectedIndex = 5; } if (!!cellFormat) { this.format = cellFormat; } else { if (wijmo.isInt(cellContent)) { this.format = '0'; } else if (wijmo.isNumber(cellContent)) { this.format = 'n2'; } else if (wijmo.isDate(cellContent)) { this.format = 'd'; } } this.selectionFormatState = fs.getSelectionFormatState(); } this._updatingSelection = false; } // check font family for the font name combobox of the ribbon. private _checkFontfamily(value: string) { let fonts = this.fonts; if (!value) { return 0; } for (let fontIndex = 0; fontIndex < fonts.length; fontIndex++) { let font = fonts[fontIndex]; if (font.name === value || font.value === value) { return fontIndex; } } return 0; } // check font size for the font size combobox of the ribbon. private _checkFontSize(value: string) { let sizeList = this.fontSizeList; if (value == null) { return 5; } for (let index = 0; index < sizeList.length; index++) { let size = sizeList[index]; if (size.value === value || size.name === value) { return index; } } return 5; } // Get the absolute position of the dom element. private _cumulativeOffset(element: HTMLElement) { let top = 0, left = 0, scrollTop = 0, scrollLeft = 0; do { top += element.offsetTop || 0; left += element.offsetLeft || 0; scrollTop += element.scrollTop || 0; scrollLeft += element.scrollLeft || 0; element = <HTMLElement>element.offsetParent; } while (element && !(element instanceof HTMLBodyElement)); scrollTop += document.body.scrollTop || document.documentElement.scrollTop; scrollLeft += document.body.scrollLeft || document.documentElement.scrollLeft; return { top: top - scrollTop, left: left - scrollLeft }; } } @NgModule({ imports: [WjGridSheetModule, WjInputModule, BrowserModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>MESCIUS Wijmo FlexSheet Formatting Cells</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/fesm2015/zone.min.js"></script> <!-- SystemJS --> <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.21.5/system.src.js" integrity="sha512-skZbMyvYdNoZfLmiGn5ii6KmklM82rYX2uWctBhzaXPxJgiv4XBwJnFGr5k8s+6tE1pcR1nuTKghozJHyzMcoA==" crossorigin="anonymous"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html>
<div class="container-fluid"> <!-- the flexsheet --> <wj-flex-sheet #flex (initialized)="initializeFlexSheet(flex)"> <wj-sheet name="Number" [rowCount]="20" [columnCount]="8"></wj-sheet> <wj-sheet name="Date" [rowCount]="20" [columnCount]="8"></wj-sheet> </wj-flex-sheet> <wj-color-picker #colorPicker style="display:none;position:fixed;z-index:100" (initialized)="colorPickerInit(colorPicker)"></wj-color-picker> <div class="well well-lg"> <div> <wj-menu header="Format" [(value)]="format"> <wj-menu-item value="0">Decimal Format</wj-menu-item> <wj-menu-item value="n2">Number Format</wj-menu-item> <wj-menu-item value="p">Percentage Format</wj-menu-item> <wj-menu-item value="c2">Currency Format</wj-menu-item> <wj-menu-separator></wj-menu-separator> <wj-menu-item value="d">Short Date</wj-menu-item> <wj-menu-item value="D">Long Date</wj-menu-item> <wj-menu-item value="f">Full Date/TIme (short time)</wj-menu-item> <wj-menu-item value="F">Full Date/TIme (long time)</wj-menu-item> </wj-menu> </div> <div> Font: <wj-combo-box #cboFontName style="width:120px" [itemsSource]="fonts" [selectedIndex]="0" [displayMemberPath]="'name'" [selectedValuePath]="'value'" [isEditable]="false" (selectedIndexChanged)="fontChanged(cboFontName)"> </wj-combo-box> <wj-combo-box #cboFontSize style="width:80px" [itemsSource]="fontSizeList" [selectedIndex]="5" [displayMemberPath]="'name'" [selectedValuePath]="'value'" [isEditable]="false" (selectedIndexChanged)="fontSizeChanged(cboFontSize)"> </wj-combo-box> <div class="btn-group"> <button type="button" class="btn btn-default {{selectionFormatState.isBold ? 'active' : ''}}" (click)="applyBoldStyle()">Bold</button> <button type="button" class="btn btn-default {{selectionFormatState.isItalic ? 'active' : ''}}" (click)="applyItalicStyle()">Italic</button> <button type="button" class="btn btn-default {{selectionFormatState.isUnderline ? 'active' : ''}}" (click)="applyUnderlineStyle()">Underline</button> </div> </div> <div> Color: <div class="btn-group"> <button type="button" class="btn btn-default" (click)="showColorPicker($event, false)">Fore Color</button> <button type="button" class="btn btn-default" (click)="showColorPicker($event, true)">Fill Color</button> </div> Alignment: <div class="btn-group"> <button type="button" class="btn btn-default {{selectionFormatState.textAlign === 'left' ? 'active' : ''}}" (click)="applyCellTextAlign('left')">Left</button> <button type="button" class="btn btn-default {{selectionFormatState.textAlign === 'center' ? 'active' : ''}}" (click)="applyCellTextAlign('center')">Center</button> <button type="button" class="btn btn-default {{selectionFormatState.textAlign === 'right' ? 'active' : ''}}" (click)="applyCellTextAlign('right')">Right</button> </div> </div> </div> </div>
.wj-flexsheet { height: 400px; margin: 6px 0; }
(function (global) { SystemJS.config({ transpiler: './plugin-typescript.js', typescriptOptions: { "target": "ES2022", "module": "system", "emitDecoratorMetadata": true, "experimentalDecorators": true, }, baseURL: 'node_modules/', meta: { 'typescript': { "exports": "ts" }, '*.css': { loader: 'systemjs-plugin-css' } }, paths: { // paths serve as alias 'npm:': '' }, packageConfigPaths: [ '/node_modules/*/package.json', "/node_modules/@angular/*/package.json", "/node_modules/@mescius/*/package.json" ], map: { 'core-js': 'https://cdn.jsdelivr.net/npm/core-js@2.6.12/client/shim.min.js', 'typescript': 'https://cdnjs.cloudflare.com/ajax/libs/typescript/5.2.2/typescript.min.js', "rxjs": "https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.1/rxjs.umd.min.js", 'systemjs-plugin-css': 'https://cdn.jsdelivr.net/npm/systemjs-plugin-css@0.1.37/css.js', '@mescius/wijmo': 'npm:@mescius/wijmo/index.js', '@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js', '@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles', '@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures', '@mescius/wijmo.chart': 'npm:@mescius/wijmo.chart/index.js', '@mescius/wijmo.chart.analytics': 'npm:@mescius/wijmo.chart.analytics/index.js', '@mescius/wijmo.chart.animation': 'npm:@mescius/wijmo.chart.animation/index.js', '@mescius/wijmo.chart.annotation': 'npm:@mescius/wijmo.chart.annotation/index.js', '@mescius/wijmo.chart.finance': 'npm:@mescius/wijmo.chart.finance/index.js', '@mescius/wijmo.chart.finance.analytics': 'npm:@mescius/wijmo.chart.finance.analytics/index.js', '@mescius/wijmo.chart.hierarchical': 'npm:@mescius/wijmo.chart.hierarchical/index.js', '@mescius/wijmo.chart.interaction': 'npm:@mescius/wijmo.chart.interaction/index.js', '@mescius/wijmo.chart.radar': 'npm:@mescius/wijmo.chart.radar/index.js', '@mescius/wijmo.chart.render': 'npm:@mescius/wijmo.chart.render/index.js', '@mescius/wijmo.chart.webgl': 'npm:@mescius/wijmo.chart.webgl/index.js', '@mescius/wijmo.chart.map': 'npm:@mescius/wijmo.chart.map/index.js', '@mescius/wijmo.gauge': 'npm:@mescius/wijmo.gauge/index.js', '@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js', '@mescius/wijmo.grid.detail': 'npm:@mescius/wijmo.grid.detail/index.js', '@mescius/wijmo.grid.filter': 'npm:@mescius/wijmo.grid.filter/index.js', '@mescius/wijmo.grid.search': 'npm:@mescius/wijmo.grid.search/index.js', '@mescius/wijmo.grid.grouppanel': 'npm:@mescius/wijmo.grid.grouppanel/index.js', '@mescius/wijmo.grid.multirow': 'npm:@mescius/wijmo.grid.multirow/index.js', '@mescius/wijmo.grid.transposed': 'npm:@mescius/wijmo.grid.transposed/index.js', '@mescius/wijmo.grid.transposedmultirow': 'npm:@mescius/wijmo.grid.transposedmultirow/index.js', '@mescius/wijmo.grid.pdf': 'npm:@mescius/wijmo.grid.pdf/index.js', '@mescius/wijmo.grid.sheet': 'npm:@mescius/wijmo.grid.sheet/index.js', '@mescius/wijmo.grid.xlsx': 'npm:@mescius/wijmo.grid.xlsx/index.js', '@mescius/wijmo.grid.selector': 'npm:@mescius/wijmo.grid.selector/index.js', '@mescius/wijmo.grid.cellmaker': 'npm:@mescius/wijmo.grid.cellmaker/index.js', '@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js', '@mescius/wijmo.odata': 'npm:@mescius/wijmo.odata/index.js', '@mescius/wijmo.olap': 'npm:@mescius/wijmo.olap/index.js', '@mescius/wijmo.rest': 'npm:@mescius/wijmo.rest/index.js', '@mescius/wijmo.pdf': 'npm:@mescius/wijmo.pdf/index.js', '@mescius/wijmo.pdf.security': 'npm:@mescius/wijmo.pdf.security/index.js', '@mescius/wijmo.viewer': 'npm:@mescius/wijmo.viewer/index.js', '@mescius/wijmo.xlsx': 'npm:@mescius/wijmo.xlsx/index.js', '@mescius/wijmo.undo': 'npm:@mescius/wijmo.undo/index.js', '@mescius/wijmo.interop.grid': 'npm:@mescius/wijmo.interop.grid/index.js', '@mescius/wijmo.touch': 'npm:@mescius/wijmo.touch/index.js', '@mescius/wijmo.cloud': 'npm:@mescius/wijmo.cloud/index.js', '@mescius/wijmo.barcode': 'npm:@mescius/wijmo.barcode/index.js', '@mescius/wijmo.barcode.common': 'npm:@mescius/wijmo.barcode.common/index.js', '@mescius/wijmo.barcode.composite': 'npm:@mescius/wijmo.barcode.composite/index.js', '@mescius/wijmo.barcode.specialized': 'npm:@mescius/wijmo.barcode.specialized/index.js', "@mescius/wijmo.angular2.chart.analytics": "npm:@mescius/wijmo.angular2.chart.analytics/index.js", "@mescius/wijmo.angular2.chart.animation": "npm:@mescius/wijmo.angular2.chart.animation/index.js", "@mescius/wijmo.angular2.chart.annotation": "npm:@mescius/wijmo.angular2.chart.annotation/index.js", "@mescius/wijmo.angular2.chart.finance.analytics": "npm:@mescius/wijmo.angular2.chart.finance.analytics/index.js", "@mescius/wijmo.angular2.chart.finance": "npm:@mescius/wijmo.angular2.chart.finance/index.js", "@mescius/wijmo.angular2.chart.hierarchical": "npm:@mescius/wijmo.angular2.chart.hierarchical/index.js", "@mescius/wijmo.angular2.chart.interaction": "npm:@mescius/wijmo.angular2.chart.interaction/index.js", "@mescius/wijmo.angular2.chart.radar": "npm:@mescius/wijmo.angular2.chart.radar/index.js", '@mescius/wijmo.angular2.chart.map': 'npm:@mescius/wijmo.angular2.chart.map/index.js', "@mescius/wijmo.angular2.chart": "npm:@mescius/wijmo.angular2.chart/index.js", "@mescius/wijmo.angular2.core": "npm:@mescius/wijmo.angular2.core/index.js", "@mescius/wijmo.angular2.gauge": "npm:@mescius/wijmo.angular2.gauge/index.js", "@mescius/wijmo.angular2.grid.detail": "npm:@mescius/wijmo.angular2.grid.detail/index.js", "@mescius/wijmo.angular2.grid.filter": "npm:@mescius/wijmo.angular2.grid.filter/index.js", "@mescius/wijmo.angular2.grid.grouppanel": "npm:@mescius/wijmo.angular2.grid.grouppanel/index.js", "@mescius/wijmo.angular2.grid.search": "npm:@mescius/wijmo.angular2.grid.search/index.js", "@mescius/wijmo.angular2.grid.multirow": "npm:@mescius/wijmo.angular2.grid.multirow/index.js", "@mescius/wijmo.angular2.grid.sheet": "npm:@mescius/wijmo.angular2.grid.sheet/index.js", '@mescius/wijmo.angular2.grid.transposed': 'npm:@mescius/wijmo.angular2.grid.transposed/index.js', '@mescius/wijmo.angular2.grid.transposedmultirow': 'npm:@mescius/wijmo.angular2.grid.transposedmultirow/index.js', "@mescius/wijmo.angular2.grid": "npm:@mescius/wijmo.angular2.grid/index.js", "@mescius/wijmo.angular2.input": "npm:@mescius/wijmo.angular2.input/index.js", "@mescius/wijmo.angular2.olap": "npm:@mescius/wijmo.angular2.olap/index.js", "@mescius/wijmo.angular2.viewer": "npm:@mescius/wijmo.angular2.viewer/index.js", "@mescius/wijmo.angular2.nav": "npm:@mescius/wijmo.angular2.nav/index.js", "@mescius/wijmo.angular2.directivebase": "npm:@mescius/wijmo.angular2.directivebase/index.js", '@mescius/wijmo.angular2.barcode.common': 'npm:@mescius/wijmo.angular2.barcode.common/index.js', '@mescius/wijmo.angular2.barcode.composite': 'npm:@mescius/wijmo.angular2.barcode.composite/index.js', '@mescius/wijmo.angular2.barcode.specialized': 'npm:@mescius/wijmo.angular2.barcode.specialized/index.js', 'bootstrap.css': 'npm:bootstrap/dist/css/bootstrap.min.css', 'jszip': 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js', "@angular/common/http": "https://cdn.jsdelivr.net/npm/@angular/common@16.2.6/fesm2022/http.mjs", "@angular/core": "https://cdn.jsdelivr.net/npm/@angular/core@16.2.6/fesm2022/core.mjs", "@angular/platform-browser": "https://cdn.jsdelivr.net/npm/@angular/platform-browser@16.2.6/fesm2022/platform-browser.mjs", "@angular/common": "https://cdn.jsdelivr.net/npm/@angular/common@16.2.6/fesm2022/common.mjs", "@angular/compiler": "https://cdn.jsdelivr.net/npm/@angular/compiler@16.2.6/fesm2022/compiler.mjs", "@angular/forms": "https://cdn.jsdelivr.net/npm/@angular/forms@16.2.6/fesm2022/forms.mjs", "@angular/localize": "https://cdn.jsdelivr.net/npm/@angular/localize@16.2.6/fesm2022/localize.mjs", "@angular/platform-browser-dynamic": "https://cdn.jsdelivr.net/npm/@angular/platform-browser-dynamic@16.2.6/fesm2022/platform-browser-dynamic.mjs", }, // packages tells the System loader how to load when no filename and/or no extension packages: { "./src": { defaultExtension: 'ts' }, "node_modules": { defaultExtension: 'js' }, wijmo: { defaultExtension: 'js', } } }); })(this);