你可以创建一个样式并设置对齐方式,如下所示。
然后将此样式应用于单元格,以设置文本对齐方式。
下面的列表显示了SpreadJS当前支持的对齐方式。
水平对齐:
左对齐
居中
右对齐
跨列居中 (CenterContinue)
分散对齐
垂直对齐:
顶部
居中
底部
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 name="spreadjs culture" content="zh-cn" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js"
type="text/javascript"></script>
<script
src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.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;
}