Overview

SpreadJS includes support for over 180 shapes, including many of the Excel shapes. SpreasJS also provides more advanced functionality using data-driven shapes. For example, you can dynamically generate the shape properties using data from a database or other data source that allows you to create interactive shapes such as for a manufacturing plant floor.

SpreadJS provides the following three shape types AutoShape: the autoShape can be used stand-alone. ConnectorShape: the connector shape can be used stand-along or connected to other shape. GroupShape: the group shape is not a real shape but used as a manager to process with a group of shapes easily and quickly. To use the shape feature, add the js file link into the document's head section: You can manage all shapes in a sheet using the ShapeCollection API: You can create a autoShape with the sheet.shapes.add method, as shown below: You can create a connectorShape with the sheet.shapes.addConnector method, as shown below: You can get/remove a shape with name using the following code: You can customize the properties of all shapes using the ShapeBase API: allowMove: Gets or sets whether to disable moving the shape. allowResize: Gets or sets whether to disable resizing the shape. allowRotate: Gets or sets whether to disable rotating the shape. showHandle: Gets or sets whether to show handle of shape. isSelected: Gets or sets whether this shape is selected. width/height: Gets or sets the width or height of the shape. x/y: Gets or sets the horizontal or vertical location of the shape. You can customize the properties of autoShapes using the Shape API: rotate: Gets or sets the rotated angle of the shape (unit in degree). text: Gets or sets the text of the shape. style: Gets or sets the style of the shape. You can customize the properties of connectorShapes using the ConnectorShape API: style: Gets or sets the style of the connector shape.
import { Component, NgModule, enableProdMode } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { SpreadSheetsModule } from '@grapecity/spread-sheets-angular'; import GC from '@grapecity/spread-sheets'; import "@grapecity/spread-sheets-shapes" import './styles.css'; function getEnumList(enumObject: object) { let names = []; for (var name in enumObject) { if (name === "none" || (parseInt(<any>name, 10)) == <any>name) { continue; } names.push({ name: name, value: enumObject[name] }); } names.sort(function(a, b) { return a.name > b.name ? 1 : -1 }); return names; } const lineCapStyle = { flat: 2, square: 1, round: 0 }; const lineJoinStyle = { round: 0, miter: 1, bevel: 2 }; const lineDashStyle = { solid: 0, squareDot: 1, dash: 2, longDash: 3, dashDot: 4, longDashDot: 5, longDashDotDot: 6, sysDash: 7, sysDot: 8, sysDashDot: 9, dashDotDot: 10 }; const arrowheadLength = { "short": 0, "medium": 1, "long": 2 }; const arrowheadWidth = { "narrow": 0, "medium": 1, "wide": 2 }; const horizontalAlign = { left: 0, center: 1, right: 2 }; const verticalAlign = { top: 0, center: 1, bottom: 2 }; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { spread: GC.Spread.Sheets.Workbook; hostStyle = { width: 'calc(100% - 280px)', height: '100%', overflow: 'hidden', float: 'left' }; showBorderPanel = false; showConnectorPropPanel = false; showShapeTxtPanel = false; showShapeFillPropPanel = false; rotate = 30; transparency = 0.5; background = "#00A2E8"; text = `abcdefgHIJKLMN 12356789 SpreadJSSpreadJS`; textColor = "#FFFF00"; transparencyTxt = 0.5; font = "bold 15px Georgia"; hAlign = 1; vAlign = 1; borderColor = "#00A2E8"; borderTransparency = 0.5; borderWidth = 1; borderLineStyle = 2; borderCapType = 2; borderJoinType = 2; beginStyle = 3; beginWidth = 1; beginLength = 2; endStyle = 3; endWidth = 1; endLength = 2; addShapeType = GC.Spread.Sheets.Shapes.AutoShapeType.actionButtonBackorPrevious; addConnectorShapeType = GC.Spread.Sheets.Shapes.ConnectorType.elbow; autoShapeTypeList = getEnumList(GC.Spread.Sheets.Shapes.AutoShapeType); connectorTypeList = getEnumList(GC.Spread.Sheets.Shapes.ConnectorType); horizontalAlignList = getEnumList(horizontalAlign); verticalAlignList = getEnumList(verticalAlign); lineCapStyleList = getEnumList(lineCapStyle); lineJoinStyleList = getEnumList(lineJoinStyle); lineDashStyleList = getEnumList(lineDashStyle); arrowHeadLengthList = getEnumList(arrowheadLength); arrowHeadWidthList = getEnumList(arrowheadWidth); arrowHeadStyle = getEnumList(GC.Spread.Sheets.Shapes.ArrowheadStyle); initSpread($event: any) { let spread = this.spread = $event.spread; spread.getActiveSheet().shapes.add("heart", GC.Spread.Sheets.Shapes.AutoShapeType.heart, 40, 20, 150, 150); let lineShape = spread.getActiveSheet().shapes.addConnector("line", GC.Spread.Sheets.Shapes.ConnectorType.straight, 290, 20, 420, 170); let lineShapeStyle = lineShape.style(); lineShapeStyle.line.width = 8; lineShape.style(lineShapeStyle); this.bindSpreadEvent(); } bindSpreadEvent() { let spread = this.spread, self = this; spread.bind(GC.Spread.Sheets.Events.ShapeSelectionChanged, function() { let sheet = spread.getActiveSheet(); var selectedShape = sheet.shapes.all().filter(function(sp: GC.Spread.Sheets.Shapes.ShapeBase) { return sp.isSelected(); }); var isShapeSelected = false, isConnectorSelected = false; if (selectedShape.length > 0) { selectedShape.forEach((shape: GC.Spread.Sheets.Shapes.ShapeBase) => { if (!isShapeSelected && shape instanceof GC.Spread.Sheets.Shapes.Shape) { isShapeSelected = true; } else if (!isConnectorSelected && shape instanceof GC.Spread.Sheets.Shapes.ConnectorShape) { isConnectorSelected = true; } }); self.setShapePropVisibility(isShapeSelected); self.setConnectorPropVisibility(isConnectorSelected); self.setBorderPropVisibility(true); } else { self.setShapePropVisibility(false); self.setConnectorPropVisibility(false); self.setBorderPropVisibility(false); } }) } setBorderPropVisibility(isShow: boolean) { this.showBorderPanel = isShow; } setConnectorPropVisibility(isShow: boolean) { this.showConnectorPropPanel = isShow; } setShapePropVisibility(isShow: boolean) { this.showShapeTxtPanel = isShow; this.showShapeFillPropPanel = isShow; } updateShapeStyle(action: string, value: any) { let sheet = this.spread.getActiveSheet(); let activeShape = sheet.shapes.all().filter(function(sp: GC.Spread.Sheets.Shapes.ShapeBase) { return sp.isSelected(); }); if (activeShape.length > 0) { activeShape.forEach((shape: GC.Spread.Sheets.Shapes.ShapeBase) => { if (shape instanceof GC.Spread.Sheets.Shapes.Shape) { this._setShapeStyle(shape, action, value); } }); sheet.repaint(); } } updateShapeBorderStyle(action: string, value: any) { console.log(action, value) let sheet = this.spread.getActiveSheet(); let activeShape = sheet.shapes.all().filter(function(sp: GC.Spread.Sheets.Shapes.ShapeBase) { return sp.isSelected(); }); if (activeShape.length > 0) { activeShape.forEach(function(shape: GC.Spread.Sheets.Shapes.ShapeBase) { let shapeStyle = shape.style(); shapeStyle.line[action] = value; shape.style(shapeStyle); }); sheet.repaint(); } } updateConnectorShapeStyle(action: string, value: any) { let sheet = this.spread.getActiveSheet(); let activeShape = sheet.shapes.all().filter(function(sp: GC.Spread.Sheets.Shapes.ShapeBase) { return sp.isSelected() && sp instanceof GC.Spread.Sheets.Shapes.ConnectorShape; }); if (activeShape.length > 0) { activeShape.forEach((shape: GC.Spread.Sheets.Shapes.ShapeBase) => { this._setConnectorShapeStyle(shape, action, value); }); sheet.repaint(); } } _setConnectorShapeStyle(shape: GC.Spread.Sheets.Shapes.ShapeBase, action: string, value: any) { let shapeStyle = shape.style(); let shapeStyleLine = shapeStyle.line; switch (action) { case "beginStyle": { shapeStyleLine.beginArrowheadStyle = value; break; } case "beginWidth": { shapeStyleLine.beginArrowheadWidth = value; break; } case "beginLength": { shapeStyleLine.beginArrowheadLength = value; break; } case "endStyle": { shapeStyleLine.endArrowheadStyle = value; break; } case "endWidth": { shapeStyleLine.endArrowheadWidth = value; break; } case "endLength": { shapeStyleLine.endArrowheadLength = value; break; } } shape.style(shapeStyle); } _setShapeStyle(shape: GC.Spread.Sheets.Shapes.ShapeBase, action: string, value: any) { if (action === 'rotate') { shape.rotate(value); } else if (action === 'text') { shape.text(value); } else { var shapeStyle = shape.style(); if (action === 'background') { shapeStyle.fill.color = value; } else if (action === 'transparency') { shapeStyle.fill.transparency = value; } else if (action === 'color') { shapeStyle.textEffect.color = value; } else if (action === 'transparencyTxt') { shapeStyle.textEffect.transparency = value; } else if (action === 'font') { shapeStyle.textEffect.font = value; } else if (action === 'hAlign') { shapeStyle.textFrame.hAlign = parseInt(value); } else if (action === 'vAlign') { shapeStyle.textFrame.vAlign = parseInt(value); } shape.style(shapeStyle); } } insertShape(addShapeType: GC.Spread.Sheets.Shapes.AutoShapeType) { console.log(addShapeType); let sheet = this.spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length; let x = 40 + (total % 2) * 250, y = parseInt(total / 2 + '') * 200 + 20; let shape = shapes.add('', addShapeType, x, y); this._setShapeStyle(shape, 'hAlign', 1); } insertConnectShape(addConnectorShapeType: GC.Spread.Sheets.Shapes.ConnectorType) { // addConnectorShapeType = parseInt(addConnectorShapeType, 10); let sheet = this.spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length; let x = 40 + (total % 2) * 250, y = parseInt(total / 2 + '') * 200 + 20; shapes.addConnector('', parseInt(addConnectorShapeType), x, y, x + 200, y + 200); } } @NgModule({ imports: [BrowserModule, SpreadSheetsModule, FormsModule], declarations: [AppComponent], exports: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } enableProdMode(); platformBrowserDynamic().bootstrapModule(AppModule);
<!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/angular/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- Polyfills --> <script src="$DEMOROOT$/en/angular/node_modules/core-js/client/shim.min.js"></script> <script src="$DEMOROOT$/en/angular/node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="$DEMOROOT$/en/angular/node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('$DEMOROOT$/en/lib/angular/license.ts'); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html>
<div class="sample-tutorial"> <gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)"> </gc-spread-sheets> <div class="options-container"> <div class="option-row"> Try selecting a shape from either of the drop-down menus and click the ‘Add’ button to add that shape to the Spread instance. </div> <div class="option-row"> <label for='autoShapeType'>Add Shape: </label> <select class="shapeSelect" [value]="addShapeType|number" [(ngModel)]="addShapeType"> <option *ngFor="let item of autoShapeTypeList" [value]="item.value"> {{item.name}} </option> </select> <input type='button' (click)="insertShape(addShapeType)" value="Add" /> <label for='connectShapeType'>Add Connect Shape: </label> <select class="shapeSelect" [value]="addConnectorShapeType|number" [(ngModel)]="addConnectorShapeType"> <option *ngFor="let item of connectorTypeList" [value]="item.value"> {{item.name}} </option> </select> <input type='button' (click)='insertConnectShape(addConnectorShapeType)' value="Add" /> </div> <div id="divideLine" class="divide-line"></div> <div *ngIf="showBorderPanel" class="option-row"> <label class="title">Shape Border</label> <label>Border Color: </label> <input [(ngModel)]="borderColor" type="color" /> <input type="button" (click)="updateShapeBorderStyle('color', borderColor)" value="Set" /> <label>Border Transparency: </label> <input [(ngModel)]="borderTransparency" type="text" value="0.5" /> <input type="button" (click)="updateShapeBorderStyle('transparency', borderTransparency)" value="Set" /> <label>Border Width: </label> <input [(ngModel)]="borderWidth" type="number" /> <input type="button" (click)="updateShapeBorderStyle('width', borderWidth)" value="Set" /> <label>Border Line Style: </label> <select [(ngModel)]="borderLineStyle"> <option *ngFor="let item of lineDashStyleList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateShapeBorderStyle('lineStyle', borderLineStyle)" value="Set" /> <label>Border Cap Line Style: </label> <select [(ngModel)]="borderCapType"> <option *ngFor="let item of lineCapStyleList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateShapeBorderStyle('capType', borderCapType)" value="Set" /> <label>Border Join Line Style: </label> <select [(ngModel)]="borderJoinType"> <option *ngFor="let item of lineJoinStyleList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateShapeBorderStyle('joinType', borderJoinType)" value="Set" /> </div> <div *ngIf="showShapeFillPropPanel" id="shapeFillProp" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Fill and Rotate</label> <label>Fill Color: </label> <input [(ngModel)]="background" type="color" /> <input type="button" (click)="updateShapeStyle('background', background)" value="Set" /> <label>Fill Transparency: </label> <input [(ngModel)]="transparency" [value]="transparency|number" type="text" /> <input type="button" (click)="updateShapeStyle('transparency', transparency)" value="Set" /> <label for='txtRotate'>Rotate: </label> <input type="number" min="0" max="360" [(ngModel)]="rotate" /> <input type="button" (click)="updateShapeStyle('rotate', rotate)" value="Set" /> </div> <div *ngIf="showShapeTxtPanel" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Text</label> <label>Text: </label> <textarea [(ngModel)]="text" type="text" placeholder="Shape text" rows="3"></textarea> <input type="button" (click)="updateShapeStyle('text', text)" value="Set" /> <label>Text Color: </label> <input [(ngModel)]="textColor" type="color" value="#FFFF00" /> <input type="button" (click)="updateShapeStyle('color', textColor)" value="Set" /> <label>Text Transparency: </label> <input [(ngModel)]="transparencyTxt" [value]="transparencyTxt|number" type="text" value="0.5" /> <input type="button" (click)="updateShapeStyle('transparencyTxt', transparencyTxt)" value="Set" /> <label>Text Font: </label> <input [(ngModel)]="font" type="text" /> <input type="button" (click)="updateShapeStyle('font', font)" value="Set" /> <label> Horizontal Align:</label> <select [(ngModel)]="hAlign"> <option *ngFor="let item of horizontalAlignList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateShapeStyle('hAlign', hAlign)" value="Set" /> <label>Vertical Align:</label> <select [(ngModel)]="vAlign"> <option *ngFor="let item of verticalAlignList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateShapeStyle('vAlign', vAlign)" value="Set" /> </div> <div *ngIf="showConnectorPropPanel" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Arrow Head</label> <label for="beginArrowheadStyle">Begin Arrowhead Style:</label> <select [(ngModel)]="beginStyle" class="shapeSelect"> <option *ngFor="let item of arrowHeadStyle" [value]="item.value|number"> {{item.name}} </option> </select> <input (click)="updateConnectorShapeStyle('beginStyle', beginStyle)" type="button" class='arrow-action-button' value="Set" /> <label>Begin Arrowhead Width:</label> <select [(ngModel)]="beginWidth" class="shapeSelect"> <option *ngFor="let item of arrowHeadWidthList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateConnectorShapeStyle('beginWidth', beginWidth)" class='arrow-action-button' value="Set" /> <label>Begin Arrowhead Length:</label> <select [(ngModel)]="beginLength" class="shapeSelect"> <option *ngFor="let item of arrowHeadLengthList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateConnectorShapeStyle('beginLength', beginLength)" class='arrow-action-button' value="Set" /> <label for="endArrowheadStyle">End Arrowhead Style:</label> <select [(ngModel)]="endStyle" class="shapeSelect"> <option *ngFor="let item of arrowHeadStyle" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateConnectorShapeStyle('endStyle', endStyle)" class='arrow-action-button' value="Set" /> <label>End Arrowhead Width:</label> <select [(ngModel)]="endWidth" class="shapeSelect"> <option *ngFor="let item of arrowHeadWidthList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateConnectorShapeStyle('endWidth', endWidth)" class='arrow-action-button' value="Set" /> <label>End Arrowhead Length:</label> <select [(ngModel)]="endLength" class="shapeSelect"> <option *ngFor="let item of arrowHeadLengthList" [value]="item.value|number"> {{item.name}} </option> </select> <input type="button" (click)="updateConnectorShapeStyle('endLength', endLength)" class='arrow-action-button' value="Set" /> </div> </div> </div>
.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-left: 5px; } .divide-line { width: 100%; height: 1px; background: #cbcbcb; margin-top: 10px; margin-bottom: 3px; } .title { text-align: center; font-weight: bold; } label { display: block; margin-top: 15px; margin-bottom: 5px; } p { padding: 2px 10px; background-color: lavender; } input { width: 160px; margin-left: 10px; display: inline; } input[type=button] { width: 50px; margin-left: 1px; } select { width: 160px; margin-left: 10px; display: inline; } textarea { width: 160px; margin-left: 10px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
(function (global) { System.config({ transpiler: 'ts', typescriptOptions: { tsconfig: true }, meta: { 'typescript': { "exports": "ts" }, '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'core-js': 'npm:core-js/client/shim.min.js', 'zone': 'npm:zone.js/dist/zone.min.js', 'rxjs': 'npm:rxjs/bundles/rxjs.umd.min.js', '@angular/core': 'npm:@angular/core/bundles/core.umd.min.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.min.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.min.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.min.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.min.js', '@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.min.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.min.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.min.js', 'jszip': 'npm:jszip/dist/jszip.min.js', 'typescript': 'npm:typescript/lib/typescript.js', 'ts': 'npm:plugin-typescript/lib/plugin.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', '@grapecity/spread-sheets': 'npm:@grapecity/spread-sheets/index.js', '@grapecity/spread-sheets-shapes': 'npm:@grapecity/spread-sheets-shapes/index.js', '@grapecity/spread-sheets-angular': 'npm:@grapecity/spread-sheets-angular/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'ts' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, } }); })(this);