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: 表示工作表的图片使用范围。
tag: 表示工作表的标签使用范围。
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'));
var spreadNS = GC.Spread.Sheets;
var sheet = spread.getSheet(0);
let tagType, dataType, styleType;
sheet.suspendPaint();
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']
]);
initStyle(sheet);
sheet.setTag(10, 1, "tag1");
sheet.setTag(19, 4, "tag2");
var comment = new GC.Spread.Sheets.Comments.Comment();
comment.text("Cell B11 has tag");
comment.backColor("yellow");
comment.foreColor("green");
comment.displayMode(GC.Spread.Sheets.Comments.DisplayMode.alwaysShown);
comment.autoSize(true);
sheet.getCell(10, 1).comment(comment);
comment = new GC.Spread.Sheets.Comments.Comment();
comment.text("Cell E20 has tag");
comment.backColor("yellow");
comment.foreColor("green");
comment.displayMode(GC.Spread.Sheets.Comments.DisplayMode.alwaysShown);
comment.autoSize(true);
sheet.getCell(19, 4).comment(comment);
document.getElementById('tag-used-range').addEventListener('change', function () {
tagType = this.checked;
setUsedRange(dataType, styleType, tagType, sheet);
});
document.getElementById('data-used-range').addEventListener('change', function () {
dataType = this.checked;
setUsedRange(dataType, styleType, tagType, sheet);
});
document.getElementById('style-used-range').addEventListener('change', function () {
styleType = this.checked;
setUsedRange(dataType, styleType, tagType, sheet);
});
sheet.resumePaint();
};
function 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);
}
}
}
function setUsedRange(dataType, styleType, tagType, sheet) {
let type = 0;
if (dataType) {
type = type | GC.Spread.Sheets.UsedRangeType.data;
}
if (styleType) {
type = type | GC.Spread.Sheets.UsedRangeType.style;
}
if (tagType) {
type = type | GC.Spread.Sheets.UsedRangeType.tag;
}
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);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="spreadjs culture" content="zh-cn"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sample-tutorial">
<div id="ss" class="sample-spreadsheets"></div>
<div class="options-container">
<p>Users can quickly obtain the maximum range of different data types in the current sheet</p>
<p>Check data to get the maximum range of cells with data</p>
<p>Check style to get the maximum range of cells with style</p>
<p>Check tag to get the maximum range of cells with tag</p>
<input type="checkbox" class="usedRange" id="data-used-range">
<label for="data-used-range" class="usedRange">data</label>
<div class="mid-element">
<input type="checkbox" class="usedRange" id="style-used-range">
<label for="style-used-range" class="usedRange">style</label>
</div>
<input type="checkbox" class="usedRange" id="tag-used-range">
<label for="tag-used-range" class="usedRange">tag</label>
</div>
</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;
}
.mid-element {
display: inline-block;
margin:0 40px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}