当我们在向SpreadJS的合并单元格中粘贴图片的时候,它总是会出现在单元格的左上角,如:
那么如何才能实现默认将其居中在合并单元格中呢?
我们需要通过细致的运算才能实现,核心代码请参考:
sheet.bind(GC.Spread.Sheets.Events.ShapeChanged, function (e, info) {
if (info.propertyName != "originalSize") {
return;
}
let selection = sheet.getSelections()[0];
let selectionRow = selection.row;
let selectionRowCount = selection.rowCount;
let selectionCol = selection.col;
let selectionColCount = selection.colCount;
let frontWidth = 0;
for (let i = 0; i <= selectionCol; i++) {
frontWidth += sheet.getColumnWidth(i);
}
let frontHeight = 0;
for (let i = 0; i <= selectionRow; i++) {
frontHeight += sheet.getRowHeight(i);
}
let fullWidth = 0;
for (let i = 0; i < selectionCol + selectionColCount - 1; i++) {
fullWidth += sheet.getColumnWidth(i);
}
let fullHeight = 0;
for (let i = 0; i < selectionRow + selectionRowCount - 1; i++) {
fullHeight += sheet.getRowHeight(i);
}
let shape = info.shape;
let shapeWidth = shape.width();
let shapeHeight = shape.height();
shape.x(frontWidth + (fullWidth - frontWidth) / 2 - shapeWidth / 2);
shape.y(frontHeight + (fullHeight - frontHeight) / 2 - shapeHeight / 2);
});
实现效果:
|
|