You can customize the font style using code such as the following:
You can customize the font by splitted font properties using code such as the following:
You can also set text decorations, such as underline, doubleUnderline, lineThrough, and overline
You can set the font alignment like this:
You can set the font color and cell background color like this:
You can set the font format, such as wordwrap, indent, shrinkToFit, etc.
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import './styles.css';
import { AppFunc } from './app-func';
// import { App } from './app-class';
// 1. Functional Component sample
createRoot(document.getElementById('app')).render(<AppFunc />);
// 2. Class Component sample
// createRoot(document.getElementById('app')).render(<App />);
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets } from '@mescius/spread-sheets-react';
function fontStyleOrFontWeightCssValueToSpreadValue(value) {
// transfer css font style/weight to spread font style/weight
if (value === '') {
return undefined;
} else if (value === 'normal') {
return null;
} else {
return value;
}
}
function fontStyleOrFontWeightSpreadValueToCssValue(value) {
// transfer spread font style/weight to css font style/weight
if (value === undefined) {
return '';
} else if (value === null) {
return 'normal';
} else {
return value;
}
}
function setFontToSelectedRanges(spread, setFunc) {
let activeSheet = spread.getActiveSheet();
let selections = activeSheet.getSelections();
if (!selections) {
return;
}
activeSheet.suspendPaint();
for (let i = 0; i < selections.length; i++) {
let selection = selections[i];
if (!selection) {
return;
}
for (let r = 0; r < selection.rowCount; r++) {
for (let c = 0; c < selection.colCount; c++) {
let cell = activeSheet.getCell(r + selection.row, c + selection.col);
setFunc(cell);
}
}
}
activeSheet.resumePaint();
}
function setStyles(sheet) {
sheet.suspendPaint();
//Font
sheet.getCell(2, 0).font('italic normal 12px Mangal');
sheet.getCell(4, 0).font('normal bold 15px Arial Black');
sheet.getCell(6, 0).font('normal normal 18px Georgia');
//FontFamily
sheet.getCell(2, 1).fontFamily('Mangal');
sheet.getCell(4, 1).fontFamily('Arial Black');
sheet.getCell(6, 1).fontFamily('Georgia');
//FontSize
sheet.getCell(2, 2).fontSize('12px');
sheet.getCell(4, 2).fontSize('20px');
sheet.getCell(6, 2).fontSize('28px');
//Bold
sheet.getCell(2, 3).fontWeight('bold');
sheet.getCell(4, 3).fontWeight('normal');
//Italic
sheet.getCell(2, 4).fontStyle('italic');
sheet.getCell(4, 4).fontStyle('normal');
//Line
sheet.getCell(2, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.underline);
sheet.getCell(4, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.doubleUnderline);
sheet.getCell(6, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.lineThrough);
sheet.getCell(8, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.overline);
//Align
sheet.getCell(2, 6).vAlign(GC.Spread.Sheets.VerticalAlign.top).hAlign(GC.Spread.Sheets.HorizontalAlign.left);
sheet.getCell(4, 6).vAlign(GC.Spread.Sheets.VerticalAlign.center).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
sheet.getCell(6, 6).vAlign(GC.Spread.Sheets.VerticalAlign.bottom).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
//Forecolor
sheet.getCell(2, 7).foreColor('#F7A711');
sheet.getCell(4, 7).foreColor('#82BC00');
sheet.getCell(6, 7).foreColor('#C3C3C3');
//backgroundColor
sheet.getCell(2, 8).backColor('#F7A711');
sheet.getCell(4, 8).backColor('#82BC00');
sheet.getCell(6, 8).backColor('#C3C3C3');
//WordWrap
sheet.getCell(2, 9).wordWrap(true);
sheet.getCell(3, 9).wordWrap(true);
//Indent
sheet.getCell(2, 10).textIndent(1);
sheet.getCell(4, 10).textIndent(3);
sheet.getCell(6, 10).textIndent(-2);
//ShrinkToFit
sheet.getCell(2, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.center);
sheet.getCell(4, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
sheet.resumePaint();
}
let spread = null;
export function AppFunc() {
const [fontOption, setFontOption] = React.useState({
fontStyle: 'undefined',
fontWeight: 'undefined',
fontSize: '',
fontSizeUnit: 'px',
fontFamily: '',
font: ''
});
const initSpread = (currSpread) => {
spread = currSpread;
let sheet = spread.getActiveSheet();
sheet.fromJSON(jsonData);
setStyles(sheet);
setSettings();
bindEvents();
}
const bindEvents = () => {
spread.bind(GC.Spread.Sheets.Events.SelectionChanged, () => {
setSettings();
});
}
const setSettings = () => {
let sheet = spread.getActiveSheet();
let state = { ...fontOption };
let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
state.font = activeCell.font();
state.fontStyle = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontStyle());
state.fontWeight = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontWeight());
let fontSize = activeCell.fontSize();
if (fontSize) {
let fontSizeDigit = parseFloat(fontSize);
let fontSizeUnit = fontSize.substring(('' + fontSizeDigit).length);
state.fontSize = '' + fontSizeDigit;
state.fontSizeUnit = fontSizeUnit;
} else {
state.fontSize = '';
state.fontSizeUnit = 'px';
}
state.fontFamily = activeCell.fontFamily() || '';
setFontOption(state);
}
const setFontByFont = () => {
let font = fontOption.font;
setFontToSelectedRanges(spread, (cell) => {
cell.font(font);
});
setSettings();
}
const setFontBySplittedFont = () => {
let fontStyle = fontOption.fontStyle,
fontWeight = fontOption.fontWeight,
fontFamily = fontOption.fontFamily,
fontSizeDigit = fontOption.fontSize,
fontSizeUnit = fontOption.fontSizeUnit, fontSize;
if (fontSizeDigit) {
fontSize = fontSizeDigit + (fontSizeUnit ? fontSizeUnit : 'px');
}
setFontToSelectedRanges(spread, (cell) => {
cell.fontStyle(fontStyleOrFontWeightCssValueToSpreadValue(fontStyle));
cell.fontWeight(fontStyleOrFontWeightCssValueToSpreadValue(fontWeight));
cell.fontSize(fontSize);
cell.fontFamily(fontFamily);
});
setSettings();
}
const setProperty = (e, property) => {
let value = e.target.value;
let arg = {};
arg[property] = value;
setFontOption({ ...fontOption, ...arg });
}
return (
<div className="sample-tutorial">
<div className="sample-spreadsheets">
<SpreadSheets workbookInitialized={initSpread} />
</div>
<div className="options-container">
<p>Select a cell, Set font for it.</p>
<div className="settings-row">
<label htmlFor="fontStyle">Font Style:</label>
<input type="text" onChange={(e) => { setProperty(e, 'fontStyle') }} id="fontStyle" value={fontOption.fontStyle} />
</div>
<div className="settings-row">
<label htmlFor="fontWeight">Font Weight:</label>
<input type="text" onChange={(e) => { setProperty(e, 'fontWeight') }} id="fontWeight" value={fontOption.fontWeight} />
</div>
<div className="settings-row">
<label htmlFor="fontSize">Font Size:</label>
<input id="fontSize" type="number" min="0" onChange={(e) => { setProperty(e, 'fontSize') }} value={fontOption.fontSize} />
<select id="fontSizeUnit" onChange={(e) => { setProperty(e, 'fontSizeUnit') }} value={fontOption.fontSizeUnit}>
<option value="px">px</option>
<option value="pt">pt</option>
</select>
</div>
<div className="settings-row">
<label htmlFor="fontFamily">Font Family:</label>
<input id="fontFamily" type="text" onChange={(e) => { setProperty(e, 'fontFamily') }} value={fontOption.fontFamily} />
</div>
<div className="settings-row">
<button id="set-splitted-font" className="settings-btn"
onClick={setFontBySplittedFont}>Set Font Using Font Properties
</button>
</div>
<br />
<div className="settings-row">
<label htmlFor="font">Font:</label>
<input type="text" id="font" onChange={(e) => { setProperty(e, 'font') }} value={fontOption.font} />
</div>
<div className="settings-row">
<button id="set-font" className="settings-btn" onClick={setFontByFont}>Set Font By Font
</button>
</div>
</div>
</div>
);
}
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets } from '@mescius/spread-sheets-react';
const Component = React.Component;
function fontStyleOrFontWeightCssValueToSpreadValue (value) {
// transfer css font style/weight to spread font style/weight
if (value === '') {
return undefined;
} else if (value === 'normal') {
return null;
} else {
return value;
}
}
function fontStyleOrFontWeightSpreadValueToCssValue (value) {
// transfer spread font style/weight to css font style/weight
if (value === undefined) {
return '';
} else if (value === null) {
return 'normal';
} else {
return value;
}
}
function setFontToSelectedRanges (spread, setFunc) {
let activeSheet = spread.getActiveSheet();
let selections = activeSheet.getSelections();
if (!selections) {
return;
}
activeSheet.suspendPaint();
for (let i = 0; i < selections.length; i++) {
let selection = selections[i];
if (!selection) {
return;
}
for (let r = 0; r < selection.rowCount; r++) {
for (let c = 0; c < selection.colCount; c++) {
let cell = activeSheet.getCell(r + selection.row, c + selection.col);
setFunc(cell);
}
}
}
activeSheet.resumePaint();
}
function setStyles (sheet) {
sheet.suspendPaint();
//Font
sheet.getCell(2, 0).font('italic normal 12px Mangal');
sheet.getCell(4, 0).font('normal bold 15px Arial Black');
sheet.getCell(6, 0).font('normal normal 18px Georgia');
//FontFamily
sheet.getCell(2, 1).fontFamily('Mangal');
sheet.getCell(4, 1).fontFamily('Arial Black');
sheet.getCell(6, 1).fontFamily('Georgia');
//FontSize
sheet.getCell(2, 2).fontSize('12px');
sheet.getCell(4, 2).fontSize('20px');
sheet.getCell(6, 2).fontSize('28px');
//Bold
sheet.getCell(2, 3).fontWeight('bold');
sheet.getCell(4, 3).fontWeight('normal');
//Italic
sheet.getCell(2, 4).fontStyle('italic');
sheet.getCell(4, 4).fontStyle('normal');
//Line
sheet.getCell(2, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.underline);
sheet.getCell(4, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.doubleUnderline);
sheet.getCell(6, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.lineThrough);
sheet.getCell(8, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.overline);
//Align
sheet.getCell(2, 6).vAlign(GC.Spread.Sheets.VerticalAlign.top).hAlign(GC.Spread.Sheets.HorizontalAlign.left);
sheet.getCell(4, 6).vAlign(GC.Spread.Sheets.VerticalAlign.center).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
sheet.getCell(6, 6).vAlign(GC.Spread.Sheets.VerticalAlign.bottom).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
//Forecolor
sheet.getCell(2, 7).foreColor('#F7A711');
sheet.getCell(4, 7).foreColor('#82BC00');
sheet.getCell(6, 7).foreColor('#C3C3C3');
//backgroundColor
sheet.getCell(2, 8).backColor('#F7A711');
sheet.getCell(4, 8).backColor('#82BC00');
sheet.getCell(6, 8).backColor('#C3C3C3');
//WordWrap
sheet.getCell(2, 9).wordWrap(true);
sheet.getCell(3, 9).wordWrap(true);
//Indent
sheet.getCell(2, 10).textIndent(1);
sheet.getCell(4, 10).textIndent(3);
sheet.getCell(6, 10).textIndent(-2);
//ShrinkToFit
sheet.getCell(2, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.center);
sheet.getCell(4, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
sheet.resumePaint();
}
export class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.state = {
fontStyle: 'undefined',
fontWeight: 'undefined',
fontSize: '',
fontSizeUnit: 'px',
fontFamily: '',
font: ''
}
}
render() {
return (
<div className="sample-tutorial">
<div className="sample-spreadsheets">
<SpreadSheets workbookInitialized={ this.initSpread }/>
</div>
<div className="options-container">
<p>Select a cell, Set font for it.</p>
<div className="settings-row">
<label htmlFor="fontStyle">Font Style:</label>
<input type="text" onChange={(e) => { this.setProperty(e, 'fontStyle') }} id="fontStyle" value={ this.state.fontStyle }/>
</div>
<div className="settings-row">
<label htmlFor="fontWeight">Font Weight:</label>
<input type="text" onChange={(e) => { this.setProperty(e, 'fontWeight') }} id="fontWeight" value={ this.state.fontWeight }/>
</div>
<div className="settings-row">
<label htmlFor="fontSize">Font Size:</label>
<input id="fontSize" type="number" min="0" onChange={(e) => { this.setProperty(e, 'fontSize') }} value={ this.state.fontSize }/>
<select id="fontSizeUnit" onChange={(e) => { this.setProperty(e, 'fontSizeUnit') }} value={ this.state.fontSizeUnit }>
<option value="px">px</option>
<option value="pt">pt</option>
</select>
</div>
<div className="settings-row">
<label htmlFor="fontFamily">Font Family:</label>
<input id="fontFamily" type="text" onChange={(e) => { this.setProperty(e, 'fontFamily') }} value={ this.state.fontFamily }/>
</div>
<div className="settings-row">
<button id="set-splitted-font" className="settings-btn"
onClick={ this.setFontBySplittedFont }>Set Font Using Font Properties
</button>
</div>
<br/>
<div className="settings-row">
<label htmlFor="font">Font:</label>
<input type="text" id="font" onChange={(e) => { this.setProperty(e, 'font') }} value={ this.state.font }/>
</div>
<div className="settings-row">
<button id="set-font" className="settings-btn" onClick={ this.setFontByFont }>Set Font By Font
</button>
</div>
</div>
</div>
);
}
initSpread = (spread) => {
this.spread = spread;
let sheet = spread.getActiveSheet();
sheet.fromJSON(jsonData);
setStyles(sheet);
this.setSettings(spread);
this.bindEvents(spread);
}
bindEvents = (spread) => {
let self = this;
spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () {
self.setSettings(spread);
});
}
setSettings = (spread)=> {
let sheet = spread.getActiveSheet();
let state = this.state;
let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
state.font = activeCell.font();
state.fontStyle = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontStyle());
state.fontWeight = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontWeight());
let fontSize = activeCell.fontSize();
if (fontSize) {
let fontSizeDigit = parseFloat(fontSize);
let fontSizeUnit = fontSize.substring((''+ fontSizeDigit).length);
state.fontSize = '' + fontSizeDigit;
state.fontSizeUnit = fontSizeUnit;
} else {
state.fontSize = '';
state.fontSizeUnit = 'px';
}
state.fontFamily = activeCell.fontFamily() || '';
this.setState(state);
}
setFontByFont = () => {
let self = this, spread = self.spread, state = self.state;
let font = state.font;
setFontToSelectedRanges(spread, (cell) => {
cell.font(font);
});
self.setSettings(spread);
}
setFontBySplittedFont = () => {
let self = this, spread = self.spread, state = self.state;
let fontStyle = state.fontStyle,
fontWeight = state.fontWeight,
fontFamily = state.fontFamily,
fontSizeDigit = state.fontSize,
fontSizeUnit = state.fontSizeUnit, fontSize;
if (fontSizeDigit) {
fontSize = fontSizeDigit + (fontSizeUnit ? fontSizeUnit : 'px');
}
setFontToSelectedRanges(spread, (cell) => {
cell.fontStyle(fontStyleOrFontWeightCssValueToSpreadValue(fontStyle));
cell.fontWeight(fontStyleOrFontWeightCssValueToSpreadValue(fontWeight));
cell.fontSize(fontSize);
cell.fontFamily(fontFamily);
});
self.setSettings(spread);
}
setProperty(e, property) {
let value = e.target.value;
let arg = {};
arg[property] = value;
this.setState(arg);
}
}
<!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="data.js" type="text/javascript"></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" style="height: 100%;"></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;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
label {
display: inline-block;
width: 80px;
}
.settings-row {
width: 100%;
height: 30px;
font-size: 13px;
}
.settings-btn {
display: inline-block;
width: 220px;
height: 21px;
}
#fontSize {
width: 80px;
}
.settings-row input {
width: 160px;
}
(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',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js',
'react': 'npm:react/cjs/react.production.js',
'react-dom': 'npm:react-dom/cjs/react-dom.production.js',
'react-dom/client': 'npm:react-dom/cjs/react-dom-client.production.js',
'scheduler': 'npm:scheduler/cjs/scheduler.production.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);