找回密码
 立即注册

QQ登录

只需一步,快速开始

Joestar.Xu SpreadJS 开发认证
超级版主   /  发表于:2023-10-30 11:52  /   查看:1562  /  回复:0
当我们在向SpreadJS的合并单元格中粘贴图片的时候,它总是会出现在单元格的左上角,如:


image.png205806578.png

那么如何才能实现默认将其居中在合并单元格中呢?

我们需要通过细致的运算才能实现,核心代码请参考:


  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);
  });


实现效果:

image.png614640807.png

将粘贴的图片在合并单元格中居中.js

3.5 KB, 下载次数: 208

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部