你可以设置 backColor 或者 backgroundImage 选项为每一个表单设置背景色或者背景图片。如果你同时设置背景色和背景图片, 那么背景图片将会优先显示。
或者,您可以使用workbook的** backgroundImage **选项为工作表设置背景图像。请注意,设置背景图像会覆盖所有设置的背景颜色,因此必须将其删除才能使背景颜色生效。
SpreadJS 提供控制图片布局方式的能力, 你可以设置 backgroundImageLayout 选项来完成此目标。图片布局包含以下几种类型:
stretch: 改变图像尺寸使其充满整个区域,无视实际的长宽比。
center: 图片将显示在区域的正中央。
zoom: 改变图像的尺寸使其适合控件尺寸,图片实际的长宽比不变。
none: 在显示区域中使用图像实际的尺寸显示该图像,没有任何尺寸变化。
你也可以设置 grayAreaBackColor 选项来设置表单灰色区域的背景色。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 1 });
initSpread(spread);
};
function initSpread(spread) {
var spreadNS = GC.Spread.Sheets;
var sheet = spread.getSheet(0);
var pictureUrl = '';
spread.suspendPaint();
sheet.setRowCount(15);
sheet.setColumnCount(20);
spread.options.backColor = 'white';
spread.options.grayAreaBackColor = 'gray';
spread.options.backgroundImageLayout = spreadNS.ImageLayout.stretch;
spread.options.backgroundImage = '$DEMOROOT$/spread/source/images/backImage.png';
spread.resumePaint();
_getElementById('layout').addEventListener('change',function() {
var layout = parseInt(document.getElementById('layout').value);
spread.options.backgroundImageLayout = layout;
});
var grayAreaColor = _getElementById("grayAreaBackColor");
grayAreaColor.addEventListener("change", function () {
spread.options.grayAreaBackColor = grayAreaColor.options[grayAreaColor.selectedIndex].value;
});
var workBookColor = _getElementById("spreadBackColor");
workBookColor.addEventListener("change", function () {
spread.options.backColor = workBookColor.options[workBookColor.selectedIndex].value;
});
_getElementById('file_input').addEventListener('change', function() {
if (typeof this.files[0] !== 'object') {
return;
}
var file = this.files[0];
if (!/image\/\w+/.test(file.type)) {
alert('The file must be an image!');
return false;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e) {
pictureUrl = this.result;
};
});
_getElementById('setSpreadBackgroundImage').addEventListener('click', function() {
spread.options.backgroundImage = pictureUrl;
});
_getElementById('delSpreadBackgroundImage').addEventListener('click', function() {
spread.options.backgroundImage = '';
});
}
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$/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">
<div class="option-row">
<label>Set Background Image:</label>
<input type="file" name="image" id="file_input" />
</div>
<br>
<div class="option-row">
<label>Workbook Background Image Layout:</label>
<select id="layout">
<option value="0" selected="selected">Stretch</option>
<option value="1">Center</option>
<option value="2">Zoom</option>
<option value="3">None</option>
</select>
</div>
<div class="option-row">
<input type="button" id="setSpreadBackgroundImage" value="Set" class="narrow-button" />
<input type="button" id="delSpreadBackgroundImage" value="Delete Background Image" />
</div>
<hr />
<div class="option-row">
<label>Select a color to set as the color of the workbook gray area.</label>
<select id="grayAreaBackColor">
<option value="gray">Gray</option>
<option value="rgb(220, 236, 244)">Light Blue</option>
<option value="#F4F8EB">Light Green</option>
<option value="white">White</option>
</select>
</div>
<div class="option-row">
<label>Select a color to set as the workbook background color.</label>
<select id="spreadBackColor">
<option value="white">White</option>
<option value="#F4F8EB">Light Green</option>
<option value="rgb(220, 236, 244)">Light Blue</option>
<option value="gray">Gray</option>
</select>
</div>
<label class="note"><u>Note</u>: remove background image to see this change.</label>
</div>
</div>
</body>
</html>
input[type="button"] {
width: 180px;
}
input[type="text"] {
padding: 4px;
margin-top: 4px;
width: 100%;
box-sizing: border-box;
}
label {
display: inline-block;
margin-bottom: 6px;
}
.note{
margin-top: 0px;
}
.colorLabel {
background-color: #F4F8EB;
}
.wide-label {
width: 260px;
}
input.narrow-button, .narrow-label {
width: 74px;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 300px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 300px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row:nth-child(1){
padding-bottom: 0px;
}
.option-row:nth-child(2){
margin-top: 0px;
padding-top: 0px;
}
.option-row:nth-child(3){
padding-bottom: 0px;
}
.option-row:nth-child(4){
margin-top: 0px;
padding-top: 0px;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
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;
}