概述
本 Demo 展示了 SpreadJS 的文本对齐功能,通过示例演示了 6 种水平对齐方式和 4 种垂直对齐方式的效果。Demo 提供了交互式按钮,用户可以选中单元格后点击按钮实时应用不同的对齐方式。
实现思路
初始化工作簿并加载基础表格数据
创建两个示例区域:水平对齐示例和垂直对齐示例
为每种对齐方式设置示例文本,并应用对应的对齐样式
绑定按钮点击事件,实现选中单元格后动态修改对齐方式
监听选区变化事件,更新按钮高亮状态以显示当前单元格的对齐方式
代码解析
设置水平对齐
水平对齐支持 6 种方式:左对齐、居中、右对齐、跨列居中、分散对齐和两端对齐。可以通过样式对象的 hAlign 属性或 Range 的 hAlign() 方法设置。
设置垂直对齐
垂直对齐支持 4 种方式:顶部对齐、居中对齐、底部对齐和两端对齐。
动态修改对齐方式
通过按钮事件,获取当前选中单元格的样式,修改 hAlign 或 vAlign 属性值,然后重新应用样式。
运行效果
表格上半部分展示 6 种水平对齐效果:左对齐、居中、右对齐、跨列居中、分散对齐、两端对齐
表格下半部分展示 4 种垂直对齐效果:顶部、居中、底部、两端对齐
点击右侧的对齐按钮,可以将对应对齐方式应用到当前选中的单元格
按钮会根据当前选中单元格的对齐方式自动高亮显示
API 参考
hAlign 属性
设置或获取单元格的水平对齐方式。
hAlign 属性值可以是枚举值或数字:
GC.Spread.Sheets.HorizontalAlign.left 或 0:左对齐
GC.Spread.Sheets.HorizontalAlign.center 或 1:居中
GC.Spread.Sheets.HorizontalAlign.right 或 2:右对齐
GC.Spread.Sheets.HorizontalAlign.centerContinuous 或 4:跨列居中
GC.Spread.Sheets.HorizontalAlign.distributed 或 5:分散对齐
GC.Spread.Sheets.HorizontalAlign.justify 或 6:两端对齐
vAlign 属性
设置或获取单元格的垂直对齐方式。
vAlign 属性值可以是枚举值或数字:
GC.Spread.Sheets.VerticalAlign.top 或 0:顶部对齐
GC.Spread.Sheets.VerticalAlign.center 或 1:居中对齐
GC.Spread.Sheets.VerticalAlign.bottom 或 2:底部对齐
GC.Spread.Sheets.VerticalAlign.justify 或 3:两端对齐
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:C15").backColor("Accent 6 80");
sheet.addSpan(5, 2, 6, 1);
sheet.addSpan(11, 2, 4, 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.setRowHeight(13, 60);
sheet.setRowHeight(14, 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:E15").setBorder(lineBorder, { all: true });
// set text
sheet.setValue(4, 2, "Text Alignment");
sheet.setValue(5, 2, "Horizontal Alignment");
sheet.setValue(11, 2, "Vertical Alignment");
var hAlignData = [["Left"], ["Center"], ["Right"], ["Center Across Selection"], ["Distributed Alignment"], ["Justify Alignment Justify Alignment Justify Alignment"]];
var vAlignData = [["Top"], ["Center"], ["Bottom"], ["Justify\r\nAlignment"]];
sheet.setArray(5, 3, hAlignData);
sheet.setArray(11, 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).hAlign = GC.Spread.Sheets.HorizontalAlign.justify;
sheet.getStyle(11, 3).vAlign = GC.Spread.Sheets.VerticalAlign.top;
sheet.getStyle(12, 3).vAlign = GC.Spread.Sheets.VerticalAlign.center;
sheet.getStyle(13, 3).vAlign = GC.Spread.Sheets.VerticalAlign.bottom;
sheet.getStyle(14, 3).vAlign = GC.Spread.Sheets.VerticalAlign.justify;
sheet.resumePaint();
for (let i = 0; i < spread.getSheetCount(); i++) {
var sheet = spread.getSheet(i)
sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, onSelectionChanged.bind(sheet));
}
var style = sheet.getActualStyle(0, 0);
updateAlignButtons(style.hAlign, style.vAlign);
document.getElementById('HAlign0').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.hAlign = 0;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('HAlign1').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.hAlign = 1;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('HAlign2').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.hAlign = 2;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('HAlign3').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.hAlign = 4;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('HAlign4').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.hAlign = 5;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('HAlign5').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.hAlign = 6;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('VAlign0').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.vAlign = 0;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('VAlign1').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.vAlign = 1;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('VAlign2').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.vAlign = 2;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
document.getElementById('VAlign3').onclick = function () {
var sheet = spread.getActiveSheet();
var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
style.vAlign = 3;
sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style);
updateAlignButtons(style.hAlign, style.vAlign);
};
}
function onSelectionChanged(eventName, args) {
var sheet = this;
var row = sheet.getActiveRowIndex();
var col = sheet.getActiveColumnIndex();
var style = sheet.getActualStyle(row, col);
updateAlignButtons(style.hAlign, style.vAlign);
}
function updateAlignButtons(hAlign, vAlign) {
var hBtns = ["HAlign0", "HAlign1", "HAlign2", "HAlign0", "HAlign3", "HAlign4", "HAlign5"];
var vBtns = ["VAlign0", "VAlign1", "VAlign2", "VAlign3"];
hBtns.forEach(id => document.getElementById(id).style.backgroundColor = "");
vBtns.forEach(id => document.getElementById(id).style.backgroundColor = "");
highlight(hBtns[hAlign]);
highlight(vBtns[vAlign]);
}
function highlight(id) {
var btn = document.getElementById(id);
if (btn) btn.style.backgroundColor = "lightgreen";
}
<!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">水平对齐:</label>
<input type="button" value="左对齐" id="HAlign0" />
<input type="button" value="居中" id="HAlign1" /><br>
<input type="button" value="右对齐" id="HAlign2" />
<input type="button" value="跨列居中" id="HAlign3">
<input type="button" value="分散对齐" id="HAlign4">
<input type="button" value="两端对齐" id="HAlign5">
</div>
<div class="option-row">
<label id="Text Oriention" for="textOriention">垂直对齐:</label>
<input type="button" value="顶端对齐" id="VAlign0" />
<input type="button" value="居中" id="VAlign1" /><br>
<input type="button" value="底端对齐" id="VAlign2" />
<input type="button" value="两端对齐" id="VAlign3" />
</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;
}