形状对齐

SpreadJS 提供强大的形状对齐和分布功能,支持多个形状的水平对齐、垂直对齐以及均匀分布。通过捕捉模式,形状可以自动吸附到网格线或其他形状边界,帮助用户快速创建整齐美观的表格布局。

概述 本 Demo 展示了 SpreadJS 的形状对齐和分布功能。Demo 创建了三个矩形形状,提供了水平对齐(左/中/右)、垂直对齐(上/中/下)、水平分布、垂直分布等操作,并支持设置形状的捕捉模式,使形状在拖动或调整大小时自动对齐到网格或其他形状。 实现思路 创建三个矩形形状作为操作对象 监听形状选择变化事件,动态启用或禁用对齐按钮(对齐需要至少 2 个形状,分布需要至少 3 个形状) 为对齐和分布按钮绑定点击事件,通过命令管理器执行相应的对齐命令 为捕捉模式下拉框绑定变更事件,设置形状的自动吸附行为 代码解析 创建形状 使用 sheet.shapes.add() 方法添加三个矩形形状,参数依次为形状名称、形状类型、左边距、上边距、宽度和高度。 执行对齐命令 通过命令管理器的 execute() 方法执行对齐命令。参数包括: cmd:命令类型,如 'moveShapesByHAlign'(水平对齐)、'moveShapesByVAlign'(垂直对齐) sheetName:工作表名称 shapeNames:要操作的形状名称数组 alignment:对齐方式枚举值 设置捕捉模式 使用 snapMode() 方法设置形状的捕捉模式,可选值包括: SnapMode.none:无捕捉 SnapMode.grid:对齐网格 SnapMode.shape:对齐形状边界 SnapMode.grid | SnapMode.shape:同时对齐网格和形状 对齐枚举转换 将字符串参数转换为 SpreadJS 的对齐枚举值。水平对齐使用 HorizontalAlign 枚举(left/center/right),垂直对齐使用 VerticalAlign 枚举(top/middle/bottom)。 形状选择事件处理 监听形状选择变化事件,根据选中形状的数量动态启用或禁用按钮。对齐操作需要至少 2 个形状,分布操作需要至少 3 个形状。 运行效果 选中两个或更多形状后,可以从下拉框中选择对齐方式(左/中/右、上/中/下),形状会按照选定的基准线对齐 选中三个形状后,可以点击"水平分布"或"垂直分布"按钮,形状会均匀分布 通过捕捉模式下拉框可以设置形状的吸附行为,包括无吸附、吸附网格、吸附形状边界或同时吸附两者 当选中形状数量不足时,对应的对齐或分布按钮会被禁用
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 }); initSpread(spread); }; function initSpread(spread) { let sheet = spread.getActiveSheet(); var rect1 = sheet.shapes.add('rect1', GC.Spread.Sheets.Shapes.AutoShapeType.rectangle, 100, 50, 230, 100); var rect2 = sheet.shapes.add('rect2', GC.Spread.Sheets.Shapes.AutoShapeType.rectangle, 200, 200, 150, 100); var rect3 = sheet.shapes.add('rect3', GC.Spread.Sheets.Shapes.AutoShapeType.rectangle, 500, 300, 200, 100); spread.bind(GC.Spread.Sheets.Events.ShapeSelectionChanged, function () { shapeSelectionChangedHandler(spread); }); document.querySelectorAll('.align').forEach(function (btn) { btn.addEventListener('click', function (e) { let activeSheet = spread.getActiveSheet(); let activeShapes = activeSheet.shapes.all().filter(function (sp) { return sp.isSelected(); }); let classNames = e.target.className.split(' '); let cmd = classNames[1]; let alignment = classNames[2]; var commandManager = spread.commandManager(); commandManager.execute({ cmd: cmd, sheetName: activeSheet.name(), shapeNames: activeShapes.map(function (s) { return s.name(); }), alignment: getAlignmentEnum(alignment) }); }); }); document.querySelectorAll('.align-dd').forEach(function (dd) { dd.addEventListener('change', function (e) { let activeSheet = spread.getActiveSheet(); let activeShapes = activeSheet.shapes.all().filter(function (sp) { return sp.isSelected(); }); let classNames = e.target.className.split(' '); let cmd = classNames[1]; var commandManager = spread.commandManager(); commandManager.execute({ cmd: cmd, sheetName: activeSheet.name(), shapeNames: activeShapes.map(function (s) { return s.name(); }), alignment: getAlignmentEnum(e.target.value) }); }); }); document.querySelector('#changeSnapMode').addEventListener('change', function (e) { let sheet = spread.getActiveSheet(); switch (e.target.value) { case '0': sheet.shapes.snapMode(GC.Spread.Sheets.Shapes.SnapMode.none); return; case '1': sheet.shapes.snapMode(GC.Spread.Sheets.Shapes.SnapMode.grid); return; case '2': sheet.shapes.snapMode(GC.Spread.Sheets.Shapes.SnapMode.shape); return; case '3': sheet.shapes.snapMode(GC.Spread.Sheets.Shapes.SnapMode.grid | GC.Spread.Sheets.Shapes.SnapMode.shape); return; default: return; } }); shapeSelectionChangedHandler(spread); } function toggleEnable(enable, className) { document.querySelectorAll('.' + className).forEach(function (btn) { if (enable) { btn.removeAttribute('disabled'); } else { btn.setAttribute('disabled', ''); } }) } function getAlignmentEnum(alignment) { switch (alignment) { case 'left': return GC.Spread.Sheets.Shapes.HorizontalAlign.left; case 'center': return GC.Spread.Sheets.Shapes.HorizontalAlign.center; case 'right': return GC.Spread.Sheets.Shapes.HorizontalAlign.right; case 'top': return GC.Spread.Sheets.Shapes.VerticalAlign.top; case 'middle': return GC.Spread.Sheets.Shapes.VerticalAlign.middle; case 'bottom': return GC.Spread.Sheets.Shapes.VerticalAlign.bottom; default: return; } } function _getElementById(id) { return document.getElementById(id); } function shapeSelectionChangedHandler(spread) { let activeSheet = spread.getActiveSheet(); let shapes = activeSheet.shapes.all(), activeShapes = []; for (let i = 0; i < shapes.length; i++) { let shape = shapes[i]; if (shape.isSelected()) { activeShapes.push(shape); } } if (activeShapes.length > 1) { toggleEnable(true, 'justify'); } else { toggleEnable(false, 'justify'); } if (activeShapes.length > 2) { toggleEnable(true, 'distribute'); } else { toggleEnable(false, 'distribute'); } }
<!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-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.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="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"> 选择两个或更多形状,然后更改下方不同的对齐选项以查看效果。 </div> <div class="option-row"> *对于垂直和水平分布,必须选中所有三个形状。 </div> <div class="option-row"> <label>- 水平对齐</label> <hr> <select id="moveShapesByHAlign" class="align-dd moveShapesByHAlign"> <option value="left" selected>左</option> <option value="center">居中</option> <option value="right">右</option> </select> <br /> <br /> <label>水平分布所选形状</label> <input type="button" class="align moveShapesByHDistribute distribute" value="水平"> <br /> <label>- 垂直对齐</label> <hr> <select id="moveShapesByVAlign" class="align-dd moveShapesByVAlign"> <option value="top" selected>顶部</option> <option value="middle">中间</option> <option value="bottom">底部</option> </select> <br /> <br /> <label>垂直分布所选形状</label> <input type="button" class="align moveShapesByVDistribute distribute" value="垂直"> <br /> <label>更改吸附模式选项,以便基于网格线或工作表中的其他形状,将形状对齐、拖动或调整大小到最近的边缘。</label> <hr> <select id="changeSnapMode"> <option value="0" selected>无</option> <option value="1">网格</option> <option value="2">形状</option> <option value="3">网格和形状</option> </select> <br /> </div> </div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 350px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 350px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 5px; } .sample-options { z-index: 1000; } label { display: block; margin-bottom: 6px; } p{ padding:2px 10px; background-color:#F4F8EB; } input { padding: 4px 6px; width: 160px; } input[type=button] { margin-top: 6px; display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }