Text Alignment

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

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
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> </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" v-on:click="hAlignLeft" /> <input type="button" value="Center" id="HAlign1" v-on:click="hAlignCenter" /><br> <input type="button" value="Right" id="HAlign2" v-on:click="hAlignRight" /> <input type="button" value="CenterContinues" id="HAlign3" v-on:click="hAlignCenterContinues" /> <input type="button" value="Distributed" id="HAlign4" v-on:click="hAlignDistributed" /> </div> <div class="option-row"> <label id="Text Oriention" for="textOriention">VAlign:</label> <input type="button" value="Top" id="VAlign0" v-on:click="vAlignTop" /> <input type="button" value="Center" id="VAlign1" v-on:click="vAlignCenter" /><br> <input type="button" value="Bottom" id="VAlign2" v-on:click="vAlignBottom" /> </div> </div> </div> </template> <script setup> import '@mescius/spread-sheets-vue' import { ref } from "vue"; import GC from "@mescius/spread-sheets"; const textOriention = ref(0); const spreadRef = ref(null); const sheetRef = ref(null); const top = ref(0); const left = ref(0); const right = ref(0); const bottom = ref(0); function hAlignLeft() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.hAlign = 0; sheetRef.value.setStyle(acRow, acCol, style); } function hAlignCenter() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.hAlign = 1; sheetRef.value.setStyle(acRow, acCol, style); } function hAlignRight() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.hAlign = 2; sheetRef.value.setStyle(acRow, acCol, style); } function hAlignCenterContinues() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.hAlign = 4; sheetRef.value.setStyle(acRow, acCol, style); } function hAlignDistributed() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.hAlign = 5; sheetRef.value.setStyle(acRow, acCol, style); } function vAlignTop() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.vAlign = 0; sheetRef.value.setStyle(acRow, acCol, style); } function vAlignCenter() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.vAlign = 1; sheetRef.value.setStyle(acRow, acCol, style); } function vAlignBottom() { let acRow = sheetRef.value.getActiveRowIndex(), acCol = sheetRef.value.getActiveColumnIndex(); var style = sheetRef.value.getActualStyle(acRow, acCol); style.vAlign = 2; sheetRef.value.setStyle(acRow, acCol, style); } function initSpread(spread) { spreadRef.value = spread; spread.fromJSON(jsonData); sheetRef.value = spread.getActiveSheet(); sheetRef.value.suspendPaint(); // set table style sheetRef.value.getRange("C5:E5").backColor("Accent 1 80"); sheetRef.value.getRange("C6:C13").backColor("Accent 6 80"); sheetRef.value.addSpan(5, 2, 5, 1); sheetRef.value.addSpan(10, 2, 3, 1); sheetRef.value.setColumnWidth(2, 140); sheetRef.value.setColumnWidth(3, 200); sheetRef.value.setColumnWidth(4, 80); sheetRef.value.setRowHeight(10, 60); sheetRef.value.setRowHeight(11, 60); sheetRef.value.setRowHeight(12, 60); sheetRef.value.getRange("C6:C13").vAlign(GC.Spread.Sheets.VerticalAlign.center); sheetRef.value.getRange("C6:C13").hAlign(GC.Spread.Sheets.HorizontalAlign.center); sheetRef.value.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); sheetRef.value.getRange("C5:E13").setBorder(lineBorder, { all: true }); // set text sheetRef.value.setValue(4, 2, "Text Alignment"); sheetRef.value.setValue(5, 2, "Horizontal Alignment"); sheetRef.value.setValue(10, 2, "Vertical Alignment"); var hAlignData = [["Left"], ["Center"], ["Right"], ["Center Across Selection"], ["Distributed Alignment"]]; var vAlignData = [["Top"], ["Center"], ["Bottom"]]; sheetRef.value.setArray(5, 3, hAlignData); sheetRef.value.setArray(10, 3, vAlignData); // set Alignment sheetRef.value.getStyle(5, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.left; sheetRef.value.getStyle(6, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.center; sheetRef.value.getStyle(7, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.right; sheetRef.value.getRange(8, 3, 1, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.centerContinuous); sheetRef.value.getStyle(9, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.distributed; sheetRef.value.getStyle(10, 3).vAlign = GC.Spread.Sheets.VerticalAlign.top; sheetRef.value.getStyle(11, 3).vAlign = GC.Spread.Sheets.VerticalAlign.center; sheetRef.value.getStyle(12, 3).vAlign = GC.Spread.Sheets.VerticalAlign.bottom; sheetRef.value.resumePaint(); } </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; } 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; } </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="$DEMOROOT$/spread/source/data/alignment.js" type="text/javascript"></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);