Font

SpreadJS provides a rich font style, such as font, font style, bold, italic, and so on.

You can customize the font style using code such as the following: You can customize the font by splitted font properties using code such as the following: You can also set text decorations, such as underline, doubleUnderline, lineThrough, and overline You can set the font alignment like this: You can set the font color and cell background color like this: You can set the font format, such as wordwrap, indent, shrinkToFit, etc.
// @ts-ignore import { Component, NgModule, enableProdMode } from '@angular/core'; // @ts-ignore import { BrowserModule } from '@angular/platform-browser'; // @ts-ignore import { FormsModule } from '@angular/forms'; // @ts-ignore import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; // @ts-ignore import { SpreadSheetsModule } from '@mescius/spread-sheets-angular'; import GC from '@mescius/spread-sheets'; import './styles.css'; function fontStyleOrFontWeightCssValueToSpreadValue (value) { // transfer css font style/weight to spread font style/weight if (value === '') { return undefined; } else if (value === 'normal') { return null; } else { return value; } } function fontStyleOrFontWeightSpreadValueToCssValue (value) { // transfer spread font style/weight to css font style/weight if (value === undefined) { return ''; } else if (value === null) { return 'normal'; } else { return value; } } function setFontToSelectedRanges (spread, setFunc) { let activeSheet = spread.getActiveSheet(); let selections = activeSheet.getSelections(); if (!selections) { return; } activeSheet.suspendPaint(); for (let i = 0; i < selections.length; i++) { let selection = selections[i]; if (!selection) { return; } for (let r = 0; r < selection.rowCount; r++) { for (let c = 0; c < selection.colCount; c++) { let cell = activeSheet.getCell(r + selection.row, c + selection.col); setFunc(cell); } } } activeSheet.resumePaint(); } function setStyles (sheet) { sheet.suspendPaint(); //Font sheet.getCell(2, 0).font('italic normal 12px Mangal'); sheet.getCell(4, 0).font('normal bold 15px Arial Black'); sheet.getCell(6, 0).font('normal normal 18px Georgia'); //FontFamily sheet.getCell(2, 1).fontFamily('Mangal'); sheet.getCell(4, 1).fontFamily('Arial Black'); sheet.getCell(6, 1).fontFamily('Georgia'); //FontSize sheet.getCell(2, 2).fontSize('12px'); sheet.getCell(4, 2).fontSize('20px'); sheet.getCell(6, 2).fontSize('28px'); //Bold sheet.getCell(2, 3).fontWeight('bold'); sheet.getCell(4, 3).fontWeight('normal'); //Italic sheet.getCell(2, 4).fontStyle('italic'); sheet.getCell(4, 4).fontStyle('normal'); //Line sheet.getCell(2, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.underline); sheet.getCell(4, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.doubleUnderline); sheet.getCell(6, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.lineThrough); sheet.getCell(8, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.overline); //Align sheet.getCell(2, 6).vAlign(GC.Spread.Sheets.VerticalAlign.top).hAlign(GC.Spread.Sheets.HorizontalAlign.left); sheet.getCell(4, 6).vAlign(GC.Spread.Sheets.VerticalAlign.center).hAlign(GC.Spread.Sheets.HorizontalAlign.center); sheet.getCell(6, 6).vAlign(GC.Spread.Sheets.VerticalAlign.bottom).hAlign(GC.Spread.Sheets.HorizontalAlign.right); //Forecolor sheet.getCell(2, 7).foreColor('#F7A711'); sheet.getCell(4, 7).foreColor('#82BC00'); sheet.getCell(6, 7).foreColor('#C3C3C3'); //backgroundColor sheet.getCell(2, 8).backColor('#F7A711'); sheet.getCell(4, 8).backColor('#82BC00'); sheet.getCell(6, 8).backColor('#C3C3C3'); //WordWrap sheet.getCell(2, 9).wordWrap(true); sheet.getCell(3, 9).wordWrap(true); //Indent sheet.getCell(2, 10).textIndent(1); sheet.getCell(4, 10).textIndent(3); sheet.getCell(6, 10).textIndent(-2); //ShrinkToFit sheet.getCell(2, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.getCell(4, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.bottom); sheet.resumePaint(); } @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { spread: GC.Spread.Sheets.Workbook; fontStyle = 'undefined'; fontWeight = 'undefined'; fontSize = ''; fontSizeUnit = 'px'; fontFamily = ''; font = ''; hostStyle = { width: 'calc(100% - 280px)', height: '100%', overflow: 'hidden', float: 'left' }; initSpread($event: any) { let spread = this.spread = $event.spread; let sheet = this.spread.getActiveSheet(); sheet.fromJSON(jsonData); setStyles(sheet); this.setSettings(spread); this.bindEvents(spread); } bindEvents (spread: GC.Spread.Sheets.Workbook) { let self = this; spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () { self.setSettings(spread); }); } setSettings (spread: GC.Spread.Sheets.Workbook) { let sheet = spread.getActiveSheet(); let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); this.font = activeCell.font(); this.fontStyle = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontStyle()); this.fontWeight = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontWeight()); let fontSize = activeCell.fontSize(); if (fontSize) { let fontSizeDigit = parseFloat(fontSize); let fontSizeUnit = fontSize.substring((''+ fontSizeDigit).length); this.fontSize = '' + fontSizeDigit; this.fontSizeUnit = fontSizeUnit; } else { this.fontSize = ''; this.fontSizeUnit = 'px'; } this.fontFamily = activeCell.fontFamily() || ''; } setFontBySplittedFont () { let self = this, spread = self.spread; let fontStyle = self.fontStyle, fontWeight = self.fontWeight, fontFamily = self.fontFamily, fontSizeDigit = self.fontSize, fontSizeUnit = self.fontSizeUnit, fontSize; if (fontSizeDigit) { fontSize = fontSizeDigit + (fontSizeUnit ? fontSizeUnit : 'px'); } setFontToSelectedRanges(spread, (cell) => { cell.fontStyle(fontStyleOrFontWeightCssValueToSpreadValue(fontStyle)); cell.fontWeight(fontStyleOrFontWeightCssValueToSpreadValue(fontWeight)); cell.fontSize(fontSize); cell.fontFamily(fontFamily); }); self.setSettings(spread); } setFontByFont () { let self = this, spread = self.spread; let font = self.font; setFontToSelectedRanges(spread, (cell) => { cell.font(font); }); self.setSettings(spread); } } @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="data.js" type="text/javascript"></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, Set font for it.</p> <div class="settings-row"> <label for="fontStyle">Font Style:</label> <input id="fontStyle" type="text" [(ngModel)]="fontStyle"/> </div> <div class="settings-row"> <label for="fontWeight">Font Weight:</label> <input id="fontWeight" type="text" [(ngModel)]="fontWeight"/> </div> <div class="settings-row"> <label for="fontSize">Font Size:</label> <input id="fontSize" type="number" min="0" [(ngModel)]="fontSize"/> <select id="fontSizeUnit" [(ngModel)]="fontSizeUnit"> <option value="px">px</option> <option value="pt">pt</option> </select> </div> <div class="settings-row"> <label for="fontFamily">Font Family:</label> <input id="fontFamily" type="text" [(ngModel)]="fontFamily"/> </div> <div class="settings-row"> <button id="set-splitted-font" class="settings-btn" (click)="setFontBySplittedFont()">Set Font Using Font Properties</button> </div> <br/> <div class="settings-row"> <label for="font">Font:</label> <input type="text" id="font" [(ngModel)]="font"/> </div> <div class="settings-row"> <button id="set-font" class="settings-btn" (click)="setFontByFont()">Set Font By Font</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: 80px; } .settings-row { width: 100%; height: 30px; font-size: 13px; } .settings-btn { display: inline-block; width: 220px; height: 21px; } #fontSize { width: 80px; } .settings-row input { width: 160px; }
(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);