文字方向与垂直文本

SpreadJS 支持设置单元格文字的旋转角度和垂直显示方式。用户可以通过 textOrientation 属性将文字旋转 -90° 到 +90°,或使用 isVerticalText 方法实现垂直文本布局,满足多样化的表格排版需求。

概述 本 Demo 展示了如何设置单元格文字的旋转角度和垂直显示。Demo 提供了两个交互功能:设置文本旋转角度(-90° 到 +90°)和切换垂直文本显示,帮助用户理解 SpreadJS 的文字方向控制能力。 实现思路 使用 fromJSON 加载预设的表格数据 为第 0 行的特定单元格设置文本装饰(删除线、上划线、下划线) 提供交互按钮,允许用户选择单元格后设置文字旋转角度 提供切换按钮,允许用户为选中单元格启用或关闭垂直文本显示 代码解析 设置文本装饰 这段代码为第 0 行的单元格设置不同的文本装饰效果,包括删除线、上划线和下划线。 设置文字旋转角度 这段代码为按钮添加点击事件监听器。当用户点击按钮时,获取当前活动单元格和用户输入的角度值,验证角度在 -90 到 90 之间后,调用 textOrientation() 方法设置文字旋转角度。 切换垂直文本 这段代码实现垂直文本的切换功能。isVerticalText() 方法接受布尔值参数,true 表示垂直显示文本,false 表示水平显示。通过 !cell.isVerticalText() 实现切换效果。 运行效果 Demo 启动后显示预设的表格数据,第 0 行的单元格展示了不同的文本装饰效果 选择一个单元格(例如 G5 的"数学"),在输入框中输入角度值(如 30),点击"设置文本角度"按钮,文字将旋转到指定角度 点击"设置垂直文本"按钮,可以将选中单元格的文字在水平和垂直显示之间切换 旋转角度必须在 -90 到 90 之间,否则会提示错误 当单元格设置旋转时,边框和背景色会跟随旋转到相应角度 API 参考 textOrientation 方法 value:旋转角度,范围 -90 到 90 返回值:当前旋转角度(无参数时)或单元格对象(有参数时) isVerticalText 方法 value:布尔值,true 表示垂直显示,false 表示水平显示 返回值:当前垂直文本状态(无参数时)或单元格对象(有参数时)
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 5 }); initSpread(spread); }; function initSpread(spread) { spread.fromJSON(jsonData); var sheet = spread.getSheet(3); sheet.getCell(0, 2).textDecoration(GC.Spread.Sheets.TextDecorationType.lineThrough); sheet.getCell(0, 3).textDecoration(GC.Spread.Sheets.TextDecorationType.overline); sheet.getCell(0, 4).textDecoration(GC.Spread.Sheets.TextDecorationType.underline); document.getElementById("btnTextOriention").addEventListener('click', function () { var sheet = spread.getActiveSheet(); if (sheet) { var cell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); var rotationValue = parseInt(document.getElementById("textOriention").value); if(-90 <= rotationValue && rotationValue <= 90){ cell.textOrientation(rotationValue); } else { alert("please input correct rotation angle") } } }); document.getElementById("btnVerticalText").addEventListener('click', function () { var sheet = spread.getActiveSheet(); if (sheet) { var cell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); cell.isVerticalText(!cell.isVerticalText()); } }); }
<!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/textOriention.js" type="text/javascript"></script> <script src="app.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"> <p>选择一个单元格,例如 G5(“数学”),然后将角度更改为 ‘30’</p> <label id="Text Oriention" for="textOriention">角度(-90<= 角度 <=90)</label> <input type="text" id="textOriention" /> <input type="button" value="设置文本角度" id="btnTextOriention" /> </div> <div class="option-row"> <label id="Text Oriention" for="textOriention">设置垂直文本</label> <input type="button" value="设置垂直文本" id="btnVerticalText" /> </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; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } label { display: block; margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }