To Image

SpreadJS allows you to export shapes, connector shapes or groups of shapes as images.

Description
app.jsx
app-func.jsx
app-class.jsx
index.html
styles.css
Copy to CodeMine

After getting the shape or shape base object, you can get the image source by using the shape.toImageSrc() API.

You can get the shape image sourc using the following code:

    let shapeImageSrc = sheet.shapes.all()[0].toImageSrc();
After getting the shape or shape base object, you can get the image source by using the shape.toImageSrc() API. You can get the shape image sourc using the following code:
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 "@mescius/spread-sheets-shapes" import { SpreadSheets } from '@mescius/spread-sheets-react'; import './styles.css'; export function AppFunc() { return (<div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => initSpread(spread)}> </SpreadSheets> </div> <div class="options-container"> <div class="option-row"> Try selecting a shape: </div> <div class="option-row"> <img id="selectShapeImage" src="" alt="Original Image"></img> </div> <div class="option-row"> <input type="button" id="downloadButton" value="Download Image"></input> </div> </div> </div>); } async function initSpread(spread) { spread.suspendPaint(); spread.setSheetCount(1); const loadingTip = addLoadingTip(); const res = await fetch('$DEMOROOT$/en/sample/features/shapes/shape-toImage/spread.json'); await spread.fromJSON(await res.json()); loadingTip.remove(); spread.resumePaint(); bindShapesEvent(spread); initPanel(spread); } function addLoadingTip() { const div = document.createElement('div'); div.style.position = 'absolute'; div.style.inset = '0'; div.style.display = 'flex'; div.style.alignItems = 'center'; div.style.justifyContent = 'center'; div.style.background = 'white'; div.style.zIndex = '100'; div.textContent = 'Loading data from server ...'; document.body.appendChild(div); return div; } function bindShapesEvent(spread) { let shapeName; spread.bind(GC.Spread.Sheets.Events.ShapeSelectionChanged, function (event, args) { let shape = args.shape; if (!shape) { return; } let url = shape.toImageSrc(); shapeName = shape.name(); let imgElement = document.getElementById("selectShapeImage"); let img = new Image(); img.onload = function () { if (img.width <= 250 && img.height <= 250) { imgElement.style.width = "auto"; imgElement.style.height = "auto"; imgElement.src = url; } else { if (img.width > img.height) { imgElement.style.width = "250px"; imgElement.style.height = "auto"; } else { imgElement.style.width = "auto"; imgElement.style.height = "250px"; } imgElement.src = url; } }; img.src = url; }); document.getElementById("downloadButton").addEventListener("click", function () { let imgElement = document.getElementById("selectShapeImage"); let imgUrl = imgElement.src; let a = document.createElement("a"); a.href = imgUrl; a.download = shapeName + ".png"; document.body.appendChild(a); a.click(); document.body.removeChild(a); }); } function initPanel(spread) { let sheet = spread.getActiveSheet(); let shape = sheet.shapes.all()[0]; shape.isSelected(true); let url = shape.toImageSrc(); let imgElement = document.getElementById("selectShapeImage"); imgElement.src = url; }
import * as React from 'react'; import GC from '@mescius/spread-sheets'; import "@mescius/spread-sheets-shapes" import { SpreadSheets } from '@mescius/spread-sheets-react'; import './styles.css'; const Component = React.Component; export class App extends Component { constructor(props) { super(props); this.spread = null; this.autoGenerateColumns = false; } render() { return (<div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => initSpread(spread)}> </SpreadSheets> </div> <div class="options-container"> <div class="option-row"> Try selecting a shape: </div> <div class="option-row"> <img id="selectShapeImage" src="" alt="Original Image"></img> </div> <div class="option-row"> <input type="button" id="downloadButton" value="Download Image"></input> </div> </div> </div>); } } async function initSpread(spread) { spread.suspendPaint(); spread.setSheetCount(1); const loadingTip = addLoadingTip(); const res = await fetch('$DEMOROOT$/en/sample/features/shapes/shape-toImage/spread.json'); await spread.fromJSON(await res.json()); loadingTip.remove(); spread.resumePaint(); bindShapesEvent(spread); initPanel(spread); } function addLoadingTip() { const div = document.createElement('div'); div.style.position = 'absolute'; div.style.inset = '0'; div.style.display = 'flex'; div.style.alignItems = 'center'; div.style.justifyContent = 'center'; div.style.background = 'white'; div.style.zIndex = '100'; div.textContent = 'Loading data from server ...'; document.body.appendChild(div); return div; } function bindShapesEvent(spread) { let shapeName; spread.bind(GC.Spread.Sheets.Events.ShapeSelectionChanged, function (event, args) { let shape = args.shape; if (!shape) { return; } let url = shape.toImageSrc(); shapeName = shape.name(); let imgElement = document.getElementById("selectShapeImage"); let img = new Image(); img.onload = function () { if (img.width <= 250 && img.height <= 250) { imgElement.style.width = "auto"; imgElement.style.height = "auto"; imgElement.src = url; } else { if (img.width > img.height) { imgElement.style.width = "250px"; imgElement.style.height = "auto"; } else { imgElement.style.width = "auto"; imgElement.style.height = "250px"; } imgElement.src = url; } }; img.src = url; }); document.getElementById("downloadButton").addEventListener("click", function () { let imgElement = document.getElementById("selectShapeImage"); let imgUrl = imgElement.src; let a = document.createElement("a"); a.href = imgUrl; a.download = shapeName + ".png"; document.body.appendChild(a); a.click(); document.body.removeChild(a); }); } function initPanel(spread) { let sheet = spread.getActiveSheet(); let shape = sheet.shapes.all()[0]; shape.isSelected(true); let url = shape.toImageSrc(); let imgElement = document.getElementById("selectShapeImage"); imgElement.src = url; }
<!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% - 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: #F4F8EB; } input { width: 160px; margin-left: 10px; display: inline; } 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; } #app{ width: 100%; 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-react': 'npm:@mescius/spread-sheets-react/index.js', '@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/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);