Text Alignment

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

Description
app.js
index.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
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 }); initSpread(spread); }; function initSpread(spread) { spread.fromJSON(jsonData); var sheet = spread.getActiveSheet(); sheet.suspendPaint(); // set table style sheet.getRange("C5:E5").backColor("Accent 1 80"); sheet.getRange("C6:C13").backColor("Accent 6 80"); sheet.addSpan(5, 2, 5, 1); sheet.addSpan(10, 2, 3, 1); sheet.setColumnWidth(2, 140); sheet.setColumnWidth(3, 200); sheet.setColumnWidth(4, 80); sheet.setRowHeight(10, 60); sheet.setRowHeight(11, 60); sheet.setRowHeight(12, 60); sheet.getRange("C6:C13").vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.getRange("C6:C13").hAlign(GC.Spread.Sheets.HorizontalAlign.center); 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); sheet.getRange("C5:E13").setBorder(lineBorder, { all: true }); // set text sheet.setValue(4, 2, "Text Alignment"); sheet.setValue(5, 2, "Horizontal Alignment"); sheet.setValue(10, 2, "Vertical Alignment"); var hAlignData = [["Left"], ["Center"], ["Right"], ["Center Across Selection"],["Distributed Alignment"]]; var vAlignData = [["Top"], ["Center"], ["Bottom"]]; sheet.setArray(5, 3, hAlignData); sheet.setArray(10, 3, vAlignData); // set Alignment sheet.getStyle(5, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.left; sheet.getStyle(6, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.center; sheet.getStyle(7, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.right; sheet.getRange(8, 3, 1, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.centerContinuous); sheet.getStyle(9, 3).hAlign = GC.Spread.Sheets.HorizontalAlign.distributed; sheet.getStyle(10, 3).vAlign = GC.Spread.Sheets.VerticalAlign.top; sheet.getStyle(11, 3).vAlign = GC.Spread.Sheets.VerticalAlign.center; sheet.getStyle(12, 3).vAlign = GC.Spread.Sheets.VerticalAlign.bottom; sheet.resumePaint(); document.getElementById('HAlign0').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.hAlign = 0; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('HAlign1').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.hAlign = 1; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('HAlign2').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.hAlign = 2; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('HAlign3').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.hAlign = 4; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('HAlign4').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.hAlign = 5; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('VAlign0').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.vAlign = 0; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('VAlign1').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.vAlign = 1; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('VAlign2').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.vAlign = 2; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; }
<!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/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/data/alignment.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/jquery-1.8.2.min.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <div class="option-row"> <label id="Text Oriention" for="textOriention">HAlign:</label> <input type="button" value="Left" id="HAlign0" /> <input type="button" value="Center" id="HAlign1" /><br> <input type="button" value="Right" id="HAlign2" /> <input type="button" value="CenterContinues" id="HAlign3"> <input type="button" value="Distributed" id="HAlign4"> </div> <div class="option-row"> <label id="Text Oriention" for="textOriention">VAlign:</label> <input type="button" value="Top" id="VAlign0" /> <input type="button" value="Center" id="VAlign1" /><br> <input type="button" value="Bottom" id="VAlign2" /> </div> </div> </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; } 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; }