You can align shapes using the following code:
You can change snapMode to align shapes easily
Now when you drag or resize a shape, the shape will align to grid lines automatically.
You can also align to other shapes:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import './styles.css';
import { AppFunc } from './app-func';
// import { App } from './app-class';
// 1. Functional Component sample
ReactDOM.render(<AppFunc />, document.getElementById('app'));
// 2. Class Component sample
// ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets } from '@mescius/spread-sheets-react';
import "@mescius/spread-sheets-shapes";
let spread = null;
export function AppFunc() {
const [alignmentOption, setAlignmentOption] = React.useState({
canAlign: false,
canDistribute: false
});
const initSpread = (currSpread) => {
spread = currSpread;
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 () {
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);
}
}
const newAlignmentOption = {};
newAlignmentOption.canAlign = activeShapes.length > 1;
newAlignmentOption.canDistribute = activeShapes.length > 2;
setAlignmentOption(newAlignmentOption);
});
}
const align = (cmd, alignment) => {
let activeSheet = spread.getActiveSheet();
let activeShapes = activeSheet.shapes.all().filter(function (sp) {
return sp.isSelected();
});
var commandManager = spread.commandManager();
commandManager.execute({
cmd: cmd,
sheetName: activeSheet.name(),
shapeNames: activeShapes.map(function (s) {
return s.name();
}),
alignment: getAlignmentEnum(alignment)
});
}
const alignDd = (e, cmd) => {
let activeSheet = spread.getActiveSheet();
let activeShapes = activeSheet.shapes.all().filter(function (sp) {
return sp.isSelected();
});
var commandManager = spread.commandManager();
commandManager.execute({
cmd: cmd,
sheetName: activeSheet.name(),
shapeNames: activeShapes.map(function (s) {
return s.name();
}),
alignment: getAlignmentEnum(e.target.value)
});
}
const 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;
}
}
const changeSnapMode = (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;
}
}
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => initSpread(spread)}>
</SpreadSheets>
</div>
<div class="options-container">
<div class="option-row">
Select two or more shapes then change the different alignment options below to see the effects.
</div>
<div class="option-row">
*for the vertical and horizontal distribution, all three shapes must be selected.
</div>
<div class="option-row">
<label>- Horizontal Alignment</label>
<hr />
<select disabled={!alignmentOption.canAlign} onChange={($event) => {alignDd($event, 'moveShapesByHAlign'); }}>
<option value="left">left</option>
<option value="center">center</option>
<option value="right">right</option>
</select>
<br /> <br />
</div>
<div class="option-row">
<label>Horizontal Distribute Selected Shapes</label>
<input type="button" disabled={!alignmentOption.canDistribute} value="horizontal" onClick={() => { align('moveShapesByHDistribute'); }}></input>
</div>
<div class="option-row">
<label>- Vertical Alignment</label>
<hr />
<select disabled={!alignmentOption.canAlign} onChange={($event) => {alignDd($event, 'moveShapesByVAlign'); }}>
<option value="top">top</option>
<option value="middle">middle</option>
<option value="bottom">bottom</option>
</select>
<br /> <br />
<label>Vertical Distribute Selected Shapes</label>
<input type="button" disabled={!alignmentOption.canDistribute} value="vertical" onClick={() => { align('moveShapesByVDistribute'); }}></input>
</div>
<div class="option-row">
<label>Change the SnapMode options to align, drag, or resize shapes to the nearest edge based on gridlines or other shapes in the worksheet. </label>
<select id="changeSnapMode" onChange={($event) => {changeSnapMode($event); }}>
<option value="0" selected>none</option>
<option value="1">grid</option>
<option value="2">shape</option>
<option value="3">grid and shape</option>
</select>
</div>
</div>
</div>);
}
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets } from '@mescius/spread-sheets-react';
import "@mescius/spread-sheets-shapes";
const Component = React.Component;
export class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.state = {
canAlign: false,
canDistribute: false
}
}
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
</SpreadSheets>
</div>
<div class="options-container">
<div class="option-row">
Select two or more shapes then change the different alignment options below to see the effects.
</div>
<div class="option-row">
*for the vertical and horizontal distribution, all three shapes must be selected.
</div>
<div class="option-row">
<label>- Horizontal Alignment</label>
<hr />
<select disabled={!this.state.canAlign} onChange={($event) => {this.alignDd($event, 'moveShapesByHAlign'); }}>
<option value="left">left</option>
<option value="center">center</option>
<option value="right">right</option>
</select>
<br /> <br />
</div>
<div class="option-row">
<label>Horizontal Distribute Selected Shapes</label>
<input type="button" disabled={!this.state.canDistribute} value="horizontal" onClick={() => { this.align('moveShapesByHDistribute'); }}></input>
</div>
<div class="option-row">
<label>- Vertical Alignment</label>
<hr />
<select disabled={!this.state.canAlign} onChange={($event) => {this.alignDd($event, 'moveShapesByVAlign'); }}>
<option value="top">top</option>
<option value="middle">middle</option>
<option value="bottom">bottom</option>
</select>
<br /> <br />
<label>Vertical Distribute Selected Shapes</label>
<input type="button" disabled={!this.state.canDistribute} value="vertical" onClick={() => { this.align('moveShapesByVDistribute'); }}></input>
</div>
<div class="option-row">
<label>Change the SnapMode options to align, drag, or resize shapes to the nearest edge based on gridlines or other shapes in the worksheet. </label>
<select id="changeSnapMode" onChange={($event) => {this.changeSnapMode($event); }}>
<option value="0" selected>none</option>
<option value="1">grid</option>
<option value="2">shape</option>
<option value="3">grid and shape</option>
</select>
</div>
</div>
</div>);
}
initSpread(spread) {
let self = this;
self.spread = 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 () {
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) {
self.setState({ canAlign: true });
} else {
self.setState({ canAlign: false });
}
if (activeShapes.length > 2) {
self.setState({ canDistribute: true });
} else {
self.setState({ canDistribute: false });
}
});
}
align(cmd, alignment) {
let self = this, spread = self.spread;
let activeSheet = spread.getActiveSheet();
let activeShapes = activeSheet.shapes.all().filter(function (sp) {
return sp.isSelected();
});
var commandManager = spread.commandManager();
commandManager.execute({
cmd: cmd,
sheetName: activeSheet.name(),
shapeNames: activeShapes.map(function (s) {
return s.name();
}),
alignment: self.getAlignmentEnum(alignment)
});
}
alignDd(e, cmd){
let self = this, spread = self.spread;
let activeSheet = spread.getActiveSheet();
let activeShapes = activeSheet.shapes.all().filter(function (sp) {
return sp.isSelected();
});
var commandManager = spread.commandManager();
commandManager.execute({
cmd: cmd,
sheetName: activeSheet.name(),
shapeNames: activeShapes.map(function (s) {
return s.name();
}),
alignment: self.getAlignmentEnum(e.target.value)
});
}
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;
}
}
changeSnapMode(e) {
let self = this, spread = self.spread;
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;
}
}
}
<!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$/en/react/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/en/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/en/lib/react/license.js').then(function () {
System.import('./src/app');
});
</script>
</head>
<body>
<div id="app"></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;
}
#app {
height: 100%;
}
(function (global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true,
react: true
},
meta: {
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/index.js',
'@mescius/spread-sheets-react': 'npm:@mescius/spread-sheets-react/index.js',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js',
'react': 'npm:react/umd/react.production.min.js',
'react-dom': 'npm:react-dom/umd/react-dom.production.min.js',
'css': 'npm:systemjs-plugin-css/css.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: 'jsx'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);