The following sample shows how to make a shape a Text Box.
Every autoShape can be changed to a text box.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import './styles.css';
import { AppFunc } from './app-func';
// 1. Functional Component sample
ReactDOM.render(<AppFunc />, document.getElementById('app'));
import React, { useState } from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets } from '@mescius/spread-sheets-react';
import "@mescius/spread-sheets-shapes";
const shapeTips = "Text Box With resizeToFitText\nPlease try typing some text here.";
const optionsTips = "Please select a shape and try to modify the options.";
function setTextBox(shape) {
let shapeStyle = shape.style();
shapeStyle.fill.color = 'white';
shapeStyle.textEffect.color = 'black';
shapeStyle.line.color = 'black';
shape.style(shapeStyle);
shape.isTextBox(true);
}
function addShape(sheet) {
let textBox = sheet.shapes.add('', GC.Spread.Sheets.Shapes.AutoShapeType.rectangle, 100, 50);
setTextBox(textBox);
textBox.text("Text Box");
let textBox2 = sheet.shapes.add('', GC.Spread.Sheets.Shapes.AutoShapeType.rectangle, 350, 50, 250);
textBox2.text(shapeTips);
setTextBox(textBox2);
let style = textBox2.style();
style.textFrame.resizeToFitText = true;
textBox2.style(style);
}
export function AppFunc() {
const [isTextBox, setIsTextBox] = useState(false);
const [resizeToFitText, setresizeToFitText] = useState(false);
const [isShapeSelected, setIsShapeSelected] = useState(false);
const [spread, setSpread] = useState(null);
const handleIsTextBoxChange = (event) => {
const sheet = spread.getActiveSheet();
let shapes = sheet.shapes.all();
shapes.filter((shape) => shape.isSelected()).forEach((shape) => {
shape.isTextBox(event.target.checked);
});
setIsTextBox(event.target.checked);
};
const handeresizeToFitTextChange = (event) => {
const sheet = spread.getActiveSheet();
let shapes = sheet.shapes.all();
shapes.filter((shape) => shape.isSelected()).forEach((shape) => {
let shapeStyle = shape.style();
shapeStyle.textFrame.resizeToFitText = event.target.checked;
shape.style(shapeStyle);
});
setresizeToFitText(event.target.checked);
}
const initSpread = (spread) => {
setSpread(spread);
const sheet = spread.getActiveSheet();
addShape(sheet);
sheet.bind(GC.Spread.Sheets.Events.ShapeSelectionChanged, undefined, (type, data) => {
let shape = data.shape;
if (shape && shape.isSelected()) {
setIsTextBox(shape.isTextBox());
setresizeToFitText(shape.style().textFrame.resizeToFitText);
setIsShapeSelected(true);
} else {
setIsShapeSelected(false);
}
});
}
return (
<div class="sample-tutorial">
<div id="ss" class="sample-spreadsheets">
<SpreadSheets workbookInitialized={initSpread}>
</SpreadSheets>
</div>
<div className="options-container">
<div className="options-title">Options</div>
<div className="options-tips">{optionsTips}</div>
<input type="checkbox" id="isTextBoxOption" checked={isTextBox} onChange={handleIsTextBoxChange} disabled={!isShapeSelected}/>
<label for="isTextBoxOption">isTextBox</label>
<br/>
<input type="checkbox" id="resizeToFitTextOption" checked={resizeToFitText} onChange={handeresizeToFitTextChange} disabled={!isShapeSelected}/>
<label for="resizeToFitTextOption">resizeToFitText</label>
</div>
</div>
);
}
<!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>
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#app {
width: 100%;
height: 100%;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 250px);
height: 100%;
/* overflow: hidden; */
}
.options-container {
position: absolute;
top: 0px;
right: 0px;
width: 250px;
height: 100%;
padding: 12px;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
user-select: none;
}
.options-title {
font-size: larger;
font-weight: bolder;
margin-bottom: 10px;
}
.options-tips {
margin-bottom: 10px;
}
(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);