Check Box

The CheckBox CellType represents a check box cell. This can be useful when you want to have some sort of form in your page that users can fill out, and it makes getting what they have selected easy to do.

To create a check box cell, follow this example: The CheckBox can support a three-state check box. You can use the isThreeState method to get and set whether the check box supports three states. For example: The three states are true, false, or indeterminate. Every state has its own text; you can use the textTrue, textFalse, and textIndeterminate methods to get and set these states' text. For example: You can use the caption method to get and set the caption of the check box cell. Use the textAlign method to get and set the text alignment relative to the check box. The setting is a CheckBoxTextAlign enumeration value. top: Text is on top of the check box. bottom: Text is below the check box. left: Text is to the left of the check box. right: Text is to the right of the check box. You can use the boxSize method to get and set checkbox size. You can set number or "auto" to cellType. When the cell style "wordWrap” is set to true and the cell width is not enough for the text, the text will display wrapped.
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, Worksheet } from '@mescius/spread-sheets-react'; const spreadNS = GC.Spread.Sheets; let spread = null; export function AppFunc() { const [checkboxCellTypeOption, setCheckboxCellTypeOption] = React.useState({ hasCaption: null, disabled: false, caption: '', textTrue: 'textTrue', textIndeterminate: 'textIndeterminate', textFalse: 'textFalse', textAlign: 'top', isThreeState: true, boxSize: 12, }); const initSpread = (currSpread) => { spread = currSpread; const sheet = spread.getSheet(0); sheet.bind(spreadNS.Events.SelectionChanged, (e) => { propertyChange(e); }); sheet.suspendPaint(); sheet.setColumnWidth(1, 120); sheet.setColumnWidth(2, 130); sheet.setRowHeight(1, 35); sheet.setValue(1, 1, "caption"); sheet.getCell(1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("green"); const captionCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); captionCellType.caption("Caption"); sheet.setCellType(1, 2, captionCellType); sheet.getCell(1, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.setRowHeight(3, 35); sheet.setValue(3, 1, "threeState"); sheet.getCell(3, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("red"); const threeStateCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); threeStateCellType.isThreeState(false); threeStateCellType.textTrue("Checked!"); threeStateCellType.textFalse("Check Me!"); sheet.setCellType(3, 2, threeStateCellType); sheet.getCell(3, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.setRowHeight(5, 35); sheet.setValue(5, 1, "textAlign"); sheet.getCell(5, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("blue"); const textAlignCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); textAlignCellType.isThreeState(false); textAlignCellType.caption("textAlign"); textAlignCellType.textAlign(GC.Spread.Sheets.CellTypes.CheckBoxTextAlign.bottom); sheet.setCellType(5, 2, textAlignCellType); sheet.getCell(5, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.setRowHeight(7, 35); sheet.setValue(7, 1, "text wrap"); sheet.getCell(7, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("orange"); const textWrapCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); textWrapCellType.caption("This is a long long text"); sheet.setCellType(7, 2, textWrapCellType); sheet.getCell(7, 2).wordWrap(true); sheet.resumePaint(); } const propertyChange = (e, settings) => { const sheet = spread.getActiveSheet(); const sels = sheet.getSelections(); if (sels && sels.length > 0) { const sel = getActualRange(sels[0], sheet.getRowCount(), sheet.getColumnCount()); const checkboxCellType = sheet.getCellType(sel.row, sel.col); if (!(checkboxCellType instanceof spreadNS.CellTypes.CheckBox)) { setCheckboxCellTypeOption({ ...checkboxCellTypeOption, disabled: true }); return; } if (!settings) { setCheckboxCellTypeOption({ disabled: false, hasCaption: checkboxCellType.caption() ? true : false, caption: checkboxCellType.caption(), textTrue: checkboxCellType.textTrue(), textIndeterminate: checkboxCellType.textIndeterminate(), textFalse: checkboxCellType.textFalse(), textAlign: checkboxCellType.textAlign(), isThreeState: checkboxCellType.isThreeState(), boxSize: checkboxCellType.boxSize(), }); } else { checkboxCellType.caption(settings.caption); checkboxCellType.textTrue(settings.textTrue); checkboxCellType.textIndeterminate(settings.textIndeterminate); checkboxCellType.textFalse(settings.textFalse); checkboxCellType.textAlign(Number(settings.textAlign)); checkboxCellType.isThreeState(settings.isThreeState); var boxSizeValue = settings.boxSize; var boxSize = Number(boxSizeValue); if (isNaN(boxSize)) { checkboxCellType.boxSize(boxSizeValue); } else { checkboxCellType.boxSize(boxSize); } } } sheet.repaint(); } const getActualRange = (range, maxRowCount, maxColCount) => { const row = range.row < 0 ? 0 : range.row; const col = range.col < 0 ? 0 : range.col; const rowCount = range.rowCount < 0 ? maxRowCount : range.rowCount; const colCount = range.colCount < 0 ? maxColCount : range.colCount; return new spreadNS.Range(row, col, rowCount, colCount); } return (<div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={initSpread}> <Worksheet /> </SpreadSheets> </div> <Panel propertyChange={propertyChange} setting={checkboxCellTypeOption} /> </div>); } function Panel(props) { const [setting, setSetting] = React.useState(props.setting); React.useEffect(() => { setSetting(props.setting); }, [props.setting]); return ( <div class="options-container"> <label>Select the check box cell in Spread and edit its options with these text boxes.</label> <div class="option-row" style={{display: setting.hasCaption === null || setting.hasCaption ? 'block' : 'none'}}> <label for="caption">caption:</label> <input id="caption" type="text" value={setting.caption} onChange={(e) => { setSetting({ ...setting, caption: e.target.value }); }} /> </div> <div class="option-row" style={{display: setting.hasCaption === null || !setting.hasCaption ? 'block' : 'none'}}> <label for="textTrue">textTrue:</label> <input id="textTrue" type="text" value={setting.textTrue} onChange={(e) => { setSetting({ ...setting, textTrue: e.target.value }); }} /> </div> <div class="option-row" style={{display: setting.hasCaption === null || !setting.hasCaption ? 'block' : 'none'}}> <label for="textIndeterminate">textIndeterminate(for 3-state option):</label> <input id="textIndeterminate" type="text" value={setting.textIndeterminate} onChange={(e) => { setSetting({ ...setting, textIndeterminate: e.target.value }); }} /> </div> <div class="option-row" style={{display: setting.hasCaption === null || !setting.hasCaption ? 'block' : 'none'}}> <label for="textFalse">textFalse:</label> <input id="textFalse" type="text" value={setting.textFalse} onChange={(e) => { setSetting({ ...setting, textFalse: e.target.value }); }} /> </div> <div class="option-row" style={{display: setting.hasCaption === null || !setting.hasCaption ? 'block' : 'none'}}> <label>boxSize</label> <input id="boxSize" type="text" min="1" onChange={(e) => { setSetting({ ...setting, boxSize: e.target.value }) }}></input> </div> <div class="option-row"> <label>textAlign:</label> <select id="textAlign" value={setting.textAlign} onChange={(e) => { setSetting({ ...setting, textAlign: e.target.value }); }}> <option value="0">top</option> <option value="1">bottom</option> <option value="2">left</option> <option value="3">right</option> </select> </div> <div class="option-row" style={{display: setting.hasCaption === null || !setting.hasCaption ? 'block' : 'none'}}> <input id="isThreeState" type="checkbox" checked={setting.isThreeState} onChange={(e) => { setSetting({ ...setting, isThreeState: e.target.checked }); }} /> <label for="isThreeState">isThreeState:</label> </div> <div class="option-row"> <input type="button" id="setProperty" value="Update" disabled={setting.disabled} onClick={(e) => {props.propertyChange(e, setting)} } /> </div> </div> ); }
import * as React from 'react'; import GC from '@mescius/spread-sheets'; import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react'; const spreadNS = GC.Spread.Sheets; const Component = React.Component; export class App extends Component { constructor(props) { super(props); this.spread = null; this.state = { hasCaption: null, disabled: false, caption: '', textTrue: 'textTrue', textIndeterminate: 'textIndeterminate', textFalse: 'textFalse', textAlign: 'top', isThreeState: true, boxSize: 12, } } render() { return (<div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => this.initSpread(spread)}> <Worksheet /> </SpreadSheets> </div> <Panel propertyChange={(e, settings) => {this.propertyChange(e, settings)}} setting={this.state} /> </div>); } initSpread(spread) { this.spread = spread; const sheet = this.spread.getSheet(0); sheet.bind(spreadNS.Events.SelectionChanged, (e) => { this.propertyChange(e); }); sheet.suspendPaint(); sheet.setColumnWidth(1, 120); sheet.setColumnWidth(2, 130); sheet.setRowHeight(1, 35); sheet.setValue(1, 1, "caption"); sheet.getCell(1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("green"); const captionCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); captionCellType.caption("Caption"); sheet.setCellType(1, 2, captionCellType); sheet.getCell(1, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.setRowHeight(3, 35); sheet.setValue(3, 1, "threeState"); sheet.getCell(3, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("red"); const threeStateCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); threeStateCellType.isThreeState(false); threeStateCellType.textTrue("Checked!"); threeStateCellType.textFalse("Check Me!"); sheet.setCellType(3, 2, threeStateCellType); sheet.getCell(3, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.setRowHeight(5, 35); sheet.setValue(5, 1, "textAlign"); sheet.getCell(5, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("blue"); const textAlignCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); textAlignCellType.isThreeState(false); textAlignCellType.caption("textAlign"); textAlignCellType.textAlign(GC.Spread.Sheets.CellTypes.CheckBoxTextAlign.bottom); sheet.setCellType(5, 2, textAlignCellType); sheet.getCell(5, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.setRowHeight(7, 35); sheet.setValue(7, 1, "text wrap"); sheet.getCell(7, 1).hAlign(GC.Spread.Sheets.HorizontalAlign.center).vAlign(GC.Spread.Sheets.VerticalAlign.center).foreColor("orange"); const textWrapCellType = new GC.Spread.Sheets.CellTypes.CheckBox(); textWrapCellType.caption("This is a long long text"); sheet.setCellType(7, 2, textWrapCellType); sheet.getCell(7, 2).wordWrap(true); sheet.resumePaint(); } propertyChange(e, settings) { const sheet = this.spread.getActiveSheet(); const sels = sheet.getSelections(); if (sels && sels.length > 0) { const sel = this.getActualRange(sels[0], sheet.getRowCount(), sheet.getColumnCount()); const checkboxCellType = sheet.getCellType(sel.row, sel.col); if (!(checkboxCellType instanceof spreadNS.CellTypes.CheckBox)) { this.setState({ disabled: true }); return; } if (!settings) { this.setState({ disabled: false, hasCaption: checkboxCellType.caption() ? true : false, caption: checkboxCellType.caption(), textTrue: checkboxCellType.textTrue(), textIndeterminate: checkboxCellType.textIndeterminate(), textFalse: checkboxCellType.textFalse(), textAlign: checkboxCellType.textAlign(), isThreeState: checkboxCellType.isThreeState(), boxSize: checkboxCellType.boxSize(), }); } else { checkboxCellType.caption(settings.caption); checkboxCellType.textTrue(settings.textTrue); checkboxCellType.textIndeterminate(settings.textIndeterminate); checkboxCellType.textFalse(settings.textFalse); checkboxCellType.textAlign(Number(settings.textAlign)); checkboxCellType.isThreeState(settings.isThreeState); var boxSizeValue = settings.boxSize; var boxSize = Number(boxSizeValue); if (isNaN(boxSize)) { checkboxCellType.boxSize(boxSizeValue); } else { checkboxCellType.boxSize(boxSize); } } } sheet.repaint(); } getActualRange(range, maxRowCount, maxColCount) { const row = range.row < 0 ? 0 : range.row; const col = range.col < 0 ? 0 : range.col; const rowCount = range.rowCount < 0 ? maxRowCount : range.rowCount; const colCount = range.colCount < 0 ? maxColCount : range.colCount; return new spreadNS.Range(row, col, rowCount, colCount); } } class Panel extends Component { constructor(props) { super(props); this.state = this.props.setting; } componentWillReceiveProps(nextProps) { this.setState(nextProps.setting); } render() { const { hasCaption } = this.state; return ( <div class="options-container"> <label>Select the check box cell in Spread and edit its options with these text boxes.</label> <div class="option-row" style={{display: hasCaption === null || hasCaption ? 'block' : 'none'}}> <label for="caption">caption:</label> <input id="caption" type="text" value={this.state.caption} onChange={(e) => { this.setState({ caption: e.target.value }); }} /> </div> <div class="option-row" style={{display: hasCaption === null || !hasCaption ? 'block' : 'none'}}> <label for="textTrue">textTrue:</label> <input id="textTrue" type="text" value={this.state.textTrue} onChange={(e) => { this.setState({ textTrue: e.target.value }); }} /> </div> <div class="option-row" style={{display: hasCaption === null || !hasCaption ? 'block' : 'none'}}> <label for="textIndeterminate">textIndeterminate(for 3-state option):</label> <input id="textIndeterminate" type="text" value={this.state.textIndeterminate} onChange={(e) => { this.setState({ textIndeterminate: e.target.value }); }} /> </div> <div class="option-row" style={{display: hasCaption === null || !hasCaption ? 'block' : 'none'}}> <label for="textFalse">textFalse:</label> <input id="textFalse" type="text" value={this.state.textFalse} onChange={(e) => { this.setState({ textFalse: e.target.value }); }} /> </div> <div class="option-row" style={{display: hasCaption === null || !hasCaption ? 'block' : 'none'}}> <label>boxSize</label> <input id="boxSize" type="text" min="1" onChange={(e) => { this.setState({ boxSize: e.target.value }) }}></input> </div> <div class="option-row"> <label>textAlign:</label> <select id="textAlign" value={this.state.textAlign} onChange={(e) => { this.setState({ textAlign: e.target.value }); }}> <option value="0">top</option> <option value="1">bottom</option> <option value="2">left</option> <option value="3">right</option> </select> </div> <div class="option-row" style={{display: hasCaption === null || !hasCaption ? 'block' : 'none'}}> <input id="isThreeState" type="checkbox" checked={this.state.isThreeState} onChange={(e) => { this.setState({ isThreeState: e.target.checked }); }} /> <label for="isThreeState">isThreeState:</label> </div> <div class="option-row"> <input type="button" id="setProperty" value="Update" disabled={this.state.disabled} onClick={(e) => {this.props.propertyChange(e, this.state)} } /> </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>
.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; overflow: auto; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; } .sample-options{ z-index: 1000; } .option { padding-bottom: 6px; } .option-row { font-size: 14px; padding: 5px; } .checkbox { padding-right: 12px; display: inline-block; } label { padding-bottom: 4px; display: block; } input, select { width: 100%; padding: 4px 8px; box-sizing: border-box; } input[type=checkbox] { width: auto; } input[type=checkbox]+label { display: inline-block; width: auto; } 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-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);