你可以创建一个样式并设置对齐方式,如下所示。
然后将此样式应用于单元格,以设置文本对齐方式。
下面的列表显示了SpreadJS当前支持的对齐方式。
水平对齐:
左对齐
居中
右对齐
跨列居中 (CenterContinue)
分散对齐
垂直对齐:
顶部
居中
底部
<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>
import Vue from 'vue';
import '@grapecity-software/spread-sheets-vue';
import GC from '@grapecity-software/spread-sheets';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import './styles.css';
let App = Vue.extend({
name: "app",
data: function() {
return {
textOriention: 0,
spread: null,
top: 0,
left: 0,
right: 0,
bottom: 0
};
},
methods:{
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(spread) {
this.spread = 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();
}
}
});
new Vue({ render: h => h(App) }).$mount('#app');
</script>
<!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$/zh/vue/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/vue/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>
System.import('./src/app.vue');
System.import('$DEMOROOT$/zh/lib/vue/license.js');
</script>
</head>
<body>
<div id="app" style="height: 100%;"></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;
}
(function(global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
meta: {
'*.css': { loader: 'css' },
'*.vue': { loader: 'vue-loader' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-vue': 'npm:@grapecity-software/spread-sheets-vue/index.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js',
'jszip': 'npm:jszip/dist/jszip.js',
'css': 'npm:systemjs-plugin-css/css.js',
'vue': 'npm:vue/dist/vue.min.js',
'vue-loader': 'npm:systemjs-vue-browser/index.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'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
}
}
});
})(this);