你可以获取和设置值给指定的单元格。
也可以获取或设置一系列值给一片区域内的单元格, 你可以参照如下示例代码:
你也可以获取或设置文本给特定的一个单元格。你可以参照如下示例代码:
如果你想删除一些数据, 你可以参照如下示例代码删除指定区域内单元格的数据。
SpreadJS 也支持将一系列数据从一片区域复制和移动到另一片区域。
SpreadJS 也支持 usedRange。 您可以使用 API (getUsedRange) 并设置不同的 'usedRangeType' 来获取相应的范围。
UsedRangeType 枚举包含以下类型:
all: 工作表所有使用的范围类型。
axis: 工作表标题使用范围。
style: 工作表单元格样式使用的范围。
rowStyle: 工作表行样式使用范围。
colStyle: 工作表列样式使用范围。
data: 工作表数据使用范围。
formula: 工作表公式使用范围。
span: 工作表跨度使用范围。
shape: 工作表形状使用范围。
table: 工作表表格使用范围。
sparkLine: 工作表迷你图使用范围。
comment: 工作表注释使用范围。
slicer: 切片器使用范围。
pivottable: 工作表数据透视表使用范围。
filter: 工作表过滤器使用范围。
dataValidation: 工作表数据验证使用的范围。
conditionFormat: 工作表条件格式使用范围。
chart: 工作表图表使用范围。
picture: 工作表图片使用范围。
你可以使用sheet.options.showZeros选项来控制是否显示单元格中的零值。当为假时,具有零值的单元格会出现空白,而不是显示数字0。
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import {SpreadSheets, Worksheet} from '@grapecity-software/spread-sheets-react';
import GC from '@grapecity-software/spread-sheets';
import './styles.css';
const Component = React.Component, useState = React.useState, spreadNS = GC.Spread.Sheets;
function _getElementById(id) {
return document.getElementById(id);
}
class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.styleType = false;
this.dataType = false;
}
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
<Worksheet>
</Worksheet>
</SpreadSheets>
</div>
<Panel
copyTo={() => {this.copyTo()}}
moveTo={() => {this.moveTo()}}
updateStyleUsedRange={() => {this.updateStyleUsedRange()}}
updateDataUsedRange={() => {this.updateDataUsedRange()}}
updateShowZeros={(e) => {this.updateShowZeros(e)}}
></Panel>
</div>
);
}
initSpread(spread) {
this.spread = spread;
spread.suspendPaint();
let sheet = spread.getSheet(0);
sheet.setValue(1, 1, 'setValue');
sheet.setValue(1, 2, 2014);
sheet.setText(3, 1, 'setText');
sheet.setText(3, 2, '01/01/2014');
sheet.setText(5, 1, '0 Value');
sheet.setValue(5, 2, 0);
sheet.setColumnWidth(2, 90);
sheet.setColumnWidth(8, 120);
sheet.setColumnWidth(9, 120);
sheet.setValue(0, 7, 'setArray');
sheet.setArray(1, 7, [
[ 'ID', 'Phone Number', 'Address' ],
[ 1, '021-432378', 'Marbury Road' ],
[ 2, '021-432668', 'Chester Road' ],
[ 3, '021-432238', 'Gertt Road' ],
[ 4, '021-432533', 'Jnyliner Road' ],
[ 5, '021-432125', 'Approach Road' ],
[ 6, '021-432789', 'Jones Road' ]
]);
this.initStyle(sheet);
spread.resumePaint();
}
copyTo() {
let spread = this.spread, sheet = spread.getActiveSheet();
sheet.copyTo(1, 7, 10, 7, 7, 3, spreadNS.CopyToOptions.all);
}
moveTo() {
let spread = this.spread, sheet = spread.getActiveSheet();
sheet.moveTo(0, 7, 9, 7, 8, 3, spreadNS.CopyToOptions.all);
}
initStyle (sheet) {
let style = new GC.Spread.Sheets.Style();
style.backColor = '#c6c0ca';
for (let row = 1; row < 6; row++) {
for (let col = 1; col < 3; col++) {
sheet.setStyle(row, col, style);
}
}
style = new GC.Spread.Sheets.Style();
style.backColor = '#bbd2cc';
for (let row = 1; row < 8; row++) {
for (let col = 7; col < 10; col++) {
sheet.setStyle(row, col, style);
}
}
style = new GC.Spread.Sheets.Style();
style.backColor = '#fcd8dd';
for (let row = 10; row < 17; row++) {
for (let col = 7; col < 10; col++) {
sheet.setStyle(row, col, style);
}
}
}
updateStyleUsedRange () {
this.styleType = !this.styleType;
this.updateUsedRange();
}
updateDataUsedRange () {
this.dataType = !this.dataType;
this.updateUsedRange();
}
updateShowZeros (e) {
this.spread.getActiveSheet().options.showZeros = e.target.checked;
}
updateUsedRange () {
let type = 0, styleType = this.styleType, dataType = this.dataType;
let sheet = this.spread.getSheet(0);
if (styleType) {
type = type | GC.Spread.Sheets.UsedRangeType.style;
}
if (dataType) {
type = type | GC.Spread.Sheets.UsedRangeType.data;
}
if (type) {
let usedRange = sheet.getUsedRange(type);
if (usedRange) {
sheet.setSelection(usedRange.row, usedRange.col, usedRange.rowCount, usedRange.colCount);
return;
}
}
sheet.setSelection(0, 0, 1, 1);
}
}
function Panel(props) {
return (
<div class="options-container">
<p>Users can set the data for the worksheet or get the data from the worksheet.</p>
<p>Clicking the "Copy" button will copy the data from H2:J8 to H11:J17.</p>
<p>Clicking the "Move" button will move the data from H1:J8 to H10:J17.</p>
<input type="button" value="Copy" id="btnCopy" onClick={props.copyTo}/>
<input type="button" value="Move" id="btnMove" onClick={props.moveTo}/>
<p>Users can quickly obtain the maximum range of different data types in the current sheet.</p>
<p>Check style to get the maximum range of cells with style.</p>
<p>Check data to get the maximum range of cells with data.</p>
<input type="checkbox" class="usedRange" id="style-used-range" onChange={props.updateStyleUsedRange}/>
<label for="style-used-range" class="usedRange">style</label>
<input type="checkbox" class="usedRange" id="data-used-range" onChange={props.updateDataUsedRange}/>
<label for="data-used-range" class="usedRange">data</label>
<hr/>
<input type="checkbox" class="usedRange" id="showZeros" onChange={props.updateShowZeros} defaultChecked={true}/>
<label for="showZeros" class="usedRange">Show Zero Value Cells</label>
</div>
);
}
ReactDOM.render(<App/>, _getElementById('app'));
<!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$/zh/react/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/zh/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: 5px;
margin-top: 10px;
}
label {
display: block;
margin-bottom: 6px;
margin-top: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: inline-block;
width: 85px;
}
.usedRange {
display: inline-block;
}
#btnCopy {
margin-right: 40px;
}
#data-used-range {
margin-left: 75px;
}
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: {
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-react': 'npm:@grapecity-software/spread-sheets-react/index.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/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);