Text Alignment

SpreadJS supports text alignment, so developers can set horizontal alignment and vertical alignment to get the desired text alignment effect.

Description
app.component.ts
index.html
app.component.html
styles.css
Copy to CodeMine

You can create a style and set alignment as follows:

    var style = new GC.Spread.Sheets.Style();
    style.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
    style.vAlign = GC.Spread.Sheets.VerticalAlign.bottom;

Then apply this style to the cell to set the text alignment.

The list below shows the alignment currently supported by SpreadJS.

Horizontal alignment :

  • Left
  • Center
  • Right
  • Center Across Selection (CenterContinue)
  • Distributed

Vertical alignment:

  • Top
  • Center
  • Bottom
You can create a style and set alignment as follows: Then apply this style to the cell to set the text alignment. The list below shows the alignment currently supported by SpreadJS. Horizontal alignment : Left Center Right Center Across Selection (CenterContinue) Distributed Vertical alignment: Top Center Bottom
import { Component, NgModule, enableProdMode } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { SpreadSheetsModule } from '@mescius/spread-sheets-angular'; import GC from '@mescius/spread-sheets'; import './styles.css'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { sheet: GC.Spread.Sheets.Worksheet; top = 0; left = 0; right = 0; bottom = 0; hostStyle = { width: 'calc(100% - 280px)', height: '100%', overflow: 'hidden', float: 'left' }; hAlignLeft() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.hAlign = 0; this.sheet.setStyle(acRow, acCol, style); } hAlignCenter() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.hAlign = 1; this.sheet.setStyle(acRow, acCol, style); } hAlignRight() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.hAlign = 2; this.sheet.setStyle(acRow, acCol, style); } hAlignCenterContinues() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.hAlign = 4; this.sheet.setStyle(acRow, acCol, style); } hAlignDistributed() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.hAlign = 5; this.sheet.setStyle(acRow, acCol, style); } vAlignTop() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.vAlign = 0; this.sheet.setStyle(acRow, acCol, style); } vAlignCenter() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.vAlign = 1; this.sheet.setStyle(acRow, acCol, style); } vAlignBottom() { let acRow = this.sheet.getActiveRowIndex(), acCol = this.sheet.getActiveColumnIndex(); var style = this.sheet.getActualStyle(acRow, acCol); style.vAlign = 2; this.sheet.setStyle(acRow, acCol, style); } initSpread($event: any) { let spread = $event.spread; spread.fromJSON(jsonData); this.sheet = spread.getActiveSheet(); this.sheet.suspendPaint(); // set table style this.sheet.getRange("C5:E5").backColor("Accent 1 80"); this.sheet.getRange("C6:C13").backColor("Accent 6 80"); this.sheet.addSpan(5, 2, 5, 1); this.sheet.addSpan(10, 2, 3, 1); this.sheet.setColumnWidth(2, 140); this.sheet.setColumnWidth(3, 200); this.sheet.setColumnWidth(4, 80); this.sheet.setRowHeight(10, 60); this.sheet.setRowHeight(11, 60); this.sheet.setRowHeight(12, 60); this.sheet.getRange("C6:C13").vAlign(GC.Spread.Sheets.VerticalAlign.center); this.sheet.getRange("C6:C13").hAlign(GC.Spread.Sheets.HorizontalAlign.center); this.sheet.getRange("C5:E5").hAlign(GC.Spread.Sheets.HorizontalAlign.centerContinuous); var lineStyle = GC.Spread.Sheets.LineStyle.dotted; var lineBorder = new GC.Spread.Sheets.LineBorder('black', lineStyle); this.sheet.getRange("C5:E13").setBorder(lineBorder, { all: true }); // set text this.sheet.setValue(4, 2, "Text Alignment"); this.sheet.setValue(5, 2, "Horizontal Alignment"); this.sheet.setValue(10, 2, "Vertical Alignment"); var hAlignData = [["Left"], ["Center"], ["Right"], ["Center Across Selection"],["Distributed Alignment"]]; var vAlignData = [["Top"], ["Center"], ["Bottom"]]; this.sheet.setArray(5, 3, hAlignData); this.sheet.setArray(10, 3, vAlignData); // set Alignment this.sheet.getStyle(5, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.left; this.sheet.getStyle(6, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.center; this.sheet.getStyle(7, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.right; this.sheet.getRange(8, 3, 1, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.centerContinuous); this.sheet.getStyle(9, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.distributed; this.sheet.getStyle(10, 3).vAlign = GC.Spread.Sheets.VerticalAlign.top; this.sheet.getStyle(11, 3).vAlign = GC.Spread.Sheets.VerticalAlign.center; this.sheet.getStyle(12, 3).vAlign = GC.Spread.Sheets.VerticalAlign.bottom; this.sheet.resumePaint(); } } @NgModule({ imports: [BrowserModule, SpreadSheetsModule], 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> <script src="$DEMOROOT$/spread/source/data/alignment.js" type="text/javascript"></script> <!-- SystemJS --> <script src="$DEMOROOT$/en/angular/node_modules/systemjs/dist/system.js"></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"> <div class="option-row"> <label id="Text Oriention" for="textOriention">HAlign:</label> <input type="button" value="Left" id="HAlign0" (click)="hAlignLeft($event)" /> <input type="button" value="Center" id="HAlign1" (click)="hAlignCenter($event)"/><br> <input type="button" value="Right" id="HAlign2" (click)="hAlignRight($event)"/> <input type="button" value="CenterContinues" id="HAlign3" (click)="hAlignCenterContinues($event)"/> <input type="button" value="Distributed" id="HAlign4" (click)="hAlignDistributed($event)"/> </div> <div class="option-row"> <label id="Text Oriention" for="textOriention">VAlign:</label> <input type="button" value="Top" id="VAlign0" (click)="vAlignTop($event)"/> <input type="button" value="Center" id="VAlign1" (click)="vAlignCenter($event)"/><br> <input type="button" value="Bottom" id="VAlign2" (click)="vAlignBottom($event)"/> </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; } input { width: 115px; } .option-row { font-size: 14px; padding: 5px; } label { display: block; padding-bottom: 5px; } input { padding: 4px 6px; margin-bottom: 6px; margin-right: 5px; } .paddingLabel { width: 113px; display: inline-block; text-align: center; } .paddingLabel1 { width: 50px; /* display: inline-block; */ } .paddingInput { width: 84px; } .paddingInput1 { width: 84px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
(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);