文本省略符

SpreadJS 支持在单元格文本超出显示范围时显示省略号,帮助用户清晰识别内容溢出的单元格。当鼠标悬停在显示省略号的单元格上时,会自动显示完整内容的提示信息。该功能可与文本对齐、缩进、垂直文本和单元格内边距等样式配合使用,为构建紧凑的数据展示界面提供了灵活的文本控制能力。

概述 本 Demo 展示了如何使用 showEllipsis 属性控制单元格文本溢出时的省略号显示,以及该属性与文本对齐、缩进、垂直文本和单元格内边距等样式的协同效果。Demo 提供了交互式面板,用户可以选中单元格后动态调整各种样式属性,观察省略号的显示效果变化。 实现思路 使用 fromJSON 加载预定义的表格数据,初始化 Spread 工作簿 通过 getActualStyle() 获取当前选中单元格的实际样式对象 根据用户操作修改样式对象的属性(showEllipsis、hAlign、vAlign、textIndent、isVerticalText、cellPadding) 使用 setStyle() 将修改后的样式应用到选中单元格 为每个样式控制按钮绑定点击事件处理函数 代码解析 设置省略号显示 这段代码获取选中单元格的当前样式,将 showEllipsis 属性设置为 true,然后应用样式。当文本超出单元格边界时,会显示省略号而不是溢出到相邻单元格。 设置水平对齐 水平对齐值使用数字常量:0 表示左对齐,1 表示居中对齐,2 表示右对齐。对齐方式会影响省略号的位置和文本显示区域。 设置文本缩进 文本缩进会增加文本与单元格边界的距离,从而影响省略号的显示位置。代码检查当前缩进值,递增后更新样式。 设置单元格内边距 cellPadding 属性接受类似 CSS 的格式字符串("上 右 下 左"),为单元格内容设置内边距,进一步影响文本显示区域和省略号位置。 运行效果 点击"显示"按钮,选中单元格的文本溢出时会显示省略号;点击"移除"按钮取消省略号显示 调整水平对齐(左对齐、居中、右对齐)和垂直对齐(顶部、居中、底部)可以改变省略号的位置 增加或减少文本缩进值,观察缩进对省略号显示的影响 点击"垂直"按钮切换到垂直文本模式,点击"正常"按钮恢复水平文本 设置单元格内边距(上、右、下、左),观察内边距对文本显示区域的影响 当鼠标悬停在显示省略号的单元格上时,会自动弹出提示显示完整文本内容 API 参考 showEllipsis 属性 类型:undefined | boolean 作用:控制文本超出边界时是否显示省略号 设置为 true 时,溢出文本被剪切并显示省略号,悬停时会显示完整文本的提示 getActualStyle 方法 row:行索引 col:列索引 sheetArea:可选,表单区域 返回值:单元格的实际样式对象 setStyle 方法 row、col:单元格位置 style:样式对象 sheetArea:可选,表单区域 相关样式属性 hAlign:水平对齐(0=左对齐,1=居中,2=右对齐) vAlign:垂直对齐(0=顶部对齐,1=居中,2=底部对齐) textIndent:文本缩进量 isVerticalText:是否为垂直文本 textOrientation:文本旋转角度 cellPadding:单元格内边距,格式为"上 右 下 左"
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 }); initSpread(spread); }; function initSpread(spread) { spread.fromJSON(data); var sheet = spread.getActiveSheet(); 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('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); }; document.getElementById('IndentUp').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); if (!isNaN(parseInt(style.textIndent))) { style.textIndent = (parseInt(style.textIndent) + 1) + ""; } else if (style.textIndent === undefined) { style.textIndent = '1'; } sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('IndentDwon').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); if (!isNaN(parseInt(style.textIndent)) && parseInt(style.textIndent) > 0) { style.textIndent = (parseInt(style.textIndent) - 1) + ""; } sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('Vertical').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.isVerticalText = true; style.textOrientation = 0; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('Normal').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.isVerticalText = undefined; style.textOrientation = undefined; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('SetPadding').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.cellPadding = document.getElementById('Top').value + ' ' + document.getElementById('Right').value + ' ' +document.getElementById('Bottom').value + ' '+document.getElementById('Left').value; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('ShowEllipsis').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.showEllipsis = true; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; document.getElementById('RemoveEllipsis').onclick = function () { var style = sheet.getActualStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); style.showEllipsis = undefined; sheet.setStyle(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), style); }; }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="spreadjs culture" content="zh-cn" /> <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/ellipsis.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"> <label id="Text Oriention" for="textOriention">显示省略号:</label> <input type="button" value="显示" id="ShowEllipsis" /> <input type="button" value="移除" id="RemoveEllipsis" /> </div> <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" /> </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" /> </div> <div class="option-row"> <label id="Text Oriention" for="textOriention">缩进:</label> <input type="button" value="增加" id="IndentUp" /> <!-- Fixed typo in original ID: "IndentDwon" -> "IndentDown" --> <input type="button" value="减少" id="IndentDown" /> </div> <div class="option-row"> <label id="Text Oriention" for="textOriention">垂直文本:</label> <input type="button" value="垂直" id="Vertical" /> <input type="button" value="正常" id="Normal" /> </div> <div class="option-row"> <label id="Text Oriention" for="textOriention">内边距:</label> <table> <tr> <td> <label class="paddingLabel1">顶部:</label><input class="paddingInput1" id="Top" type="number" /><br> </td> <td> <label class="paddingLabel1">右侧:</label><input class="paddingInput1" id="Right" type="number" /><br> </td> </tr> <tr> <td> <label class="paddingLabel1">底部:</label><input class="paddingInput1" id="Bottom" type="number" /><br> </td> <td> <label class="paddingLabel1">左侧:</label><input class="paddingInput1" id="Left" type="number" /> </td> </tr> </table> <input style="margin-left: 117px;" id="SetPadding" type="button" value="设置" /> </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: 100px; } .option-row { font-size: 14px; padding: 5px; } label { display: block; padding-bottom: 5px; } input { padding: 4px 6px; margin-bottom: 6px; margin-right: 10px; } .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; }