概述
本 Demo 展示了如何通过配置工作簿选项来控制滚动条的显示和行为。Demo 提供了交互式界面,用户可以实时调整滚动条的六种属性:水平/垂直滚动条的显示、滚动提示的显示方式、滚动条最大对齐、滚动条显示最大值,以及是否忽略隐藏行列。
实现思路
初始化 Workbook 实例,创建工作簿
为每个配置选项绑定 change 事件监听器
用户勾选复选框或选择下拉菜单时,实时更新 spread.options 的相应属性
所有配置更改立即生效,用户可直观看到滚动条行为的变化
代码解析
控制滚动条显示
这段代码为水平滚动条和垂直滚动条的复选框绑定了 change 事件。当用户勾选或取消勾选时,会实时更新 spread.options 的 showHorizontalScrollbar 和 showVerticalScrollbar 属性,控制滚动条的显示与隐藏。
配置滚动提示
滚动提示通过下拉菜单选择,值为 0-3 的整数,对应 GC.Spread.Sheets.ShowScrollTip 枚举值:
0 - 无(none):不显示滚动提示
1 - 水平(horizontal):仅在水平滚动时显示提示
2 - 垂直(vertical):仅在垂直滚动时显示提示
3 - 两者(both):水平和垂直滚动时都显示提示
当用户拖拽滚动滑块时,会根据配置显示对应的滚动提示信息。
设置滚动条对齐和显示范围
scrollbarMaxAlign 控制滚动条是否对齐到工作表的最后一行或列,避免滚动区域超出实际数据范围。启用后,用户无法滚动到工作表最后一行/列的下方或右侧空白区域。
scrollbarShowMax 控制滚动条是否基于工作表的全部行列总数显示滚动范围。启用后,滚动条的滑块大小和位置会反映整个工作表的数据范围。
忽略隐藏行列
当启用 scrollIgnoreHidden 时,滚动条在计算滚动范围时会忽略隐藏的行或列。以下情况被视为隐藏:
行高或列宽为 0
行/列的 visible 属性为 false
被收起的分组行列
被筛选掉的行
这使得滚动范围更符合用户可见的实际内容,提升用户体验。
运行效果
勾选或取消勾选"显示水平滚动条"复选框,可以控制水平滚动条的显示与隐藏
勾选或取消勾选"显示垂直滚动条"复选框,可以控制垂直滚动条的显示与隐藏
从"滚动提示"下拉框选择选项,拖动滚动滑块时会显示相应的提示信息
勾选"滚动条最大对齐"后,滚动范围会限制在实际数据区域内
勾选"滚动条显示最大值"后,滚动条会基于工作表的总行列数显示
勾选"忽略隐藏"后,滚动范围会忽略隐藏的行列,只计算可见内容
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 1 });
initSpread(spread);
};
function initSpread(spread) {
var sheet = spread.getActiveSheet();
// Store initial values for restore
var initialRowCount = sheet.getRowCount();
var initialColumnCount = sheet.getColumnCount();
var initialScrollbarMaxAlign = spread.options.scrollbarMaxAlign;
var initialScrollbarShowMax = spread.options.scrollbarShowMax;
// Initialize row/column count editors
_getElementById('columnCount').value = sheet.getColumnCount();
_getElementById('rowCount').value = sheet.getRowCount();
/* Binding settings */
_getElementById('showHorizontalScrollbar').addEventListener('change', function() {
var value = this.value;
spread.options.showHorizontalScrollbar = value === 'auto' ? 'auto' : value === 'true';
if (value === 'auto') {
// Set scrollbarMaxAlign and scrollbarShowMax to true, set column count to 10
spread.options.scrollbarMaxAlign = true;
spread.options.scrollbarShowMax = true;
sheet.setColumnCount(10);
_getElementById('columnCount').value = sheet.getColumnCount();
_getElementById('scrollbarMaxAlign').checked = true;
_getElementById('scrollbarShowMax').checked = true;
} else {
// Restore column count
sheet.setColumnCount(initialColumnCount);
_getElementById('columnCount').value = sheet.getColumnCount();
// Only restore scrollbarMaxAlign and scrollbarShowMax if showVerticalScrollbar is not 'auto'
if (_getElementById('showVerticalScrollbar').value !== 'auto') {
spread.options.scrollbarMaxAlign = initialScrollbarMaxAlign;
spread.options.scrollbarShowMax = initialScrollbarShowMax;
_getElementById('scrollbarMaxAlign').checked = initialScrollbarMaxAlign;
_getElementById('scrollbarShowMax').checked = initialScrollbarShowMax;
}
}
});
_getElementById('showVerticalScrollbar').addEventListener('change', function() {
var value = this.value;
spread.options.showVerticalScrollbar = value === 'auto' ? 'auto' : value === 'true';
if (value === 'auto') {
// Set scrollbarMaxAlign and scrollbarShowMax to true, set row count to 10
spread.options.scrollbarMaxAlign = true;
spread.options.scrollbarShowMax = true;
sheet.setRowCount(10);
_getElementById('rowCount').value = sheet.getRowCount();
_getElementById('scrollbarMaxAlign').checked = true;
_getElementById('scrollbarShowMax').checked = true;
} else {
// Restore row count
sheet.setRowCount(initialRowCount);
_getElementById('rowCount').value = sheet.getRowCount();
// Only restore scrollbarMaxAlign and scrollbarShowMax if showHorizontalScrollbar is not 'auto'
if (_getElementById('showHorizontalScrollbar').value !== 'auto') {
spread.options.scrollbarMaxAlign = initialScrollbarMaxAlign;
spread.options.scrollbarShowMax = initialScrollbarShowMax;
_getElementById('scrollbarMaxAlign').checked = initialScrollbarMaxAlign;
_getElementById('scrollbarShowMax').checked = initialScrollbarShowMax;
}
}
});
_getElementById('scrollbarMaxAlign').addEventListener('change', function() {
spread.options.scrollbarMaxAlign = this.checked;
initialScrollbarMaxAlign = this.checked;
});
_getElementById('scrollbarShowMax').addEventListener('change', function() {
spread.options.scrollbarShowMax = this.checked;
initialScrollbarShowMax = this.checked;
});
_getElementById('optShowScrollTip').addEventListener('change', function() {
var result = parseInt(this.value);
spread.options.showScrollTip = result;
});
_getElementById('scrollIgnoreHidden').addEventListener('change', function() {
spread.options.scrollIgnoreHidden = this.checked;
});
_getElementById('columnCount').addEventListener('change', function() {
var value = parseInt(this.value);
if (value > 0) {
sheet.setColumnCount(value);
initialColumnCount = value;
}
});
_getElementById('rowCount').addEventListener('change', function() {
var value = parseInt(this.value);
if (value > 0) {
sheet.setRowCount(value);
initialRowCount = value;
}
});
}
function _getElementById(id){
return document.getElementById(id);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="zh-cn" />
<meta charset="utf-8" />
<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$/spread/source/data/data.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>修改以下不同属性,查看它们对工作簿滚动条不同方面的影响。</p>
<div class="option-row">
<label>显示滚动提示:</label>
<select id="optShowScrollTip">
<option value="0" selected="selected">无</option>
<option value="1">水平</option>
<option value="2">垂直</option>
<option value="3">两者</option>
</select>
</div>
<hr />
<div class="option-row">
<label for="columnCount">列数:</label>
<input type="number" id="columnCount" min="1" value="20"
style="width: 100px; height: 19px; padding: 0; box-sizing: border-box;" />
</div>
<div class="option-row">
<label for="showHorizontalScrollbar">水平滚动条:</label>
<select id="showHorizontalScrollbar">
<option value="true" selected>显示</option>
<option value="false">隐藏</option>
<option value="auto">自动</option>
</select>
</div>
<label>设置水平滚动条的可见性。使用“auto”可自动显示。</label>
<div class="option-row">
<label for="rowCount">行数:</label>
<input type="number" id="rowCount" min="1" value="200"
style="width: 100px; height: 19px; padding: 0; box-sizing: border-box;" />
</div>
<div class="option-row">
<label for="showVerticalScrollbar">垂直滚动条:</label>
<select id="showVerticalScrollbar">
<option value="true" selected>显示</option>
<option value="false">隐藏</option>
<option value="auto">自动</option>
</select>
</div>
<label>设置垂直滚动条的可见性。使用“auto”可自动显示。</label>
<div class="option-row">
<input type="checkbox" id="scrollbarMaxAlign" />
<label for="scrollbarMaxAlign">滚动条最大对齐</label>
</div>
<label>设置此项可将滚动限制在最大行数和列数范围内。</label>
<div class="option-row">
<input type="checkbox" id="scrollbarShowMax" checked="checked" />
<label for="scrollbarShowMax">滚动条显示最大值</label>
</div>
<label>设置此项以使滚动条显示整个工作表的最大范围。</label>
<hr />
<div class="option-row">
<input type="checkbox" id="scrollIgnoreHidden" />
<label for="scrollIgnoreHidden">滚动时忽略隐藏项</label>
</div>
<label>设置此项以忽略隐藏的行或列。</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;
}
.option-row select {
width: 100px;
}
label {
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
}
hr {
border-color: #fff;
opacity: .2;
margin-top: 20px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}