你可以使用 src 方法来将图片设置给图片浮动对象元素, 图片浮动对象元素的 src 可以设置 base64 格式的图片。
你可以通过 pictureStretch 方法来改变图片的布局。 pictureStretch 的类型是 ImageLayout 枚举。并且你可以使用 getOriginalWidth 和 getOriginalHeight 方法来获取图片的原始宽度和原始高度。
你也可以设置图片的边框; 边框的设置和标准 DOM 的边框设置一样。例如:
如果图片的部分地方有透明, 你可以通过设置 backColor 来获取或者设置图片的背景色。例如:
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
initSpread(spread);
};
function initSpread(spread) {
var sheet = spread.getSheet(0);
var picture = sheet.pictures.add("f2", "$DEMOROOT$/spread/source/images/spreadLogo.png", 50, 50, 100, 100);
picture.backColor("black");
document.getElementById('pictureStretch').onchange = function (e) {
var sheet = spread.getActiveSheet();
var pictures = sheet.pictures.all();
var stretch = parseInt(e.target.value);
if (pictures) {
for (var index = 0, len = pictures.length; index < len; index++) {
var picture = pictures[index];
if (picture.isSelected()) {
picture.pictureStretch(stretch);
}
}
}
};
document.getElementById('borderStyle').onchange = function (e) {
var sheet = spread.getActiveSheet();
var pictures = sheet.pictures.all();
var borderStyle = e.target.value;
if (pictures) {
for (var index = 0, len = pictures.length; index < len; index++) {
var picture = pictures[index];
if (picture.isSelected()) {
picture.borderStyle(borderStyle);
}
}
}
};
document.getElementById('set').onclick = function () {
var sheet = spread.getActiveSheet();
var pictures = sheet.pictures.all();
var borderColor = document.getElementById('borderColor').value;
var borderWidth = parseFloat(document.getElementById('borderWidth').value);
var borderStyle = document.getElementById('borderStyle').value;
var borderRadius = parseFloat(document.getElementById('borderRadius').value);
var backColor = document.getElementById('backColor').value;
var pictureStretch = parseInt(document.getElementById('pictureStretch').value);
if (pictures) {
for (var index = 0, len = pictures.length; index < len; index++) {
var picture = pictures[index];
if (picture.isSelected()) {
picture.borderColor(borderColor);
picture.borderWidth(borderWidth);
picture.borderStyle(borderStyle);
picture.borderRadius(borderRadius);
picture.backColor(backColor);
picture.pictureStretch(pictureStretch);
}
}
}
sheet.repaint();
};
document.getElementById('resetPicture').onclick = function () {
var sheet = spread.getActiveSheet();
var pictures = sheet.pictures.all();
if (pictures) {
for (var index = 0, len = pictures.length; index < len; index++) {
var picture = pictures[index];
if (picture.isSelected()) {
var originalWidth = picture.getOriginalWidth();
var originalHeight = picture.getOriginalHeight();
if (originalWidth > 0 && originalHeight > 0) {
picture.width(originalWidth);
picture.height(originalHeight);
}
}
}
}
sheet.repaint();
};
};
<!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/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/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">
<p class="desc">Select the picture in the Spread component and change it using these options</p>
</div>
<div class="option-row">
<label for="borderColor">Border Color:</label>
<input type="text" id="borderColor" />
</div>
<div class="option-row">
<label for="borderWidth">Border Width:</label>
<input type="text" id="borderWidth" />
</div>
<div class="option-row">
<label for="borderRadius">Border Radius:</label>
<input type="text" id="borderRadius" />
</div>
<div class="option-row">
<label for="backColor">Back Color:</label>
<input type="text" id="backColor" />
</div>
<div class="option-row">
<label>Border Style:</label>
<select id="borderStyle">
<option value="solid" selected="selected">solid</option>
<option value="dotted">dotted</option>
<option value="dashed">dashed</option>
<option value="double">double</option>
<option value="groove">groove</option>
<option value="ridge">ridge</option>
<option value="inset">inset</option>
<option value="outset">outset</option>
</select>
</div>
<div class="option-row">
<label>Picture Stretch:</label>
<select id="pictureStretch">
<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="set" value="Set" />
<input type="button" id="resetPicture" value="Reset to original size" />
</div>
</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: 5px;
}
select, input {
padding: 4px 6px;
box-sizing: border-box;
margin-top: 6px;
width: 100%;
}
.desc{
padding:2px 10px;
background-color:#F4F8EB;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}